Join useThinkScript to post your question to a community of 21,000+ developers and traders.
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
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);
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)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
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);
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?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
I want to use this inside my double bollinger band idea but I would like to know a couple of thingsYes, 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.
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.
Hello BenTen,@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);
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
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);
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!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!
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);
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);
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);
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.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);
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);
JESUS!!! this work like Hell Good! Work fine in the chart and the column too!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.
You are welcome. Yes, you should be able to just replace close with open.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
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;
Use the same logic in this post. instead of crosses upper band change it to crosses midline. You can find the syntax to change your points to squares here: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?
Your label looks great.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?
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);
Start a new thread and receive assistance from our community.
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.
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.