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

Re: Ehlers - SineTrend



PureBytes Links

Trading Reference Links

Henry Amand wrote:

> anyone any idea where this code might go wrong ? I unfortunatly
> can not seem to find it...


Mystery solved. With my Vars i had some O's instead of 0's.
Font PowerEditor uses does not realy make that clear unfortunatly.
(it however clearly shows in my outlook express)

Here's the good code.

greetings

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

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

 Written by: Ehlers - Rocket science for traders
             typed by Henry Amand

 Description: SineTrend system, page 120

****************************************}

Inputs: Price((H+L)/2);

Vars: Smooth (0),
      Detrender (0) ,
      I1 (0),
      Q1 (0),
      jI (0),
      JQ (0),
      I2 (0),
      Q2 (0),
      Re (0),
      Im (0),
      Period (0),
      SmoothPeriod (0),
      SmoothPrice (0),
      DCPeriod (0),
      RealPart (0),
      Imagpart (0),
      count (0),
      DCPhase (0),
      DCSine (0),
      LeadSine (0),
      Itrend (0),
      Trendline (0),
      Trend (0),
      DaysinTrend (0);



If CurrentBar  >  5  then begin


Smooth =   (4*Price  +  3*Price[1]   + 2*Price[2]   + Price[3])   /  10;
Detrender =   (.0962 * Smooth + .5769 * Smooth[2] - .5769 * Smooth[4] -
.0962*Smooth[6]) * (.075 * Period[1] + .54);


{Compute  InPhase and Quadrature components.}

Q1  =   (.0962 * Detrender + .5769 * Detrender[2] - .5769 * Detrender[4] -
.0962 * Detrender[6]) * (.075 * Period[1] + .54);
I1  = Detrender[3];


{Advance the phase of  I1 and Q1 by 90 degrees}

jI = (.0962 * I1 + .5769 * I1[2] - .5769 * I1[4] - .0962 * I1[6]) * (.075 *
Period[1] + .54);
JQ = (.0962 * Q1 + .5769 * Q1[2] - .5769 * Q1[4] - .0962 * Q1[6]) * (.075 *
Period[1] + .54);


{Phasor addition for 3 bar averaging)}

I2 = I1 - JQ;
Q2 = Q1 + jI;


{Smooth the  I and Q components before applying the discriminator}

I2 = .2 * I2 + .8 * I2[1];
Q2 = .2 * Q2 + .8 * Q2[1];


{Homodyne Discriminator}

Re = I2 * I2[1] + Q2 * Q2[1];
Im = I2 * Q2[1] - Q2 * I2[1];
Re = .2 * Re + .8 * Re[1];
Im = .2 * Im + .8 * Im[1];

If Im <> 0 and Re <> 0 then Period = 360 / ArcTangent (Im/Re);
If Period > 1.5 * Period[1] then Period = 1.5 * Period[1];
If Period < .67 * Period[1] then Period = .67 * Period[1];
If Period < 6 then Period = 6;
If Period > 50 then Period = 50;
Period = .2 * Period + .8 * Period[1];
SmoothPeriod = .33 * Period + .67 * SmoothPeriod[1];


{Compute Dominant Cycle  Phase}

SmoothPrice = (4 * price + 3 * Price[1] + 2 * Price[2] + Price[3]) / 10;
DCPeriod = IntPortion(SmoothPeriod + .5);
RealPart = 0;
ImagPart = 0;


For count = 0 To DCPeriod - 1 begin

RealPart = RealPart + sine (360 * count / DCPeriod) * (SmoothPrice[count]);
ImagPart = imagPart + CoSine (360 * count / DCPeriod) *
(SmoothPrice[count]);

End;

If AbsValue(ImagPart) > 0 then DCPhase = Arctangent(RealPart / ImagPart);
If AbsValue(ImagPart) <= .001 then DCPhase = DCPhase + 90 * Sign(RealPart);
DCPhase = DCPhase + 90;


{Compensate for one bar lag of the Weighted Moving Average}

DCPhase = DCPhase + 360 / SmoothPeriod;

If ImagPart < 0 then DCPhase = DCPhase + 180;
If DCPhase > 315 then DCPhase = DCPhase - 360;


{Compute the Sine and LeadSine Indicators}

DCSine = Sine(DCPhase);
LeadSine = Sine(DCPhase + 45);


{Compute Trendline as simple average over the measured dominant cycle
period}

ITrend = 0;

For count = 0 to DCPeriod - 1 begin
ITrend = ITrend + Price[count];
End;

If DCPeriod > 0 then ITrend = ITrend / DCPeriod;
Trendline = (4 * ITrend + 3 * ITrend[1] + 2 * ITrend[2] + ITrend[3]) / 10;
If CurrentBar < 12 then Trendline = Price;


{Assume Trend Mode}

Trend = 1;


{Measure days in trend from last crossing of the Sinewave Indicator lines}

If Sine(DCPhase) Crosses Over Sine(DCPhase + 45) or Sine(DCPhase) Crosses
Under Sine(DCPHase + 45) Then begin
DaysInTrend = 0;
Trend = 0;
End;

DaysInTrend = DaysInTrend + 1;
If DaysInTrend < .5 * SmoothPeriod then Trend = 0;


{Cycle Mode If delta phase is +/- 50% of dominant cycle change of phase}

If SmoothPeriod <> 0 and (DCPhase - DCPhase[1] > .67 * 360 / SmoothPeriod
and DCPhase - DCPhase[1] < 1.5 * 360 / SmoothPeriod) then Trend = 0;


{Declare a Trend Mode if the SmoothPrice is more than 1.5% from the
Trendline}

If AbsValue ((SmoothPrice - Trendline) / Trendline) >= .015 then Trend = 1;

If Trend = 1 then begin

If Trend [1] = 0 then begin

If MarketPosition = -1 and SmoothPrice >= Trendline then buy;
If MarketPosition = 1 and SmoothPrice < Trendline then sell;

End;

If SmoothPrice Crosses Over Trendline then buy;
If SmoothPrice Crosses Under Trendline then sell;
End;

If Trend = 0 then begin

If LeadSine Crosses Over DCSine then buy;
If LeadSine Crosses Under DCSine then sell;

End;
End;