user21866272
user21866272

Reputation: 3

How do I use array data from 'request.security_lower_tf' to generate an ema/a plot in Pinescript?

I want to generate several EMAs, calculated using bars from different timeframes (lower than the chart's timeframe e.g using 1m candle closes on a 5m chart). How do I convert the array from 'request.security_lower_tf' into a form suitable for an MA calculation or a plot?

I'm trying to use the 'request.security_lower_tf' command to fetch data from lower timeframes but when I input it into a calculation I keep getting the following error in my 'ta.ema()' calculation: "Cannot call 'ta.ema()' with argument 'data'. An argument of 'float[]' type was used but a 'series float' is expected".

Here is my code so far:

//@version=5
indicator("MTF EMA")

// Set the input variables
emaLength = input.int(20, title="EMA Length 1")

// Fetch data from lower timeframe
data = request.security_lower_tf(syminfo.tickerid, "D", close)

// Calculate the EMAs
ema1 = ta.ema(data, emaLength)

// Plot the EMAs
plot(ema1, color=color.blue, title="EMA 1")

Upvotes: 0

Views: 816

Answers (1)

vitruvius
vitruvius

Reputation: 21342

request.security_lower_tf() returns an array so you should use the array.get() function. You should be careful about not trying to access beyond the boundaries of the array. Therefore, you can do a size check.

// Fetch data from lower timeframe
data = request.security_lower_tf(syminfo.tickerid, "D", close)

data_size = array.size(data)
float close1 = (data_size > 0) ? array.get(data, 0) : na

// Calculate the EMAs
ema1 = ta.ema(close1, emaLength)

Upvotes: 0

Related Questions