【问题标题】:Pine Script, series[0] type is not integer?Pine Script,series[0] 类型不是整数?
【发布时间】:2020-06-10 08:45:20
【问题描述】:

错误信息的原因是什么:

//@version=4
study("sample")
int dep=0

if close>open
        dep:=dep+1

cma=sma(close,dep[0])

plot(cma)

错误代码:

line 11: Cannot call 'sma' with arguments (series[float], series[integer]); available overloads: sma(series[float], integer) => series[float];

【问题讨论】:

  • 我可以问你为什么有dep[0] 而不是只有dep?我经常看到人们使用这种结构,但我不明白为什么。
  • 得到同样的结果。
  • 是的,我知道结果是一样的。我只是好奇为什么要使用[0]。这是 100% 正确的,但有点不寻常,我只是想知道人们是如何想到这种格式的。
  • 使用dep报错,换个方式试试。
  • 基于其他语言,[] 下标为您提供数组中的值。期望 dep[0] 将从 dep 数组(或系列)返回一个整数听起来很正常。遗憾的是,这不是 Pine 脚本中类型的工作方式

标签: pine-script


【解决方案1】:

正如错误消息所说:您正在尝试将series integer 传递给sma 函数,而sma 函数只需要integer 尝试将其设置为另一个值时,您正在制作 dep 系列。所以dep 变量在每根柱上都有不同的值,这使它成为系列。 您可以使用 sma 的 pine 版本来修复脚本: https://www.tradingview.com/pine-script-reference/v4/#fun_sma

【讨论】:

  • 感谢您的回答,但我的问题仍未解决。请看下面的答案。
【解决方案2】:
//@version=4
study("sample")
int dep=1

if close>open
    dep:=dep+1

//cma=sma(close,dep[0])                     //wrong

pine_sma(x, y) =>
    sum = 0.0
    for i = 0 to y - 1
        sum := sum + x[i] / y
    sum

//plot(pine_sma(close, 15))                 //correct

plot(dep)

plot(pine_sma(close, dep))                  //When dep type is int, its value is always 2.
                                            //Set dep type is var,Code run time error.

我的问题仍未解决。 我的理解是 series[integer] 应该得到 int。不是吗?

【讨论】:

  • series[integer] should get int.Isn't it 没有。它返回series integer
  • 那么,我怎样才能得到我想要的结果呢?
  • 几件事:1)在上面的代码中,您还应该说明在关闭不高于打开的情况下dep应该是什么:例如if close>open dep:=dep+1 else dep:=dep。 2) 在 for 循环中,您应该只在最后进行除法,因此:sum := sum + x[i] 并在 for 执行后:sum := sum/(y-1)
  • 感谢您的关注,这只是一个例子,dep是一个计数器,我无法得到dep的值来计算sam值。其他不重要。
  • 我不太明白为什么使用内置 sma 而不是 pine_sma 对您很重要,这可以解决您的问题,但我希望在最近的时间(假设,下周) sma 与 series integer 合作
【解决方案3】:
//@version=4
study("sample", max_bars_back = 100)
var dep=1

if close>open
    dep:=dep+1

dep := min(dep, 100) // we needs some limit or must be some condition when dep := 0

pine_sma(x, y) =>
    sum = 0.0
    for i = 0 to y - 1
        sum := sum + x[i]
    sum / y

plot(dep)
plot(pine_sma(close, dep))

【讨论】:

  • 谢谢,但这不是我想要的结果。
猜你喜欢
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 2021-04-08
  • 2021-09-15
  • 2021-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多