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

[amibroker] Ehler's SineWave Indicator Code check form AB GURU please.



PureBytes Links

Trading Reference Links

Hi,
Can any AB guru who is familiar with Ehler's work check to see if the
following SineWave Indicator code is correct?  TIA

SetBarsRequired( 200, 0 );

// Ehlers Sine Wave Indicator
// Cybernetic Analysis for Stocks and Futures
// Chapter 11, p. 154.
// Original code is at:

function CyclePeriod(array, alpha)
// Figure 9.4 on p. 111
{
  smooth = (array + 2*Ref(array, -1) + 2*Ref(array, -2) + Ref(array,
-3))/6; // Weighted Average

//  for(i = 0; i < 7; i++) cycle[i]=array[i]; // Initialize early
values and as array

  for(i = 0; i < 6; i++) {
     InstPeriod[i] = 0; // Initialize early values and as array
     DeltaPhase[i] = 0;
     cycle[i]=0;
     Period[i]=0;
  }

  for(i = 6; i < BarCount; i++) {
     cycle[i] = (1 - .5*alpha)*(1 - .5*alpha)*(smooth[i] -
2*smooth[i-1] + smooth[i-2]) +
                2*(1 - alpha)*cycle[i-1] - (1 - alpha)*(1 -
alpha)*cycle[i-2];
     Q1[i] = (.0962*cycle[i] + .5769*cycle[i-2] -.5769*cycle[i-4] -
..0962*cycle[i-6])*(.5 + .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;

     //----- Speed up the median calculation by placing it inline

     mlen = 5;
     for(k = mlen - 1; k >= 0; k--) {temparray[k] = DeltaPhase[i + k -
(mlen - 1)];}

     temp=0;
     for(k = mlen - 1; k > 0; k--) {
       for (j = mlen - 1; j > 0; j--) {
         if (temparray[j-1] > temparray[j]) {
           temp = temparray[j-1];
           temparray[j-1] = temparray[j];
           temparray[j] = temp;
         }
       }
     }
     MedianDelta[i] = temparray[mlen - 1 - (mlen / 2)];

     //----- End median calculation

     if(MedianDelta[i] == 0) DC[i] = 15; 
     else DC[i] = 6.28318/MedianDelta[i] + .5;

     InstPeriod[i] = .33*DC[i] + .67*InstPeriod[i-1];
     Period[i] = .15*InstPeriod[i] + .85*Period[i-1];
  }
  return Period;
}

function CyberCycle( array, alpha )
{
  smooth = ( array + 2 * Ref( array, -1 ) +
             2 * Ref( array, -2 ) + Ref( array, -3 ) ) / 6;
  // init value
  Cycle = ( array[ 2 ] - 2 * array[ 1 ] + array[ 0 ] )/4;
  for( i = 6; i < BarCount; i++ )
  {
     Cycle[ i ] = ( ( 1 - 0.5 * alpha) ^ 2 ) *
                  ( smooth[ i ] - 2 * smooth[ i - 1 ] + smooth[ i - 2] ) +
                  2 * ( 1 - alpha ) * Cycle[ i - 1 ] -
                  ( ( 1 - alpha) ^ 2 ) * Cycle[ i - 2 ];
  }
  return Cycle;
}

// compute cycle
Cycle = CyberCycle( (H+L)/2, 0.07 );

// Compute Dominant Cycle.
dominantPeriod = CyclePeriod ( (H+L)/2, 0.07 );

// Compute Dominant Cycle Phase.
DCPeriod = int ( LastValue(dominantPeriod) );
RealPart = 0;
ImagPart = 0;

for ( Count = 0 ; Count < DCPeriod ; Count++ ) {
  RealPart = RealPart + ( sin (360 * (Count/DCPeriod)) * (Cycle[Count]));
  ImagPart = ImagPart + ( cos (360 * (Count/DCPeriod)) * (Cycle[Count]));
}

if ( abs(ImagPart) > 0.001 ) { DCPhase = atan (RealPart/ImagPart); }
if ( abs(ImagPart) <= 0.001 ) { DCPhase = 90 * sign (RealPart); }

DCPhase = DCPhase + 90;

if ( ImagPart < 0 ) { DCPhase = DCPhase + 180; }
if ( DCPhase > 315 ) { DCPhase = DCPhase - 360; }

Plot ( sin (DCPhase) , "Sine" , colorBlue , styleLine + styleThick );
Plot ( sin (DCPhase + 45), "LeadSine", colorRed , styleLine +
styleThick );




------------------------ Yahoo! Groups Sponsor --------------------~--> 
Help Sudanese refugees rebuild their lives through GlobalGiving.
http://us.click.yahoo.com/hjNroD/EbOLAA/cosFAA/GHeqlB/TM
--------------------------------------------------------------------~-> 

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/