Navigation´╝ÜHome > Content >

MACD_Alert2_fixed.mq4

Time: 2011-08-14 | Download file:MACD_Alert2_fixed.mq4

//+------------------------------------------------------------------+
//|                                              MACD Alert2.mq4 |
//|                                       http://www.trading-team.it |
//+------------------------------------------------------------------+
#property copyright "Nick Davidson"
#property link      "http://www.coolwork4easymoney.blogspot.com"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 C'15,15,64'
#property indicator_color2 Aqua
#property indicator_color3 Lime
#property indicator_color4 Red


extern int FastEMA   = 5;
extern int SlowEMA   = 84;
extern int SignalSMA = 18;
extern bool Alerts   = false;

double Macd_Buffer[];
double Signal_Buffer[];
double Green_Buffer[];
double Red_Buffer[];

int Bars1 = 0;
int Bars2 = 0;
int Bars3 = 0;
int Bars4 = 0;

string IndiName;

//+----------------------init----------------------------------------+

int init() {
   
   IndiName = "MACD Alert2(" + FastEMA + "," + SlowEMA + "," + SignalSMA + ")"; 
   
   SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexDrawBegin(1, SignalSMA);
   IndicatorDigits(Digits + 1);
   SetIndexBuffer(0, Macd_Buffer);
   SetIndexBuffer(1, Signal_Buffer);
   SetIndexBuffer(2, Green_Buffer);
   SetIndexBuffer(3, Red_Buffer);
   IndicatorShortName(IndiName);
   SetIndexLabel(0, "MACD");
   SetIndexLabel(1, "Signal");
   SetIndexLabel(2, "Green");
   SetIndexLabel(3, "Red");
   
   return (0);
}

//+----------------------deinit--------------------------------------+
int deinit() {
   ObjectDelete("LOGO");
   return (0);
}

//+----------------------start---------------------------------------+
int start() 
   {

   
   int TotalBars = Bars;
   
   for (int i = 0; i < TotalBars; i++) 
       {Macd_Buffer[i] = iMA(NULL, 0, FastEMA, 0, MODE_EMA, PRICE_TYPICAL, i) - iMA(NULL, 0, SlowEMA, 0, MODE_EMA, PRICE_TYPICAL, i);}
       
   for (i = 0; i < TotalBars; i++) 
       {Signal_Buffer[i] = iMAOnArray(Macd_Buffer, Bars, SignalSMA, 0, MODE_SMA, i);}
   
   for (i = 0; i < TotalBars; i++) 
       {if (Macd_Buffer[i] > Signal_Buffer[i])
           {Green_Buffer[i] = Macd_Buffer[i];
            Red_Buffer[i] = EMPTY_VALUE;}
        else 
           {Red_Buffer[i] = Macd_Buffer[i];
            Green_Buffer[i] = EMPTY_VALUE;}
   }     
   if (Alerts)
      {if (Green_Buffer[1] == Macd_Buffer[1] && Green_Buffer[2] != Macd_Buffer[2]) AlertOnce(Symbol() + " MACD: Buy signal!", 1);
       if (Red_Buffer[1]   == Macd_Buffer[1] && Red_Buffer[2]   != Macd_Buffer[2]) AlertOnce(Symbol() + " MACD: Sell signal!", 1);}
   
   return (0);
}

//+----------------------AlertOnce------------------------------------+
void AlertOnce(string Message, int Number) {
   
   switch (Number) {
     case 1:
        if (Bars1 == 0 || Bars1 < Bars) {
         Alert(Message);
         Bars1 = Bars;}
        break;
      
     case 2:
        if (Bars2 == 0 || Bars2 < Bars) {
         Alert(Message);
         Bars2 = Bars;}
        break;

     case 3:
        if (Bars3 == 0 || Bars3 < Bars) {
         Alert(Message);
         Bars3 = Bars;}
        break;
      
     case 4:
        if (Bars4 == 0 || Bars4 < Bars) {
         Alert(Message);
         Bars4 = Bars;}
        break;
   }
   
}

Recommend