# [Beginner-7] Best Algo Trading Tutorial- CCI Indicator ## Best Algo Trading Tutorial For The Beginner - CCI Indicator

Algo trading is not as difficult as you think. In this tutorial article, there are four simple steps to lead the beginner to understand how to code your own best algorithm trading strategy for CCI indicator. All the coding is using pine script and based on the Tradingview platform.

#### Introduction of CCI indicator

Developed by Donald Lambert, the Commodity Channel Index (CCI) is a momentum-based oscillator used to help determine when an investment vehicle is reaching a condition of being overbought or oversold. It is also used to assess price trend direction and strength. This information allows traders to determine if they want to enter or exit a trade, refrain from taking a trade, or add to an existing position.

Below is the full version code:

`//@version=4strategy("Yaonology CCI Tutoring", overlay=false, default_qty_type= strategy.percent_of_equity ,default_qty_value=100, currency=currency.USD, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0)period = input(title = "period",defval=20)tp = (high+low+close)/3avg_tp = sma(tp,period)mean_dev = sma(abs(tp-avg_tp),period)CCI = (tp-avg_tp)/(0.015*mean_dev)plot(CCI, color = color.blue)plot(100,color = color.red)plot(-100,color = color.green)if CCI < -100 and CCI > -100    strategy.entry(id = "long",long = true)if CCI > 100 and CCI < 100    strategy.close(id = "long")`

#### Step One: Strategy Setting

//@version=4

strategy(“Yaonology CCI Tutoring“, overlay=false, default_qty_type= strategy.percent_of_equity ,default_qty_value=100,currency=currency.USD,initial_capital=10000,commission_type=strategy.commission.percent,commission_value=0)

strategy : Name of strategy (Yaonology sets the name as Yaonology CCI Tutoring)

overlay : No – False

default_qty_type : Type of quantity – percent of equity

default_qty_value : Value of quantity – 100

currency:  USD

initial_capital: Initial trading price – 10000

commission_type : Type of commission – percent

commission_value : Value of commission ( there is no commission, so we set 0)

#### Step Two: Input

Part 1:

period = input(title = “period“,defval=20)

The default length is 20 consecutive trading days

The parameters can be modified in setting -> input.

Part 2: Strategy Exponential Moving Average

tp = (high+low+close)/3

tp is the day’s Typical Price, which is the average of the day’s three key prices:

high:the day’s highest price

low:the day’s lowest price

close: the day’s close price

avg_tp = sma(tp,period)

calculate the moving average of Typical Prices throughout the period as specified above(20 in default)

mean_dev = sma(abs(tp-avg_tp),period)

calculate the mean of deviation of Typical Prices from the average Typical Price as calculated above(avg_tp)

CCI = (tp-avg_tp)/(0.015*mean_dev)

calculate the CCI indicator for each day, where 0.015 is a standardization coefficient that’s widely recognized

#### Step Three: Plot

plot(CCI, color = color.blue)

-this line will be set as color blue

plot(100,color = color.red)

this line labels the threshold where we enter into long position in red

plot(-100,color = color.green)

this line labels the threshold where we exit the current position in green

#### Step Five: Strategy.entry & Strategy.close

In the end, we need to decide when to buy or sell based on CCI Index we plot:

if CCI < -100 and CCI > -100

strategy.entry(id = “long“,long = true)

=>   means one day before;

if this condition applies, this is the point to consider to buy it.

Strategy.entry: The command to enter market position;

id: The order name. Yaonology sets as long

long: This means long position, and here is true

if CCI > 100 and CCI < 100

strategy.close(id = “long“)

=>   means one day before

If this condition applies, this is the point to consider to sell it.

Strategy.close: The command to close market position

The id should set the same one as the entry one since it is the same order

## Complete Version

Net Profit: total net profit by applying the strategy to the historical market

Percent profitable: percentage of trades with positive profit

Profit factor: total profit / total loss

Max Drawdown: total loss by applying the strategy to the historical market (Using S&P 500 as example)

Average Number of Days In Trades: the average number of days between entering and exiting a position by applying the strategy to the historical market

Yaonology.com 