//+------------------------------------------------------------------+
//|                                                 trendxplorer.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Peeter Paan"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 2
//--- plot first
#property indicator_label1  "first"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrWhite
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot second
#property indicator_label2  "second"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

//
//
//

input int               closePeriod          = 5;                // Close period
input int               lowPeriod            = 13;               // Low period
input int               highPeriod           = 34;               // High period
input bool              alertsOn             = true;             // Alerts true/false?
input bool              alertsOnCurrent      = false;            // Alerts open bar true/false?
input bool              alertsMessage        = true;             // Alerts message true/false?
input bool              alertsSound          = true;             // Alerts sound true/false?
input bool              alertsNotify         = false;            // Alerts push notification true/false?
input bool              alertsEmail          = false;            // Alerts email true/false?
input string            soundFile            = "alert2.wav";     // Sound file

double  firstBuffer[],secondBuffer[],trend[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   IndicatorBuffers(3);
   SetIndexBuffer(0,firstBuffer);
   SetIndexBuffer(1,secondBuffer);
   SetIndexBuffer(2,trend);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int  OnCalculate(const int rates_total,const int prev_calculated,const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   int i=rates_total-prev_calculated+1; if (i>=rates_total) i=rates_total-1;
   
   //
   //
   //
   
   for (; i>=0 && !_StopFlag; i--)
   {
      double emac = iMA(NULL,0,closePeriod,0,1,0,i);  //Takes EMA with Close Price
      double emal = iMA(NULL,0,lowPeriod  ,0,1,3,i);  //Takes EMA with Low Price
      double emah = iMA(NULL,0,highPeriod ,0,1,2,i);  //Takes EMA with High Price

      firstBuffer[i]  = emac-emah;
      secondBuffer[i] = emal-emac;
      trend[i] = (i<rates_total-1) ? (firstBuffer[i]>secondBuffer[i]) ? 1 : (firstBuffer[i]<secondBuffer[i]) ? -1 : trend[i+1]  : 0;  

   }
   if (alertsOn)
   {
      int whichBar = (alertsOnCurrent) ? 0 : 1;
      if (trend[whichBar] != trend[whichBar+1])
      {
         if (trend[whichBar] == 1) doAlert(" crossing up");
         if (trend[whichBar] ==-1) doAlert(" crossing down");       
      }         
   }      
return(rates_total);
}

//------------------------------------------------------------------
// Alert function
//------------------------------------------------------------------

void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          //
          //
          //
          //
          //

          message = timeFrameToString(_Period)+" "+_Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" HLCTrend "+doWhat;
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(_Symbol+" HLCTrend ",message);
             if (alertsSound)   PlaySound(soundFile);
      }
}

//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

