Bollinger Bands Format, Watchlist, Label, Scan For ThinkOrSwim

Moved your post here. You will find many interesting /fascinating ways to create the Bollinger band clouds.
Here is a simple one to start you off:
https://usethinkscript.com/threads/...el-scan-for-thinkorswim.762/page-4#post-67959
Thank you much!
I used this code from that thread(thanks to the author), and shaded with a color of my choosing(GRAY)
---------------------------------
#
# TD Ameritrade IP Company, Inc. (c) 2007-2019
#

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;

LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));

AddCloud(UpperBand, LowerBand, Color.Green, Color.Green);
#AddCloud(LowerBand, Midline, Color.Red, Color.Red);
-----------------------------
 
If anyone could help that be awesome, I'm looking for a script for Upper and Lower BB labels only i don't need the midline.
 
I got a technical issue here with these Bollinger Bands, I'm trying to put them together under one line of code but the BB do not display at all? they can be together in one line of code or not?

Code:
input length = 20;
input DevUpDn = 2.0;
input DevUpDn2 = 2.5;

def sDev = stdev(close, length);

plot MidLine = ExpAverage(close, length);

plot LowerBand = MidLine + -DevUpDn * sDev and MidLine + -DevUpDn2 * sDev;

plot UpperBand = MidLine + DevUpDn * sDev and MidLine + DevUpDn2 * sDev;
 
Last edited:
@mbarcala Try this

Code:
input length = 20;
input DevUpDn = 2.0;
input DevUpDn2 = 2.5;

def sDev = stdev(close, length);

plot MidLine = ExpAverage(close, length);
plot LowerBand = MidLine + -DevUpDn * sDev and MidLine + -DevUpDn2 * sDev;
plot UpperBand = MidLine + DevUpDn * sDev and MidLine + DevUpDn2 * sDev;
 
Last edited:
@mbarcala Try this

Code:
input length = 20;
input DevUpDn = 2.0;
input DevUpDn2 = 2.5;

def sDev = stdev(close, length);

plot MidLine = ExpAverage(close, length);
plot LowerBand = MidLine + -DevUpDn * sDev and MidLine + -DevUpDn2 * sDev;
plot UpperBand = MidLine + DevUpDn * sDev and MidLine + DevUpDn2 * sDev;
Sorry I don't know why I have a def when I got plot in reality. thanks any way and no they do not display the BB well, it show me a horizontal line at the bottom of the chart. Any body any idea why?
 
Sorry I don't know why I have a def when I got plot in reality. thanks any way and no they do not display the BB well, it show me a horizontal line at the bottom of the chart. Any body any idea why?

your PLOT is using AND to combine 2 formulas, which is converting it into a boolean statement, 1 or 0.

what are you trying to do?
add them together? try + , and maybe ( ) around each section


plot LowerBand =
( MidLine + -DevUpDn * sDev ) +
( MidLine + -DevUpDn2 * sDev );
 
your PLOT is using AND to combine 2 formulas, which is converting it into a boolean statement, 1 or 0.

what are you trying to do?
add them together? try + , and maybe ( ) around each section


plot LowerBand =
( MidLine + -DevUpDn * sDev ) +
( MidLine + -DevUpDn2 * sDev );
I'm just trying to display 2 Bollinger Bands in 1 line of code, it just cosmetic and reducing in reality? the + do not work
 
Last edited:
I'm just trying to display 2 Bollinger Bands in 1 line of code, it just cosmetic and reducing in reality? the + do not work


please reread my post 127

the word AND in those 2 formulas is converting them into boolean formulas.
those 2 formulas are always equal to 1.
when plotted they are horizontal lines at 1.


i don't understand, why you are combining line formulas.
if you want an upper and lower lines, but at different levels, then change this variable to a different number.
input DevUpDn = 2.0;


your code with notes and 4 lines at the end

Ruby:
# bol_band_lines_00

input length = 20;
input DevUpDn = 2.0;
input DevUpDn2 = 2.5;

def sDev = stdev(close, length);

plot MidLine = ExpAverage(close, length);

# ----------------------------------------------------
# ----------------------------------------------------
# the word  AND  in these 2 formulas is converting them into boolean formulas.
# these 2 formulas are always equal to 1.
# when plotted they are horizontal lines at 1.

plot LowerBandz = MidLine + -DevUpDn * sDev and MidLine + -DevUpDn2 * sDev;
plot UpperBandz = MidLine + DevUpDn * sDev and MidLine + DevUpDn2 * sDev;
# ----------------------------------------------------
# ----------------------------------------------------

# the 4 line formulas separated
plot LowerBand = MidLine + -DevUpDn * sDev;
plot l2 = MidLine + -DevUpDn2 * sDev;

plot UpperBand = MidLine + DevUpDn * sDev;
plot u2 = MidLine + DevUpDn2 * sDev;
 
BollingerBandwidth()."Bandwidth" from 1 bars ago is greater than or equal to 0.7 and BollingerBandwidth()."Bandwidth" from 1 bars ago is less than or equal to 1.4;

BollingerBandwidth()."Bandwidth" from 2 bars ago is greater than or equal to 0.7 and BollingerBandwidth()."Bandwidth" from 2 bars ago is less than or equal to 1.4;

ETC

But getting errors, can some someone kindly help. thank you
 
BollingerBandwidth()."Bandwidth" from 1 bars ago is greater than or equal to 0.7 and BollingerBandwidth()."Bandwidth" from 1 bars ago is less than or equal to 1.4;

BollingerBandwidth()."Bandwidth" from 2 bars ago is greater than or equal to 0.7 and BollingerBandwidth()."Bandwidth" from 2 bars ago is less than or equal to 1.4;

ETC

But getting errors, can some someone kindly help. thank you

i'm not sure where you want to use this code.
see if this helps. i typed this on my cell, it is untested.
enable only 1 plot, which one depends on where you use this. chart, column, scan.

Code:
input uppervalue = 1.4;
input lowervalue = 0.7;
input barsback = 10;
input barsback_offset = 1;

def bbb = reference BollingerBandwidth()."Bandwidth";

def bbb_sqz = if ( bbb >= lowervalue and 
bbb <= uppervalue ) then 1 else 0;

def sqz_cnt = sum( bbb_sqz[ barsback_offset ] , barsback );

#----------------------------------
# scan plot code
# true if there was a sqz in prev x bars
plot is_sqz = if sqz_cnt > 0 then 1 else 0;


# use this in a column or lower study. disable prev plot
# plot z = sqz_cnt;


https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/A-B/BollingerBandwidth
 
Working on a Bollinger Band Crossover indicator Column Watchlist. Where if a condition is triggered it will light up a specific color for each individual condition. I can get two separate Columns to work with a different code, but I need to condense the two crosses. One crosses above one crosses below.

AssignBackgroundColor (if BollingerBandsCrossover("crossing type" = "below") is true then Color.WHITE else Color.BLACK);

AssignBackgroundColor (if BollingerBandsCrossover() is true then Color.Cyan else Color.Black);

I'm missing a plot, but not sure what to do? I went to condition wizard and added two conditions together. Not sure how to connect both together.


Here is the script for the Bollinger Band Crossover

input length = 20;
input Std_Deviation = 2.0;
input band = {default upper, middle, lower};
input crossingType = {default above, below};
input averageType = AverageType.SIMPLE;

def bollinger;
switch (band) {
case upper:
bollinger = BollingerBands(length = length, Num_Dev_Dn = -Std_Deviation, Num_Dev_up = Std_Deviation, averageType = averageType).UpperBand;
case middle:
bollinger = BollingerBands(length = length, Num_Dev_Dn = -Std_Deviation, Num_Dev_up = Std_Deviation, averageType = averageType).MidLine;
case lower:
bollinger = BollingerBands(length = length, Num_Dev_Dn = -Std_Deviation, Num_Dev_up = Std_Deviation, averageType = averageType).LowerBand;
}

plot signal = Crosses(close, bollinger, crossingType == CrossingType.above);

signal.DefineColor("Above", GetColor(1));
signal.DefineColor("Below", GetColor(2));
signal.AssignValueColor(if crossingType == CrossingType.above then signal.color("Above") else signal.color("Below"));

signal.SetPaintingStrategy(if crossingType == CrossingType.above
then PaintingStrategy.BOOLEAN_ARROW_UP
else PaintingStrategy.BOOLEAN_ARROW_DOWN);



When I do the condition wizard I have it set for the band set to upper and the crossing set for above, and for the next condition in the condition wizard the band set for upper and the crossing set for below. If that helps.
 
Last edited by a moderator:
Slightly confused by the question as I don't use the "Condition Wizard" but if you want to pick between the colors then wouldn't it be:

AssignBackgroundColor (if BollingerBandsCrossover("crossing type" = "below") is true then Color.WHITE else if BollingerBandsCrossover() is true then Color.Cyan else Color.Black);

Only issue here is the first argument trumps the second where if it is true then it never gets to the second argument.

Does this help?
 
I have a question. Can this
https://usethinkscript.com/threads/...ve-below-previous-days-h-l.13416/#post-112598
also be made to keep track when price is above or below the Bollinger bands?


this creates 4 zones related to bollinger bands

"1 Up+" , (close > upperband)
"2 Mid+" , (close <= upperband and close > midline)
"3 Mid-" , (close <= midline and close >= lowerband)
"4 dwn-" , (close < lowerband)


column study
zbbrange1
http://tos.mx/jUuBDWJ
15 min


Code:
# zbbrange1

#https://usethinkscript.com/threads/watchlist-column-indicator-study-for-above-below-bollinger-bands.13417/
# Watchlist Column Indicator/Study For Above/Below Bollinger Bands
# TDTOS  11/19

# keep track when price is above or below the Bollinger bands?

#---------------------------------------

# BollingerBands
# TD Ameritrade
def na = double.nan;

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);


def MidLine = MovingAverage(averageType, data = price[-displace], length = length);
def LowerBand = MidLine + num_Dev_Dn * sDev;
def UpperBand = MidLine + num_Dev_Up * sDev;

input show_bb_lines = yes;
plot zup = if show_bb_lines then upperband else na;
plot zmid = if show_bb_lines then midline else na;
plot zlow = if show_bb_lines then lowerband else na;
zup.SetDefaultColor(GetColor(5));
zmid.SetDefaultColor(GetColor(1));
zlow.SetDefaultColor(GetColor(0));

# ------------------------

def bbabove = (close > upperband);
def bbmidup = (close <= upperband and close > midline);
def bbmiddwn = (close <= midline and close >= lowerband);
def bbbelow = (close < lowerband);


Addlabel(1,
(if bbabove then "1 Up+"
else if bbmidup then "2 Mid+"
else if bbmiddwn then "3 Mid-"
else if bbbelow then "4 dwn-"
else "--")
, color.black);

#, (if bbabove then color.green
#else if bbbelow then color.red
#else if bbmidup then color.cyan
#else if bbmiddwn then color.yellow
#else color.gray));


AssignBackgroundColor( (if bbabove then color.green else if bbbelow then color.red else if bbmidup then color.cyan else if bbmiddwn then color.yellow else color.gray));
#




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


test study for upper chart

Code:
# bb_range_0_upper

#https://usethinkscript.com/threads/watchlist-column-indicator-study-for-above-below-bollinger-bands.13417/
# Watchlist Column Indicator/Study For Above/Below Bollinger Bands
# TDTOS  11/19

# keep track when price is above or below the Bollinger bands?

#---------------------------------------

# BollingerBands
# TD Ameritrade
def na = double.nan;

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);


def MidLine = MovingAverage(averageType, data = price[-displace], length = length);
def LowerBand = MidLine + num_Dev_Dn * sDev;
def UpperBand = MidLine + num_Dev_Up * sDev;

input show_bb_lines = yes;
plot zup = if show_bb_lines then upperband else na;
plot zmid = if show_bb_lines then midline else na;
plot zlow = if show_bb_lines then lowerband else na;
zup.SetDefaultColor(GetColor(5));
zmid.SetDefaultColor(GetColor(1));
zlow.SetDefaultColor(GetColor(0));

# ------------------------

def bbabove = (close > upperband);
def bbmidup = (close <= upperband and close > midline);
def bbmiddwn = (close <= midline and close >= lowerband);
def bbbelow = (close < lowerband);


Addlabel(1,
(if bbabove then "Up+"
else if bbmidup then "Mid+"
else if bbmiddwn then "Mid-"
else if bbbelow then "dwn-"
else "--")
, (if bbabove then color.green
else if bbbelow then color.red
else if bbmidup then color.cyan
else if bbmiddwn then color.yellow
else color.gray));


#AssignBackgroundColor( (if bbabove then color.green else if bbbelow then color.red else if bbmidup then color.cyan else if bbmiddwn then color.yellow else color.gray));
#
 
Hi All,

My current trading strategy is mean reversion trading, and I generally take trades once the upper or lower bollinger bands have been pierced on the daily chart and price starts to revert back to the mean. I have previously relied on looking at numerous charts everyday (on the daily timeframe) to see what tickers meet this criteria, but I am hoping to build a custom watchlist script in TOS that will tell me this based on a 5 period lookback or something like that.

I came across the below free code from TOS indicators on youtube and it appears to address what I am looking for, but it is an indicator that is plotted on a chart. I am hoping for something similar as a watchlist script that that tells me "Upper" or "Lower" depending on which criteria occurs.

Can someone help me with this? Thanks!



#Written by TOS Indicators 2023
#Indicator: Bollinger Bands Reversal Indicator
#Full Tutorial Link: tosindicators.com/indicators/bollinger-bands-reversal

#Defining Upper and Lower Bollinger Bands
def lowerBand = BollingerBands().lowerBand;
def upperBand = BollingerBands().upperBand;
def mid = BollingerBands().MidLine;

#Requiremtn #1
def cond1 = low[1] < lowerBand[1];
def cond2 = high[1] > upperBand[1];

#Requirement #2
def cond3 = close > high[1];
def cond4 = close < low[1];

#Reduce false signals:
def cond5 = high < mid;
def cond6 = low > mid;

#Buy Signal
plot bullSignal = cond1 and cond3 and cond5;
bullSignal.SetPaintingStrategy(PaintingStrategy.Boolean_Arrow_Up);
bullSignal.setLineWeight(3);

#Sell Signal
plot bearSignal = cond2 and cond4 and cond6;
bearSignal.SetPaintingStrategy(PaintingStrategy.Boolean_Arrow_Down);
bearSignal.setLineWeight(3);
 
Last edited by a moderator:
Hi All,

My current trading strategy is mean reversion trading, and I generally take trades once the upper or lower bollinger bands have been pierced on the daily chart and price starts to revert back to the mean. I have previously relied on looking at numerous charts everyday (on the daily timeframe) to see what tickers meet this criteria, but I am hoping to build a custom watchlist script in TOS that will tell me this based on a 5 period lookback or something like that.

I came across the below free code from TOS indicators on youtube and it appears to address what I am looking for, but it is an indicator that is plotted on a chart. I am hoping for something similar as a watchlist script that that tells me "Upper" or "Lower" depending on which criteria occurs.

Can someone help me with this? Thanks!
This turns the script that you provided into a watchlist.

Bollinger Band Watchlist
shared watchlist link: http://tos.mx/n4L9jJW Click here for --> Easiest way to load shared links
ERMlBeT.png

Ruby:
#Written by TOS Indicators 2023
#Indicator: Bollinger Bands Reversal Indicator
#Full Tutorial Link: tosindicators.com/indicators/bollinger-bands-reversal

input price = close ;
input length= 20 ;
input sd1 = 2.0 ;
input sd2 = -2.0 ;

#Defining Upper and Lower Bollinger Bands
def lowerBand = reference BollingerBands("price" = price, "length" = length, "num dev dn" = sd2, "num dev up" = sd1)."lowerBand";

def upperBand = reference BollingerBands("price" = price, "length" = length, "num dev dn" = sd2, "num dev up" = sd1)."upperBand";

def mid = reference BollingerBands("price" = price, "length" = length, "num dev dn" = sd2, "num dev up" = sd1)."MidLine";

#Requiremtn #1
def cond1 = low[1] < lowerBand[1];
def cond2 = high[1] > upperBand[1];

#Requirement #2
def cond3 = close > high[1];
def cond4 = close < low[1];

#Reduce false signals:
def cond5 = high < mid;
def cond6 = low > mid;

#Buy Signal
def bullSignal = cond1 and cond3 and cond5;

#Sell Signal
def bearSignal = cond2 and cond4 and cond6;

AddLabel(yes,
if bullsignal then "bullsignal" else
if bearsignal then "bearsignal" else " ");

AssignBackgroundColor(
if bullsignal then color.green else
if bearsignal then color.red else color.light_gray);
 
Last edited:
This turns the script that you provided into a watchlist.

Bollinger Band Watchlist
shared watchlist link: http://tos.mx/n4L9jJW Click here for --> Easiest way to load shared links
ERMlBeT.png

Ruby:
#Written by TOS Indicators 2023
#Indicator: Bollinger Bands Reversal Indicator
#Full Tutorial Link: tosindicators.com/indicators/bollinger-bands-reversal

#Defining Upper and Lower Bollinger Bands
def lowerBand = BollingerBands().lowerBand;
def upperBand = BollingerBands().upperBand;
def mid = BollingerBands().MidLine;

#Requiremtn #1
def cond1 = low[1] < lowerBand[1];
def cond2 = high[1] > upperBand[1];

#Requirement #2
def cond3 = close > high[1];
def cond4 = close < low[1];

#Reduce false signals:
def cond5 = high < mid;
def cond6 = low > mid;

#Buy Signal
def bullSignal = cond1 and cond3 and cond5;

#Sell Signal
def bearSignal = cond2 and cond4 and cond6;

AddLabel(yes,
if bullsignal then "bullsignal" else
if bearsignal then "bearsignal" else " ");


AssignBackgroundColor(
if bullsignal then color.green else
if bearsignal then color.red else color.light_gray);


Hi MerryDay. Thanks for this! What is the lookback period for this? 1 day? How would I change it to a longer lookback period?
 
Hi MerryDay. Thanks for this! What is the lookback period for this? 1 day? How would I change it to a longer lookback period?
The script that you provided did not allow for the ToS defaults to be modified.
The above watchlist has been updated to allow the Bollinger bands lengths and the other inputs to be changed.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
385 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