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

huangapple go评论41阅读模式
英文:

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?

我想生成多个指数移动平均线(EMA),使用不同时间框架的K线计算(比图表时间框架低,例如在5分钟图上使用1分钟蜡烛收盘价)。我如何将来自'request.security_lower_tf'的数组转换为适用于MA计算或绘图的形式?

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".

我试图使用'request.security_lower_tf'命令从较低的时间框架获取数据,但当我将其输入到计算中时,我不断收到以下错误消息:“无法使用参数'data'调用'ta.ema()'。 使用了'float[]'类型的参数,但期望的是'series float'”。

Here is my code so far:

以下是迄今为止的我的代码:

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

// 设置输入变量
emaLength = input.int(20, title="EMA Length 1")

// 从较低时间框架获取数据
data = request.security_lower_tf(syminfo.tickerid, "D", close)

// 计算EMA
ema1 = ta.ema(data, emaLength)

// 绘制EMA
plot(ema1, color=color.blue, title="EMA 1")
英文:

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")

答案1

得分: 0

request.security_lower_tf() 返回一个数组,所以你应该使用 array.get() 函数。你应该小心不要尝试访问数组的边界之外的元素。因此,你可以进行大小检查。

// 从较低时间框架获取数据
data = request.security_lower_tf(syminfo.tickerid, "D", close)

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

// 计算指数移动平均线(EMAs)
ema1 = ta.ema(close1, emaLength)
英文:

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)

huangapple
  • 本文由 发表于 2023年5月18日 08:26:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定