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

Re: Precision Errors



PureBytes Links

Trading Reference Links

At 2:16 AM +0200 8/1/01, pierre.orphelin wrote:

>I have had the temptation to compare the clever implementation of
>linregslope credited to Bob Fulks and compare with the linearregslope
>in TS2000 for various length values.

<snip>

>Bob's version do not use large number as TS version does, so it means
>that his version is with no doubt, more precise with the same TS
>precison used in both cases.
>
>This is a masterpiece workaround, and I suppose that TRAD should ask
>him for a copyright to recode all of the current version using
>linearregsomething. They will gain speed that is obvious, and more
>precision up to TS float precision.
>
>Just curious, have you found this FIR implementation in a book or by
>yourself?

I developed it myself.

There are FIR (Finite Impulse Response) digital filter
implementations of many such functions. Listed below is the code for
one for LinearRegValue which was published in Technical Analysis on
Stocks & Commodities many years ago. I saw this and figured that
there must be a way to do the same thing for LinearRegSlope.

The general case of these kind of filters are called "Savitzky-Golay
filters". The FIR implementations of these polynomial filters were
first published in a famous paper by in 1964. (Savitzky A., and
Golay, M.J.E. 1964, Analytical Chemistry, vol. 36, pp. 1627-1639)
Their original work has been extended to include linear, quadratic,
cubic, and quartic polynomials and can calculate the value, slope,
acceleration, etc., at any point on the best-fit polynomial through
the points. The "C" code for these was published in Numerical Recipes
in "C" section 14. Chuck Kaucher developed a DLL for TradeStation for
the general case. It is pretty complex and requires a matrix
inversion.

His DLL is overkill if you just want one of the cases, such as the
linear regression slope. So I spent some time finding an algorithm
for finding the coefficients for the linear regression slope case.

These FIR implementations are orders of magnitudes faster than the
TradeStation versions and do not subtract near equal terms so they
retain the full 1-part-in-10^7 precision of the TradeStation
arithmetic.

Bob Fulks


{ *******************************************************************

   Study         : LinearReg
  
   Last Edit     : 5/17/97

   Provided By   : Bob Fulks

   Description   : Calculates the value today of a linear regression
      line drawn through the last <Length> price points. Equivalent
      to Omega's LinearRegValue function with a TargetBar of zero
      but much faster.

      Uses the algorithm described in TASC 10/95 pg 21 & TASC 2/96
      pg 12
                 
********************************************************************}
Inputs :  Price(NumericSeries), Length(NumericSimple);
Vars   :  Sum(0), Counter(0), CSum(0), Factor(0), Offset(0);

Sum       = 0;
CSum      = 0;
Offset    = -(Length + 1) / 3;

for Counter = 0 to Length - 1 begin
   Factor = Length  + Offset - Counter;
   Sum    = Sum + Price[Counter] * Factor;
   CSum   = CSum + Factor;
end;
if CSum > 0 then
   LinearReg = Sum / CSum
else
   LinearReg = 0;