[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Obscure TradeStation bug (was "Brain teasers")



PureBytes Links

Trading Reference Links

Hi all,

Thanks to all who responded to my earlier post with the subject line:
  "Brain teasers (are these known TradeStation bugs?"

I thought I'd let folks know what I discovered about one of the three
problems (and hopefully save others from tripping across an obscure
TradeStation bug).

In my earlier post, I said:
>> (TS2000i and TS6): The following error shows up (only
>> occasionally): "Infinite loop detected in this analysis
>> technique.  Check all 'FOR' and/or 'WHILE' loops for proper
>> termination conditions."
>> The study in question uses only two simple "for i=1 to 5"
>> loops.

Well, I was wrong about the loops.  It turns out my study was
making a call to a function I wrote to implement Dick Arms' Volume
Adjusted Moving Average.  That function _does_ contain a WHILE loop
of the form:

Line 1:   Index = 0;
Line 2:   RemainingIncr = Length;
Line 3:   while RemainingIncr > 0 begin
Line 4:      VolRatio = Volume[Index] / VolIncr;
Line 5:      Factor = MinList(VolRatio, RemainingIncr);
Line 6:      Sum = Sum + Factor * Price[Index];
Line 7:      RemainingIncr = RemainingIncr - Factor;
Line 8:      Index = Index + 1;
Line 9:   end;

When I dug into the problem, what I discovered is that the
"auto-detect" feature that is supposed to determine the max bars back
that a function will reference only "mostly works".

By scattering print statements within the code, I was able to see that
the infinite loop occurs when the value of Index is equal to 34.  At
that point, on Line 4 the loop is supposed to calculate:

   VolRatio = Volume[Index] / VolIncr;

However, because of a bug in the "auto-detect" feature, TradeStation
doesn't realize that Volume[34] would exceed the previous max bars
back value for the study and recalculate the study using a larger max
bars back. Instead, it just returns a bogus value for Volume[Index] on
this and subsequent iterations through the loop. The value returned is
Volume[0] (instead of Volume[34], Volume[35], ...). Since that volume
happens to be zero, the loop condition is never met and the "infinite
loop" error happens. It looks like the "auto-detect max bars back"
feature doesn't work if the previous max bars back is exceeded while
within a loop.

Since, the actual max bars back value for the function depends on the
length of the volume adjusted average and the volume values
themselves, I can't just assign a fixed max bars back value to the
study. In order to get the auto-detect facility to work, I ended up
adding the following assignment to a dummy variable right after the
end of the loop:

Line 10:  Dummy = Volume[3*Index];

Since this assignment is outside the loop, the auto detect mechanism
works. It's not pretty but I no longer see infinite loop errors when
applying the study to a chart.

If anyone has suggestions for a better workaround, I'd love to hear
them. In any event, I hope this post saves somebody else some
debugging time.  This problem appears to be in both TS2000i and TS6.

Cheers,
Rich