last close above or below channel

Butler

Member
Is there a way to make a lower study that is green when the last close was above the bollinger bands and then turns red when the last close was below the lower band? Not interested in what happens in between the bands. Only need to know when a candle closes the first time above or below the bands. Thank you.
 
Last edited:
This should be close to what you need.
Code:
declare lower;

input Bollinger_length = 20;
input Bollinger_deviations = 2;

def bb_upper = BollingerBands(length = Bollinger_length, "num dev dn" = -Bollinger_deviations, "num dev up" = Bollinger_deviations).UpperBand;

def bb_lower = BollingerBands(length = Bollinger_length, "num dev dn" = -Bollinger_deviations, "num dev up" = Bollinger_deviations).LowerBand;

def c = close;

plot indicator = if c crosses above bb_upper then 0 else if c crosses below bb_lower then 0 else double.nan;
indicator.SetPaintingStrategy(PaintingStrategy.POINTS);
indicator.SetLineWeight(5);
indicator.AssignValueColor(if c < bb_lower then color.red else if c > bb_upper then color.green else color.white);
-mashume
 
This should be close to what you need.
Code:
declare lower;

input Bollinger_length = 20;
input Bollinger_deviations = 2;

def bb_upper = BollingerBands(length = Bollinger_length, "num dev dn" = -Bollinger_deviations, "num dev up" = Bollinger_deviations).UpperBand;

def bb_lower = BollingerBands(length = Bollinger_length, "num dev dn" = -Bollinger_deviations, "num dev up" = Bollinger_deviations).LowerBand;

def c = close;

plot indicator = if c crosses above bb_upper then 0 else if c crosses below bb_lower then 0 else double.nan;
indicator.SetPaintingStrategy(PaintingStrategy.POINTS);
indicator.SetLineWeight(5);
indicator.AssignValueColor(if c < bb_lower then color.red else if c > bb_upper then color.green else color.white);
-mashume

Many Many Many Thanks. Is there any way to make it place an up arrow and down arrow on the chart also?
 
Many Many Many Thanks. Is there any way to make it place an up arrow and down arrow on the chart also?
A lower study cannot plot on the upper chart, nor can an upper study plot on a lower chart. You can add an additional study to plot arrows on the upper. Note that to get different arrows (up and down) we need two plots. You can't change arrow direction dynamically.

Code:
declare upper;

input Bollinger_length = 20;
input Bollinger_deviations = 2;

def bb_upper = BollingerBands(length = Bollinger_length, "num dev dn" = -Bollinger_deviations, "num dev up" = Bollinger_deviations).UpperBand;
def bb_lower = BollingerBands(length = Bollinger_length, "num dev dn" = -Bollinger_deviations, "num dev up" = Bollinger_deviations).LowerBand;
def c = close;

plot up_indicator = if c crosses above bb_upper then LOW else double.nan;
plot dn_indicator = if c crosses below bb_lower then HIGH else double.nan;
up_indicator.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
up_indicator.SetLineWeight(2);
up_indicator.SetDefaultColor(color.green);
dn_indicator.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
dn_indicator.SetLineWeight(2);
dn_indicator.SetDefaultColor(color.red);

-mashume
 
A lower study cannot plot on the upper chart, nor can an upper study plot on a lower chart. You can add an additional study to plot arrows on the upper. Note that to get different arrows (up and down) we need two plots. You can't change arrow direction dynamically.

Code:
declare upper;

input Bollinger_length = 20;
input Bollinger_deviations = 2;

def bb_upper = BollingerBands(length = Bollinger_length, "num dev dn" = -Bollinger_deviations, "num dev up" = Bollinger_deviations).UpperBand;
def bb_lower = BollingerBands(length = Bollinger_length, "num dev dn" = -Bollinger_deviations, "num dev up" = Bollinger_deviations).LowerBand;
def c = close;

plot up_indicator = if c crosses above bb_upper then LOW else double.nan;
plot dn_indicator = if c crosses below bb_lower then HIGH else double.nan;
up_indicator.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
up_indicator.SetLineWeight(2);
up_indicator.SetDefaultColor(color.green);
dn_indicator.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
dn_indicator.SetLineWeight(2);
dn_indicator.SetDefaultColor(color.red);

-mashume
mashume, Thank you again sir. It looks great! I am going to see if I can come up with some filters to make it better for entries. Do you have any good ideas for filters to keep me out of some of my losing trades, besides 200sma?
 
Last edited by a moderator:
mashume, Thank you again sir. It looks great! I am going to see if I can come up with some filters to make it better for entries. Do you have any good ideas for filters to keep me out of some of my losing trades, besides 200sma?
Any good ideas? I've recently come back to Fractal Energy and a really long moving average (take your pick -- I think I'm using something I have called 3rd generation moving average, but I can't remember whence I have the code) I only trade in the direction of the slow average. But I trade futures, and I'm only in a trade for 10 to 20 minutes (sometimes less) and don't concern myself with much else.

Mostly, I'm a good coder, and leave the strategizing to others.

-mashume
 
Any good ideas? I've recently come back to Fractal Energy and a really long moving average (take your pick -- I think I'm using something I have called 3rd generation moving average, but I can't remember whence I have the code) I only trade in the direction of the slow average. But I trade futures, and I'm only in a trade for 10 to 20 minutes (sometimes less) and don't concern myself with much else.

Mostly, I'm a good coder, and leave the strategizing to others.

-mashume
Mashume, Thank you again for your response. Ive never heard of a 3rd Generation Moving Average. Do you know where I might start researching it?
 
Is there any way to use a filter or two with these arrows? Only have it draw UP arrows when it is currently trading above the 200SMA and the RSI is greater than 50 on a stock 14 period rsi and DOWN arrows when it is currently trading below the 200SMA and currently below 50 on a stock 14 period RSI?
 
A lower study cannot plot on the upper chart, nor can an upper study plot on a lower chart. You can add an additional study to plot arrows on the upper. Note that to get different arrows (up and down) we need two plots. You can't change arrow direction dynamically.

Code:
declare upper;

input Bollinger_length = 20;
input Bollinger_deviations = 2;

def bb_upper = BollingerBands(length = Bollinger_length, "num dev dn" = -Bollinger_deviations, "num dev up" = Bollinger_deviations).UpperBand;
def bb_lower = BollingerBands(length = Bollinger_length, "num dev dn" = -Bollinger_deviations, "num dev up" = Bollinger_deviations).LowerBand;
def c = close;

plot up_indicator = if c crosses above bb_upper then LOW else double.nan;
plot dn_indicator = if c crosses below bb_lower then HIGH else double.nan;
up_indicator.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
up_indicator.SetLineWeight(2);
up_indicator.SetDefaultColor(color.green);
dn_indicator.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
dn_indicator.SetLineWeight(2);
dn_indicator.SetDefaultColor(color.red);

-mashume
mashume,

Thank you again for your help. Is there any way to make it just plot the first close above the band and not every close above the band. The first instance closing above the upper band when preceded by a close below the lower band is what I am needing. Same on a close below the lower band. Just need the first instance of a close below the lower band only when preceded by a close above the upper band. The way it is now it plots an arrow every time a candle closes outside the band. Sometime 4 or 5 arrows in a row on the same side of the band. Only need the first instance of a close on the opposite side of the band.
 
Last edited:
@Butler
you can use the crosses above or crosses below functions with the two (band and close) and get just the points where it was below the previous bar and above this bar.
I think that's the code I have above somewhere, but I could be wrong. I try to code exactly what people ask for, whether it's what I think they want or not. Just part of having worked as a developer for a long time. Can you post your code and I'll try to tell you why it's behaving the way it is rather than the way you expect it to be?

-mashume
 
Here is the code. Notice how it plots multiple arrows up before it plots a down arrow. Only need an up arrow the first time it closes above the upper band when preceded by a close below the lower band. The previous close below could have been 1 or more candles prior. Just need an arrow up the first instance above and an arrow down the first instance below. Currently it plots multiple arrows down in a row before it ever switches side. Only need to see the first time the side switches for my filter. I was thinking maybe when it closes above the upper band making a variable that = 1. And when it closes below the lower channel making a variable that = 2. Then I could do an if then statement that says if it closes above the upper band and the variable = 2 then draw arrow up else dont draw duplicate arrows. And a statement that says if it closes below the lower band and the variable = 1 then draw a down arrow else dont draw duplicate arrows. Just not sure about the syntax. Thank you for looking at this.


declare upper;
input price = close;
#200sma is another filter to later be used. I can program this part with an AND statement
#input length = 200;
input displace = 0;
input showBreakoutSignals = no;
#plot SMA = Average(price[-displace], length);

input length = 20;
input Num_Dev_Dn = -1.0;
input Num_Dev_up = 1.0;
input averageType = AverageType.Simple;

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

def 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 Bollinger_length = 20;
input Bollinger_deviations = 1;

def bb_upper = BollingerBands(length = Bollinger_length, "num dev dn" = -Bollinger_deviations, "num dev up" = Bollinger_deviations).UpperBand;
def bb_lower = BollingerBands(length = Bollinger_length, "num dev dn" = -Bollinger_deviations, "num dev up" = Bollinger_deviations).LowerBand;
def c = close;

plot up_indicator = if c crosses above bb_upper and close then LOW else double.nan;
plot dn_indicator = if c crosses below bb_lower then HIGH else double.nan;
up_indicator.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
up_indicator.SetLineWeight(2);
up_indicator.SetDefaultColor(color.green);
dn_indicator.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
dn_indicator.SetLineWeight(2);
dn_indicator.SetDefaultColor(color.red);
@Butler
you can use the crosses above or crosses below functions with the two (band and close) and get just the points where it was below the previous bar and above this bar.
I think that's the code I have above somewhere, but I could be wrong. I try to code exactly what people ask for, whether it's what I think they want or not. Just part of having worked as a developer for a long time. Can you post your code and I'll try to tell you why it's behaving the way it is rather than the way you expect it to be?

-mashume
 
Last edited:
use these three lines to replace your two plot lines:
Code:
def trend = if c crosses above bb_upper then 1 else if c crosses below bb_lower then -1 else trend[1];

plot up_indicator = if c crosses above bb_upper AND trend[1] == -1 then LOW else double.nan;
plot dn_indicator = if c crosses below bb_lower AND trend[1] == 1 then HIGH else double.nan;

The trend just flips values between -1 and 1 depending on the trend direction (based on the last crossover) and is a condition for the arrow to plot. We use trend[1] so that we look at what it was last bar (otherwise it won't plot anything at all), and only plot if the crossover represents a change in the trend.

NOTE that this will not plot the very first arrow on your chart, since on the previous bar the trend is undefined.

-mashume
 
Last edited:
Mashume,

This is perfect!!!!! I hope one day that I can be as good as you at coding thinkscripts. Thank you for your kindness. And thank you to this wonderful forum and all of its contributors.


use these three lines to replace your two plot lines:
Code:
def trend = if c crosses above bb_upper then 1 else if c crosses below bb_lower then -1 else trend[1];

plot up_indicator = if c crosses above bb_upper AND trend[1] == -1 then LOW else double.nan;
plot dn_indicator = if c crosses below bb_lower AND trend[1] == 1 then HIGH else double.nan;

The trend just flips values between -1 and 1 depending on the trend direction (based on the last crossover) and is a condition for the arrow to plot. We use trend[1] so that we look at what it was last bar (otherwise it won't plot anything at all), and only plot if the crossover represents a change in the trend.

NOTE that this will not plot the very first arrow on your chart, since on the previous bar the trend is undefined.

-mashu
 
Mashume,

This is perfect!!!!! I hope one day that I can be as good as you at coding thinkscripts. Thank you for your kindness. And thank you to this wonderful forum and all of its contributors.
Mashume,

Do you know of a way that I could tell if a candle engulfs both the top and the bottom of the bollinger bands ? Im trying to avoid really big candles for my filter. Something like bb_upper minus bb_lower and that would define the height to be less than for the candle? So I need to avoid candles that touch the bottom of the bollinger band and the top all at once.
 
Mashume,

This is perfect!!!!! I hope one day that I can be as good as you at coding thinkscripts. Thank you for your kindness. And thank you to this wonderful forum and all of its contributors.
Do you know how to tell it to make the up and down arrows a size 5 if the ADX is greater than 20?
 
Do you know how to tell it to make the up and down arrows a size 5 if the ADX is greater than 20?
You cannot dynamically size things in ToS, so that approach won't work.

However, you can add an extra set of plots and adjust their line weights...

Code:
def trend = if c crosses above bb_upper then 1 else if c crosses below bb_lower then -1 else trend[1];
def local_adx = ADX();
def adx_condition = if local_adx > 20 then 1 else 0;

plot adx_under_up_indicator = if c crosses above bb_upper AND trend[1] == -1 AND adx_condition == 0 then LOW else double.nan;
plot adx_under_dn_indicator = if c crosses below bb_lower AND trend[1] == 1 AND adx_condition == 0 then HIGH else double.nan;
plot adx_over_up_indicator = if c crosses above bb_upper AND trend[1] == -1 AND adx_condition == 1 then LOW else double.nan;
plot adx_over_dn_indicator = if c crosses below bb_lower AND trend[1] == 1 AND adx_condition == 1 then HIGH else double.nan;

adx_under_up_indicator.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
adx_under_up_indicator.SetLineWeight(2);
adx_under_up_indicator.SetDefaultColor(color.green);
adx_under_dn_indicator.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
adx_under_dn_indicator.SetLineWeight(2);
adx_under_dn_indicator.SetDefaultColor(color.red);

adx_over_up_indicator.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
adx_over_up_indicator.SetLineWeight(5);
adx_over_up_indicator.SetDefaultColor(color.green);
adx_over_dn_indicator.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
adx_over_dn_indicator.SetLineWeight(5);
adx_over_dn_indicator.SetDefaultColor(color.red);

-mashume
 
You cannot dynamically size things in ToS, so that approach won't work.

However, you can add an extra set of plots and adjust their line weights...

Code:
def trend = if c crosses above bb_upper then 1 else if c crosses below bb_lower then -1 else trend[1];
def local_adx = ADX();
def adx_condition = if local_adx > 20 then 1 else 0;

plot adx_under_up_indicator = if c crosses above bb_upper AND trend[1] == -1 AND adx_condition == 0 then LOW else double.nan;
plot adx_under_dn_indicator = if c crosses below bb_lower AND trend[1] == 1 AND adx_condition == 0 then HIGH else double.nan;
plot adx_over_up_indicator = if c crosses above bb_upper AND trend[1] == -1 AND adx_condition == 1 then LOW else double.nan;
plot adx_over_dn_indicator = if c crosses below bb_lower AND trend[1] == 1 AND adx_condition == 1 then HIGH else double.nan;

adx_under_up_indicator.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
adx_under_up_indicator.SetLineWeight(2);
adx_under_up_indicator.SetDefaultColor(color.green);
adx_under_dn_indicator.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
adx_under_dn_indicator.SetLineWeight(2);
adx_under_dn_indicator.SetDefaultColor(color.red);

adx_over_up_indicator.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
adx_over_up_indicator.SetLineWeight(5);
adx_over_up_indicator.SetDefaultColor(color.green);
adx_over_dn_indicator.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
adx_over_dn_indicator.SetLineWeight(5);
adx_over_dn_indicator.SetDefaultColor(color.red);

-mashume
This code works great but I am trying to figure out a way to replace the CROSSES ABOVE and CROSSESS BELOW statement with CLOSES ABOVE and CLOSES BELOW

Any ideas?
 
This code works great but I am trying to figure out a way to replace the CROSSES ABOVE and CROSSESS BELOW statement with CLOSES ABOVE and CLOSES BELOW

Any ideas?
when the close price is above a threshold this bar and below that threshold last bar, it did cross. There is no closes above function, you use the syntax close crosses above something else.

-mashume
 

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