//+------------------------------------------------------------------+
//|                                                    EMA_Status.mq5 |
//|                          Version 1.05, ChatGPT                  |
//+------------------------------------------------------------------+
#property copyright "ChatGPT"
#property version   "1.05"
#property strict

input ENUM_TIMEFRAMES SelectedTimeframe = PERIOD_M15; // Timeframe for EMAs
input bool         Show_EMAs = true;                    // Show/Hide EMAs on chart

// EMA periods
#define EMA1_PERIOD 50
#define EMA2_PERIOD 100
#define EMA3_PERIOD 200

// Colors for EMAs
#define COLOR_EMA1 clrGreen
#define COLOR_EMA2 clrBlue
#define COLOR_EMA3 clrRed

string label_name = "EMA_Status_Label";

int ema1_handle = INVALID_HANDLE;
int ema2_handle = INVALID_HANDLE;
int ema3_handle = INVALID_HANDLE;

// Flags whether the indicators were added to chart
bool added_ema1 = false;
bool added_ema2 = false;
bool added_ema3 = false;

//+------------------------------------------------------------------+
//| Expert initialization                                            |
//+------------------------------------------------------------------+
int OnInit()
  {
   // Create EMA indicator handles on selected timeframe
   ema1_handle = iMA(_Symbol, SelectedTimeframe, EMA1_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
   if (ema1_handle == INVALID_HANDLE)
     {
      Print("Failed to create EMA1 handle");
      return INIT_FAILED;
     }

   ema2_handle = iMA(_Symbol, SelectedTimeframe, EMA2_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
   if (ema2_handle == INVALID_HANDLE)
     {
      Print("Failed to create EMA2 handle");
      IndicatorRelease(ema1_handle);
      return INIT_FAILED;
     }

   ema3_handle = iMA(_Symbol, SelectedTimeframe, EMA3_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
   if (ema3_handle == INVALID_HANDLE)
     {
      Print("Failed to create EMA3 handle");
      IndicatorRelease(ema1_handle);
      IndicatorRelease(ema2_handle);
      return INIT_FAILED;
     }

   // Add indicators to chart if option enabled
   if (Show_EMAs)
      AddIndicatorsToChart();

   CreateStatusLabel();

   return INIT_SUCCEEDED;
  }
//+------------------------------------------------------------------+
//| Add built-in EMA indicators to chart                             |
//+------------------------------------------------------------------+
void AddIndicatorsToChart()
  {
   // Add indicator objects by handle, subwindow=0 (main chart)
   added_ema1 = ChartIndicatorAdd(0, 0, ema1_handle);
   added_ema2 = ChartIndicatorAdd(0, 0, ema2_handle);
   added_ema3 = ChartIndicatorAdd(0, 0, ema3_handle);

   // Set colors and widths for each EMA line
   SetEMAColor(EMA1_PERIOD, COLOR_EMA1);
   SetEMAColor(EMA2_PERIOD, COLOR_EMA2);
   SetEMAColor(EMA3_PERIOD, COLOR_EMA3);
  }
//+------------------------------------------------------------------+
//| Set color and width of EMA indicator object on the chart        |
//+------------------------------------------------------------------+
void SetEMAColor(int period, color clr)
  {
   string period_str = "(" + IntegerToString(period) + ")";

   int total = ObjectsTotal(0);
   for (int i = 0; i < total; i++)
     {
      string name = ObjectName(0, i);
      // Check name starts with "EMA" and contains period string
      if (StringFind(name, "EMA") == 0 && StringFind(name, period_str) >= 0)
        {
         // Optionally skip type check, since OBJ_INDICATOR is undefined in MQL5
         // Just set properties by name:

         ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
         ObjectSetInteger(0, name, OBJPROP_WIDTH, 2);

         break; // Found and set, exit loop
        }
     }
  }
//+------------------------------------------------------------------+
//| Create status label on chart                                     |
//+------------------------------------------------------------------+
void CreateStatusLabel()
  {
   if (ObjectFind(0, label_name) == -1)
     {
      ObjectCreate(0, label_name, OBJ_LABEL, 0, 0, 0);
      ObjectSetInteger(0, label_name, OBJPROP_CORNER, CORNER_RIGHT_UPPER);
      ObjectSetInteger(0, label_name, OBJPROP_XDISTANCE, 100);
      ObjectSetInteger(0, label_name, OBJPROP_YDISTANCE, 10);
      ObjectSetInteger(0, label_name, OBJPROP_BACK, true);
      ObjectSetInteger(0, label_name, OBJPROP_SELECTABLE, false);
      ObjectSetInteger(0, label_name, OBJPROP_SELECTED, false);
      ObjectSetInteger(0, label_name, OBJPROP_FONTSIZE, 14);
      ObjectSetString(0, label_name, OBJPROP_FONT, "Arial");
      ObjectSetInteger(0, label_name, OBJPROP_COLOR, clrWhite);
      ObjectSetString(0, label_name, OBJPROP_TEXT, "Wait");
     }
  }
//+------------------------------------------------------------------+
//| Expert deinitialization                                          |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ObjectDelete(0, label_name);

   if (Show_EMAs)
     {
      if (added_ema1) ChartIndicatorDelete(0, 0, ema1_handle);
      if (added_ema2) ChartIndicatorDelete(0, 0, ema2_handle);
      if (added_ema3) ChartIndicatorDelete(0, 0, ema3_handle);
     }

   // Release indicator handles
   if (ema1_handle != INVALID_HANDLE) IndicatorRelease(ema1_handle);
   if (ema2_handle != INVALID_HANDLE) IndicatorRelease(ema2_handle);
   if (ema3_handle != INVALID_HANDLE) IndicatorRelease(ema3_handle);
  }
//+------------------------------------------------------------------+
//| Main tick processing                                            |
//+------------------------------------------------------------------+
void OnTick()
  {
   double ema1_buf[], ema2_buf[], ema3_buf[];

   // Get EMA values for last closed bar (index=1) - safer than current forming bar (0)
   if (CopyBuffer(ema1_handle, 0, 1, 1, ema1_buf) <= 0) return;
   if (CopyBuffer(ema2_handle, 0, 1, 1, ema2_buf) <= 0) return;
   if (CopyBuffer(ema3_handle, 0, 1, 1, ema3_buf) <= 0) return;

   double ema1 = ema1_buf[0];
   double ema2 = ema2_buf[0];
   double ema3 = ema3_buf[0];

   string status = "Wait";
   if (ema1 > ema2 && ema2 > ema3)
      status = "Buy only";
   else if (ema1 < ema2 && ema2 < ema3)
      status = "Sell only";

   ObjectSetString(0, label_name, OBJPROP_TEXT, status);
  }
//+------------------------------------------------------------------+