【问题标题】:How to use fold statement index in function call如何在函数调用中使用折叠语句索引
【发布时间】:2021-12-17 08:47:13
【问题描述】:

fold manual 举个例子:

input price = close;
input length = 9;
plot SMA = (fold n = 0 to length with s do s + getValue(price, n, length - 1)) / lenth;

这有效地迭代调用函数,就像在 for 循环体中一样。

当我使用这个语句来调用我自己的函数时,它会中断,因为循环索引变量没有被识别为可以传递给我的函数的变量:

script getItem{ 
    input index = 0;
    plot output = index * index;
}
script test{
    def total = fold index = 0 to 10 with accumulator = 0 do
    accumulator + getItem(index);########## Error: No such variable: index
}

【问题讨论】:

    标签: thinkscript


    【解决方案1】:

    这是一个已知的错误/限制。已确认没有修复时间线。没有可用的解决方法。

    【讨论】:

      【解决方案2】:

      您是否尝试过在折叠中为定义的变量添加一个小余数,然后传递该变量?您可以去除整数值,然后将余数用作计数器值。我一直在玩类似的东西,但它不起作用(还)。这是一个例子:

      script TailOverlap{
      input i = 0;
      def ii = (Round(i, 1) - i) * 1000;
      ... more stuff
      plot result = result;
      };
      
      def _S = ( 
      fold i = displace to period
      with c = 0
      do if              
          TailOverlap(i = _S)  #send cur val of _S to script    
      then _S[1] + 1.0001      #increment variable and counter
      else _S[1] + 0.0001      #increment the counter only
      );
      

      我会继续玩这个。如果我让它工作,我会发布最终的解决方案。如果您能够完成这项工作(或发现了其他解决方案),请在此处发布,以便我知道。

      谢谢!

      【讨论】:

        猜你喜欢
        • 2019-07-06
        • 2011-09-29
        • 1970-01-01
        • 2020-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多