【问题标题】:If statement converting 'simple int' to 'series int'If 语句将 \'simple int\' 转换为 \'series int\'
【发布时间】:2022-08-19 00:02:41
【问题描述】:

在交易视图 pine 脚本中,我尝试使用动态长度调用 ta.rma,但我尝试的所有操作都会在控制台中出现此错误:

无法使用参数 \'length\'=\'length\' 调用 \'ta.rma\'。使用了 \'series int\' 类型的参数,但应使用 \'simple int\'

使用简单的分配工作(即 ta.rma 函数没有错误):

// This works
length = 20

但是,如果我使用 \'if\' 语句,长度将转换为 \'series int\',我不知道为什么或如何解决它:

length = if syminfo.ticker == \'SPY\'
    10
else
    20

rma 电话:

ta.rma(high - low, length)

我正在使用//@version=5 脚本

  • 更改了 if 条件以更准确地反映我想要实现的目标。

标签: pine-script


【解决方案1】:

正如错误消息告诉您的那样,length 参数需要 simple int

simple 是一种数据类型,它们的值在第一个柱上是已知的,并且在执行期间它们永远不会改变。您可以阅读this 以了解有关类型系统的更多信息。

在您的情况下,您使用条件运算符将值分配给length。这使它不是simple,因为它的值可能会根据您的情况而改变。因此出现错误消息。

len1 = input.int(14)
len2 = 14
len3 = math.round(bar_index / 2)

rma = ta.rma(close, 14)    // Fine
rma1 = ta.rma(close, len1) // Fine
rma2 = ta.rma(close, len2) // Fine
rma3 = ta.rma(close, len3) // Error as len3 will be different on each bar

【讨论】:

  • 感谢您的解释。我的条件是基于所选工具的,所以你是说在这种情况下不可能将值保持为简单的 int 吗?您共享的链接很简单:Values of “simple” form are known only when a script begins execution on the first bar of a chart’s history, and they never change during the execution of the script. 我不希望长度值在为给定乐器设置后改变。我只希望它在仪器发生变化时发生变化。有没有办法我可以做到这一点?
  • 使用用户输入的长度?
【解决方案2】:

如果使用三元运算符,结果会改变吗?

length = (variable_1 == 'ABC') ? 10 : 20

【讨论】:

  • 这应该是评论而不是答案。它没有提供对 OP 问题的答案。
  • 不,不幸的是它给出了同样的错误。
猜你喜欢
  • 1970-01-01
  • 2015-11-09
  • 2020-07-24
  • 2017-06-30
  • 1970-01-01
  • 2019-12-01
  • 2013-05-27
  • 1970-01-01
  • 2020-05-28
相关资源
最近更新 更多