//+------------------------------------------------------------------+
//|                                                  Custom MACD.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+

#property  copyright "Aleksandr Pak,Almaty, 2006"
#property  link      "ekr-ap@mail.ru" 
#property  link      "http://www.metaquotes.net/"

//
//
//
//
//

#property  indicator_separate_window
#property  indicator_buffers   3
#property  indicator_color1    Green
#property  indicator_color2    Red
#property  indicator_color3    Silver
#property  indicator_width1    2
#property  indicator_width2    2
#property indicator_levelstyle STYLE_DOT
#property indicator_levelcolor MediumOrchid

//
//
//
//
//

extern string TimeFrame       = "Current time frame";
extern int    FastMa          = 12;
extern int    FastMaMode      = MODE_EMA;
extern int    SlowMa          = 26;
extern int    SlowMaMode      = MODE_EMA;
extern int    SignalMa        = 9;
extern int    SignalMaMode    = MODE_SMA;

extern string _               = "alerts settings";
extern bool   alertsOn        = true;
extern bool   alertsOnCurrent = true;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = false;
extern bool   alertsEmail     = false;


//
//
//
//
//

double macd[];   
double Up[];   
double Dn[];
double signal[];
double trend[];

//
//
//
//
//

string indicatorFileName;
int    timeFrame;
bool   returnBars;
bool   calculateValue;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
  
  IndicatorBuffers(5);
   SetIndexBuffer(0,Up);    SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,Dn);    SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,signal); 
   SetIndexBuffer(3,macd);
   SetIndexBuffer(4,trend);
      FastMa   = MathMax(FastMa,1);
      SlowMa   = MathMax(SlowMa,1);
      SignalMa = MathMax(SignalMa,1);

      //
      //
      //
      //
      //
      //
   
      indicatorFileName = WindowExpertName();
      returnBars        = (TimeFrame == "returnBars");     if (returnBars)     return(0);
      calculateValue    = (TimeFrame == "calculateValue"); if (calculateValue) return(0);
      timeFrame         = stringToTimeFrame(TimeFrame);   

      //
      //
      //
      //
      //
   
  IndicatorShortName(timeFrameToString(timeFrame)+"  Macd_color("+FastMa+","+SlowMa+","+SignalMa+")");
return(0);
}

int deinit() { return(0); }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { Up[0] = limit+1; return(0); }

   //
   //
   //
   //
   //

   if (calculateValue || timeFrame==Period())
   {
   
      for(i=limit; i>=0; i--) macd[i]   = iMA(NULL,0,FastMa,0,FastMaMode,PRICE_CLOSE,i) - iMA(NULL,0,SlowMa,0,SlowMaMode,PRICE_CLOSE,i);
      for(i=limit; i>=0; i--) signal[i] = iMAOnArray(macd,Bars,SignalMa,0,SignalMaMode,i);
      for(i=limit; i>=0; i--)
      { 
         
          Dn[i]    = EMPTY_VALUE;
          Up[i]    = EMPTY_VALUE;
          trend[i] = trend[i+1];
      
          if(macd[i+1] - macd[i] > 0) { Dn[i] = macd[i]; trend[i] = -1; }
             else                     { Up[i] = macd[i]; trend[i] =  1; }
     }
    
   manageAlerts(); 
   return(0);
   }
   
   //
   //
   //
   //
   //
    
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));

   for(i=limit; i>=0; i--)
   {  
   int y = iBarShift(NULL,timeFrame,Time[i]);
      Up[i]    = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastMa,FastMaMode,SlowMa,SlowMaMode,SignalMa,SignalMaMode,0,y);
      Dn[i]    = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastMa,FastMaMode,SlowMa,SlowMaMode,SignalMa,SignalMaMode,1,y);
      signal[i]= iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastMa,FastMaMode,SlowMa,SlowMaMode,SignalMa,SignalMaMode,2,y);
      macd[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastMa,FastMaMode,SlowMa,SlowMaMode,SignalMa,SignalMaMode,3,y);
      trend[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastMa,FastMaMode,SlowMa,SlowMaMode,SignalMa,SignalMaMode,4,y);
      
    }
    
   manageAlerts(); 
   
return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = StringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string StringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int char = StringGetChar(s, length);
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                     s = StringSetChar(s, length, char - 32);
         else if(char > -33 && char < 0)
                     s = StringSetChar(s, length, char + 224);
   }
   return(s);
}

//
//
//
//
//

void manageAlerts()
{
   if (!calculateValue && alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar));
      if (trend[whichBar] != trend[whichBar+1])
      {
         if (trend[whichBar] == 1) doAlert(whichBar,"buy");
         if (trend[whichBar] ==-1) doAlert(whichBar,"sell");
      }         
   }
}   

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[forBar]) {
       previousAlert  = doWhat;
       previousTime   = Time[forBar];

       //
       //
       //
       //
       //

       message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," - ",timeFrameToString(timeFrame)+" macd ",doWhat);
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol()," macd "),message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}

//
//
//
//
//
   