//+------------------------------------------------------------------+
//|                                                      TripleEMA.mq4 |
//|                                  Created By Josmellon            |
//|                                                                  |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 clrBlue
#property indicator_color2 clrOrange
#property indicator_color3 clrRed
#property indicator_color4 clrLime
#property indicator_color5 clrMagenta
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1

// Input parameters
input int FastEMA = 9;     // Fast EMA Period
input int MediumEMA = 20;  // Medium EMA Period
input int SlowEMA = 45;    // Slow EMA Period

// Buffers for EMA values and signals
double FastBuffer[], MediumBuffer[], SlowBuffer[];
double BuySignal[], SellSignal[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   // Initialize EMA buffers
   SetIndexBuffer(0, FastBuffer);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexLabel(0, "Fast EMA");

   SetIndexBuffer(1, MediumBuffer);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexLabel(1, "Medium EMA");

   SetIndexBuffer(2, SlowBuffer);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexLabel(2, "Slow EMA");

   // Initialize signal buffers
   SetIndexBuffer(3, BuySignal);
   SetIndexStyle(3, DRAW_ARROW);
   SetIndexArrow(3, 233); // Up arrow
   SetIndexLabel(3, "Buy Signal");

   SetIndexBuffer(4, SellSignal);
   SetIndexStyle(4, DRAW_ARROW);
   SetIndexArrow(4, 234); // Down arrow
   SetIndexLabel(4, "Sell Signal");

   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[])
{
   int limit = rates_total - prev_calculated;
   if(prev_calculated > 0) limit++;

   // Calculate EMA values
   for(int i = 0; i < limit; i++)
   {
      FastBuffer[i] = iMA(NULL, 0, FastEMA, 0, MODE_EMA, PRICE_CLOSE, i);
      MediumBuffer[i] = iMA(NULL, 0, MediumEMA, 0, MODE_EMA, PRICE_CLOSE, i);
      SlowBuffer[i] = iMA(NULL, 0, SlowEMA, 0, MODE_EMA, PRICE_CLOSE, i);
   }

   // Detect crossovers and plot signals
   for(int j = limit-1; j >= 0; j--) // Changed 'i' to 'j' to avoid redeclaration
   {
      BuySignal[j] = EMPTY_VALUE;
      SellSignal[j] = EMPTY_VALUE;

      if(j >= rates_total-2) continue;

      bool buyCondition = (FastBuffer[j] > MediumBuffer[j] && FastBuffer[j] > SlowBuffer[j]) &&
                          (FastBuffer[j+1] <= MediumBuffer[j+1] || FastBuffer[j+1] <= SlowBuffer[j+1]);

      bool sellCondition = (FastBuffer[j] < MediumBuffer[j] && FastBuffer[j] < SlowBuffer[j]) &&
                           (FastBuffer[j+1] >= MediumBuffer[j+1] || FastBuffer[j+1] >= SlowBuffer[j+1]);

      if(buyCondition)
         BuySignal[j] = low[j] - 100 * Point(); // Place arrow below the bar
      else if(sellCondition)
         SellSignal[j] = high[j] + 100 * Point(); // Place arrow above the bar
   }

   return(rates_total);
}
//+------------------------------------------------------------------+