1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.
Source Code
81 lines of Pine Script v5 from strategies/momentum/cci_strategy.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.
cci_strategy.pine
//@version=5
strategy("CCI Strategy - Eterna MCP",
shorttitle="CCI",
overlay=false,
default_qty_type=strategy.percent_of_equity,
default_qty_value=100,
initial_capital=1000)
// ============================================
// PARAMETERS
// ============================================
cciLength = input.int(20, "CCI Length", minval=1)
overboughtLevel = input.int(100, "Overbought Level", minval=50)
oversoldLevel = input.int(-100, "Oversold Level", maxval=-50)
extremeOB = input.int(200, "Extreme Overbought", minval=150)
extremeOS = input.int(-200, "Extreme Oversold", maxval=-150)
leverageSize = input.float(5.0, "Leverage", minval=1, maxval=100)
positionSize = input.float(0.01, "Position Size", minval=0.001, maxval=1)
// ============================================
// CCI CALCULATION
// ============================================
cci = ta.cci(close, cciLength)
// ============================================
// SIGNAL GENERATION
// ============================================
// Standard signals: CCI crosses threshold
bullishStandard = ta.crossover(cci, oversoldLevel)
bearishStandard = ta.crossunder(cci, overboughtLevel)
// Extreme reversal: CCI reverses from extreme levels
extremeBullish = cci[1] < extremeOS and cci > extremeOS and cci < oversoldLevel
extremeBearish = cci[1] > extremeOB and cci < extremeOB and cci > overboughtLevel
// Zero-line cross (trend following)
bullishTrend = ta.crossover(cci, 0)
bearishTrend = ta.crossunder(cci, 0)
// Combined signal logic
buySignal = bullishStandard or extremeBullish
sellSignal = bearishStandard or extremeBearish
// ============================================
// STRATEGY LOGIC
// ============================================
if buySignal and strategy.position_size == 0
strategy.entry("Long", strategy.long)
alert('{"action":"buy","symbol":"' + syminfo.ticker + '","size":' + str.tostring(positionSize) + ',"leverage":' + str.tostring(leverageSize) + '}', alert.freq_once_per_bar_close)
if sellSignal and strategy.position_size == 0
strategy.entry("Short", strategy.short)
alert('{"action":"sell","symbol":"' + syminfo.ticker + '","size":' + str.tostring(positionSize) + ',"leverage":' + str.tostring(leverageSize) + '}', alert.freq_once_per_bar_close)
// Exit on opposite signal or zero cross
if sellSignal and strategy.position_size > 0
strategy.close("Long")
if buySignal and strategy.position_size < 0
strategy.close("Short")
// ============================================
// PLOTTING
// ============================================
plot(cci, "CCI", color=color.blue, linewidth=2)
hline(overboughtLevel, "Overbought", color=color.red, linestyle=hline.style_dashed)
hline(oversoldLevel, "Oversold", color=color.green, linestyle=hline.style_dashed)
hline(extremeOB, "Extreme OB", color=color.maroon, linestyle=hline.style_solid)
hline(extremeOS, "Extreme OS", color=color.teal, linestyle=hline.style_solid)
hline(0, "Zero Line", color=color.gray, linestyle=hline.style_dotted)
// Highlight extreme zones
bgcolor(cci > extremeOB ? color.new(color.red, 90) : na)
bgcolor(cci < extremeOS ? color.new(color.green, 90) : na)
plotshape(buySignal, "Buy", shape.triangleup, location.bottom, color.green, size=size.normal)
plotshape(sellSignal, "Sell", shape.triangledown, location.top, color.red, size=size.normal)
// Trend indicators
plotshape(bullishTrend, "Bullish Trend", shape.circle, location.bottom, color.lime, size=size.tiny)
plotshape(bearishTrend, "Bearish Trend", shape.circle, location.top, color.orange, size=size.tiny)
Overview
A long opens when CCI crosses above the oversold level or rebounds from an extreme oversold zone; a short uses the opposite conditions. It closes the position on the opposite signal and has no stop loss or take profit. Length, threshold levels, leverage, and position size are adjustable. Zero-line crosses are marked but do not trigger trades.
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
CCI Length
int
20
cciLength
Overbought Level
int
100
overboughtLevel
Oversold Level
int
-100
oversoldLevel
Extreme Overbought
int
200
extremeOB
Extreme Oversold
int
-200
extremeOS
Leverage
float
5.0
leverageSize
Position Size
float
0.01
positionSize
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.
MACD + Divergences [Alorse] is a momentum strategy that calculates the MACD oscillator and identifies regular and hidden divergences using pivot high and low points. It filters trade signals with dual Exponential Moving Averages (EMA 150 and 600) to ensure tra
This strategy combines the ATR-banded Q-Trend indicator with the Klinger Volume Oscillator (KVO) to execute confluence trades. It enters long when a Q-Trend buy trigger aligns with a positive KVO value and a bullish candle confirmation, taking short trades und
This strategy calculates the Moving Average Convergence Divergence indicator and plots the MACD line, signal line, and histogram in a separate pane. It enters long positions when the MACD line crosses above the signal line and enters short positions when it cr
This strategy estimates price behavior using ordinary least squares regression between higher-timeframe data and a generated dataset, smoothed by a Karobein oscillator. It enters long positions when the oscillator turns upward and short positions when it turns
This strategy executes mean-reversion trades based on Bollinger Bands and the Stochastic Oscillator. Long orders trigger when price dips below the lower band alongside a bullish Stochastic crossover, with an optional oversold threshold. Short orders execute on
This strategy calculates a 20-period Relative Strength Index (RSI) and generates entry orders based on crossovers and crossunders of the 50 level. When RSI crosses above 50, it places a long stop entry at the current high price with a custom alert message. Whe