Pinescript v5 – 如何声明浮点数系列

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

Pinescript v5 - How to declare float series

问题

我声明如下:

type valutaType
    string valuta
    float level

var arrayValute = array.new<valutaType>()

我编写了以下方法:

getLevelFromCurrency(currency) =>
    var levels = array.new_float()
    for item in arrayValute
        if item.valuta == currency
            levels := item.level

我收到以下错误信息:

变量'levels'被声明为'float[]'类型,无法将其分配给'type 'series float'的表达式

如何在getLevelFromCurrency(currency)中将levels声明为浮点数系列(float series),而不是浮点数数组?

我尝试了以下方法:

  • array.new_float()
  • series

我添加了一个小型测试脚本:

//@version=5
indicator("testScriptV5", precision=2, overlay=true)

type valutaType
    string valuta
    float[] level

var arrayValute = array.new<valutaType>()

sma200 = ta.sma(close, 200)

array.push(arrayValute, valutaType.new("EUR", sma200))
levelssss = array.new_float()

for item in arrayValute
    if item.valuta == "EUR"
        levelssss := item.level

plot(levelssss)
英文:

I have declare:

type valutaType
    string valuta
    float level

var arrayValute = array.new&lt;valutaType&gt;()

Wrote down this method:


getLevelFromCurrency(currency) =&gt;
    var levels = array.new_float()
    for item in arrayValute
    	if item.valuta == currency
	    levels := item.level

I receive this errors:
> Variable 'levels' was declared with 'float[]' type. Cannot assign it expression of type 'series float'
>

How to declare levels in getLevelFromCurrency(currency) as float series instead array of float?

I've tried:

  • array.new_float()
  • series

Added a tiny test-script:

//@version=5
indicator(&quot;testScriptV5&quot;, precision=2, overlay=true)

type valutaType
    string valuta
    float[] level

var arrayValute = array.new&lt;valutaType&gt;()

sma200 = ta.sma(close, 200)

array.push(arrayValute, valutaType.new(&quot;EUR&quot;, sma200))
levelssss = array.new_float()

for item in arrayValute
	if item.valuta == &quot;EUR&quot;
		levelssss := item.level

plot(levelssss)

答案1

得分: 1

你在这里将levels声明为一个数组:var levels = array.new_float()

然后你尝试将item.level分配给它,这是不正确的。item.level的类型是float

levels := item.level

如果您想在条件为true时存储item.level,您可以将其推送到您的数组中。

getLevelFromCurrency(currency) =>
    var levels = array.new_float()
    for item in arrayValute
        if item.valuta == currency
            array.push(levels, item.level)
英文:

You declare levels as an array here : var levels = array.new_float().

Then you try to assign item.level to it which is not correct. item.level is of type float.

levels := item.level.

If you want to store item.level whenever your conditions is true, you can just push it to your array.

getLevelFromCurrency(currency) =&gt;
    var levels = array.new_float()
    for item in arrayValute
        if item.valuta == currency
            array.push(levels, item.level)

huangapple
  • 本文由 发表于 2023年2月16日 02:48:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464218.html
匿名

发表评论

匿名网友

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

确定