//+------------------------------------------------------------------+
//|                                accelerator-oscillator-alert.mq4   |
//|  Plots the Accelerator Oscillator and alerts on a second           |
//|  consecutive same-colored bar (the classic Bill Williams two-bar   |
//|  trigger).                                                        |
//|  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 ACBuffer[];

int lastSignal = 0; // 0 = none, 1 = two green bars, -1 = two red bars

int OnInit()
{
   SetIndexBuffer(0, ACBuffer);
   SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexLabel(0, "AC");

   IndicatorShortName("Accelerator 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 - 40)
      start = rates_total - 40;
   if (start < 0)
      return(rates_total);

   for (int i = start; i >= 0; i--)
      ACBuffer[i] = iAC(NULL, 0, i);

   CheckTwoBarSignal();
   return(rates_total);
}

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

   double acTwoBack = iAC(NULL, 0, 3);
   double acPrev    = iAC(NULL, 0, 2);
   double acLast    = iAC(NULL, 0, 1);

   bool twoGreen = (acLast > acPrev && acPrev > acTwoBack);
   bool twoRed   = (acLast < acPrev && acPrev < acTwoBack);

   int signal = 0;
   if (twoGreen)
      signal = 1;
   else if (twoRed)
      signal = -1;

   if (signal != 0 && signal != lastSignal)
   {
      string msg = (signal == 1)
         ? Symbol() + " " + EnumToString((ENUM_TIMEFRAMES)Period()) + ": Accelerator Oscillator printed a second consecutive green bar"
         : Symbol() + " " + EnumToString((ENUM_TIMEFRAMES)Period()) + ": Accelerator Oscillator printed a second consecutive red bar";

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

      lastSignal = signal;
   }
}
