//+------------------------------------------------------------------+
//|                                    awesome-oscillator-alert.mq4    |
//|  Plots the Awesome Oscillator and alerts on a zero-line cross.     |
//|  Source: web-forex (educational, free to use and modify)          |
//+------------------------------------------------------------------+
#property copyright "web-forex"
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 clrGreen
#property indicator_level1 0

extern bool EnableAlert = true;
extern bool EnablePush  = false;

double AOBuffer[];

int lastSignal = 0; // 0 = none, 1 = crossed above zero, -1 = crossed below zero

int OnInit()
{
   SetIndexBuffer(0, AOBuffer);
   SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexLabel(0, "AO");

   IndicatorShortName("Awesome Oscillator Alert");
   return(INIT_SUCCEEDED);
}

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 start = rates_total - prev_calculated;
   if (start > rates_total - 35)
      start = rates_total - 35;
   if (start < 0)
      return(rates_total);

   for (int i = start; i >= 0; i--)
   {
      AOBuffer[i] = iAO(NULL, 0, i);
   }

   CheckZeroCrossSignal();
   return(rates_total);
}

void CheckZeroCrossSignal()
{
   if (!EnableAlert && !EnablePush)
      return;

   double prev = iAO(NULL, 0, 2);
   double last = iAO(NULL, 0, 1);

   int signal = 0;
   if (prev <= 0 && last > 0)
      signal = 1;  // crossed above zero
   else if (prev >= 0 && last < 0)
      signal = -1; // crossed below zero

   if (signal != 0 && signal != lastSignal)
   {
      string msg = (signal == 1)
         ? Symbol() + " " + EnumToString((ENUM_TIMEFRAMES)Period()) + ": Awesome Oscillator crossed above zero"
         : Symbol() + " " + EnumToString((ENUM_TIMEFRAMES)Period()) + ": Awesome Oscillator crossed below zero";

      if (EnableAlert)
         Alert(msg);
      if (EnablePush)
         SendNotification(msg);

      lastSignal = signal;
   }
}
