如何在扫描中处理thinkScript IDataHolder数组

时间:2019-04-20 22:03:15

标签: thinkscript

由于不能修改变量,所以计数器等由IDataHolder数组实现,其中,计数器通过将值与上一个值相加来获取值,然后在前进到下一个位置之前将其存储在当前位置。该机制在以下扫描脚本中有部分中断,在该脚本中,读取变量似乎会更改其值,我想了解原因:

# Sum Test

# Build sum starting at the left end
def sum;
if (BarNumber() < 5) {
    if (BarNumber() == 1) {
        sum = 1;
    } else {
        sum = sum[1] + 1;
    }
} else {
    sum = sum[1]; # This causes the problem.
    #sum = Double.NaN;# alternative: does not change previous value but useless.
}

# Test that the first sum entry is 1 as expected
plot scan = GetValue(sum, BarNumber() -1) == 1;

1 个答案:

答案 0 :(得分:0)

这是一个错误,是thinkScript当前版本中的一个缺陷。 Referencing Historical Data,即,在问题描述的常见情况下,读取它会覆盖历史数据,从而导致数据损坏,数据丢失。值得注意的是,在功能强大但功能有限的thinkScript系统中,可以使用问题中的简单语句来检查包含历史数据的IDataHolder数组var中具有固定偏移量的单元格:

input offset = 0;
plot scan = GetValue(var, BarNumber() -1 + offset);