Page 1 of 1

Bollinger Bands on higher time frame

Posted: Sun Jul 31, 2022 1:32 am
by augerpro
I created an indicator that identifies candle patterns on the 15 minute chart. However, for a setup to be valid it must touch the bollinger band on the 30 and 60 minute charts also. Using the training video for higher time frame EMA setup as a template, I tried to generate bollinger bands but am having no luck getting them to match the actual BB values on a 60 minute chart. I think it has to do with my source. Below is the current script. I tried two methods, one is // out, but it should be clear what I was trying to do.

//user input
res60 = input.timeframe(title="SMA Timeframe", defval="60")
res30 = input.timeframe(title="SMA Timeframe", defval="30")
len = input.int(title="SMA length", defval=20)

//calculate SMA
sma = ta.sma(close, 20)
sma60 = request.security(syminfo.tickerid, res60, sma)
//value60 = request.security(syminfo.tickerid, res60, close)
sma30 = request.security(syminfo.tickerid, res30, sma)

stdev60 = ta.stdev(sma60, 20)
bbup60 = sma60 + (stdev60 * 2)
bbdown60 = sma60 - (stdev60 * 2)
//bb60 = ta.bb(value60, 30, 2)

plot(sma60, title="sma60")
//plot(sma30)
//plot(bb60, color=color.green)
plot(bbup60, title="bbup60", color=color.green)
plot(bbdown60, title="bbdown60", color=color.green)

Re: Bollinger Bands on higher time frame

Posted: Tue Aug 23, 2022 2:03 am
by augerpro
No ideas from anyone? I really need to bring this higher timeframe indicator into the lower timeframe chart indicator, so I can create alerts the require certain conditions on both time frames.

Re: Bollinger Bands on higher time frame

Posted: Thu Aug 25, 2022 8:28 pm
by Deep-Wave
Hi augerpro,

check out this link, it is pretty advanced but it does what you want. :up:

https://www.tradingview.com/script/90mq ... oders-FAQ/

Best of luck with your coding

Re: Bollinger Bands on higher time frame

Posted: Tue Aug 30, 2022 3:07 pm
by augerpro
Thank you! I will look into that and report back if I have issues. I really appreciate the help!

Re: Bollinger Bands on higher time frame

Posted: Tue Aug 30, 2022 3:15 pm
by augerpro
Actually I do I have one question. In my script I defined a variable "value60 = request.security(syminfo.tickerid, res60, close)", then attempted to apply ta.bb to it "bb60 = ta.bb(value60, 30, 2)", but my plot "plot(bb60, color=color.green)" throws this error when compiling "syntax error: variables of array type are not support". I didn't intend to make an array, and I'm not sure why it considers this to be one?

Re: Bollinger Bands on higher time frame

Posted: Tue Aug 30, 2022 3:47 pm
by augerpro
Deep-wave> unfortunately that indicator doesn't solve my problem. Looking through it, it handles calculating a higher time frame RSI essentially the same way I'm calculating my HTF SMA. And my SMA is not the problem, it calculates correctly. My issue is the ta.stdev does not, and I don't understand why. You can see in the pic that my HTF bollinger bands on the 15M chart do not match those on 1H chart. The SMA is the same for both, but not the bands.

Image

Re: Bollinger Bands on higher time frame

Posted: Tue Aug 30, 2022 6:31 pm
by Deep-Wave
Hi,
here is a little code snippet that should get you started (originally from LonesomeTheBlue).
Remember that the higher time frame values will stay the same on the lower time frame until a new candle is finished on the higher time frame. This behaviour is intended this way! (google repainting if you need clarification on this)

Let's take a look at a simple example:
Let's assume you have 2 time frames, 15m & 1h. On the 15m chart, the values of the 1h Bollinger bands will change on the close of the candle with the starting time of "quarter to" of every hour (so: 00:45, 01:45, 02:45 etc). That's because the candle with a starting time of for example "12:45" closes at "13:00". At 13:00 the 1h candle from 12:00 to 13:00 is closed so we have new values for the Bollinger bands from the higher time frame. These values will stay the same on the 15m chart until the candle with the starting time 13:45 closes and so on.

Hope this helps you out.
Cheers,
:beer:

Code: Select all

//@version=5
indicator('Bollinger Bands MTF', overlay=true)

HigherTimeFrame = input.timeframe(title='Higher time frame', defval='60')
bblength = input.int(20, title='Length', minval=1)
multi = input.float(2.0, title='Mult', minval=1.0)
basis = ta.sma(close, bblength)
dev = multi * ta.stdev(close, bblength)
upper = basis + dev
lower = basis - dev

// Higher Time Frame
basis_HTF = request.security(syminfo.tickerid, HigherTimeFrame, basis, lookahead=barmerge.lookahead_off)
upper_HTF = request.security(syminfo.tickerid, HigherTimeFrame, upper, lookahead=barmerge.lookahead_off)
lower_HTF = request.security(syminfo.tickerid, HigherTimeFrame, lower, lookahead=barmerge.lookahead_off)


// Plots Bollinger Bands - Chart Time Frame
L1 = plot(upper, color=color.new(color.gray, 0), title='BB Upper Band')
L2 = plot(lower, color=color.new(color.gray, 0), title='BB Lower Band')
plot(basis, color=color.new(color.black, 0), title='BB Mid Band')
fill(L1, L2, color=color.new(color.green, 95))

// Plots Bollinger Bands - Higher Time Frame
LA1 = plot(upper_HTF, color=color.new(color.aqua, 0), title='BB HTF Upper Band', linewidth=2)
LA2 = plot(lower_HTF, color=color.new(color.aqua, 0), title='BB HTF Lower Band', linewidth=2)
plot(basis_HTF, color=color.new(color.black, 0), title='BB HTF Mid Band', linewidth=2)
fill(LA1, LA2, color=color.new(color.aqua, 95))

Re: Bollinger Bands on higher time frame

Posted: Thu Sep 01, 2022 2:19 am
by augerpro
I think that might just work. Thank you!