/*+-------------------------------------------------------------------+
  |                                                        iMAXhp.mq4 |
  |                                                                   |
  | "Modified Optimum Elliptic Filter"                                |
  | Source of calculations:                                           |
  | Stocks & Commodities vol 18:7 p20-29                              |
  | Optimal Detrending by John F. Ehlers                              |
  |                                                                   |
  |                                              Crafted by Wylie     |
  |                                              Copyright � 2008     |
  |                                              dazzle.html@live.com |
  +-------------------------------------------------------------------+*/

#property copyright "Copyright � 2008, Wylie"
#property link      "dazzle.html@live.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1  Aqua
#property indicator_color2  Red
#property indicator_width1  1
#property indicator_width2  1

/*+-------------------------------------------------------------------+
  | iMAXhp Parameters                                                 |
  +-------------------------------------------------------------------+*/

int    MinBars,c;

double iBuf0[],iBuf1[],Ph2step;

/*+-------------------------------------------------------------------+
  | iMAXhp Initialization                                             |
  +-------------------------------------------------------------------+*/

int init()
{
 IndicatorBuffers(2);

 // Index Buffer 0

 SetIndexBuffer(0,iBuf0);
 SetIndexShift(0,0);
 SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,Aqua);

 // Index Buffer 1

 SetIndexBuffer(1,iBuf1);
 SetIndexShift(1,0);
 SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1,Red);

 MinBars = 20;

 switch(Period())
     {case PERIOD_M1:  Ph2step = 0.0001;  break;
      case PERIOD_M5:  Ph2step = 0.00015; break;
      case PERIOD_M15: Ph2step = 0.00030; break;
      case PERIOD_M30: Ph2step = 0.0006;  break;
      case PERIOD_H1:  Ph2step = 0.0012;  break;
      case PERIOD_H4:  Ph2step = 0.0024;  break;
      case PERIOD_D1:  Ph2step = 0.0048;  break;
      case PERIOD_W1:  Ph2step = 0.0084;  break;
      case PERIOD_MN1: Ph2step = 0.0168;  break;}

 IndicatorShortName("iMAXhp");

 return (0);
} // int init()

/*+-------------------------------------------------------------------+
  | iMAXhp Main cycle                                                 |
  +-------------------------------------------------------------------+*/

int start()
{
 if(Bars <= MinBars){return (0);}

 int countedBars = IndicatorCounted();

 if(countedBars < 0){return (-1);}
 if(countedBars > 0){countedBars--;}

 int limit = Bars - countedBars - 1;
 double x  = 0.5;

 c = limit;

 while(c >= 0)
     {
      iBuf1[c] = (0.13785 * (2 * ((High[c]   + Low[c])   * x) - ((High[c+1] + Low[c+1]) * x)))
               + (0.0007  * (2 * ((High[c+1] + Low[c+1]) * x) - ((High[c+2] + Low[c+2]) * x)))
               + (0.13785 * (2 * ((High[c+2] + Low[c+2]) * x) - ((High[c+3] + Low[c+3]) * x)))
               + (1.2103  * iBuf1[c + 1] - 0.4867 * iBuf1[c + 2]); 

      if(Close[c] > iBuf1[c])
         {iBuf0[c] = iBuf1[c] + Ph2step;}

      if(Close[c] < iBuf1[c])
         {iBuf0[c] = iBuf1[c] - Ph2step;}

      c--;
     } // for(c = limit;c >= 0;c--)

 return (0);  
} // int start()

/*+-------------------------------------------------------------------+
  | End iMAXhp Main cycle                                             |
  +-------------------------------------------------------------------+*/