Bollinger Bands Format, Watchlist, Label, Scan For ThinkOrSwim

I want to scan my watchlist (over 300 symbols) to get a count of how many symbols have candles that have crossed the upper (or lower) Bollinger band, and are now heading in the opposite direction. I would like this for the Daily time frame. I find that when that happens to a large number of symbols, it is very likely a "moving day" where trades are plentiful. Any help with this is appreciated.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

I want to scan my watchlist (over 300 symbols) to get a count of how many symbols have candles that have crossed the upper (or lower) Bollinger band, and are now heading in the opposite direction. I would like this for the Daily time frame. I find that when that happens to a large number of symbols, it is very likely a "moving day" where trades are plentiful. Any help with this is appreciated.


when the goal is to make a scan, i start with a chart study, so i can see things to verify what is happening. either an upper or lower, depending on primary study used.
then i make a lower study that plots 1 or 0. this should work in a scan. i don't scan so i didn't test it.

------------------

lower / scan code
this has an output of 1 or 0, when price is above the bands or below the bands and reversing.


Code:
# bb_bands_cross_rev_00b_lower


#https://usethinkscript.com/threads/need-a-bollinger-band-study-and-or-scan.14603/
#Need a Bollinger Band study and/or scan
#ttsdmagic 
#2/24

#I want to scan my watchlist (over 300 symbols) to get a count of how many symbols have candles that have crossed the upper (or lower) Bollinger band, and are now heading in the opposite direction. I would like this for the Daily time frame. I find that when that happens to a large number of symbols, it is very likely a "moving day" where trades are plentiful. Any help with this is appreciated.


#   assign # for each step
# 2 = close > upper Bollinger band  and  close < close[1] (dropping)
# 1 = close crossed above upper Bollinger band and close > close[1] (rising)
# 0 = cancel ... #close crosses below upper  or   close crosses above lower
# -1 = close crossed below lower Bollinger band and close < close[1] (dropping)
# -2 = close < lower Bollinger band  and  close > close[1] (rising)


declare lower;

def bn = barnumber();
def na = double.nan;

def bbupper = BollingerBands().UpperBand;
def bblower = BollingerBands().LowerBand;
def bbmid = BollingerBands().MidLine;


def t = if bn == 1 then 0
  else if close crosses below bbmid or close crosses above bbmid then 0
  else if close > bbupper and close[1] < bbupper then 1
  else if close < bblower and close[1] > bblower then -1
  else if t[1] == 1 and close < close[1] then 2
  else if t[1] == -1 and close > close[1] then -2
  else t[1];


# wedges - crossing
def z1 = if t == 1 and t[1] != 1 then 1 else 0;
def z2 = if t == -1 and t[1] != -1 then 1 else 0;

#  arrows - reversal
def z3 = if t == 2 and t[1] != 2 then 1 else 0;
def z4 = if t == -2 and t[1] != -2 then 1 else 0;

# reversal signals
plot z = if z3 or z4 then 1 else 0;


addlabel(0,
(if z1 then "xup"
else if z2 then "xdwn"
else if z3 then "REV dwn"
else if z4 then "REV up"
else "-")
, (if z1 then color.cyan
else if z2 then color.yellow
else if z3 then color.red
else if z4 then color.green
else color.gray)
);

#

upper and lower studies
wedges on crossing of a bb line
arrows when price reverses, after a crossing
c8OoSWz.jpg



-------------------


upper study for testing


Code:
# bb_bands_cross_rev_00b_upper

#https://usethinkscript.com/threads/need-a-bollinger-band-study-and-or-scan.14603/
#Need a Bollinger Band study and/or scan

def bn = barnumber();
def na = double.nan;


#def green = close > close[1];
#def red = close < close[1];

def bbupper = BollingerBands().UpperBand;
def bblower = BollingerBands().LowerBand;
def bbmid = BollingerBands().MidLine;

input show_bb_lines = yes;
plot bu = bbupper;
plot bm = bbmid;
plot bl = bblower;


#   assign # for each step
# 2 = close > upper Bollinger band  and  close < close[1] (dropping)
# 1 = close crossed above upper Bollinger band and close > close[1] (rising)
# 0 = cancel ... #close crosses below upper  or   close crosses above lower
# -1 = close crossed below lower Bollinger band and close < close[1] (dropping)
# -2 = close < lower Bollinger band  and  close > close[1] (rising)


def t = if bn == 1 then 0
  else if close crosses below bbmid or close crosses above bbmid then 0
  else if close > bbupper and close[1] < bbupper then 1
  else if close < bblower and close[1] > bblower then -1
  else if t[1] == 1 and close < close[1] then 2
  else if t[1] == -1 and close > close[1] then -2
  else t[1];


plot z1 = if t == 1 and t[1] != 1 then 1 else 0;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_up);
z1.SetDefaultColor(Color.yellow);
z1.setlineweight(2);
z1.hidebubble();

plot z2 = if t == -1 and t[1] != -1 then 1 else 0;
z2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
z2.SetDefaultColor(Color.yellow);
z2.setlineweight(2);
z2.hidebubble();


#  arrows
plot z3 = if t == 2 and t[1] != 2 then high else na;
z3.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
z3.SetDefaultColor(Color.red);
z3.setlineweight(3);
z3.hidebubble();

plot z4 = if t == -2 and t[1] != -2 then low else na;
z4.SetPaintingStrategy(PaintingStrategy.ARROW_up);
z4.SetDefaultColor(Color.green);
z4.setlineweight(3);
z4.hidebubble();
#

-------------------

watchlist study



column study

zbbxrev
http://tos.mx/Lf4aZln

Code:
# zbbxrev

# chg lower to a column
# bb_bands_cross_rev_00b_lower

#https://usethinkscript.com/threads/need-a-bollinger-band-study-and-or-scan.14603/
#Need a Bollinger Band study and/or scan

#   assign # for each step
# 2 = close > upper Bollinger band  and  close < close[1] (dropping)
# 1 = close crossed above upper Bollinger band and close > close[1] (rising)
# 0 = cancel ... #close crosses below upper  or   close crosses above lower
# -1 = close crossed below lower Bollinger band and close < close[1] (dropping)
# -2 = close < lower Bollinger band  and  close > close[1] (rising)

def bn = barnumber();
def na = double.nan;

def bbupper = BollingerBands().UpperBand;
def bblower = BollingerBands().LowerBand;
def bbmid = BollingerBands().MidLine;

def t = if bn == 1 then 0
  else if close crosses below bbmid or close crosses above bbmid then 0
  else if close > bbupper and close[1] < bbupper then 1
  else if close < bblower and close[1] > bblower then -1
  else if t[1] == 1 and close < close[1] then 2
  else if t[1] == -1 and close > close[1] then -2
  else t[1];

# find when t changes
def z1 = if t == 1 and t[1] != 1 then 1 else 0;
def z2 = if t == -1 and t[1] != -1 then 1 else 0;
def z3 = if t == 2 and t[1] != 2 then 1 else 0;
def z4 = if t == -2 and t[1] != -2 then 1 else 0;


addlabel(1,
(if z1 then "xup"
else if z2 then "xdwn"
else if z3 then "REV dwn"
else if z4 then "REV up"
else "-")
, color.black);


assignbackgroundcolor( 
 (if z1 then color.cyan
else if z2 then color.yellow
else if z3 then color.red
else if z4 then color.green
else color.gray)
);

#



-------------------------

ref
https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/A-B/BollingerBands
Plots
. MidLine
. LowerBand
. UpperBand
 
I need help coding for a bubble on the right side of the screen and a line to show the Bollinger Band level. If it above the upper BB changes color from Red to Green and visa versa. Need to be able to set the BB level.
 
Last edited:
Reaching out for coding for a BB label to be displayed on the right side of the screen. Positive BB label displayed in green. Negative BB label to be displayed in red. With an adjustable period and number devin the input section. For all those who have assisted me with coding in the past, all have worked and I am appreciative of your assistance
 
I'm running into a similar issue with the wrong type cast error when attempting a script that displays Bollinger Bands as a label. The error comes at the very end:

ADDLABEL(yes, "UpperBand " + " " OR "LowerBand " + " " OR " ", IF close > UpperBand THEN "UpperBand " + " " AND COLOR.WHITE
ELSE IF close < LowerBand
THEN "LowerBand " + " " AND COLOR.RED
ELSE " " AND CREATECOLOR (255, 255, 51));
 
I'm running into a similar issue with the wrong type cast error when attempting a script that displays Bollinger Bands as a label. The error comes at the very end:

ADDLABEL(yes, "UpperBand " + " " OR "LowerBand " + " " OR " ", IF close > UpperBand THEN "UpperBand " + " " AND COLOR.WHITE
ELSE IF close < LowerBand
THEN "LowerBand " + " " AND COLOR.RED
ELSE " " AND CREATECOLOR (255, 255, 51));

https://usethinkscript.com/threads/...st-label-scan-for-thinkorswim.762/#post-11745
 
Last edited:
Hi I was trying to make a Strategy for buying Forex based on BB and RSI for the minute chart but I'm getting errors from thinkscript, and I don't know how to fix them. Can anyone help me and maybe give me some suggestions on how to improve the strategy?

No such constant: bb.lowerband at 15:17
No such constant: bb.upperband at 16:17
No such constant: bb.lowerband at 15:17
Expected double at 15:5
No such constant: bb.upperband at 16:17
Expected double at 16:5
LOWER not allowed in strategies
Only constants expected here: _inline_referenced_param_bSRP_141_length CL constant function parameter 'length' at 18:16
Only constants expected here: _inline_referenced_param_bSRP_141_length CL constant function parameter 'length' at 18:16
No such constant: bb.lowerband at 15:17
No such constant: bb.upperband at 16:17
No such constant: bb.lowerband at 15:17
Expected double at 15:5
No such constant: bb.upperband at 16:17
Expected double at 16:5
Only constants expected here: _inline_referenced_param_bSRP_141_length CL constant function parameter 'length' at 18:16
Only constants expected here: _inline_referenced_param_bSRP_141_length CL constant function parameter 'length' at 18:16
 

Attachments

  • RSI-BB-FOREX.txt
    1.7 KB · Views: 174
Hi I was trying to make a Strategy for buying Forex based on BB and RSI for the minute chart but I'm getting errors from thinkscript, and I don't know how to fix them. Can anyone help me and maybe give me some suggestions on how to improve the strategy?

No such constant: bb.lowerband at 15:17
No such constant: bb.upperband at 16:17
No such constant: bb.lowerband at 15:17
Expected double at 15:5
No such constant: bb.upperband at 16:17
Expected double at 16:5
LOWER not allowed in strategies
Only constants expected here: _inline_referenced_param_bSRP_141_length CL constant function parameter 'length' at 18:16
Only constants expected here: _inline_referenced_param_bSRP_141_length CL constant function parameter 'length' at 18:16
No such constant: bb.lowerband at 15:17
No such constant: bb.upperband at 16:17
No such constant: bb.lowerband at 15:17
Expected double at 15:5
No such constant: bb.upperband at 16:17
Expected double at 16:5
Only constants expected here: _inline_referenced_param_bSRP_141_length CL constant function parameter 'length' at 18:16
Only constants expected here: _inline_referenced_param_bSRP_141_length CL constant function parameter 'length' at 18:16



errors in strategy,
it lists all the errors at the bottom of code. look at them and go look at the code line.

can't use lower in a strategy
declare lower;

----------------

this is wrong. should have the plot plot name after the () . can't use it on a variable
def bb = BollingerBands(price, length = bbLength, numDevDn = numDevDn, numDevUp = numDevUp);
def lowerBand = bb.lowerBand;
def upperBand = bb.upperBand;

fixed
def bb_lower = BollingerBands(price, length = bbLength, numDevDn = numDevDn, numDevUp = numDevUp).lowerBand;
def bb_upper = BollingerBands(price, length = bbLength, numDevDn = numDevDn, numDevUp = numDevUp).upperBand;

----------------

wrong parameters in RSI(). you didn't specify parameter names , so they have to be in the correct order and quantity. price is 4th parameter, so would need to specify the 3 before price (without names)
def rsiValue = RSI(close, rsiLength);

fixed
def rsiValue = RSI(length = rsiLength, price = close);

go here and learn about the RSI study & parameters
https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/R-S/RSI

-------------

you are missing addorder to buy on open
addorders are missing parameters and don't have all the names listed


addorders, you have 2 sell to close. i would separate the conditions in first one, so each just has 1 condition. one for target and 1 for stop

AddOrder(OrderType.SELL_TO_CLOSE, sellCondition or stopLossCondition, tickcolor = GetColor(8), arrowcolor = GetColor(8), name = "Sell");


added addorder for buy


----------------

Code:
#help_fix_strat2

#https://usethinkscript.com/threads/help-with-strategy-code.18069/
#Help With Strategy Code
#Jank  2/26  #1
#Hi I was trying to make a Strategy for buying Forex based on BB and RSI for the minute chart but I'm getting errors from thinkscript, and I don't know how to fix them. Can anyone help me and maybe give me some suggestions on how to improve the strategy?

# Bollinger Bands and RSI Strategy with Visual Signals

#declare lower;

input bbLength = 20;
input numDevDn = -2.0;
input numDevUp = 2.0;
input rsiLength = 14;
input oversoldThreshold = 40;
input overboughtThreshold = 65;
input stopLossPercent = 5.0;

def price = close;
#def bb = BollingerBands(price, length = bbLength, numDevDn = numDevDn, numDevUp = numDevUp);
#def lowerBand = bb.lowerBand;
#def upperBand = bb.upperBand;

def bb_lower = BollingerBands(price, length = bbLength, numDevDn = numDevDn, numDevUp = numDevUp).lowerBand;
def bb_upper = BollingerBands(price, length = bbLength, numDevDn = numDevDn, numDevUp = numDevUp).upperBand;


#def rsiValue = RSI(close, rsiLength);
def rsiValue = RSI(length = rsiLength, price = close);


def buyCondition = close < bb_lower and rsiValue <= oversoldThreshold;
def sellCondition = close > bb_upper and rsiValue >= overboughtThreshold;

# Buy Signal
plot BuySignal = buyCondition;
BuySignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BuySignal.SetDefaultColor(Color.GREEN);

# Sell Signal
plot SellSignal = sellCondition;
SellSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SellSignal.SetDefaultColor(Color.RED);

# Stop Loss
def entryPrice = if buyCondition and !buyCondition[1] then close else entryPrice[1];
def stopLossLevel = entryPrice * (1.0 - stopLossPercent / 100);
def stopLossCondition = close <= stopLossLevel;

input tradesize = 1;

# Exit on Stop Loss
#AddOrder(OrderType.SELL_TO_CLOSE, sellCondition or stopLossCondition, tickcolor = GetColor(8), arrowcolor = GetColor(8), name = "Sell");

AddOrder(OrderType.SELL_TO_CLOSE, stopLossCondition, tickcolor = GetColor(8), arrowcolor = GetColor(8), name = "Sell");

# Exit on Target
AddOrder(OrderType.SELL_TO_CLOSE, sellCondition, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "Sell");

# Set Stop Loss Label
AddLabel(stopLossCondition, "Stop Loss", Color.RED);

# Plot visual signals on the chart
AddChartBubble(buyCondition, low, "Buy", Color.GREEN, no);
AddChartBubble(sellCondition or stopLossCondition, high, "Sell", Color.RED, yes);


addorder(type = OrderType.buy_to_open, condition = buyCondition, price = open[-1], tradesize = tradesize,  tickcolor = Color.green, arrowcolor = Color.green, name = "BUY");

#
Capture.JPG
 
Last edited by a moderator:
Would it be possible to help me create a custom watchlist that displays 3 colors. Green for when price close below bollinger band, Red for when above, and Yellow for when in between? or perhaps there is something already made.

Many thanks!
 
Last edited by a moderator:
Try the following code...

Ruby:
def price = close;
def displace = 0;
def length = 20;
def Num_Dev_Dn = -2.0;
def Num_Dev_up = 2.0;

def upperBand = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_up, AverageType.SIMPLE)."UpperBand";
def lowerBand = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_up, AverageType.SIMPLE)."LowerBand";

AddLabel(yes, " ");

AssignBackgroundColor(if price is greater than upperBand then Color.RED else if price is less than lowerBand then Color.GREEN else Color.YELLOW);
 
Try the following code...

Ruby:
def price = close;
def displace = 0;
def length = 20;
def Num_Dev_Dn = -2.0;
def Num_Dev_up = 2.0;

def upperBand = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_up, AverageType.SIMPLE)."UpperBand";
def lowerBand = BollingerBands(price, displace, length, Num_Dev_Dn, Num_Dev_up, AverageType.SIMPLE)."LowerBand";

AddLabel(yes, " ");

AssignBackgroundColor(if price is greater than upperBand then Color.RED else if price is less than lowerBand then Color.GREEN else Color.YELLOW);
Perfect!! Many thanks!
 
Good morning, could someone help me? I'm trying to add shadow to the Bollinger bands in Thinkorswim, but it always comes out light red. I want to be able to choose the color of the shadow and for it to be transparent. Thank you very much. If someone can help me, I'll attach the study that I modified.

Ruby:
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;


def sDev = stdev(data = price[-displace], length = length);

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;
AddCloud(LowerBand,UpperBand,Color.lIME); 
LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));
 
Last edited by a moderator:
Good morning, could someone help me? I'm trying to add shadow to the Bollinger bands in Thinkorswim, but it always comes out light red. I want to be able to choose the color of the shadow and for it to be transparent. Thank you very much. If someone can help me, I'll attach the study that I modified.

Ruby:
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;


def sDev = stdev(data = price[-displace], length = length);

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;
AddCloud(LowerBand,UpperBand,Color.lIME);
LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));

Here are some bollinger bands with 'shadows':
https://usethinkscript.com/threads/...el-scan-for-thinkorswim.762/page-3#post-49073

The above link actually has double bollinger bands. Delete the 2nd set, if not desired.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
404 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top