Bollinger Bands Format, Watchlist, Label, Scan For ThinkOrSwim

Hi,

I am trying to come up with a script for price closing above Bollinger Bands for a consecutive number of days, lets say 3 days. But I really have no idea how to do it. Can anyone help me with this? Thanks in advance
 
Hi,

I am trying to come up with a script for price closing above Bollinger Bands for a consecutive number of days, lets say 3 days. But I really have no idea how to do it. Can anyone help me with this? Thanks in advance

Here is an example that should give you some ideas. Rather than the bollinger band indicator, this uses a reference to it. You could have copied the TOS version and added the counting and arrow code to it.

Capture.jpg
Ruby:
input days      = 3;

plot upperband  = reference BollingerBands().UpperBand;
plot lowerband  = reference BollingerBands().LowerBand;

def closeabove  = if close crosses above upperband
                  then 1
                  else if closeabove[1] >= 1 and close > upperband
                  then closeabove[1] + 1
                  else 0;
plot arrowabove = if closeabove == days then 1 else 0;
arrowabove.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
arrowabove.SetLineWeight(5);

def closebelow  = if close crosses below lowerband
                  then 1
                  else if closebelow[1] >= 1 and close < lowerband
                  then closebelow[1] + 1
                  else 0;
plot arrowbelow = if closebelow == days then 1 else 0;
arrowbelow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
arrowbelow.SetLineWeight(5);
 
Last edited:
Hi again, just to clarify as I am not good with writing scripts and trying to learn more.

plot upperband = reference BollingerBands().UpperBand;
plot lowerband = reference BollingerBands().LowerBand;

For the above lines, if I want the bands to be 1.5, do I have to input 1.5 within the ()?

And also how can I tweaked the script to make it such that the last 3 consecutive day price close above the upper band?

Thanks in advance again
 
Hi again, just to clarify as I am not good with writing scripts and trying to learn more.

plot upperband = reference BollingerBands().UpperBand;
plot lowerband = reference BollingerBands().LowerBand;

For the above lines, if I want the bands to be 1.5, do I have to input 1.5 within the ()?

And also how can I tweaked the script to make it such that the last 3 consecutive day price close above the upper band?

Thanks in advance again
Yes, you can make that change in the parentheses () or you can copy the bollinger band code and append the crossing info from my original post (see below where I have done this for you)

The 3 consecutive day code is already in the code. I have added a debug option to the revised code below that you can turn on/off at the input debug. It is one way to verify that the code does what you want. The consecutive count is shown by the numbers above the candles.

Capture.jpg
Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

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

input days      = 3;

def closeabove  = if close crosses above UpperBand
                  then 1
                  else if closeabove[1] >= 1 and close > UpperBand
                  then closeabove[1] + 1
                  else 0;
plot arrowabove = if closeabove == days then 1 else 0;
arrowabove.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
arrowabove.SetLineWeight(5);

def closebelow  = if close crosses below LowerBand
                  then 1
                  else if closebelow[1] >= 1 and close < LowerBand
                  then closebelow[1] + 1
                  else 0;
plot arrowbelow = if closebelow == days then 1 else 0;
arrowbelow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
arrowbelow.SetLineWeight(5);

#debug
input debug = yes;
plot x = if !debug then double.nan else closeabove;
x.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
 
Hi,
I have a bollinger band with a Deviation of 3.0 and I would like to have a Point on top of any candle every time it cross my Deviation, is this posible? and if this can be transfer to a watchlist column too?

any help in advance thanks
what bollinger band length ? and do you want just the High crosses above? Close crossing above? Low crossing above, The same goes for crossing below that level?
 
Yes, you can make that change in the parentheses () or you can copy the bollinger band code and append the crossing info from my original post (see below where I have done this for you)

The 3 consecutive day code is already in the code. I have added a debug option to the revised code below that you can turn on/off at the input debug. It is one way to verify that the code does what you want. The consecutive count is shown by the numbers above the candles.
I want to use this inside my double bollinger band idea but I would like to know a couple of things

I would like to know if instead counting numbers I can have a points ONLY on top those candles that goes above/below Deviations (I use 2.7)
The other thing is if can put the Points on top of the candle even if did not close above/below my 2.7 Deviations

I hope some one help me because this is really nice for what I want
 
Yes, you can make that change in the parentheses () or you can copy the bollinger band code and append the crossing info from my original post (see below where I have done this for you)

The 3 consecutive day code is already in the code. I have added a debug option to the revised code below that you can turn on/off at the input debug. It is one way to verify that the code does what you want. The consecutive count is shown by the numbers above the candles.
@Aliikhatami Here you go:

Code:
#
# Double Bollinger Bands
# Assembled by BenTen at useThinkScript.com
# Based on request/concept of @aliikhatami

input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn1 = -1.0;
input Num_Dev_up1 = 1.0;
input Num_Dev_Dn3 = -3.0;
input Num_Dev_up3 = 3.0;
input averageType = AverageType.EXPONENTIAL;

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

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand1 = MidLine + num_Dev_Dn1 * sDev;
plot UpperBand1 = MidLine + num_Dev_Up1 * sDev;
plot UpperBand3 = MidLine + num_Dev_Up3 * sDev;
plot LowerBand3 = MidLine + num_Dev_Dn3 * sDev;

AddCloud(UpperBand1, UpperBand3, color.green, color.green);
AddCloud(LowerBand1, LowerBand3, color.red, color.red);
Hello BenTen,

I have a Double BB too and I would like to add a piece of the code that I found here on page 5 that will help me to mark with a number a candle once it closes over my higher/lower Dev but at the same time it shows me the 0 value an and don't want to see the 0 value.

actually its possible to change the numbers value on top of the candles for a point or dot with setlineweight 3

Code:
input days = 3;

def closeabove = if close crosses above UpperBand2
                  then 1
                  else if closeabove[1] >= 1 and close > UpperBand2
                  then closeabove[1] + 1
                  else 0;
plot arrowabove = if closeabove == days then 1 else 0;
arrowabove.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
arrowabove.SetLineWeight(5);

def closebelow = if close crosses below LowerBand2
                  then 1
                  else if closebelow[1] >= 1 and close < LowerBand2
                  then closebelow[1] + 1
                  else 0;
plot arrowbelow = if closebelow == days then 1 else 0;
arrowbelow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
arrowbelow.SetLineWeight(5);

#debug
input debug = yes;
plot x = if !debug then double.nan else closeabove;
x.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
 
I want to use this inside my double bollinger band idea but I would like to know a couple of things

I would like to know if instead counting numbers I can have a points ONLY on top those candles that goes above/below Deviations (I use 2.7)
The other thing is if can put the Points on top of the candle even if did not close above/below my 2.7 Deviations

I hope some one help me because this is really nice for what I want

This is set to plot a cyan colored dot on the high of a candle that closes above the upperband, and if not, but the high closes above the upperband a yellow colored dot will plot on the high of the candle. The reverse logic will plot and color dots on the low of candles below the lowerband.

Capture.jpg
Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.7;
input Num_Dev_up = 2.7;
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));

def aboveupper  = if close > UpperBand then 1 else if close < UpperBand and high > UpperBand then 2 else Double.NaN;
plot abovepoints = if aboveupper then high else Double.NaN;
abovepoints.SetPaintingStrategy(PaintingStrategy.POINTS);
abovepoints.SetLineWeight(5);
abovepoints.AssignValueColor(if aboveupper == 1 then Color.CYAN else Color.YELLOW);

def belowlower  = if close < LowerBand then 1 else if close > LowerBand and low < LowerBand then 2 else Double.NaN;
plot belowpoints = if belowlower then low else Double.NaN;
belowpoints.SetPaintingStrategy(PaintingStrategy.POINTS);
belowpoints.SetLineWeight(5);
belowpoints.AssignValueColor(if belowlower == 1 then Color.CYAN else Color.YELLOW);
 
This is set to plot a cyan colored dot on the high of a candle that closes above the upperband, and if not, but the high closes above the upperband a yellow colored dot will plot on the high of the candle. The reverse logic will plot and color dots on the low of candles below the lowerband.
last question, can you add a little space of 0.20 between the POINT and the candle? and SleepyZ this is exactly what I was looking for!
 
last question, can you add a little space of 0.20 between the POINT and the candle? and SleepyZ this is exactly what I was looking for!

Just adjust the input, tick_spacer (currently set at 20 for 0.20 on a stock chart) to move the dots to where you want them.

Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.7;
input Num_Dev_up = 2.7;
input averageType = AverageType.SIMPLE;
input tick_spacer = 20;

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

def aboveupper  = if close > UpperBand then 1 else if close < UpperBand and high > UpperBand then 2 else Double.NaN;
plot abovepoints = if aboveupper then high + ticksize() * tick_spacer else Double.NaN;
abovepoints.SetPaintingStrategy(PaintingStrategy.POINTS);
abovepoints.SetLineWeight(5);
abovepoints.AssignValueColor(if aboveupper == 1 then Color.CYAN else Color.YELLOW);

def belowlower  = if close < LowerBand then 1 else if close > LowerBand and low < LowerBand then 2 else Double.NaN;
plot belowpoints = if belowlower then low - ticksize() * tick_spacer else Double.NaN;
belowpoints.SetPaintingStrategy(PaintingStrategy.POINTS);
belowpoints.SetLineWeight(5);
belowpoints.AssignValueColor(if belowlower == 1 then Color.CYAN else Color.YELLOW);
 
I'm trying to have a column in my watchlist that displays me in different colors when the price cross each of my 2 Bollinger Bands deviation

Code:
def price = close;
def displace = 0;
## First Deviation
def length = 20;
def Num_Dev_up1 = 2.0;
def Num_Dev_Dn1 = -2.0;
## Second Deviation
def Num_Dev_Up2 = 2.7;
def Num_Dev_Dn2 = -2.7;
def averageType = AverageType.EXPONENTIAL;

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

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand1 = MidLine + num_Dev_Dn1 * sDev;
plot LowerBand2 = MidLine + Num_Dev_Dn2 * sDev;
plot UpperBand1 = MidLine + num_Dev_Up1 * sDev;
plot UpperBand2 = MidLine + Num_Dev_Up2 * sDev;

#UpperBand and LowerBand
def UBbu = open() > UpperBand2;
def LBbu = open() < LowerBand2;
AddLabel(UBbu, "UpB", Color.RED);
AddLabel(LBbu, "DnB", Color.GREEN);

if someone here can help me with this matter I will appreciate
 
I have a regular bollinger band length 20 and I would like to have a square every time the price crosses and close the middle line, can anybody help me with this piece of the code?
 
I looking for some help here with this idea that's driving me crazy, I'm trying to learn to play with thinkscript. As a mention before I'm trying to have a label that when cross my 2 BBand deviations change color plus display the deviation number inside the label too.

I write a posible def here for my label and I don't don't know if work or will work in reality and I would like some opinion and help too?

Code:
def UBbu = if open() > UpperBand1 then 1 else if open() > UpperBand2 then 2 else if open() < UpperBand2 or UpperBand1 then 0 else Double.NaN;
def LBbu = if open() < LowerBand1 then 1 else if open() < LowerBand2 then 2 else if open() > LowerBand2 or LowerBand1 then 0 else Double.NaN;

AddLabel(UBbu, "Above UpperBand" + Num_Dev_up1, Color.RED);
AddLabel(LBbu, "Below LowerBand" + Num_Dev_Dn1, Color.GREEN);

In the label part im confuse how to address the code for my second deviation, I have a separate code for each moment but I would like to see how I can convine?

As a last request how can I put this label idea into a watchlist column?
 
Can some one put me in the right direction, I'm not a coder and I get loose really easy.

I'm trying to put this piece of code here into a watchlist column and I can't make work!

Code:
input price = close;
input displace = 0;
input length = 20;

## First Deviation
input sdUp1 = 2.0;
input sdDn1 = -2.0;
## Second Deviation
input sdUp2 = 2.7;
input sdDn2 = -2.7;
input averageType = AverageType.EXPONENTIAL;

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

def MidLine = MovingAverage(averageType, data = price[-displace], length = length);
def UpperBand1 = MidLine + sdUp1 * sDev;
def LowerBand1 = MidLine + sdDn1 * sDev;
def UpperBand2 = MidLine + sdUp2 * sDev;
def LowerBand2 = MidLine + sdDn2 * sDev;

#UpperBand and LowerBand
plot cond1 = close > sdUp1;
plot cond2 = close > sdDn1;
plot cond3 = close < sdUp2;
plot cond4 = close < sdDn2;


AddLabel(cond1, " ", Color.BLACK);
#AddLabel(cond2, " ", Color.RED);
AssignBackgroundColor(if cond1 then Color.DARK_RED else Color.CURRENT);

AddLabel(cond3, " ", Color.BLACK);
#AddLabel(cond4, " ", Color.GREEN);
AssignBackgroundColor(if cond3 then Color.DARK_GREEN else Color.CURRENT);
 
Can some one put me in the right direction, I'm not a coder and I get loose really easy.

I'm trying to put this piece of code here into a watchlist column and I can't make work!

Code:
input price = close;
input displace = 0;
input length = 20;

## First Deviation
input sdUp1 = 2.0;
input sdDn1 = -2.0;
## Second Deviation
input sdUp2 = 2.7;
input sdDn2 = -2.7;
input averageType = AverageType.EXPONENTIAL;

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

def MidLine = MovingAverage(averageType, data = price[-displace], length = length);
def UpperBand1 = MidLine + sdUp1 * sDev;
def LowerBand1 = MidLine + sdDn1 * sDev;
def UpperBand2 = MidLine + sdUp2 * sDev;
def LowerBand2 = MidLine + sdDn2 * sDev;

#UpperBand and LowerBand
plot cond1 = close > sdUp1;
plot cond2 = close > sdDn1;
plot cond3 = close < sdUp2;
plot cond4 = close < sdDn2;


AddLabel(cond1, " ", Color.BLACK);
#AddLabel(cond2, " ", Color.RED);
AssignBackgroundColor(if cond1 then Color.DARK_RED else Color.CURRENT);

AddLabel(cond3, " ", Color.BLACK);
#AddLabel(cond4, " ", Color.GREEN);
AssignBackgroundColor(if cond3 then Color.DARK_GREEN else Color.CURRENT);
The first thing I would recommend is make sure that your watchlist will work is by testing it on a chart. I plotted the upper and lower bands so that you can more easily test your ideas.

Since you used the deviation band inputs (2.0/2.7) in your conditions rather than the bands themselves (upper/lower), I made modifications to your code as one way to use the bands themselves and to have each condition if it was true, then all the others would be false. Lastly, as far as the color scheme, I could not tell whether you wanted closes above the upperbands to be green or red and vice versa with the lowerbands, so I left it the way it seemed you were heading.

If you need assistance after making any necessary changes, please let us know.

Ruby:
input price = close;
input displace = 0;
input length = 20;

## First Deviation
input sdUp1 = 2.0;
input sdDn1 = -2.0;
## Second Deviation
input sdUp2 = 2.7;
input sdDn2 = -2.7;
input averageType = AverageType.EXPONENTIAL;

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

def MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot UpperBand1 = MidLine + sdUp1 * sDev;
plot LowerBand1 = MidLine + sdDn1 * sDev;
plot UpperBand2 = MidLine + sdUp2 * sDev;
plot LowerBand2 = MidLine + sdDn2 * sDev;

#UpperBand and LowerBand
def cond1 = close > upperband1 and close < upperband2;
def cond2 = close < lowerband1 and close > lowerband2;
def cond3 = close > upperband2;
def cond4 = close < lowerband2;

AddLabel(yes, if cond1 then "1" else if cond2 then "2" else if cond3 then "3" else if cond4 then "4" else "", Color.white);
#AddLabel(cond2, " ", Color.RED);
AssignBackgroundColor(if cond1 then Color.DARK_RED else if cond3 then color.red else if cond2 then color.dark_greeN else if cond4 then color.green else Color.CURRENT);

#AddLabel(cond3, "3", Color.BLACK);
#AddLabel(cond4, " ", Color.GREEN);
#AssignBackgroundColor(if cond3 then Color.DARK_GREEN else Color.CURRENT);
 
Last edited:
The first thing I would recommend is make sure that your watchlist will work is by testing it on a chart. I plotted the upper and lower bands so that you can more easily test your ideas.

Since you used the deviation band inputs (2.0/2.7) in your conditions rather than the bands themselves (upper/lower), I made modifications to your code as one way to use the bands themselves and to have each condition if it was true, then all the others would be false. Lastly, as far as the color scheme, I could not tell whether you wanted closes above the upperbands to be green or red and vice versa with the lowerbands, so I left it the way it seemed you were heading.

If you need assistance after making any necessary changes, please let us know.
JESUS!!! this work like Hell Good! Work fine in the chart and the column too!

one last question if I want to detect this when's happening can I change "close for open" and if open should go with brakes or without?

thanks for the help
 
Last edited:
JESUS!!! this work like Hell Good! Work fine in the chart and the column too!

one last question if I want to detect this when's happening can I change "close for open" and if open should go with brakes or without?

thanks for the help
You are welcome. Yes, you should be able to just replace close with open.

Ruby:
#UpperBand and LowerBand
def cond1 = open > upperband1 and open < upperband2;
def cond2 = open < lowerband1 and open > lowerband2;
def cond3 = open > upperband2;
def cond4 = open < lowerband2;
 
I looking for some help here with this idea that's driving me crazy, I'm trying to learn to play with thinkscript. As a mention before I'm trying to have a label that when cross my 2 BBand deviations change color plus display the deviation number inside the label too.

I write a posible def here for my label and I don't don't know if work or will work in reality and I would like some opinion and help too?

Code:
def UBbu = if open() > UpperBand1 then 1 else if open() > UpperBand2 then 2 else if open() < UpperBand2 or UpperBand1 then 0 else Double.NaN;
def LBbu = if open() < LowerBand1 then 1 else if open() < LowerBand2 then 2 else if open() > LowerBand2 or LowerBand1 then 0 else Double.NaN;

AddLabel(UBbu, "Above UpperBand" + Num_Dev_up1);
AddLabel(LBbu, "Below LowerBand" + Num_Dev_Dn1);

In the label part im confuse how to address the code for my second deviation, I have a separate code for each moment but I would like to see how I can convine?

As a last request how can I put this label idea into a watchlist column?
Your label looks great.
To change it into a watchlist column, the syntax changes slightly:
Code:
AddLabel(UBbu, "Above UpperBand" + Num_Dev_up1, Color.RED);
AddLabel(LBbu, "Below LowerBand" + Num_Dev_Dn1, Color.GREEN);

AssignBackgroundColor(
if UBbu then color.red else
if LBbu then color.green else color.gray);
[/CODE]
 
Hi, I need a script to scan the stocks and, draw an arrow every time when the high and low of the candlestick crosses the upper and lower bands or when the candle closes above and below the upper and lower bands at the same time. I have attached a screenshot to show the idea.

Bollinger band setup:
Price = Close
Displace = 0
Length = 20
num dn = -1.5
num up= 1.5
Average type = Simple


https://drive.google.com/file/d/1vrOt3UegBX_AWijkcgzIME9Yq_IHQVpK/view?usp=sharing




Thanks,
Sam
 
Last edited:

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
328 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