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

Re: TS calculation precision problem



PureBytes Links

Trading Reference Links

Hi Leslie,

Yes, I believe that I must have 'enhanced precision' enabled since
I've never done anything to disable it. I vaguely remember this being
discussed on the list in the past, but can you remind me how one goes
about disabling it?

In terms of speed, I'm testing this DLL under TS2000i on a relatively
slow (400 MHz) PC so probably the only measures of interest are
relative timings between the DLL version and the TradeStation builtin.

What I implemented in the DLL is Pearson's coefficient R (CoeffR). You
get Pearson's R Squared by -- you guessed it -- squaring the CoeffR
result.

For an apples-to-apples comparison, I timed both the DLL version and
the TS2000i builtin for CoeffR(14) using a chart containing 91,380
1-minute bars.

The timing results are:
  TS2000i builtin:    9 seconds
  DLL implementation: 3 seconds

Since a couple of folks in private communications have asked for a
copy of the DLL code as a sample, I've included it below.  Enjoy!

Cheers,
Rich

; ********************** CoeffR.def **********************
; CoeffR.def : Declares the module parameters for the DLL.

LIBRARY      "CoeffR"
DESCRIPTION  'Standard Windows Dynamic Link Library'

EXPORTS
    ; Explicit exports can go here
        
        _COEFFR

/********************** CoeffR.h ********************** /
#include <windows.h>
#include "elkit32.h"

float __stdcall _COEFFR(LPFLOAT lpValue, FLOAT nLen,
                        DWORD dwStartAddr, DWORD dwVarSize);

/**********************  CoeffR.c ********************** /
#include "CoeffR.h"

// Extern declarations
extern double sqrt(double);

float __stdcall _COEFFR(LPFLOAT lpValue, FLOAT nLen,
                        DWORD dwStartAddr, DWORD dwVarSize)
{
   LPFLOAT lpAddr;
   float   floatValue;
   double  X, Y, sumX, sumY, sumProdXY, sumSquareX, sumSquareY;
   double  coeffR, upper, lower;
   int i, len;

   len = (int) nLen;
   floatValue = 0.0;
   X = Y = sumX = sumY = sumProdXY = sumSquareX = sumSquareY = 0.0;
   coeffR = upper = lower = 0.0;

   for (i=1; i <= len; i++)
   {
      lpAddr = FindAddress_Var(lpValue, len-i, dwStartAddr, dwVarSize);
      if (lpAddr) floatValue = *lpAddr;

      X = i;
      Y = floatValue;

      sumX       += X;
      sumY       += Y;
      sumProdXY  += (X * Y);
      sumSquareX += (X * X);
      sumSquareY += (Y * Y);
   }

   upper = len * sumProdXY - sumX * sumY;
   lower = sqrt(((nLen * sumSquareX) - (sumX * sumX)) *
                ((nLen * sumSquareY) - (sumY * sumY)));

   if (lower != 0.0)
   {
      coeffR = upper / lower;
   }

   return ((float) coeffR);
}

Sunday, February 2, 2003, 11:58:39 PM, you wrote:

> Rich:

> Two questions:  did you have that stupid 'enhanced precision'
> feature turned ON in TS?  If so it actually REDUCES precision. 
> That could have been the problem.
> (The standard install automatically turns on this wierd feature.)

> Second, how is the speed of the DLL?

> Leslie



> Rich Schaaf wrote:
>> 
>> A few words of warning that I hope will save others from tripping over
>> similar problems ...
>> 
>> In the past Bob Fulks (and others) have mentioned that the use of
>> 32-bit floating point values in TS2000i and TS6 is a problem.
>> 
>> I didn't fully appreciate this until a few days ago when I tried to
>> apply the RSquared(14) TS2000i function to a chart of the Emini
>> Nasdaq. It's a builtin function and I wasn't using a very long length
>> so it should work, right?  Wrong.
>> 
>> The values returned by the TS2000i RSquared() function only barely
>> resembled the correct values.  After digging into it, I found that the
>> intermediate values in the RSquared computation were large enough that
>> the 32-bit floats were unable to retain enough precision for the end
>> result to be useful.
>> 
>> In order to get an RSquared function that calculated correctly, I had
>> to write my own as a DLL function that uses 64-bit doubles for the
>> intermediate results of the calculation.
>> 
>> Rich