How to check moving averages on one time framer greater than the current time frame

Viewed 151

I want to check moving averages on one time frame greater than the current chart time frame in pine script For example if chart is on m15 I want to check moving averages on m30 if chart is on h1 it check on h2 and so on Somehow it get current chart time frame automatically and plot ma for 1 time frame greater

1 Answers

The security() function is what you are looking for.

//@version=4
study(title="High Time Frame MA", overlay=true)
src = close, len = 9
out = sma(src, len)
out1 = security(syminfo.tickerid, '30', out)
plot(out1)

Edit

There is no next upper timeframe. Timeframe can be anything. What Tradingview lists are some convenient values.

You can get the current timeframe in minutes, and request data from 2 * current timeframe for example.

//@version=5
indicator(title="High Time Frame MA", overlay=true)

f_resInMinutes() => 
    _resInMinutes = timeframe.multiplier * (
      timeframe.isseconds ? 1. / 60             :
      timeframe.isminutes ? 1.                  :
      timeframe.isdaily   ? 60. * 24            :
      timeframe.isweekly  ? 60. * 24 * 7        :
      timeframe.ismonthly ? 60. * 24 * 30.4375  : na)

curr_tf_in_min = f_resInMinutes()
tf_2 = str.tostring(2 * curr_tf_in_min)
src = close, len = 9
out = ta.sma(src, len)
out1 = request.security(syminfo.tickerid, tf_2, out)
plot(out1)
Related