【问题标题】:Pine script - Calculate indicator of changing lengthPine 脚本 - 计算变化长度的指标
【发布时间】:2021-06-29 10:42:12
【问题描述】:

我正在尝试计算一个长度变化的简单移动平均线(从特定点开始,每根柱线在指标计算长度上加 1),以便在 bar0 时刻指标长度为 1,在 bar1 时刻长度为2等。

我不一定需要在图表上看到它,但我想不出另一种方法可以确保计算正确。

//@version=4
study("My Script")

src = close
a = sma(src, 5)
cal = barssince(a > 29000)
len = cal == 0 ? 1 : cal // there shouldn't be any 0 values from the above function but just to make sure.

plot(len) // works
b = sma(src, len) // this line also works (at least there are no errors) but plotting it on the chart doesn't work
plot(b)

错误(我还不能发布图片): 研究错误 “sma”函数中的“length”参数 (0.0) 的值无效。它必须 > 0。

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    一旦满足起始条件,通过计算从条到条的 sma,您可以避免“Pine 无法确定系列的参考长度”错误,这是您接下来会遇到的错误。

    src = input(close)
    var bool started = false
    var int n = 1
    var float b = na
    
    a = sma(src, 5)
    
    if started == false and a > 29000
        started := true
        b := src
    else if started == true
        n += 1
        b := ((b * (n - 1)) + src) / n
    
    plot(b)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-29
      相关资源
      最近更新 更多