Bollinger Band Script

hrd

New member
I'm very new to thinkscript but have tons of experience coding for TradeStation. I am trying to convert a study to thinkscript.

plot my_pos_1 = my_upper_boll > my_upper_boll [1]
and my_middle_boll > my_middle_boll [1]
and my_lower_boll > my_lower_boll [1];

plot my_neg_1 = my_upper_boll < my_upper_boll [1]
and my_middle_boll < my_middle_boll [1]
and my_lower_boll < my_lower_boll [1];

I tried using the reserved word def to give value to my_pos_1 but I have four different scenarios which can take place, and using a case statement or if statement was not working out well for four scenarios when it meant four different plot statements, and I could not get a single plot statement to work either in a case or in an if. I suppose the only conditional test is like this, from one of the canned indicators:

plot "ZZT$" = if state == state.uptrend and !IsNan(close) then 1 else if state == state.downtrend and !IsNan(close) then -1 else Double.NaN;

Could not code that right either.

So I ended up trying the plot statement in the code above, but that gives me four plots when I can get by with one or two.

Another issue is I am trying to display this as a histogram, where the the positive test above would generate a green histogram going from 0 up to 1, while the negative test above would generate a red histogram going from 0 down to -1. However, my_pos_1 appears to come out as 1, and it's the same with my_neg_1. Comes out as 1. Boolean. I have tried multiplying by -1 but no success. Furthermore, I should be able to change my_pos_1 to anything I want it to be. This is really a simple indicator which I could do in my sleep in TradeStation.

After many hours of work, I have been able to get at least the positive logic to work right, with two plots instead of one, one of them a full histogram and one of them half that size, but they overlap each other. Help with either getting the negative side to work or the overlapping issue for the two positives gone (the half one is not displayed above) would be much appreciated.

Here is the logic I would like to use, if only the software were easy to understand:

if my_upper_boll > my_upper_boll [1]
and my_middle_boll > my_middle_boll [1]
and my_lower_boll > my_lower_boll [1]
then
plot plot1 (1 histogram green width2)
else
if my_upper_boll < my_upper_boll [1]
and my_middle_boll < my_middle_boll [1]
and my_lower_boll < my_lower_boll [1]
then
plot plot1 (-1 histogram red width2)

It's actually that easy in TradeStation.
 
Solution
The second ones just display a half a histogram. On TradeStation, one plot did both of the greens and one did the reds.

Now that I think of it, I'm plotting four things when it could be one and maybe that is what there is this issue. You wrote plot upz = up; and plot dwnz = -dwn; and maybe only one plots because one of them will fail the if test and be zero? Or maybe they are both plotting and that is causing
the issue.

At any rate, it's after 1 am here so I am going to unwind and hit the sack. Sleepy. Been working on this a lot today, with no decent documentation to help. Thanks again.
you are right, the plots could be combined. it was easier to have them separate.


Ruby:
# lbolllevels_compare_00

declare lower;

def na =...
I'm very new to thinkscript but have tons of experience coding for TradeStation. I am trying to convert a study to thinkscript.

plot my_pos_1 = my_upper_boll > my_upper_boll [1]
and my_middle_boll > my_middle_boll [1]
and my_lower_boll > my_lower_boll [1];

plot my_neg_1 = my_upper_boll < my_upper_boll [1]
and my_middle_boll < my_middle_boll [1]
and my_lower_boll < my_lower_boll [1];

I tried using the reserved word def to give value to my_pos_1 but I have four different scenarios which can take place, and using a case statement or if statement was not working out well for four scenarios when it meant four different plot statements, and I could not get a single plot statement to work either in a case or in an if. I suppose the only conditional test is like this, from one of the canned indicators:

plot "ZZT$" = if state == state.uptrend and !IsNan(close) then 1 else if state == state.downtrend and !IsNan(close) then -1 else Double.NaN;

Could not code that right either.

So I ended up trying the plot statement in the code above, but that gives me four plots when I can get by with one or two.

Another issue is I am trying to display this as a histogram, where the the positive test above would generate a green histogram going from 0 up to 1, while the negative test above would generate a red histogram going from 0 down to -1. However, my_pos_1 appears to come out as 1, and it's the same with my_neg_1. Comes out as 1. Boolean. I have tried multiplying by -1 but no success. Furthermore, I should be able to change my_pos_1 to anything I want it to be. This is really a simple indicator which I could do in my sleep in TradeStation.

After many hours of work, I have been able to get at least the positive logic to work right, with two plots instead of one, one of them a full histogram and one of them half that size, but they overlap each other. Help with either getting the negative side to work or the overlapping issue for the two positives gone (the half one is not displayed above) would be much appreciated.

Here is the logic I would like to use, if only the software were easy to understand:

if my_upper_boll > my_upper_boll [1]
and my_middle_boll > my_middle_boll [1]
and my_lower_boll > my_lower_boll [1]
then
plot plot1 (1 histogram green width2)
else
if my_upper_boll < my_upper_boll [1]
and my_middle_boll < my_middle_boll [1]
and my_lower_boll < my_lower_boll [1]
then
plot plot1 (-1 histogram red width2)

It's actually that easy in TradeStation.

TD is doing maintenance now, so my charts won't load, so i didn't test this.

when you say ' four different scenarios ' , do you mean , 2 formulas, each with 2 states ( true or false) ?

i copied the formulas at the end of your post and added to them.

i could have referenced the bollingerband values with something like
def upper = BollingerBands().upperband;
but the study is fairly short, so i just copied it and changed the plots to def.
that way you have all the inputs, if you want to tweak the settings.

see if this is close to what you want

Code:
# bolllevels_compare_00

declare lower;

# BollingerBands() study
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;


# ----------------------------
def my_upper_boll = UpperBand;
def my_middle_boll = MidLine;
def my_lower_boll = LowerBand;


def up = if (my_upper_boll > my_upper_boll[1]
and my_middle_boll > my_middle_boll[1]
and my_lower_boll > my_lower_boll[1])
then 1 else 0;

def dwn = if (my_upper_boll < my_upper_boll[1]
and my_middle_boll < my_middle_boll[1]
and my_lower_boll < my_lower_boll[1])
then 1 else 0;


plot upz = up;
upz.SetPaintingStrategy(PaintingStrategy.histogram);
upz.SetDefaultColor(Color.green);
upz.setlineweight(2);
upz.hidebubble();

plot dwnz = -dwn;
dwnz.SetPaintingStrategy(PaintingStrategy.histogram);
dwnz.SetDefaultColor(Color.red);
dwnz.setlineweight(2);
dwnz.hidebubble();

plot z = 0;
z.SetDefaultColor(Color.gray);
#
 
Last edited:

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

Thank you for the help and for such quick help!

I was able to get it to work pretty easily using your code. I just needed from where you wrote:

def up = if (my_upper_boll > my_upper_boll[1]

and on to the bottom.

There are four scenarios. It now displays them all. One is green for bullish and the second is also green but a half histogram to denote where the price had pulled back during the uptrend. Opposite for downtrend. So, two green histograms and two red histograms. There is only one minor issue left, where it seems the two half histograms are being overlayed by the full histograms. I tested this out by changing the color of the half histograms and widening them to a width of 5 from 1 and that proved it was overlaying, although the numbers that appear to the right of the study name pretty much did the same thing.
 
Thank you for the help and for such quick help!

I was able to get it to work pretty easily using your code. I just needed from where you wrote:

def up = if (my_upper_boll > my_upper_boll[1]

and on to the bottom.

There are four scenarios. It now displays them all. One is green for bullish and the second is also green but a half histogram to denote where the price had pulled back during the uptrend. Opposite for downtrend. So, two green histograms and two red histograms. There is only one minor issue left, where it seems the two half histograms are being overlayed by the full histograms. I tested this out by changing the color of the half histograms and widening them to a width of 5 from 1 and that proved it was overlaying, although the numbers that appear to the right of the study name pretty much did the same thing.

what is the 2nd green formula and 2nd red formula?
the 2 green formulas should be able to be combined. the primary green could = 2 and the 2nd green could = 2/2 ( half of it)

maybe something like this could work?

def histoht = 2;
def up = if <primarygreen> then histoht
else if <2ndgreen> then histoht/2
else na;

can you post your code?

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

for the heck of it, i scaled the band lines to fit on a lower chart
add this to the end of my study.

Code:
# scale signals to fit on the lower ,  to 2 to -2 ?

def lol;
def maxht;
if bn == 1 then {
  lol = lowestall(lowerband);
  maxht = highestall(UpperBand) - lol;
} else {
  lol = lol[1];
  maxht = maxht[1];
}

def maxhtrng = 4;
def yu = (((UpperBand-lol) * maxhtrng) / maxht) - (maxhtrng/2);
def ym = (((midline-lol) * maxhtrng) / maxht) - (maxhtrng/2);
def yl = (((lowerband-lol) * maxhtrng) / maxht) - (maxhtrng/2);

input show_bollinger_lines = yes;
plot u = if show_bollinger_lines then yu else na;
plot m = if show_bollinger_lines then ym else na;
plot l = if show_bollinger_lines then yl else na;
u.SetDefaultColor(Color.gray);
m.SetDefaultColor(Color.orange);
l.SetDefaultColor(Color.gray);
 
The second ones just display a half a histogram. On TradeStation, one plot did both of the greens and one did the reds.

Now that I think of it, I'm plotting four things when it could be one and maybe that is what there is this issue. You wrote plot upz = up; and plot dwnz = -dwn; and maybe only one plots because one of them will fail the if test and be zero? Or maybe they are both plotting and that is causing
the issue.

At any rate, it's after 1 am here so I am going to unwind and hit the sack. Sleepy. Been working on this a lot today, with no decent documentation to help. Thanks again.
 
The second ones just display a half a histogram. On TradeStation, one plot did both of the greens and one did the reds.

Now that I think of it, I'm plotting four things when it could be one and maybe that is what there is this issue. You wrote plot upz = up; and plot dwnz = -dwn; and maybe only one plots because one of them will fail the if test and be zero? Or maybe they are both plotting and that is causing
the issue.

At any rate, it's after 1 am here so I am going to unwind and hit the sack. Sleepy. Been working on this a lot today, with no decent documentation to help. Thanks again.
you are right, the plots could be combined. it was easier to have them separate.


Ruby:
# lbolllevels_compare_00

declare lower;

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

# BollingerBands()
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;


# ----------------------------
def my_upper_boll = UpperBand;
def my_middle_boll = MidLine;
def my_lower_boll = LowerBand;


def up = if (my_upper_boll > my_upper_boll[1]
and my_middle_boll > my_middle_boll[1]
and my_lower_boll > my_lower_boll[1])
then 1 else 0;

def dwn = if (my_upper_boll < my_upper_boll[1]
and my_middle_boll < my_middle_boll[1]
and my_lower_boll < my_lower_boll[1])
then 1 else 0;


plot zo = if up > 0 then up else if dwn > 0 then -dwn else 0;
zo.SetPaintingStrategy(PaintingStrategy.histogram);
zo.AssignValueColor( if up > 0 then color.green else if dwn > 0 then color.red else color.gray);
#zo.SetDefaultColor(Color.green);
zo.setlineweight(2);
zo.hidebubble();

plot z = 0;
z.SetDefaultColor(Color.gray);


# scale signals to fit on the lower ,  to 2 to -2 ?

def lol;
def maxht;
if bn == 1 then {
  lol = lowestall(lowerband);
  maxht = highestall(UpperBand) - lol;
} else {
  lol = lol[1];
  maxht = maxht[1];
}

def maxhtrng = 4;
def yu = (((UpperBand-lol) * maxhtrng) / maxht) - (maxhtrng/2);
def ym = (((midline-lol) * maxhtrng) / maxht) - (maxhtrng/2);
def yl = (((lowerband-lol) * maxhtrng) / maxht) - (maxhtrng/2);

input show_bollinger_lines = yes;
plot u = if show_bollinger_lines then yu else na;
plot m = if show_bollinger_lines then ym else na;
plot l = if show_bollinger_lines then yl else na;
u.SetDefaultColor(Color.gray);
m.SetDefaultColor(Color.orange);
l.SetDefaultColor(Color.gray);

#
 
Solution
I got it to work. I started with just the bullish code to simplify, knowing adding in the bearish code would be simple afterwards. Your statement:

plot zo = if up > 0 then up else if dwn > 0 then -dwn else 0;

is what did the trick.

plot my_plot_bull = if my_uptrend_pullback == 50 then my_uptrend_pullback else if my_uptrend == 100 then my_uptrend else 0;

my_plot_bull.SetPaintingStrategy(PaintingStrategy.histogram);
my_plot_bull.SetDefaultColor(Color.CYAN);
my_plot_bull.SetLineWeight(1);

plot my_plot_bear = if my_downtrend_pullback == -50 then my_downtrend_pullback else if my_downtrend == -100 then my_downtrend else 0;

my_plot_bear.SetPaintingStrategy(PaintingStrategy.histogram);
my_plot_bear.SetDefaultColor(Color.RED);
my_plot_bear.SetLineWeight(1);

That gives me all four scenarios.

I did not multiply by -1 in the second plot statement because I had defined my_downtrend and my_downtrend_pullback as either a negative number or zero in the DEF conditional statement.

I tried to insert a pic I took of the indicator in action but can't find how to do that.

Thanks again!

Also, you might want to check out Kay on twitter: https://twitter.com/optionsprochick
 
I got it to work. I started with just the bullish code to simplify, knowing adding in the bearish code would be simple afterwards. Your statement:

plot zo = if up > 0 then up else if dwn > 0 then -dwn else 0;

is what did the trick.

plot my_plot_bull = if my_uptrend_pullback == 50 then my_uptrend_pullback else if my_uptrend == 100 then my_uptrend else 0;

my_plot_bull.SetPaintingStrategy(PaintingStrategy.histogram);
my_plot_bull.SetDefaultColor(Color.CYAN);
my_plot_bull.SetLineWeight(1);

plot my_plot_bear = if my_downtrend_pullback == -50 then my_downtrend_pullback else if my_downtrend == -100 then my_downtrend else 0;

my_plot_bear.SetPaintingStrategy(PaintingStrategy.histogram);
my_plot_bear.SetDefaultColor(Color.RED);
my_plot_bear.SetLineWeight(1);

That gives me all four scenarios.

I did not multiply by -1 in the second plot statement because I had defined my_downtrend and my_downtrend_pullback as either a negative number or zero in the DEF conditional statement.

I tried to insert a pic I took of the indicator in action but can't find how to do that.

Thanks again!

Also, you might want to check out Kay on twitter: https://twitter.com/optionsprochick
glad you got it working.

in the future when posting code,
click on </> in the header,
to open a code window. you don't have to pick a type, it's just for formatting and coloring. some people pick ruby.

here is a link about posting pics.
click on thinkscript at top of any page,
click on questions. a link is near the top.
https://usethinkscript.com/threads/how-to-insert-image-in-a-post-thread.277/
 
I got it to work. I started with just the bullish code to simplify, knowing adding in the bearish code would be simple afterwards. Your statement:

plot zo = if up > 0 then up else if dwn > 0 then -dwn else 0;

is what did the trick.

plot my_plot_bull = if my_uptrend_pullback == 50 then my_uptrend_pullback else if my_uptrend == 100 then my_uptrend else 0;

my_plot_bull.SetPaintingStrategy(PaintingStrategy.histogram);
my_plot_bull.SetDefaultColor(Color.CYAN);
my_plot_bull.SetLineWeight(1);

plot my_plot_bear = if my_downtrend_pullback == -50 then my_downtrend_pullback else if my_downtrend == -100 then my_downtrend else 0;

my_plot_bear.SetPaintingStrategy(PaintingStrategy.histogram);
my_plot_bear.SetDefaultColor(Color.RED);
my_plot_bear.SetLineWeight(1);

That gives me all four scenarios.

I did not multiply by -1 in the second plot statement because I had defined my_downtrend and my_downtrend_pullback as either a negative number or zero in the DEF conditional statement.

I tried to insert a pic I took of the indicator in action but can't find how to do that.

Thanks again!

Also, you might want to check out Kay on twitter: https://twitter.com/optionsprochick
Hi, thanks
I got it to work. I started with just the bullish code to simplify, knowing adding in the bearish code would be simple afterwards. Your statement:

plot zo = if up > 0 then up else if dwn > 0 then -dwn else 0;

is what did the trick.

plot my_plot_bull = if my_uptrend_pullback == 50 then my_uptrend_pullback else if my_uptrend == 100 then my_uptrend else 0;

my_plot_bull.SetPaintingStrategy(PaintingStrategy.histogram);
my_plot_bull.SetDefaultColor(Color.CYAN);
my_plot_bull.SetLineWeight(1);

plot my_plot_bear = if my_downtrend_pullback == -50 then my_downtrend_pullback else if my_downtrend == -100 then my_downtrend else 0;

my_plot_bear.SetPaintingStrategy(PaintingStrategy.histogram);
my_plot_bear.SetDefaultColor(Color.RED);
my_plot_bear.SetLineWeight(1);

That gives me all four scenarios.

I did not multiply by -1 in the second plot statement because I had defined my_downtrend and my_downtrend_pullback as either a negative number or zero in the DEF conditional statement.

I tried to insert a pic I took of the indicator in action but can't find how to do that.

Thanks again!

Also, you might want to check out Kay on twitter: https://twitter.com/optionsprochick
Do you have final version of the script.?thanks.
 
halcyonguy, the third indicator also works now. Just one minor issue with the half histogram being overlayed by the full histogram in situations where there should be just a half histogram, which I am trying to debug without a debugger. I wrote it much faster than the one I asked about here, thanks to your assistance. It was working last night.
 
halcyonguy, the third indicator also works now. Just one minor issue with the half histogram being overlayed by the full histogram in situations where there should be just a half histogram, which I am trying to debug without a debugger. I wrote it much faster than the one I asked about here, thanks to your assistance. It was working last night.
why use multiple histograms?
use just 1
 
The half a histogram, which I think I mentioned before, means there was a pullback to the trend. The way I had it on TradeStation, the half one was a bit thicker as well. I had not been able to thicken it, because I can't change the setlinewidth to a variable. Because of that, I tried two plots, one for the full histogram and one for the half histogram, just so I could have two different line widths. Before I tried to change the line width, the indicator worked perfectly, last night, with the exception of the line width staying the same. A minor issue, but it's what I am used to on TradeStation and I plan to continue using TradeStation for my development work, especially after seeing how much more difficult it is to code in thinkorswim. No debugger, can't put a plot in an if statement, can't put a plot in a case statement, can't protect the software, many more issues.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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