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

PolyFitProject.txt



PureBytes Links

Trading Reference Links

Here is text file of indicator and function which you can
cut and paste into whatever TS system you are using.

Clyde
- - - - - - - - - - - - - - - - - - - - -  - - - - - - -
Clyde Lee   Chairman/CEO          (Home of SwingMachine)
SYTECH Corporation          email: clydelee@xxxxxxxxxxxx  
7910 Westglen, Suite 105       Office:    (713) 783-9540
Houston,  TX  77063               Fax:    (713) 783-1092
Details at:                      www.theswingmachine.com
- - - - - - - - - - - - - - - - - - - -  - - - - - - - -

{
Indicator:   PolyFit_Proj
}

Input:      Price(C),     {Price data to fit polynomial to and project }
            DegPoly(3),   {Degree of polynomial to fit -- max=7        }
            ChanWidth(5), {Percent of price to setup for channel lines }
                          {Negative = multiplier of 21 bar AvgTrueRange}
            NumPoint(25), {Number of data points to use in fit--max=53 }
            NPointPd(0);  {Number of points forward to predict price   }
Input:      EndDate(0);   {Ending date for computation of coefficients }
            {
            SmoLeng(0);   {Number of points in T3Average filter        }
            }


Array:  CoefAry[12](0);
Vars:   EndKnt(0), IsEnd(False), HoldBar(0), ATR21(AvgTrueRange(21)),
        ChanWide(0), ChanMult(Iff(ChanWidth<0,-ChanWidth,ChanWidth*.01));

If EndKnt=0 Or IsEnd=False then Value1=PolyFit_Project(Price, DegPoly, NumPoint, NPointPd);

IsEnd = Date>EndDate and EndDate<>0;
If ChanWidth<0 then begin
  ATR21=AvgTrueRange(21);
  ChanWide=ATR21*ChanMult;
End
Else ChanWide=Average(c,51)*ChanMult;


{
If SmoLeng>0 then Value2=T3Average(Value1,SmoLeng)
Else              Value2=Value1;

If SmoLeng>0 then begin
  Value2=Average(Value1,SmoLeng);
  Plot2[-NPointPd+IntPortion(SmoLeng/2+.5)](Value2,"CMA");
End;
}

If IsEnd and LastBarOnChart=False then begin
  If HoldBar=0 then HoldBar=CurrentBar;
  Value1=PolyFit_Project(Price, DegPoly, NumPoint, CurrentBar-HoldBar);
  Plot1[-(CurrentBar-HoldBar)-NPointPd](Value1,"EstPrice");
End 
Else If LastBarOnChart then begin
  If HoldBar=0 then HoldBar=CurrentBar;
  If EndKnt<MaxBarsForward then begin
    EndKnt=0;
    For Value2=0 downto -(MaxBarsForward-NPointPd) begin
      Value1=PolyFit_Project(Price, DegPoly, NumPoint, CurrentBar-HoldBar+EndKnt+NPointPd);
      Plot1[-(CurrentBar-HoldBar)-EndKnt-NPointPd](Value1,"EstPrice");
      Plot2[-(CurrentBar-HoldBar)-EndKnt-NPointPd](Value1+ChanWide,"Est+C");
      Plot3[-(CurrentBar-HoldBar)-EndKnt-NPointPd](Value1-ChanWide,"Est-C");
      EndKnt=EndKnt+1;
    End;
  End;
End
Else begin
  Plot1[-NPointPd](Value1,"EstPrice");
  Plot2[-NPointPd](Value1+ChanWide,"Est+C");
  Plot3[-NPointPd](Value1-ChanWide,"Est-C");
End;









{
Function:   PolyFit_Proj
}

Input:      Price(NumericSeries),    {Price data to fit polynomial to and project }
            DegPolyI(NumericSimple), {Degree of polynomial to fit -- max=11       }
            NumPointI(NumericSimple), {Number of data points to use in fit         }
            NPointPdI(NumericSimple); {Number of points forward to predict price   }



{      PROGRAM LPOLYNOM}
{C     ----------------------------------------------------------------}
{C     Alg5"2.for   FORTRAN program for implementing Algorithm 5.2}
{C     }
{C     NUMERICAL METHODS: FORTRAN Programs, (c) John H. Mathews 1995}
{C     To accompany the text:}
{C     NUMERICAL METHODS for Math., Science & Engineering, 2nd Ed, 1992}
{C     Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.AX.}
{C     Prentice Hall, Inc.; USA, Canada, Mexico ISBN 0-13-624990-6}
{C     Prentice Hall, International Editions:   ISBN 0-13-625047-5}
{C     This free software is compliments of the author.}
{C     E-mail address:       in%"mathews@xxxxxxxxxxxxx"}
{C     }
{C     Algorithm 5.2 (Least Squares Polynomial).}
{C     Section   5.2, Curve Fitting, Page 278}
{C     ----------------------------------------------------------------}

{
      SUBROUTINE SOLVELI(Price,NumPoint,AX,BX,DegPoly,CX)
      INTEGER Col,IX,JX,KX,DegPoly,IP,RX,Row,T
      REAL AX,BX,CX,XX,Price,PX,Sum,Pow,Prod,XK,YK,Err,Z1
      DIMENSION AX(1:8,1:8),BX(1:8),CX(1:8),Price(1:53)
      DIMENSION Row(1:7),ZX(1:7)
      DIMENSION Pow(0:14)
}

Vars:  NumPoint(AbsValue(NumPointI)),
       DegPoly (iff(NumPoint<=DegPolyI,NumPoint-1,DegPolyI)),
       NPointPd(AbsValue(NPointPdI));
Vars:  Col(0),IX(0),JX(0),KX(0),IP(0),RX(0),Temp(0);
Vars:  XX(0),PX(0),Sum(0),Prod(0),XK(0),YK(0),Z1(0);
Array: AX[12,12](0),BX[12](0),CX[12](0),Row[12](0),ZX[12](0),Pow[24](0) ;

{C FILL MATRIX FIRST}
If NumPoint=0 then  Sum=Price 
Else begin
  FOR RX=1 TO DegPoly+1 BEGIN
    BX[RX]=0;
  END;
  FOR KX=1 TO NumPoint BEGIN
    YK=Price[NumPoint-KX];
    XK=KX;
    Prod=1;
    FOR RX=1 TO DegPoly+1 BEGIN
      BX[RX]=BX[RX]+YK*Prod;
      Prod=Prod*XK;
    END;
  END;
  FOR JX=1 TO 2*DegPoly BEGIN
    Pow[JX]=0;
  END;
  Pow[0]=NumPoint;
  FOR KX=1 TO NumPoint BEGIN
    XK=KX;
    Prod=KX;
    FOR JX=1 TO 2*DegPoly BEGIN
      Pow[JX]=Pow[JX]+Prod;
      Prod=Prod*XK;
    END;
  END;
  FOR RX=1 TO DegPoly+1 BEGIN
    FOR Col=1 TO DegPoly+1 BEGIN
      AX[RX,Col]=Pow[RX+Col-2];
    END;
  END;
  
  {C NOW SOLVE FOR COEFFICIENTS}
  
  FOR JX=1 TO DegPoly+1 BEGIN
    Row[JX]=JX;
  END;
  FOR IP=1 TO DegPoly BEGIN
    FOR KX=IP+1 TO DegPoly+1 BEGIN
      IF ABSValue(AX[Row[KX],IP])>ABSValue(AX[Row[IP],IP]) THEN BEGIN;
        Temp=Row[IP];
        Row[IP]=Row[KX];
        Row[KX]=Temp;
      END;
    END;
    FOR KX=IP+1 TO DegPoly+1 BEGIN
      AX[Row[KX],IP]=AX[Row[KX],IP]/AX[Row[IP],IP];
      FOR Col=IP+1 TO DegPoly+1 BEGIN
        AX[Row[KX],Col]=AX[Row[KX],Col]-AX[Row[KX],IP]*AX[Row[IP],Col];
      END;
    END;
  END;
  ZX[1]=BX[Row[1]];
  FOR KX=2 TO DegPoly+1 BEGIN
    Sum=0;
    FOR Col=1 TO KX-1 BEGIN
      Sum=Sum+AX[Row[KX],Col]*ZX[Col];
    END;
    ZX[KX]=BX[Row[KX]]-Sum;
  END;
  CX[DegPoly+1]=ZX[DegPoly+1]/AX[Row[DegPoly+1],DegPoly+1];
{If LastBarOnChart and NPointPd=0 then print(date,time,DegPoly,ZX[DegPoly+1],AX[Row[DegPoly+1],DegPoly+1]);}
  FOR KX=DegPoly DOWNTO 1 BEGIN
    Sum=0;
    FOR Col=KX+1 TO DegPoly+1 BEGIN
      Sum=Sum+AX[Row[KX],Col]*CX[Col];
    END;
    CX[KX]=(ZX[KX]-Sum)/AX[Row[KX],KX];
  END;
  
  {C NOW COMPUTE NEXT POINT IN SERIES AND RETURN}
  
  Sum=CX[DegPoly+1];
  FOR KX=DegPoly DOWNTO 1  BEGIN
    Sum=CX[KX]+Sum*(NumPoint+NPointPd);
  END;
END;
PolyFit_Project=Sum;