clickchamps
New member
Can any assist me in writing a CustomScript to put in a watchlist that would count the number of bars the Mobo Band has turn Green or Red. Right now I can get a count if stays outside the bands but once it goes inside it goes to 0 and restarts when it goes outside in the direction of the trend, other it goes to count as expect on the breakout on the other side but reset to 0 on a visit back into the band.
Its a lot of extra code from a Mobo Study I found here
https://usethinkscript.com/threads/momentum-bollinger-band-channels-for-thinkorswim.5977/
---------------------------------------------------------------------------------------
#Momentum Bollinger band channel v1.0 by XeoNoX via usethinkscript.com
#Request 03-16-2021
#Based of Pensar's Keltner channel Momentum bands via usethinkscript.com
#which is based on FW-MOBO-Advanced https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/E-F/FW-MOBO-Advanced
#Inputs
input factor = 1;
input price = close;
input type = AverageType.simple;
input pricecolor = yes;
input fill = yes;
input arrows = yes;
input alerts = yes;
input sound = {default "Ding", "Bell", "Chimes", "NoSound", "Ring"};
input displace = 0;
input length = 13;
input Num_Dev_Dn = -.8;
input Num_Dev_up = .8;
input averageType = AverageType.simple;
def sDev = StDev(data = price[-displace], length = length);
#Variables
def nan = Double.NaN;
def shift = factor * MovingAverage(type, TrueRange(high, close, low), length);
plot avg = MovingAverage(averageType, data = price[-displace], length = length);
def line1 = avg + Num_Dev_Dn * sDev;
def line2 = avg + Num_Dev_up * sDev;
def Chg = If(close > line2, 1, If(close < line1, -1, 0));
def Hold = CompoundValue(1, If(Hold[1] == Chg or Chg == 0, Hold[1], If(Chg == 1, 1, -1)), 0);
def ArUp = if !arrows or Hold[0] == Hold[1] then nan else if Hold[0] == 1 then line1 else nan;
def ArDn = if !arrows or Hold[0] == Hold[1] then nan else if Hold[0] == -1 then line2 else nan;
def LBUp = if fill and Hold[0] == 1 then line2 else nan;
def UBUp = if fill and Hold[0] == 1 then line1 else nan;
def LBDn = if fill and Hold[0] == -1 then line2 else nan;
def UBDn = if fill and Hold[0] == -1 then line1 else nan;
def AlertUp = alerts and Hold[1] == 1 and (Hold[1] <> Hold[2]);
def AlertDn = alerts and Hold[1] == -1 and (Hold[1] <> Hold[2]);
#Colors
DefineGlobalColor("Cloud Up", Color.DARK_GREEN);
DefineGlobalColor("Cloud Dn", Color.DARK_RED);
DefineGlobalColor("Channel Up", Color.Dark_Gray);
DefineGlobalColor("Channel Down", Color.dark_gray);
DefineGlobalColor("Price Up", Color.GREEN);
DefineGlobalColor("Price Neutral", Color.GRAY);
DefineGlobalColor("Price Down", Color.RED);
#Clouds
AddCloud(LBUp, UBUp, GlobalColor("Cloud Up"), GlobalColor("Cloud Dn"));
AddCloud(LBDn, UBDn, GlobalColor("Cloud Dn"), GlobalColor("Cloud Up"));
#Price Color
AssignPriceColor(if pricecolor then if close > line2 and close > open then GlobalColor("Price Up")
else if close > line2 and close < open then color.dark_green
else if close < line1 and close < open then GlobalColor("Price Down")
else if Close < line1 and close > open then color.dark_RED
else if close > open then color.white
else GlobalColor ("Price Neutral")
else Color.CURRENT);
#Alerts
Alert(AlertUp, "BREAKOUT!", Alert.BAR, Sound);
Alert(AlertDn, "BREAKDOWN!", Alert.BAR, Sound);
def barUp = chg==1;
def barDown = chg==-1;
def barUpCount = CompoundValue(1, if barUp then barUpCount[1] + 1 else 0, 0);
def pBarUpCount = if barUpCount > 0 then barUpCount else nan;
def barDownCount = CompoundValue(1, if bardown then barDownCount[1] - 1 else 0, 0);
def pBarDownCount = if barDownCount < 0 then barDownCount else nan;
AddLabel(yes, (Round(barDownCount + barUpCount, 1)), if barUpCount > 1 then Color.GREEN else if barDownCount < -1 then Color.RED else color.blue);
#AssignBackgroundColor( if BarUp and barUpCount >= 2 then Color.dark_GRAY else if bardownCount #<= -2 then Color.plum else if BarUp and barUpCount < 2 then Color.dark_Green else if BarDown #and barDownCount > -2 then Color.Red else color.black);
AssignBackgroundColor( if Close < Line1 then Color.plum else if close > line1 and close < avg then color.magenta else if Close > Line2 then color.dark_green else if close < line2 and close > avg then color.lime else color.dark_gray)
Its a lot of extra code from a Mobo Study I found here
https://usethinkscript.com/threads/momentum-bollinger-band-channels-for-thinkorswim.5977/
---------------------------------------------------------------------------------------
#Momentum Bollinger band channel v1.0 by XeoNoX via usethinkscript.com
#Request 03-16-2021
#Based of Pensar's Keltner channel Momentum bands via usethinkscript.com
#which is based on FW-MOBO-Advanced https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/E-F/FW-MOBO-Advanced
#Inputs
input factor = 1;
input price = close;
input type = AverageType.simple;
input pricecolor = yes;
input fill = yes;
input arrows = yes;
input alerts = yes;
input sound = {default "Ding", "Bell", "Chimes", "NoSound", "Ring"};
input displace = 0;
input length = 13;
input Num_Dev_Dn = -.8;
input Num_Dev_up = .8;
input averageType = AverageType.simple;
def sDev = StDev(data = price[-displace], length = length);
#Variables
def nan = Double.NaN;
def shift = factor * MovingAverage(type, TrueRange(high, close, low), length);
plot avg = MovingAverage(averageType, data = price[-displace], length = length);
def line1 = avg + Num_Dev_Dn * sDev;
def line2 = avg + Num_Dev_up * sDev;
def Chg = If(close > line2, 1, If(close < line1, -1, 0));
def Hold = CompoundValue(1, If(Hold[1] == Chg or Chg == 0, Hold[1], If(Chg == 1, 1, -1)), 0);
def ArUp = if !arrows or Hold[0] == Hold[1] then nan else if Hold[0] == 1 then line1 else nan;
def ArDn = if !arrows or Hold[0] == Hold[1] then nan else if Hold[0] == -1 then line2 else nan;
def LBUp = if fill and Hold[0] == 1 then line2 else nan;
def UBUp = if fill and Hold[0] == 1 then line1 else nan;
def LBDn = if fill and Hold[0] == -1 then line2 else nan;
def UBDn = if fill and Hold[0] == -1 then line1 else nan;
def AlertUp = alerts and Hold[1] == 1 and (Hold[1] <> Hold[2]);
def AlertDn = alerts and Hold[1] == -1 and (Hold[1] <> Hold[2]);
#Colors
DefineGlobalColor("Cloud Up", Color.DARK_GREEN);
DefineGlobalColor("Cloud Dn", Color.DARK_RED);
DefineGlobalColor("Channel Up", Color.Dark_Gray);
DefineGlobalColor("Channel Down", Color.dark_gray);
DefineGlobalColor("Price Up", Color.GREEN);
DefineGlobalColor("Price Neutral", Color.GRAY);
DefineGlobalColor("Price Down", Color.RED);
#Clouds
AddCloud(LBUp, UBUp, GlobalColor("Cloud Up"), GlobalColor("Cloud Dn"));
AddCloud(LBDn, UBDn, GlobalColor("Cloud Dn"), GlobalColor("Cloud Up"));
#Price Color
AssignPriceColor(if pricecolor then if close > line2 and close > open then GlobalColor("Price Up")
else if close > line2 and close < open then color.dark_green
else if close < line1 and close < open then GlobalColor("Price Down")
else if Close < line1 and close > open then color.dark_RED
else if close > open then color.white
else GlobalColor ("Price Neutral")
else Color.CURRENT);
#Alerts
Alert(AlertUp, "BREAKOUT!", Alert.BAR, Sound);
Alert(AlertDn, "BREAKDOWN!", Alert.BAR, Sound);
def barUp = chg==1;
def barDown = chg==-1;
def barUpCount = CompoundValue(1, if barUp then barUpCount[1] + 1 else 0, 0);
def pBarUpCount = if barUpCount > 0 then barUpCount else nan;
def barDownCount = CompoundValue(1, if bardown then barDownCount[1] - 1 else 0, 0);
def pBarDownCount = if barDownCount < 0 then barDownCount else nan;
AddLabel(yes, (Round(barDownCount + barUpCount, 1)), if barUpCount > 1 then Color.GREEN else if barDownCount < -1 then Color.RED else color.blue);
#AssignBackgroundColor( if BarUp and barUpCount >= 2 then Color.dark_GRAY else if bardownCount #<= -2 then Color.plum else if BarUp and barUpCount < 2 then Color.dark_Green else if BarDown #and barDownCount > -2 then Color.Red else color.black);
AssignBackgroundColor( if Close < Line1 then Color.plum else if close > line1 and close < avg then color.magenta else if Close > Line2 then color.dark_green else if close < line2 and close > avg then color.lime else color.dark_gray)
Attachments
Last edited by a moderator: