#study(shorttitle="CCI & BB", title="CCI and Bollinger Bands")
# Converted by [email protected] - 11/2022
declare lower;
#//input ------
input alerts = yes;
input sound = {default "NoSound", "Ding", "Bell", "Chimes", "Ring"};
input cci_len = 14; # "CCI length"
input cci_src = close; # "CCI source"
input length = 14; # "BB length"
input mult = 2.0; # "Deviations"
input adD_8_lines = no;
input line1 = 100;
input line2 = 200;
input line3 = 300;
input line4 = 400;
def na = Double.NaN;
#//CCI -----
def price = cci_src;
def linDev = lindev(price, cci_len);
def nCCI = if linDev == 0 then 0 else (price - Average(price, cci_len)) / linDev / 0.015;
def cci =nCCI;
#//BB -----
def bb_src = cci;
def basis = Average(bb_src, length);
def dev = mult * stdev(bb_src, length);
def upper = basis + dev;
def lower = basis - dev;
#//bgcolor -----
def c1 = cci > upper;
def c2 = cci < upper and cci > basis;
def c3 = cci > lower and cci < basis;
def c4 = cci < lower;
def color = if c1 then 2 else if c2 then 1 else if c3 then -1 else -2;#? #F8B856 : c2 ? #F6AD3C : c3 ? #F5A21B : #F39800
#def fill_bb_color = c1 ? lime : c2 ? green : c3 ? maroon : red
def cross_color_1 = if crosses(cci, 100, CrossingDirection.ABOVE) then 100 else
if crosses(cci, -100, CrossingDirection.ABOVE) then -100 else na;
def cross_color_2 = if crosses(cci, 100, CrossingDirection.BELOW) then 100 else
if crosses(cci, -100, CrossingDirection.BELOW) then -100 else na;
plot SigUp = cross_color_1;
plot SigDn = cross_color_2;
SigUp.SetPaintingStrategy(PaintingStrategy.POINTS);
SigUp.SetDefaultColor(Color.GREEN);
SigUp.SetLineWeight(3);
SigDn.SetPaintingStrategy(PaintingStrategy.POINTS);
SigDn.SetDefaultColor(Color.RED);
SigDn.SetLineWeight(3);
#//plot -----
plot p1 = upper; # "uppper band"
p1.SetDefaultColor(CreateColor(0,153,204));
plot p2 = lower; # "lower band"
p2.SetDefaultColor(CreateColor(0,153,204));
plot CenterLine = basis; # "center line"
CenterLine.SetDefaultColor(CreateColor(0,153,204));
CenterLine.SetStyle(Curve.MEDIUM_DASH);
plot cciLine = cci; # "CCI"
cciLine.SetDefaultColor(Color.ORANGE);
cciLine.SetLineWeight(2);
plot ZeroLine = if isNaN(close) then na else 0; # "zero line"
ZeroLine.SetDefaultColor(Color.DARK_GRAY);
ZeroLine.SetStyle(Curve.SHORT_DASH);
plot h1 = if isNaN(close) then na else 100; # "upper line"
h1.SetDefaultColor(Color.DARK_GRAY);
h1.SetStyle(Curve.MEDIUM_DASH);
plot h2 = if isNaN(close) then na else -100; # "lower line"
h2.SetDefaultColor(Color.DARK_GRAY);
h2.SetStyle(Curve.MEDIUM_DASH);
AddCloud(h1, h2, color.DARK_GRAY); # "CCI fill"
AddCloud(if color==2 then p1 else na, p2, Color.GREEN); #"BB fill")
AddCloud(if color==1 then p1 else na, p2, Color.DARK_GREEN); #"BB fill")
AddCloud(if color==-1 then p1 else na, p2, Color.DARK_RED); #"BB fill")
AddCloud(if color==-2 then p1 else na, p2, Color.RED); #"BB fill")
plot "1" = if isNaN(close) then na else if(!adD_8_lines, na,line1);
plot "2" = if isNaN(close) then na else if(!adD_8_lines, na,line2);
plot "3" = if isNaN(close) then na else if(!adD_8_lines, na,line3);
plot "4" = if isNaN(close) then na else if(!adD_8_lines, na,line4);
plot "-1"= if isNaN(close) then na else if(!adD_8_lines, na,-line1);
plot "-2"= if isNaN(close) then na else if(!adD_8_lines, na,-line2);
plot "-3"= if isNaN(close) then na else if(!adD_8_lines, na,-line3);
plot "-4"= if isNaN(close) then na else if(!adD_8_lines, na,-line4);
"1".SetDefaultColor(Color.DARK_GRAY);
"2".SetDefaultColor(Color.DARK_GRAY);
"3".SetDefaultColor(Color.DARK_GRAY);
"4".SetDefaultColor(Color.DARK_GRAY);
"-1".SetDefaultColor(Color.DARK_GRAY);
"-2".SetDefaultColor(Color.DARK_GRAY);
"-3".SetDefaultColor(Color.DARK_GRAY);
"-4".SetDefaultColor(Color.DARK_GRAY);
#---- Alerts
def CrossUp = Crosses(cciLine, p1);
def CrossDn = Crosses(cciLine, p2);
Alert(alerts and CrossUp, "CCI Crosses Upper Band", Alert.BAR, sound);
Alert(alerts and CrossDn, "CCI Crosses Lower Band", Alert.BAR, sound);
#--- END Code