stefano introini
stefano introini

Reputation: 37

Pine Script - Lower timeframe moving average not working

I have used this script to calculate the simple moving average based on m1 close data:

float[] m1_close = request.security_lower_tf(syminfo.tickerid, '1', close)
ma1 = float(na)
for i = 0 to array.size(m1_close) - 1
    if array.size(m1_close) > 0
        ma1 := ta.sma(array.get(m1_close, i), 9)
plot(ma1)

But when I plot the sma on H4 chart the result is a normal H4 sma:

enter image description here

How can i get the lower timeframe moving average?

Upvotes: 0

Views: 941

Answers (1)

vitruvius
vitruvius

Reputation: 21179

You can never plot a lower timeframe information (as it is on the lower timeframe) on your chart. That's because there won't be enough bars on the higher timeframe.

With request.security_lower_tf() you will get an array of lower timeframe data. It is up to you which value from this array to use.

In the below example, I'm on the 4h timeframe and requesting data from 1h timeframe. Therefore, the array will contain 4 values. I will be using the latest value from this array to plot.

//@version=5
indicator("My script", overlay=true)

sma = ta.sma(close, 9)

float[] h1_sma = request.security_lower_tf(syminfo.tickerid, '60', sma)

len = array.size(h1_sma)
float lower_sma = na

if (len > 0)
    lower_sma := nz(array.get(h1_sma, len-1))
plot(lower_sma)

enter image description here

Upvotes: 1

Related Questions