如何在Pine脚本中创建变量数组

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

How to create array of variables in pine script

问题

我已经在TradingView(Pine脚本)中创建了三个指数移动平均线(EMA),我想要从这些EMA创建一个数组。

我尝试了这样的代码:

//@version=5
indicator("My script")
ema3 = ta.ema(close, 3)
ema6 = ta.ema(close, 6)
ema9 = ta.ema(close, 9)

arr = array.new_float([ema3, ema6, ema9])

但是我得到了这个错误:

无法使用参数'size'='ema3,ema6,ema9'调用'array.new_float'。使用了一个'[series float, series float, series float]'类型的参数,但需要一个'series int'。

我需要创建一个包含这些EMA的数组(或列表)。例如,在Python中,我会这样做:

arr = [ema3, ema6, ema9]

有人可以帮助我吗?

英文:

I have created in tradingview (pine script) three Exponential Moving Averages and I want to create an array from such EMA's.

I have tried this:

//@version=5
indicator("My script")
ema3 = ta.ema(close,3)
ema6 = ta.ema(close,6)
ema9 = ta.ema(close,9)


arr = array.new_float([ema3,ema6,ema9])

But I get this error:

Cannot call 'array.new_float' with argument 'size'='ema3,ema6,ema9'. An argument of '[series float, series float, series float]' type was used but a 'series int' is expected.

I need to create an array (or a list) containing those EMA's. For example, in Python I'd do:

arr = [ema3,ema6,ema9]

Can anyone help me please?

答案1

得分: 1

你可以使用 array.from() 函数。

> 该函数接受一个可变数量的参数,参数可以是以下类型之一:整数、浮点数、布尔值、字符串、标签、线条、颜色、框、表格、填充线,然后返回相应类型的数组。

//@version=5
indicator("我的脚本")

ema3 = ta.ema(close,3)
ema6 = ta.ema(close,6)
ema9 = ta.ema(close,9)

arr = array.from(ema3, ema6, ema9)

plot(array.get(arr, 0), color=color.green)  // ema3
plot(ema3, color=color.red)                 // ema3
英文:

You can use the array.from() function.

> The function takes a variable number of arguments with one of the
> types: int, float, bool, string, label, line, color, box, table,
> linefill, and returns an array of the corresponding type.

//@version=5
indicator("My script")

ema3 = ta.ema(close,3)
ema6 = ta.ema(close,6)
ema9 = ta.ema(close,9)

arr = array.from(ema3, ema6, ema9)

plot(array.get(arr, 0), color=color.green)  // ema3
plot(ema3, color=color.red)                 // ema3

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

发表评论

匿名网友

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

确定