My Martingale Logics is not working my EA. Please help me to check

 

Please anyone should help me out on this EA.

It's opening perfectly based on the indicator signal but i added a martingale feature which will happen after a loss trade. but it's not working . please check the code.

#property copyright "Copyright 2025, AGTech Ltd."
#property link      "https://t.me/ForexrubyAIbot"
#property version   "1.00"
//+------------------------------------------------------------------+
//|                                                MasterEA.mq5      |
//|               Global Martingale logic based on last loss         |
//+------------------------------------------------------------------+
#property strict

input double LotSize = 1.0;
input double MartingaleMultiplier = 2.0;
input int Slippage = 5;
input int StopLossPoints = 1000;
input int TakeProfitPoints = 1000;
input string IndicatorName = "MasterScanner";

int indicatorHandle;
datetime lastBarTime = 0;
ulong magicNumber = 555555;
int lastSignal = -1;

double lastClosedLot = 0.0;
double lastClosedProfit = 0.0;
double currentLotSize = 0.0;  // 🔄 Track global lot size

//+------------------------------------------------------------------+
int OnInit()
  {
   indicatorHandle = iCustom(Symbol(), Period(), IndicatorName);
   if(indicatorHandle == INVALID_HANDLE)
     {
      Print("❌ Failed to get handle for indicator: ", IndicatorName);
      return INIT_FAILED;
     }

   currentLotSize = LotSize;  // ✅ Init global lot tracker
   return INIT_SUCCEEDED;
  }

//+------------------------------------------------------------------+
void OnTick()
  {
   datetime currentBarTime = iTime(Symbol(), Period(), 0);
   if(currentBarTime == lastBarTime)
      return;

   lastBarTime = currentBarTime;

   double signalBuffer[];
   if(CopyBuffer(indicatorHandle, 4, 1, 1, signalBuffer) < 0)
     {
      Print("❌ Failed to read from buffer 4, Error: ", GetLastError());
      return;
     }

   double signal = signalBuffer[0];
   if(signal == EMPTY_VALUE)
      return;

   int intSignal = (int)signal;
   if(intSignal == lastSignal)
      return;

   lastSignal = intSignal;

   if(intSignal == 0)
     {
      if(PositionSelect(Symbol()) &&
         PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
         ClosePosition();

      if(!PositionSelect(Symbol()))
         OpenPosition(ORDER_TYPE_BUY);
     }

   else if(intSignal == 1)
     {
      if(PositionSelect(Symbol()) &&
         PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
         ClosePosition();

      if(!PositionSelect(Symbol()))
         OpenPosition(ORDER_TYPE_SELL);
     }
  }

//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
   if(trans.type == TRADE_TRANSACTION_DEAL_ADD &&
      (trans.deal_type == DEAL_TYPE_BUY || trans.deal_type == DEAL_TYPE_SELL))
     {
      ulong dealTicket = trans.deal;

      double profit  = HistoryDealGetDouble(dealTicket, DEAL_PROFIT);
      double volume  = HistoryDealGetDouble(dealTicket, DEAL_VOLUME);
      ulong entry    = (ulong)HistoryDealGetInteger(dealTicket, DEAL_ENTRY);

      if(entry == DEAL_ENTRY_OUT)
        {
         lastClosedProfit = profit;
         lastClosedLot = volume;

         if(profit < 0)
            currentLotSize = NormalizeDouble(volume * MartingaleMultiplier, 2);
         else
            currentLotSize = LotSize;

         Print("💼 Closed: Profit=", profit, ", Lot=", volume, ", Next Lot=", currentLotSize);
        }
     }
  }

//+------------------------------------------------------------------+
double GetCurrentLotSize()
  {
   return NormalizeDouble(currentLotSize, 2);
  }

//+------------------------------------------------------------------+
bool SendWithFillingFallback(MqlTradeRequest &request, MqlTradeResult &result)
  {
   ENUM_ORDER_TYPE_FILLING fillModes[] = {ORDER_FILLING_IOC, ORDER_FILLING_FOK, ORDER_FILLING_RETURN};

   for(int i = 0; i < ArraySize(fillModes); i++)
     {
      request.type_filling = fillModes[i];
      if(OrderSend(request, result) && result.retcode == TRADE_RETCODE_DONE)
         return true;
     }

   Print("❌ All filling modes failed. Last retcode: ", result.retcode, ", ", result.comment);
   return false;
  }

//+------------------------------------------------------------------+
bool OpenPosition(ENUM_ORDER_TYPE orderType)
  {
   MqlTradeRequest request;
   MqlTradeResult result;
   MqlTick tick;

   if(!SymbolInfoTick(_Symbol, tick))
     {
      Print("❌ Failed to get tick");
      return false;
     }

   double volume = GetCurrentLotSize();
   double price = (orderType == ORDER_TYPE_BUY) ? tick.ask : tick.bid;
   double sl = (orderType == ORDER_TYPE_BUY)
               ? price - StopLossPoints * _Point
               : price + StopLossPoints * _Point;

   double tp = (orderType == ORDER_TYPE_BUY)
               ? price + TakeProfitPoints * _Point
               : price - TakeProfitPoints * _Point;

   ZeroMemory(request);
   request.action = TRADE_ACTION_DEAL;
   request.symbol = _Symbol;
   request.volume = volume;
   request.type = orderType;
   request.price = price;
   request.sl = NormalizeDouble(sl, _Digits);
   request.tp = NormalizeDouble(tp, _Digits);
   request.deviation = Slippage;
   request.magic = magicNumber;

   if(!SendWithFillingFallback(request, result))
      return false;

   Print("✅ Trade opened: ", EnumToString(orderType), " at ", price, " lot: ", volume);
   return true;
  }

//+------------------------------------------------------------------+
void ClosePosition()
  {
   if(!PositionSelect(_Symbol))
      return;

   ulong ticket = PositionGetInteger(POSITION_TICKET);
   ENUM_POSITION_TYPE posType = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
   ENUM_ORDER_TYPE closeType = (posType == POSITION_TYPE_BUY) ? ORDER_TYPE_SELL : ORDER_TYPE_BUY;

   MqlTradeRequest request;
   MqlTradeResult result;
   MqlTick tick;

   if(!SymbolInfoTick(_Symbol, tick))
     {
      Print("❌ Failed to get tick for close");
      return;
     }

   double price = (closeType == ORDER_TYPE_BUY) ? tick.ask : tick.bid;

   ZeroMemory(request);
   request.action = TRADE_ACTION_DEAL;
   request.symbol = _Symbol;
   request.position = ticket;
   request.volume = PositionGetDouble(POSITION_VOLUME);
   request.type = closeType;
   request.price = price;
   request.deviation = Slippage;
   request.magic = magicNumber;

   if(!SendWithFillingFallback(request, result))
      return;

   Print("✅ Position closed at ", price);
  }
//+------------------------------------------------------------------+
 
Sodiq Kayode Hammed:


Note codes written by an AI or ChatGPT are often treated with ridicule in this forum. They are always full of wrong syntax and try to combine mt4 and mt5 codes.

However, OnTradeTransaction is not guaranteed to work, and if i remember correctly, I think that it is not supposed to work in strategy tester, if that is what you meant by "its not working".