Ehlers Dominant Cycle Period.afl
上传用户:shiqiang
上传日期:2009-06-12
资源大小:1289k
文件大小:3k
源码类别:

金融证券系统

开发平台:

Others

  1. //------------------------------------------------------------------------------
  2. //
  3. //  Formula Name:    Ehlers Dominant Cycle Period
  4. //  Author/Uploader: Not Too Swift 
  5. //  E-mail:          
  6. //  Date/Time Added: 2005-04-23 00:31:49
  7. //  Origin:          
  8. //  Keywords:        
  9. //  Level:           semi-advanced
  10. //  Flags:           indicator
  11. //  Formula URL:     http://www.amibroker.com/library/formula.php?id=460
  12. //  Details URL:     http://www.amibroker.com/library/detail.php?id=460
  13. //
  14. //------------------------------------------------------------------------------
  15. //
  16. //  This is another calculation from Ehlers's "Cybernetic Analysis for Stocks
  17. //  and Futures." It returns the dominant period length.
  18. //
  19. //  This code is not elegant. I would greatly appreciate help in making it more
  20. //  transparent and elegant.
  21. //
  22. //------------------------------------------------------------------------------
  23. SetBarsRequired(200, 0);
  24. // Ehlers Dominant Cycle Period
  25. // from Ehlers, John F. Cybernetic Analysis for Stocks and Futures. Wiley. 2004. 
  26. // Chapter 9, p. 107. Code on p. 111.
  27. function CyclePeriod(array, alpha)
  28. // Figure 9.4 on p. 111
  29. {
  30.   smooth = (array + 2*Ref(array, -1) + 2*Ref(array, -2) + Ref(array, -3))/6;
  31. //  for(i = 0; i < 7; i++) cycle[i]=array[i]; // Initialize early values and as array
  32.   for(i = 0; i < 6; i++) 
  33.   {
  34.      InstPeriod[i] = 0; // Initialize early values and as array
  35.      DeltaPhase[i] = 0;
  36.      cycle[i]=0;
  37.      Period[i]=0;
  38.   }
  39.   for(i = 6; i < BarCount; i++)
  40.   {
  41.      cycle[i] = (1 - .5*alpha)*(1 - .5*alpha)*(smooth[i] - 2*smooth[i-1] + smooth[i-2]) +
  42.                 2*(1 - alpha)*cycle[i-1] - (1 - alpha)*(1 - alpha)*cycle[i-2];
  43.      Q1[i] = (.0962*cycle[i] + .5769*cycle[i-2] -.5769*cycle[i-4] - .0962*cycle[i-6])*(.5 + .08*InstPeriod[i-1]);
  44.      I1[i] = cycle[i-3];
  45.      if(Q1[i] != 0 AND Q1[i-1] != 0) 
  46.         DeltaPhase[i] = (I1[i]/Q1[i] - I1[i-1]/Q1[i-1])/(1 + I1[i]*I1[i-1]/(Q1[i]*Q1[i-1]));
  47.      if(DeltaPhase[i] < 0.1) DeltaPhase[i] = 0.1;
  48.      if(DeltaPhase[i] > 1.1) DeltaPhase[i] = 1.1;
  49.      //----- Speed up the median calculation by placing it inline
  50.      mlen = 5;
  51.      for(k = mlen - 1; k >= 0; k--) {temparray[k] = DeltaPhase[i + k - (mlen - 1)];}
  52.      temp=0;
  53.      for(k = mlen - 1; k > 0; k--)
  54.      {for (j = mlen - 1; j > 0; j--)
  55.        {if (temparray[j-1] > temparray[j])
  56.          {
  57.            temp = temparray[j-1];
  58.            temparray[j-1] = temparray[j];
  59.            temparray[j] = temp;
  60.          }
  61.        }
  62.      }
  63.      MedianDelta[i] = temparray[mlen - 1 - (mlen / 2)];
  64.      //----- End median calculation
  65.      if(MedianDelta[i] == 0) DC[i] = 15; 
  66.      else DC[i] = 6.28318/MedianDelta[i] + .5;
  67.      InstPeriod[i] = .33*DC[i] + .67*InstPeriod[i-1];
  68.      Period[i] = .15*InstPeriod[i] + .85*Period[i-1];
  69.   }
  70.   return Period;
  71. }
  72. Med = (H+L)/2;
  73. // CyclePeriod
  74. CP = CyclePeriod(Med, .07);
  75. Plot(CP, "CyclePeriod", colorRed, styleLine);