I've assembled a CCI indicator based on the trend trading strategy found in this video.
I added arrows for short term and long term signals that are able to turn on/off in the settings.
The idea of this indicator is to trade with the trend. This indicator gives a good signals of when to get into a trade when you want to trade with the trend, and also good exits.
The only difference I made is his bull trend and bear trend are measured at the zero level. I set them to bull trend at 50 and bear trend at -50. In between these levels is a non-trending choppy market. If you want to change it back to 0, you can do that in the settings.
When the longer trend is bullish (thick line is above 50), then buy when the short trend (thin line) crosses above -100. This should be done closer to the beginning of the long term trend. You don't want to take a trade if the long term trend is fading and showing divergence. The opposite applies for short trades. This picture of SPY on the Daily chart illustrates long entries.
Take EXITS when the short trend(thin line) crosses below the 50 level. Below illustrates EXIT in the red circle.
When the long trend(thick line) is YELLOW, then it's in a NON-Trending, flat and choppy market. The below picture illustrates this.
Lastly, I added Divergence. As shown in the below picture. Divergence will be either a red or green line.
Here is the code.
I added arrows for short term and long term signals that are able to turn on/off in the settings.
The idea of this indicator is to trade with the trend. This indicator gives a good signals of when to get into a trade when you want to trade with the trend, and also good exits.
The only difference I made is his bull trend and bear trend are measured at the zero level. I set them to bull trend at 50 and bear trend at -50. In between these levels is a non-trending choppy market. If you want to change it back to 0, you can do that in the settings.
When the longer trend is bullish (thick line is above 50), then buy when the short trend (thin line) crosses above -100. This should be done closer to the beginning of the long term trend. You don't want to take a trade if the long term trend is fading and showing divergence. The opposite applies for short trades. This picture of SPY on the Daily chart illustrates long entries.
Take EXITS when the short trend(thin line) crosses below the 50 level. Below illustrates EXIT in the red circle.
When the long trend(thick line) is YELLOW, then it's in a NON-Trending, flat and choppy market. The below picture illustrates this.
Lastly, I added Divergence. As shown in the below picture. Divergence will be either a red or green line.
Here is the code.
Code:
#Double CCI Trend Trading Strategy
#Assembled by chewie
# 06/2025
declare lower;
input length1 = 5;
input length2 = 50;
input showBreakoutSignals = yes;
input showtrendsignals = yes;
input Bulltrend = 50;
input Beartrend = -50;
def price = close + low + high;
def linDev1 = LinDev(price, length1);
def linDev2 = LinDev(price, length2);
plot CCI1 = if linDev1 == 0 then 0 else (price - Average(price, length1)) / linDev1 / 0.015;
plot CCI2 = if linDev2 == 0 then 0 else (price - Average(price, length2)) / linDev2 / 0.015;
plot ZeroLine = 0;
plot UTrend = if CCI2 > Bulltrend then Bulltrend else double.nan;
plot DTrend = if CCI2 < Beartrend then Beartrend else double.nan;
plot up = 100;
plot down = -100;
plot hi = 150;
plot lo = -150;
plot UpSignal = if CCI1 crosses above down and CCI2 > Bulltrend then down else Double.NaN;
plot DownSignal = if CCI1 crosses below up and CCI2 < Beartrend then up else Double.NaN;
plot BullTrendSignalStart = if CCI2 crosses above Bulltrend then Bulltrend else Double.NaN;
plot BullTrendsignalEnd = if CCI2 crosses below Bulltrend then Bulltrend else Double.NaN;
plot BearTrendSignalStart = if CCI2 crosses below Beartrend then Beartrend else Double.NaN;
plot BearTrendsignalEnd = if CCI2 crosses above Beartrend then Beartrend else Double.NaN;
UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);
BullTrendSignalStart.SetHiding(!showTrendSignals);
BullTrendsignalEnd.SetHiding(!showTrendSignals);
BearTrendSignalStart.SetHiding(!showTrendSignals);
BearTrendsignalEnd.SetHiding(!showTrendSignals);
UTrend.setdefaultColor(color.green);
DTrend.setdefaultColor(color.red);
UTrend.HideBubble();
UTrend.HideTitle();
DTrend.HideBubble();
DTrend.HideTitle();
CCI1.DefineColor("OverBought", Color.green);
CCI1.DefineColor("Normal", Color.YELLOW);
CCI1.DefineColor("OverSold", Color.red);
CCI1.AssignValueColor(if CCI1 > up then CCI1.Color("OverSOLD") else if CCI1 < down then CCI1.Color("OverBOUGHT") else CCI1.Color("Normal"));
CCI1.SetLineWeight(1);
CCI2.DefineColor("OverBought", Color.red);
CCI2.DefineColor("Normal", Color.YELLOW);
CCI2.DefineColor("OverSold", Color.green);
CCI2.AssignValueColor(if CCI2 > hi then color.cyan else if CCI2 < lo then color.magenta else if CCI2 > Bulltrend then CCI2.Color("OverSOLD") else if CCI2 < Beartrend then CCI2.Color("OverBOUGHT") else CCI2.Color("Normal"));
CCI2.SetLineWeight(5);
ZeroLine.SetDefaultColor(Color.dark_gray);
ZeroLine.HideBubble();
ZeroLine.HideTitle();
up.SetDefaultColor(Color.DARK_red);
UP.HideBubble();
UP.HideTitle();
down.SetDefaultColor(Color.DARK_GREEN);
down.HideBubble();
down.HideTitle();
hi.SetDefaultColor(Color.red);
hi.HideBubble();
hi.HideTitle();
lo.SetDefaultColor(Color.GREEN);
lo.HideBubble();
lo.HideTitle();
UpSignal.SetDefaultColor(Color.WHITE);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.WHITE);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
UpSignal.SetLineWeight(1);
DownSignal.SetLineWeight(1);
BullTrendSignalStart.SetDefaultColor(Color.green);
BullTrendSignalStart.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BullTrendSignalStart.SetLineWeight(1);
BullTrendsignalEnd.SetDefaultColor(Color.red);
BullTrendsignalEnd.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
BullTrendsignalEnd.SetLineWeight(1);
BearTrendSignalStart.SetDefaultColor(Color.red);
BearTrendSignalStart.SetPaintingStrategy(PaintingStrategy.ARROW_down);
BearTrendSignalStart.SetLineWeight(1);
BearTrendsignalEnd.SetDefaultColor(Color.green);
BearTrendsignalEnd.SetPaintingStrategy(PaintingStrategy.ARROW_up);
BearTrendsignalEnd.SetLineWeight(1);
AddCloud(150, 100, Color.DARK_RED, Color.DARK_RED);
AddCloud(-150, -100, Color.DARK_GREEN, Color.DARK_GREEN);
AddCloud(CCI2, bulltrend, color.green, color.current);
AddCloud(CCI2, Beartrend, color.current, color.red);
AddCloud(CCI2, 150, color.cyan, color.current);
AddCloud(CCI2, -150, color.current, color.magenta);
# DIVERGENCE
input divergenceLength = 35; #hint divergenceLength: The number of bars used to calculate divergences.
input divergenceType = {default regular, reverse}; #hint divergenceType: The type of divergence. A regular divergence is when price is making higher highs (or lower lows), while the indicator is making lower highs (or higher lows). A reverse divergence (also called a hidden divergence) is when the indicator is making higher highs (or lower lows), while price is making lower highs (or higher lows).
#Hint: The output of this indicator is for informational and educational use only, is not an investment recommendation or advice, and should not be relied upon in making the decision to buy or sell a security or pursue a particular investment strategy.
def xDownBars;
def xUpBars;
def xDowns;
def xUps;
def hiBars;
def loBars;
def pivotTop;
def pivotBottom;
def hiInd;
def loInd;
def hiPrice;
def loPrice;
plot bearishd;
plot bullishd;
def K = CCI2;
def Over_Boughta = 100;
def Over_Solda = -100;
#K.SetDefaultColor(color.white);
#K.SetLineWeight(2);
def ind;
ind = k;
# Bearish
pivotTop =
if
divergenceType == divergenceType.regular
then
ind[1] > over_Boughta and ind[1] == Highest(ind, divergenceLength + 1)
else
ind[1] >= 50 and
ind[1] == highest(ind, divergenceLength + 1) and
ind[1] == highest(ind, divergenceLength+1)[-divergenceLength+1];
if pivotTop
then {
hiBars = 1;
hiInd = ind[1];
hiPrice = max(high[2], max(high[1], high[0]));
}
else {
hiBars = hiBars[1] + 1;
hiInd = hiInd[1];
hiPrice = hiPrice[1];
}
if ind[1] crosses below Over_Boughta
then {
xDownBars = 1;
xDowns = xDowns[1] + 1;
}
else {
xDownBars = xDownBars[1] + 1;
xDowns = if pivotTop[1] then 0 else xDowns[1];
}
def bearCond;
switch (divergenceType) {
case regular:
bearCond =
ind[1] >= zeroline and
ind < ind[1] and
high[1] == Highest(high, divergenceLength + 1) and
hiBars[1] > xDownBars[1] and
xDowns == 1 and
close < close[1] and
hiPrice[1] < high[1] and
hiInd[1] > ind[1];
case reverse:
bearCond =
ind[1] >= zeroline and
ind < ind[1] and
# high[1] == Highest(high, divergenceLength) and
# hiBars[1] > xDownBars[1] and
# xDowns == 1 and
close < close[1] and
hiPrice[1] > high[1] and hiPrice[1] > high and
hiInd[1] < ind[1];}
bearishd =
if
bearCond
then
ind[1]
else
Double.NaN;;
bearishd.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
bearishd.SetDefaultColor(Color.RED);
bearishd.setLineWeight(3);
bearishd.hideTitle();
bearishd.hideBubble();
def countBear = if bearCond[-1] then countBear[1] + 1 else countBear[1];
def recentBear = countBear == HighestAll(countBear);
def secHigh = highestAll(if bearCond[-1] and recentBear then ind else Double.NaN);
#def firstHigh = highestAll(if bearCond and recentBear and ind[1] == secHigh then hiInd[1] else double.NaN);
def FH_bar = highestAll(if recentBear and bearCond[-1] and ind == secHigh then getvalue(barNumber(), hibars) else double.NaN);
plot bearTrendline =
if
recentBear and bearCond[-1] and ind == secHigh
then
max(ind[1], ind[0])
else
# if pivotTop and hiInd == firstHigh
if
FH_bar == barNumber()
then
ind
else
double.NaN;
bearTrendline.EnableApproximation();
bearTrendline.setDefaultColor(color.RED);
bearTrendline.setLineWeight(4);
bearTrendline.hideBubble();
bearTrendline.hideTitle();
#Bullish
pivotBottom =
if
divergenceType == divergenceType.regular
then
ind[1] < over_Solda and ind[1] == lowest(ind, divergenceLength + 1)
else
ind[1] <= 50 and
ind[1] == lowest(ind, divergenceLength+1) and
ind[1] == lowest(ind, divergenceLength+1)[-divergenceLength+1];
if pivotBottom
then {
loBars = 1;
loInd = ind[1];
loPrice = min(low[2], min(low[1], low[0]));
}
else {
loBars = loBars[1] + 1;
loInd = loInd[1];
loPrice = loPrice[1];
}
if ind[1] crosses above over_Solda
then {
xUpBars = 1;
xUps = xUps[1] + 1;
}
else {
xUpBars = xUpBars[1] + 1;
xUps = if pivotBottom[1] then 0 else xUps[1];
}
def bullCond;
switch (divergenceType){
case regular:
bullCond =
ind[1] <= zeroline and
ind > ind[1] and
low[1] == Lowest(low, divergenceLength + 1) and
loBars[1] > xUpBars[1] and
xUps == 1 and
close > close[1] and
loPrice[1] > low[1] and
loInd[1] < ind[1];
case reverse:
bullCond =
ind[1] <= zeroline and
ind > ind[1] and
# low[1] == Lowest(low, divergenceLength) and
# loBars[1] > xUpBars[1] and
# xUps == 1 and
close > close[1] and
loPrice[1] < low[1] and loPrice[1] < low and
loInd[1] > ind[1];}
bullishd =
if
bullCond
then
ind[1]
else
Double.NaN;
bullishd.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
bullishd.SetDefaultColor(Color.GREEN);
bullishd.setLineWeight(3);
bullishd.HideTitle();
bullishd.HideBubble();
def countBull = if bullCond[-1] then countBull[1] + 1 else countBull[1];
def recentBull = countBull == HighestAll(countBull);
def secLow = highestAll(if bullCond[-1] and recentBull then ind else Double.NaN);
#def firstLow = highestAll(if bullCond and recentBull and ind[1] == secLow then loInd[1] else double.NaN);
def FL_bar = highestAll(if recentBull and bullCond[-1] and ind == secLow then getvalue(barNumber(), lobars) else double.NaN);
plot bullTrendline =
if
recentBull and bullCond[-1] and ind == secLow
then
min(ind[1], ind[0])
else
if
# pivotBottom and loInd == firstLow
FL_bar == barNumber()
then
ind[0]
else
double.NaN;
bullTrendline.EnableApproximation();
bullTrendline.setDefaultColor(color.GREEN);
bullTrendline.setLineWeight(4);
bullTrendline.hideBubble();
bullTrendline.hideTitle();
#End of Code
Last edited: