/* Ehlers Sinewave 
  Indicator
  by Corey Saxe ver 
  20032005
  From Ehlers book: Cybernetic Analysis for 
  Stocks and Futures, p. 154
  */
  SetBarsRequired(1000000,1000000);
  price=(H+L)/2;
  alpha=Param("alpha",0.07,0.01,1,0.01);
   
  pi=4*atan(1); //have a piece...
  RTD=180/pi; 
  //radians to 
  degrees
  DTR=1/RTD; 
  //degrees to 
  radians
  SWcount=0;SWGate=0;Jcount=0;SWCycle=0;I1=0;Q1=0;InstPeriod=0;DeltaPhase=0;MedianDelta=0;DC=0;Value1=0;
  DCPeriod=0;RealPart=0;ImagPart=0;DCPhase=0;MedianDelta=0;Cycle=0;RealPart 
  = 0; ImagPart = 0;
  // Sine wave 
  generator-----------------------------------------------------------
  freq =Param("Freq",15,6,50,1);
  SWcycle = sin( Cum(1)*(360*DTR)/freq )+2;
  Price = IIf(Param("Prices or 
  test",1,1,2,1)==1,Price,SWCycle); //Select sinewave test pattern or real prices
  //End Sine wave 
  generator--------------------------------------------------------
   
  for(i=36;i<BarCount;i++)//37
  {
  Smooth[i]=(price[i]+2*price[i-1]+2*price[i-2]+price[i-3])/6;
  Cycle[i] = ((1-0.5*alpha[i])^2)*(Smooth[i] - 2*Smooth[i-1] + 
  Smooth[i-2]) + 2*(1-alpha[i])*Cycle[i-1] - ((1-alpha[i])^2)*Cycle[i-2];
   
  //Hilbert Transform}
  Q1[i] = (0.0962*Cycle[i] + 0.5769*Cycle[i-2] - 
  0.5769*Cycle[i-4] - 0.0962*Cycle[i-6]) 
  * (0.5 + 0.08*InstPeriod[i-1]);
  I1[i] = Cycle[i-3];
  if (Q1[i] != 0 AND Q1[i-1] != 0) 
  DeltaPhase[i] = (I1[i]/Q1[i] - I1[i-1]/Q1[i-1]) / 
  (1 + I1[i]*I1[i-1]/(Q1[i]*Q1[i-1]));
  if (DeltaPhase[i] < 0.1) DeltaPhase[i] = 0.1;
  if (DeltaPhase[i] > 1.1) DeltaPhase[i] = 1.1;
  MedianDelta=Median(DeltaPhase , 5);
  //zzz[i]=Mediandelta[i]; 
  OK
  //zzz=Mediandelta[i]; NOT 
  OK
  if (MedianDelta[i] == 0) DC[i]=15;
  else 
  
  DC[i] = 2*pi/MedianDelta[i] + 0.5;//DC[i] 
  is OK
  InstPeriod[i] = 0.33*DC[i] + 0.67*InstPeriod[i-1];
  Value1[i] = 0.15*InstPeriod[i] + 0.85*Value1[i-1];
  //Compute Dominant Cycle 
  Phase
  DCPeriod[i] = int(Value1[i]);
  for (count=0; count < DCPeriod[i]-1; Count++)//DCPeriod-1 
  {
  RealPart[i] = RealPart[i] + sin(DTR*(360*count / DCPeriod[i])) * 
  Cycle[i-count];
  ImagPart[i] = ImagPart[i] + cos(DTR*(360*count / DCPeriod[i])) * 
  Cycle[i-count];
  }
   
  // Code to prevent TS arctan > 
  infinity
  if (abs(ImagPart[i]) > 0.000001) DCPhase[i] = atan(RealPart[i]/ImagPart[i]);
  if (abs(ImagPart[i]) <= 0.000001) DCPhase[i] = (pi/2) * sign(RealPart[i]);
   
  DCPhase[i] = DCPhase[i] + (pi/2);//90
  if (ImagPart[i] < 0) DCPhase[i] = DCPhase[i] + pi;// add 180
  if (DCPhase[i] > (7*pi/4)) DCPhase[i] 
  = DCPhase[i] - (2*pi);// sub 360
  }
  Sine = sin(DCPhase);
  LeadSine = sin(DCPhase+(pi/4));
  A1=Cross(sine,Leadsine);
  A2= Cross(Leadsine,sine);
  GraphXSpace=5;
  Plot(Sine,"Sine",colorRed,1);Plot(LeadSine,"LeadSine",colorGreen,1);
  //Plot(price,"",colorYellow,24|styleNoLabel);Plot(price,"Price",colorBlue,1|styleNoLabel);
  //Plot(A1 OR A2,"Grn-Trough 
  Red-Peak",IIf(A1,colorRed,IIf(A2,colorGreen,Null)),2|styleOwnScale|styleNoLabel);
  //Plot(cycle,"cycle",3,1);
  //Plot(smooth,"smooth",0,1);
  //Plot(test,"dcp",2,8);Plot(test1,"dcp1",1,8);
  //Plot(dcperiod,"DCPeriod",0,1);Plot(Count,"count",1,1);
  //Plot(DC,"DC",2,1);
  //Plot(realpart,"RP",0,1);Plot(imagpart,"IP",1,1);
  //Plot(alpha,"alpha",2,1);