Nikita Sydorenko
Nikita Sydorenko

Reputation: 11

TradingView Pine Script strategy multiple time frames

I am trying to apply Bollinger bands in Tradingnview using Pine script. I have calculated all the variables (simple moving average, upper and lower bounds) and used close/open price using a 1-day frame, however, I want to implement a strategy that relies on this time frame, but executes the order on a lower resolution, e.g. 1-hour time frame. Basically, I receive a trigger using 1-day information and want to execute the order as soon as possible. Did anyone come across this issue?

Upvotes: 1

Views: 15218

Answers (1)

rouxam
rouxam

Reputation: 81

For handling multi timeframe you can use the security() function.

Let's say you want a buy signal on the 1h 7-RSI crossing above 30 on a day where 7-day MA is above 21-days MA.

Set your chart timeframe to 1h (current timeframe) and use this for your script:

//@version=4

// Higher Time Frame
htf_fast_sma = security(syminfo.tickerid, "1D", sma(close, 7), lookahead=barmerge.lookahead_on)
htf_slow_sma = security(syminfo.tickerid, "1D", sma(close, 21), lookahead=barmerge.lookahead_on)

// Higher TF: Fast SMA above Slow SMA
high_tf_buy_signal = htf_fast_sma > htf_slow_sma

// Current TF: 7-RSI crossing over 30
curr_tf_buy_signal = crossover(rsi(close, 7), 30)

buy_signal = high_tf_buy_signal and curr_tf_buy_signal
plotshape(buy_signal ? low : na, 'Buy',  shape.labelup, location.belowbar)

Upvotes: 8

Related Questions