//+------------------------------------------------------------------+
//|                                 XUMA_4Colors_AutoTrader_MTF.mq5  |
//|                                      Generated Expert Advisor    |
//+------------------------------------------------------------------+
#property copyright "Your Name"
#property link      ""
#property version   "1.01"
#property description "MTF Auto-trades based on XU-MA v3 4 colors candle states"

#include <Trade\Trade.mqh>

//--- Inputs
input group "--- Trading Settings ---"
input double InpLotSize       = 0.01;               // Trade Lot Size
input ulong  InpMagicNumber   = 777777;             // EA Magic Number
input ulong  InpSlippage      = 3;                  // Allowed Slippage (Points)

input group "--- Indicator Settings ---"
input string          InpIndicatorName = "XU-MA v3 4 colors"; // Indicator File Name (must match exactly)
input ENUM_TIMEFRAMES InpTimeFrame     = PERIOD_H1;           // Indicator Timeframe (MTF)
input int             InpBarShift      = 1;                   // Bar Shift (0 = Live Bar, 1 = Closed Bar)

//--- Global Variables
int    h_indicator = INVALID_HANDLE;
CTrade trade; // Trade object for executing orders

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   // Initialize the custom indicator handle with the MTF input
   h_indicator = iCustom(_Symbol, InpTimeFrame, InpIndicatorName);
   
   if(h_indicator == INVALID_HANDLE)
   {
      Print("Failed to load indicator: '", InpIndicatorName, "'. Please ensure the compiled .ex5 file is in MQL5/Indicators/");
      return(INIT_FAILED);
   }

   // Setup the Trade object properties
   trade.SetExpertMagicNumber(InpMagicNumber);
   trade.SetDeviationInPoints(InpSlippage);

   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   // Clean up indicator handle
   if(h_indicator != INVALID_HANDLE)
      IndicatorRelease(h_indicator);
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   // Array to hold the color index data
   double color_buffer[];
   
   // Read Buffer 2 (candle2) which holds the Color Indices from the selected Timeframe
   if(CopyBuffer(h_indicator, 2, InpBarShift, 1, color_buffer) <= 0)
   {
      return; // Exit silently if buffer isn't ready
   }

   int color_index = (int)color_buffer[0];
   
   // Track current open positions for this EA
   bool isBuyOpen = false;
   bool isSellOpen = false;
   
   for(int i = PositionsTotal() - 1; i >= 0; i--)
   {
      ulong ticket = PositionGetTicket(i);
      if(PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == InpMagicNumber)
      {
         long posType = PositionGetInteger(POSITION_TYPE);
         if(posType == POSITION_TYPE_BUY)  isBuyOpen  = true;
         if(posType == POSITION_TYPE_SELL) isSellOpen = true;
      }
   }

   //--- Trade Logic based on Color Index ---//
   
   // 1. OPEN BUY: Candle color is Blue (96,170,204) -> Index 0
   if(color_index == 0 && !isBuyOpen)
   {
      trade.Buy(InpLotSize, _Symbol, 0, 0, 0, "XUMA Blue Buy");
   }
   
   // 2. CLOSE BUY: Candle color is Light Blue (35,100,130) -> Index 3
   else if(color_index == 3 && isBuyOpen)
   {
      CloseAllPositionsByType(POSITION_TYPE_BUY);
   }
   
   // 3. OPEN SELL: Candle color is Red (255,140,255) -> Index 2
   if(color_index == 2 && !isSellOpen)
   {
      trade.Sell(InpLotSize, _Symbol, 0, 0, 0, "XUMA Red Sell");
   }
   
   // 4. CLOSE SELL: Candle color is Light Red (180,70,180) -> Index 4
   else if(color_index == 4 && isSellOpen)
   {
      CloseAllPositionsByType(POSITION_TYPE_SELL);
   }
}

//+------------------------------------------------------------------+
//| Helper Function: Close Positions by Type                         |
//+------------------------------------------------------------------+
void CloseAllPositionsByType(long pos_type)
{
   for(int i = PositionsTotal() - 1; i >= 0; i--)
   {
      ulong ticket = PositionGetTicket(i);
      if(PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == InpMagicNumber)
      {
         if(PositionGetInteger(POSITION_TYPE) == pos_type)
         {
            trade.PositionClose(ticket);
         }
      }
   }
}
//+------------------------------------------------------------------+