Reputation: 1183
Since variables cannot be modified, counters etc. are implemented by IDataHolder arrays where the counter gets the value from an addition of a value to the previous value which is then stored in the current position before advancing to the next position. This mechanism partly breaks in the following scan script, where reading a variable appears to change its value, and I would like to understand why:
# 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;
Upvotes: 2
Views: 1143
Reputation: 1183
This is a bug, a defect in the current version of thinkScript. Referencing Historical Data, i.e. reading it overwrites the historical data in the common case that is described in the question, causing data corruption, data loss. It is noteworthy, that in the powerful but limited thinkScript system, a simple statement as in the question can be used to examine a cell with a fixed offset in an IDataHolder array var
containing historical data:
input offset = 0;
plot scan = GetValue(var, BarNumber() -1 + offset);
Upvotes: 1