///+------------------------------------------------------------------+ 
// Adaption of iMAXhpMTF v1.2 \¦/
// Knowledge of the ancients (ò ó)
//______________________o0o___(_)___o0o__
//___¦_____¦_____¦mladen¦_____¦_____¦_____¦
//¦_cja_¦_____¦_____¦_____¦_____¦_____¦__
//___¦_____¦_____¦_____¦_Xard¦777__¦_____¦
//¦____¦ihldiaf¦_____¦_____¦_____¦____¦__
//___¦_____¦_____¦_____¦Baba¦Master_¦____¦
//¦FxSniper___¦_____¦_igor_¦_____¦_____¦__           April 18th, 2009
//+------------------------------------------------------------------+
/*+-------------------------------------------------------------------+
  |                                                 iMAXhpMTFdet3.mq4 |
  |                                                                   |
  | MTF display application of the iMAXhp indicator with the          |
  | iMAXhp indicator itself built into the code.                      |
  |                                                                   |
  | This application is based upon kang_gun's                         |
  | iMAX MTF DETECTOR V1.1.mq4 Copyright © 2009, KG                   |
  | Published by Forex TSD                                            |
  |                                                                   |
  | This indicator generates global variable values.                  |
  |                                                                   |
  | This indicator requires the stdlib.mqh file to exist in the       |
  | MT4 Terminal/experts/include folder (for error handling           |
  | associated with setting global variable values).                  |
  | The stdlib.mqh file is normally already in that folder...         |
  | unless it has been removed.                                       |
  |                                                                   |
  |                                         Crafted by "Wylie"        |
  |                                         Copyright © 2009, "Wylie" |
  |                                         dazzle.html@live.com      |
  +-------------------------------------------------------------------+*/

#property copyright "Copyright © 2009, Wylie"
#property link      "dazzle.html@live.com"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1  Aqua
#property indicator_color2  DarkOrange
#property indicator_width1  2
#property indicator_width2  2

#include   <stdlib.mqh>

extern bool   show.timeleft=true;
extern string bs = "4X-Lite";
extern int win = 1;
extern int Adjust_Side_to_side = -10;
extern int Shift_UP_DN = 0;

extern int corner=0;
extern int label_x_distance=-10;
extern int label_y_distance=0;
extern int imax.win = 1;

extern int      Corner          = 1;
extern bool     SuppressPhLines = true;
extern bool     EnableAlerts    = false;
extern bool     EnableArrows    = true;
extern int      ArrowUPsize     = 3;
extern int      ArrowDNsize     = 3;
extern color    ArrowUPcolor    = Yellow;
extern color    ArrowDNcolor    = Yellow;

int             MinBars;

double          iBuf0[],iBuf1[],Ph2step,CrossUp[],CrossDn[],Clearance;

int             Flen             = 0,
                Lspc             = 20,
                per[10],c,err,dir,AST;

string          Iname            = "iMAXhpDet",
                label[12],perStr[9],signLbl[10],sign,Chart;

color           Col;

static datetime AlertX[2];

/*+-------------------------------------------------------------------+
  | iMAXhp MTF Fast Trend Detector Initialization                     |
  +-------------------------------------------------------------------+*/

int init()
{
 IndicatorBuffers(4);

 // Index Buffer 0

 SetIndexBuffer(0,iBuf0);
 SetIndexShift(0,0);
 SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2,Aqua);

 // Index Buffer 1

 SetIndexBuffer(1,iBuf1);
 SetIndexShift(1,0);
 SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2,DarkOrange);

 // Index Buffer 2

 SetIndexBuffer(2,CrossUp);
 SetIndexStyle(2,DRAW_ARROW,STYLE_SOLID,ArrowUPsize,ArrowUPcolor);
 SetIndexArrow(2,241);

 // Index Buffer 3

 SetIndexBuffer(3,CrossDn);
 SetIndexStyle(3,DRAW_ARROW,STYLE_SOLID,ArrowDNsize,ArrowDNcolor);
 SetIndexArrow(3,242);

 MinBars = 20;

 // The "Clearance" variable below sets the distance from the trendline to the arrows...
 // It can be adjusted by the user to their own preference...

 // The Ph2step parameter sets the amplitude of the phase shift... may need to be adjusted
 // for different currencies if the amplitude is insufficient for reliable phase crossings... like the JPY

 switch(Period())
     {case PERIOD_M1:  Ph2step = 0.0001;  Chart = "M1";  Clearance = 0.0003; AST =      59; break;
      case PERIOD_M5:  Ph2step = 0.00015; Chart = "M5";  Clearance = 0.0007; AST =     259; break;
      case PERIOD_M15: Ph2step = 0.0003; Chart = "M15"; Clearance = 0.0011; AST =     899; break;
      case PERIOD_M30: Ph2step = 0.0006;  Chart = "M30"; Clearance = 0.0018; AST =    1799; break;
      case PERIOD_H1:  Ph2step = 0.0012;  Chart = "H1";  Clearance = 0.0027; AST =    3599; break;
      case PERIOD_H4:  Ph2step = 0.0024;  Chart = "H4";  Clearance = 0.0038; AST =   14399; break;
      case PERIOD_D1:  Ph2step = 0.0048;  Chart = "D1";  Clearance = 0.007; AST =   86399; break;
      case PERIOD_W1:  Ph2step = 0.0084;  Chart = "W1";  Clearance = 0.013; AST =  604799; break;
      case PERIOD_MN1: Ph2step = 0.0168;  Chart = "MN1"; Clearance = 0.026; AST = 2591999; break;}

 IndicatorShortName("iMAXhp");

 perStr[1]  = "M1";
 perStr[2]  = "M5";
 perStr[3]  = "M15";
 perStr[4]  = "M30";
 perStr[5]  = "H1";
 perStr[6]  = "H4";
 perStr[7]  = "D1";
 perStr[8]  = "W1";
 perStr[9]  = "MN";
 
 per[1]     = PERIOD_M1;
 per[2]     = PERIOD_M5;
 per[3]     = PERIOD_M15;
 per[4]     = PERIOD_M30;
 per[5]     = PERIOD_H1;
 per[6]     = PERIOD_H4;
 per[7]     = PERIOD_D1;
 per[8]     = PERIOD_W1;
 per[9]     = PERIOD_MN1;

 for(c = 1;c <= 9;c++)
    {Flen     = Flen + Lspc;
     label[c] = "iMAXhp_"+perStr[c];

 // Create timeframe labels
     ObjectCreate (label[c],OBJ_LABEL,imax.win,0,0);
     ObjectSetText(label[c],perStr[c],7,"Arial Bold",Moccasin);
     ObjectSet    (label[c],OBJPROP_CORNER,corner);
     ObjectSet    (label[c],OBJPROP_XDISTANCE,Flen+label_x_distance+4);
     ObjectSet    (label[c],OBJPROP_YDISTANCE,label_y_distance+22);}

 // Create title 
 Flen = Flen + Lspc;
 
 return(0);
} // int init()

/*+-------------------------------------------------------------------+
  | iMAXhp MTF Fast Trend Detector Deinitialization                   |
  +-------------------------------------------------------------------+*/

int deinit()
{
 // Delete labels
 for(c = 1;c <= 11;c++)
     {ObjectDelete(label[c]);}

 // Delete arrows
 for(c = 1;c <= 10;c++)
     {ObjectDelete(Iname+" "+per[c]+" "+Symbol());}

 // Delete indicator "GO" object
 ObjectDelete(signLbl[10]);
  ObjectDelete(bs);

 // Delete Global Variables
 for(c = 1;c <= 11;c++)
     {ObjectDelete(Iname+" "+perStr[c]);
      if(!GlobalVariableDel(Iname+" "+per[c]+" "+Symbol())){err=GetLastError();
         Print(Iname+" - Error GVsetInt() "+Iname+" deinit(): "+perStr[c]+ErrorDescription(err));}}

 //Delete indicator "GO" global variable
 if(!GlobalVariableDel(Iname+"GO"+Symbol())){err=GetLastError();
     Print(Iname+" - Error GVsetInt() "+Iname+" deinit(): iMAXhpDetGO "+" "+ErrorDescription(err));}

  ObjectDelete("xard1#");

 return(0);
} // int deinit()

/*+-------------------------------------------------------------------+
  | iMAXhp MTF Fast Trend Detector Main cycle                         |
  +-------------------------------------------------------------------+*/

int start()
{
 double r;
   int m,s,k;
   m=Time[0]+Period()*60-CurTime();
   r=m/60.0;
   s=m%60;
   m=(m-m%60)/60;

if (show.timeleft) {
   ObjectDelete("xard1#");
   ObjectCreate("xard1#", OBJ_LABEL, win, 0, 0);
   ObjectSetText("xard1#",m+":"+s , 14, "MV Boli", Yellow);
   ObjectSet("xard1#", OBJPROP_CORNER, 0);
   ObjectSet("xard1#", OBJPROP_XDISTANCE, Adjust_Side_to_side +130);
   ObjectSet("xard1#", OBJPROP_YDISTANCE, Shift_UP_DN +  2);
}   
 
 
 if(Bars <= MinBars){return (0);}

 if(SuppressPhLines){SetIndexStyle(0,DRAW_NONE);SetIndexStyle(1,DRAW_NONE);}

 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;}

      double b0 = iBuf0[c],b1 = iBuf0[c+1],
             r0 = iBuf1[c],r1 = iBuf1[c+1];

      if(EnableArrows)
         {if(b0 > r0 && b1 < r1)
             {CrossUp[c] = r0 - Clearance;}else{CrossUp[c] = EMPTY_VALUE;}
          if(b0 < r0 && b1 > r1)
             {CrossDn[c] = r0 + Clearance;}else{CrossDn[c] = EMPTY_VALUE;}}
      c--;
     } // for(c = limit;c >= 0;c--)

 if(EnableAlerts)
    {if(b0 > r0 && b1 < r1)
        {sendAlert(0,(getDateTime()+" "+Symbol()+" "+Chart+" iMAXhp signals up trend crossing."));}
     if(b0 < r0 && b1 > r1)
        {sendAlert(1,(getDateTime()+" "+Symbol()+" "+Chart+" iMAXhp signals down trend crossing."));}}

 Flen = 0;

 for(c = 1;c <= 9;c++)
    {Flen = Flen + Lspc;

     // Read indicator values.
     double iMAXhp0=iCustom(NULL,per[c],"iMAXhp",0,0);
     double iMAXhp1=iCustom(NULL,per[c],"iMAXhp",1,0);

     // Calculate arrow type and color
     if(iMAXhp0 > iMAXhp1 && Close[0] > iMAXhp0){sign="-";Col=Lime;         dir=1;} //Trend Up Strong
     if(iMAXhp0 > iMAXhp1 && Close[0] < iMAXhp0){sign="-";Col=Green;        dir=2;} //Trend Up Weak
     if(iMAXhp0 < iMAXhp1 && Close[0] < iMAXhp0){sign="-";Col=Maroon;       dir=3;} //Trend Down Strong
     if(iMAXhp0 < iMAXhp1 && Close[0] > iMAXhp0){sign="-";Col=Red;          dir=4;} //Trend Down Weak
     if(iMAXhp0==iMAXhp1)                       {sign="-";Col=Yellow;       dir=5;} //Ranging   

     // Create arrow objects
     signLbl[c] = Iname+" "+perStr[c];

     ObjectDelete (signLbl[c]);
     ObjectCreate (signLbl[c],OBJ_LABEL,imax.win,0,0);
     ObjectSetText(signLbl[c],sign,45,"Arial",Col);
     ObjectSet    (signLbl[c],OBJPROP_CORNER,corner);
     ObjectSet    (signLbl[c],OBJPROP_XDISTANCE,Flen+label_x_distance+0);
     ObjectSet    (signLbl[c],OBJPROP_YDISTANCE,label_y_distance);
     
   
     // Set global variable values
     if(!GVsetInt(Iname+" "+per[c]+" "+Symbol(),dir))
        {err=GetLastError();Print(Iname+" - Error GVsetInt(): "+dir+" "+ErrorDescription(err));}
    } // for(c = 1;c <= 9;c++)

 // Check that global variables exist for periods
 for(c = 9;c >= 1;c--)
     {if(!GlobalVariableCheck(Iname+" "+per[c]+" "+Symbol()))
         {break;}}

 // Detect global variable values set and preset indicator "GO" coloration
 if(c == 0)
    {Col = DodgerBlue;
     if(!GVsetInt(Iname+"GO"+Symbol(),1))
        {err=GetLastError();Print(Iname+" - Error GVsetInt(): "+Iname+"GO"+" "+ErrorDescription(err));}}
 if(c != 0)
    {Col = Red;
     if(!GVsetInt(Iname+"GO"+Symbol(),0))
        {err=GetLastError();Print(Iname+" - Error GVsetInt(): "+Iname+"GO"+" "+ErrorDescription(err));}}

 // Create Indicator "GO" object
 Flen = Flen + Lspc;
 signLbl[10] = Iname+"GO";
 ObjectDelete (signLbl[10]);
 ObjectCreate (signLbl[10],OBJ_LABEL,imax.win,0,0);
 ObjectSetText(signLbl[10],Symbol(),14, "ERAS Light ITC Bold",Col);
 ObjectSet    (signLbl[10],OBJPROP_CORNER,corner);
 ObjectSet    (signLbl[10],OBJPROP_XDISTANCE,Flen+label_x_distance-175);
 ObjectSet    (signLbl[10],OBJPROP_YDISTANCE,label_y_distance+2);

 return(0);
} // int start()

/*+-------------------------------------------------------------------+
  | End iMAXhp MTF Fast Trend Detector Main cycle                     |
  +-------------------------------------------------------------------+*/

/*+-------------------------------------------------------------------+
  | iMAXhp MTF Fast Trend Detector support functions                  |
  +-------------------------------------------------------------------+*/

/*+-------------------------------------------------------------------+
  | *** Alert processing function                                     |
  +-------------------------------------------------------------------+*/

void sendAlert(int AlertNum,string AlertText)
{
 if(TimeCurrent() > AlertX[AlertNum] + AST)
    {AlertX[AlertNum] = TimeCurrent();
                                        /* Add a sound file here if you want another form of alert...
                                           The sound file must be located in the MT4 "sounds" file folder 
                                           and must be a .wav format file. Also remove the // characters before
                                           the PlaySound instruction.  The Alert command and semicolon can 
                                           be removed if the user wishes to remove the alert entirely, if favor 
                                           of a sound file... or it can be "commented out" just add // before 
                                           the alert instruction. */

//     PlaySound("alert.wav"); 
//     Print(AlertText);
     Alert(AlertText);
    } // if(TimeCurrent() > AlertX[AlertNum] + AST)

 return(0);
} // void sendAlert(int AlertNum,string AlertText)

/*+-------------------------------------------------------------------+
  | *** Date/Time display function                                    |
  +-------------------------------------------------------------------+*/

string getDateTime()
{
 string dsplyDateTime = TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS);

 return(dsplyDateTime);
} // string getDateTime()

/*+-------------------------------------------------------------------+
  | *** Set Global Variable value function                            |
  +-------------------------------------------------------------------+*/

bool GVsetInt(string a,int b)
{
 if(GlobalVariableSet(a,b) == 0)
    {return(false);}
 else
    {return(true);}

} // GVsetInt(string a,int b)

/*+-------------------------------------------------------------------+
  | End iMAXhp MTF Fast Trend Detector support functions              |
  +-------------------------------------------------------------------+*/

