Optionally, you can enable an Exponential Moving Average (EMA) to only allow trading in the direction of the larger trend. Please note that the strategy will not plot the EMA. If you want, for visual confirmation, you can add to the chart an Exponential Moving Average as a second indicator, with the same settings used in the strategy’s built-in EMA.

The strategy also allows you to set internal Stop Loss and Take Profit levels, with the option to choose between Percentage-based TP/SL or ATR-based TP/SL.

The strategy can be adapted to multiple assets and timeframes:

- Pick an asset and a timeframe

- Zoom back as far as possible to identify meaningful positive and negative peaks of the CCI

- Set Overbought and Oversold at a rough average of the peaks you identified

- Adjust TP/SL according to your risk management strategy

Like the strategy? Give it a boost!

Have any questions? Leave a comment or drop me a message.

**CAUTIONARY WARNING**

Please note that this is a complex trading strategy that involves several inputs and conditions. Before using it in live trading, it is highly recommended to thoroughly test it on historical data and use risk management techniques to safeguard your capital. After backtesting, it's also highly recommended to perform a first live test with a small amount. Additionally, it's essential to have a good understanding of the strategy's behavior and potential risks.

**Only risk what you can afford to lose**.

**USED INDICATORS**

1 — COMMODITY CHANNEL INDEX (CCI)

The Commodity Channel Index (CCI) is a technical analysis indicator used to measure the momentum of an asset. It was developed by Donald Lambert and first published in Commodities magazine (now Futures) in 1980. Despite its name, the CCI can be used in any market and is not just for commodities. The CCI compares current price to average price over a specific time period. The indicator fluctuates above or below zero, moving into positive or negative territory. While most values, approximately 75%, fall between -100 and +100, about 25% of the values fall outside this range, indicating a lot of weakness or strength in the price movement.

The CCI was originally developed to spot long-term trend changes but has been adapted by traders for use on all markets or timeframes. Trading with multiple timeframes provides more buy or sell signals for active traders. Traders often use the CCI on the longer-term chart to establish the dominant trend and on the shorter-term chart to isolate pullbacks and generate trade signals.

CCI is calculated with the following formula:

`(Typical Price - Simple Moving Average) / (0.015 x Mean Deviation)`

Some trading strategies based on CCI can produce multiple false signals or losing trades when conditions turn choppy. Implementing a stop-loss strategy can help cap risk, and testing the CCI strategy for profitability on your market and timeframe is a worthy first step before initiating trades.

2 — AVERAGE TRUE RANGE (ATR)

The Average True Range (ATR) is a technical analysis indicator that measures market volatility by calculating the average range of price movements in a financial asset over a specific period of time. The ATR was developed by J. Welles Wilder Jr. and introduced in his book “New Concepts in Technical Trading Systems” in 1978.

The ATR is calculated by taking the average of the true range over a specified period. The true range is the greatest of the following:

- The difference between the current high and the current low.

- The difference between the previous close and the current high.

- The difference between the previous close and the current low.

3 — EXPONENTIAL MOVING AVERAGE (EMA)

The Exponential Moving Average (EMA) is a type of moving average (MA) that places a greater weight and significance on the most recent data points.

The EMA is calculated by taking the average of the true range over a specified period. The true range is the greatest of the following:

- The difference between the current high and the current low.

- The difference between the previous close and the current high.

- The difference between the previous close and the current low.

The formula for calculating EMA is as follows:

- Compute the Simple Moving Average (SMA).

- Calculate the multiplier for weighting the EMA.

- Calculate the current EMA using the following formula:

`EMA = Closing price x multiplier + EMA (previous day) x (1-multiplier)`

**STRATEGY EXPLANATION**

1 — INPUTS AND PARAMETERS

The strategy uses the Commodity Channel Index (CCI) with additional options for an Exponential Moving Average (EMA), Take Profit (TP) and Stop Loss (SL).

*length*: The period length for the CCI calculation.

*overbought*: The overbought level for the CCI. When CCI crosses above this level, it may signal a potential short entry.

*oversold*: The oversold level for the CCI. When CCI crosses below this level, it may signal a potential long entry.

*useEMA*: A boolean input to enable or disable the use of Exponential Moving Average (EMA) as a filter for long and short entries.

*emaLength*: The period length for the EMA if it is used.

2 — CCI CALCULATION

The CCI indicator is calculated using the following formula:

`(src - ma) / (0.015 * ta.dev(src, length))`

*src*is the typical price (average of high, low, and close) and

*ma*is the Simple Moving Average (SMA) of src over the specified length.

3 — EMA CALCULATION

If the

*useEMA*option is enabled, an EMA is calculated with the given

*emaLength*.

4 — TAKE PROFIT AND STOP LOSS METHODS

The strategy offers two methods for TP and SL calculations: percentage-based and ATR-based.

*tpSlMethod_percentage*: A boolean input to choose the percentage-based method.

*tpSlMethod_atr*: A boolean input to choose the ATR-based method.

5 — PERCENTAGE-BASED TP AND SL

If

*tpSlMethod_percentage*is chosen, the strategy calculates the TP and SL levels based on a percentage of the average entry price.

*tp_percentage*: The percentage value for Take Profit.

*sl_percentage*: The percentage value for Stop Loss.

6 — ATR-BASED TP AND SL

If

*tpSlMethod_atr*is chosen, the strategy calculates the TP and SL levels based on Average True Range (ATR).

*atrLength*: The period length for the ATR calculation.

*atrMultiplier*: A multiplier applied to the ATR to set the SL level.

*riskRewardRatio*: The risk-reward ratio used to calculate the TP level.

7 — ENTRY CONDITIONS

The strategy defines two conditions for entering long and short positions based on CCI and, optionally, EMA.

- Long Entry: CCI crosses below the oversold level, and if useEMA is enabled, the closing price should be above the EMA.

- Short Entry: CCI crosses above the overbought level, and if useEMA is enabled, the closing price should be below the EMA.

8 — TP AND SL LEVELS

The strategy calculates the TP and SL levels based on the chosen method and updates them dynamically.

- For the percentage-based method, the TP and SL levels are calculated as a percentage of the average entry price.

- For the ATR-based method, the TP and SL levels are calculated using the ATR value and the specified multipliers.

9 — EXIT CONDITIONS

The strategy defines exit conditions for both long and short positions.

- If there is a long position, it will be closed either at TP or SL levels based on the chosen method.

- If there is a short position, it will be closed either at TP or SL levels based on the chosen method.

- Additionally, positions will be closed if CCI crosses back above oversold in long positions or below overbought in short positions.

10 — PLOTTING

The script plots the CCI line along with overbought and oversold levels as horizontal lines.

The CCI line is colored red when above the overbought level, green when below the oversold level, and white otherwise.

The shaded region between the overbought and oversold levels is plotted as well.

Versionshinweise:

Updated

*atrMultiplier*and*riskRewardRatio*to accept decimals.
Upgraded the strategy adding RSI as an additional entry trigger.

You can now enable an RSI with the relevant toggle, set the lenght and the overbought/oversold levels and use it as an additional filter for your long and short entries.

Please note that using both EMA and RSI as filters could drastically reduce the number of trades.

Added Inputs:

Added Calculations:

Added Entry Conditions:

UI Updates:

Other Changes:

Info about RSI:

The Relative Strength Index (RSI) is a momentum oscillator used in technical analysis that measures the magnitude of recent price changes to evaluate overbought or oversold conditions in the price of an asset. It was developed by J. Welles Wilder and published in a 1978 book, New Concepts in Technical Trading Systems.

The RSI is calculated using the following formula:

Traders use RSI to identify overbought and oversold conditions, as well as to determine the strength of a trend. When the RSI is above 70, it is considered overbought and when it is below 30, it is considered oversold.

You can now enable an RSI with the relevant toggle, set the lenght and the overbought/oversold levels and use it as an additional filter for your long and short entries.

Please note that using both EMA and RSI as filters could drastically reduce the number of trades.

Added Inputs:

- useRSI - RSI On/Off Toggle

- rsiLength - Length of the RSI

- rsiOverbought - RSI Overbought level

- rsiOversold - RSI Oversold level

Added Calculations:

- RSI -> rsi = ta.rsi(src, rsiLength)

Added Entry Conditions:

- Long -> and (not useRSI or rsi < rsiOversold)

- Short -> and (not useRSI or rsi < rsiOversbought)

UI Updates:

- Added CCI, RSI and EMA groups to better differentiate inputs in UI

Other Changes:

- Changed cci-related variable names to better differentiate them (e.g. "lenght" > "cciLength")

- Plotting RSI as a blue line (if enabled). Disable it under Style if the indicator becomes too hard to read

Info about RSI:

The Relative Strength Index (RSI) is a momentum oscillator used in technical analysis that measures the magnitude of recent price changes to evaluate overbought or oversold conditions in the price of an asset. It was developed by J. Welles Wilder and published in a 1978 book, New Concepts in Technical Trading Systems.

The RSI is calculated using the following formula:

```
RSI = 100 - (100 / (1 + RS))
RS = Average gain of up periods during the specified time frame / Average loss of down periods during the specified time frame.
```

Traders use RSI to identify overbought and oversold conditions, as well as to determine the strength of a trend. When the RSI is above 70, it is considered overbought and when it is below 30, it is considered oversold.