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

Re: Warning about accuracy of Array functions in TS2000i



PureBytes Links

Trading Reference Links

At 1:39 AM -0500 3/18/00, Canyon678@xxxxxxx wrote:

>Can you please elaborate, the difference between the two  i.e current bar & the mod function


They initialize the function by doing the detailed, complete calculation at CurrentBar = 1.

They then calculate just the differences of the value for the new bar from that of the previous bar (by adding the value for the new bar and subtracting the one for the old bar that dropped off of the average).

This saves a lot of calculations and is generally a good thing to do. But with only 16 bit arithmetic, the small errors of all the additions and subtractions can accumulate when you are talking about tens of thousands of bars.

But simply replacing:

   if CurrentBar = 1 then begin  
              
with:
 
  if Mod(CurrentBar, 100) = 1 then begin

would force the complete accurate calculation of the Average every 100 bars so the calculation would be accurate at every 100th bar. Then they would use the addition/subtraction scheme for the next 99 bars as in the original code. The errors would then only build up over 99 bars and would not be significant.

The cost would be a complete calculation once out of every 100 bars which would be insignificant.

The "Mod" function returns the value of the remainder after dividing CurrentBar by 100 so the condition would be true on CurrentBar = 1, 101, 201, 301, ... 

To test it try the code listed below. On 12000 bars of INDU data I could see errors exceeding 0.03. It varies with the Length so try various values.

The effect is small in this case but is often much larger in other cases, particularly those that multiply prices together or multiply prices by a length.

A good example is the LinearRegressionSlope function. I applied it to 100 bars of INDU data (the DJIA) and had it print the values of all internal variables. The numbers were as follows.

SumBars                  4,950
SumSqrBars             328,350
SumY                 1,103,023
Sum1                54,584,252
Sum2             5,459,962,368
Len*Sum1         5,458,425,200
Num1                -1,537,168  = 5,458,425,200 - 5,459,962,368
Num2                -8,332,500
SumBars*SumBars     24,502,500
Len*SumSqrBars      32,835,000

These are floating point variables so the accuracy might be OK for this application. But you can see how the numbers can get way out of the normal ranges we think about.

Bob Fulks

----------------------------------------

{Test Code for Average Function}

Input: Length(4);

Vars: Price(Close), EMax(0), EMin(0), Err(0), Ave1(Close), Ave2(Close);

Ave1 = Average(Close, Length);
Price = Close;
Ave2 = Average(Price, Length);
Err = Ave2 - Ave1;

if CurrentBar > 100 then begin
	EMax = iff(Err > EMax, Err, EMax);
	EMin = iff(Err < EMin, Err, EMin);
end;

Plot1(Err, "1");
Plot2(EMax, "2");
Plot3(EMin, "3");
Plot4(0, "4");

------
 
This looks as if "Err" will always be zero but it will not be. Here's why:

--------

    Ave1 = Average(Close, Length);

This form will cause TradeStation to use the "series" version of the Average function, which is the one that has the problem we are discussing.

--------

    Price = Close;
    Ave2 = Average(Price, Length); 

This code will cause TradeStation to use the "simple" version of the Average function which does an exact calculation on every bar. This is because we used a variable called "Price" as the input. (Look at the code for each function to see the difference.)

--------

    Err = Ave2 - Ave1;

This code finds the difference between the two versions.

--------

if CurrentBar > 100 then begin
	EMax = iff(Err > EMax, Err, EMax);
	EMin = iff(Err < EMin, Err, EMin);
end;

This code keeps track of the maximum and minimum error (after a start up period).

-------

Plot1(Err, "1");
Plot2(EMax, "2");
Plot3(EMin, "3");
Plot4(0, "4");

This code plots everything.

------