//+------------------------------------------------------------------+
//|                                            EMA Wave and GRaB.mq5  |
//|                        Custom Indicator Script                   |
//|                       Converted from PineScript                   |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 clrRed
#property indicator_color2 clrGreen
#property indicator_color3 clrBlack
#property indicator_color4 clrGray

input bool ShowPAC = true;      // Show EMA Wave
input bool ShowBarColor = true; // Show Coloured GRaB Candles
input bool ShowTrendIndi = false; // Show Trend Indicator
input int PACLen = 34;          // EMA Wave Length (34 by default)
input ENUM_APPLIED_PRICE src = PRICE_CLOSE; // Source for Wave centre EMA

double pacCeBuffer[];
double pacLoBuffer[];
double pacHiBuffer[];
double TrendBuffer[];

color green100 = clrGreen;
color lime100  = clrLime;
color red100   = clrRed;
color blue100  = clrBlue;
color aqua100  = clrAqua;
color darkred100 = clrDarkRed;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0, pacLoBuffer);
   SetIndexBuffer(1, pacHiBuffer);
   SetIndexBuffer(2, pacCeBuffer);
   SetIndexBuffer(3, TrendBuffer);

   PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_LINE);
   PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_LINE);
   PlotIndexSetInteger(2, PLOT_DRAW_TYPE, DRAW_LINE);
   PlotIndexSetInteger(3, PLOT_DRAW_TYPE, DRAW_FILLING);

   IndicatorSetString(INDICATOR_SHORTNAME, "WAVEGRAB");

   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[])
  {
   if (rates_total < PACLen) return(0);

   int start = prev_calculated > 0 ? prev_calculated - 1 : PACLen;

   for (int i = start; i < rates_total; i++)
     {
      // EMA Wave calculations
      pacCeBuffer[i] = iMA(NULL, 0, PACLen, 0, MODE_EMA, src, i);
      pacLoBuffer[i] = iMA(NULL, 0, PACLen, 0, MODE_EMA, PRICE_LOW, i);
      pacHiBuffer[i] = iMA(NULL, 0, PACLen, 0, MODE_EMA, PRICE_HIGH, i);

      // Bar colors based on position relative to PAC
      if (ShowBarColor) {
         color bColour = close[i] >= open[i] ? (close[i] >= pacHiBuffer[i] ? lime100 : (close[i] <= pacLoBuffer[i] ? red100 : aqua100)) : (close[i] >= pacHiBuffer[i] ? green100 : (close[i] <= pacLoBuffer[i] ? darkred100 : blue100));
         SetIndexLabel(0, "High Wave EMA");
         SetIndexLabel(1, "Low Wave EMA");
         SetIndexLabel(2, "Centre Wave EMA");
         SetIndexStyle(0, DRAW_LINE);
         SetIndexStyle(1, DRAW_LINE);
         SetIndexStyle(2, DRAW_LINE);
         SetIndexBuffer(0, pacLoBuffer);
         SetIndexBuffer(1, pacHiBuffer);
         SetIndexBuffer(2, pacCeBuffer);
         SetIndexBuffer(3, TrendBuffer);
         SetIndexLabel(0, "High Wave EMA");
         SetIndexLabel(1, "Low Wave EMA");
         SetIndexLabel(2, "Centre Wave EMA");
         SetIndexStyle(0, DRAW_LINE);
         SetIndexStyle(1, DRAW_LINE);
         SetIndexStyle(2, DRAW_LINE);
         SetIndexBuffer(0, pacLoBuffer);
         SetIndexBuffer(1, pacHiBuffer);
         SetIndexBuffer(2, pacCeBuffer);
         SetIndexBuffer(3, TrendBuffer);
         SetIndexBuffer(4, TrendBuffer);
         SetIndexStyle(4, DRAW_ARROW, 0, 2, clrNone);
      }

      // Trend direction indicator
      if (ShowTrendIndi) {
         color wcolor = (high[i] > pacHiBuffer[i] && low[i] > pacHiBuffer[i]) ? lime100 : (low[i] < pacLoBuffer[i] && high[i] < pacLoBuffer[i] ? red100 : clrGray);
         TrendBuffer[i] = src;
         PlotIndexSetInteger(3, PLOT_LINE_COLOR, i, wcolor);
      }
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+
