Closing all trades stopped working since last update.

 

my code below just prints best/worst equity when these are breached. And when equity hits X then closes all trades.

This worked fine for oever 3 months til my lateest update 5833. i dont know if this is a bug or is my amateur coding. Here's my coding below. If you see anything that is wrong or can be done better, PLEASE TELL ME.

klast week and today, it didnt close all trades when it was supposed to do so. and then, even when i manually right clicked on a trade and selected : Bulk Positions >> Close all positions, i had to do this 4 times before all my 100 trades had closed! i am suspicious about both my coding AND 5833.

//+------------------------------------------------------------------+
//|                                    EA5.rA3m_Reporting26.0426.mq5 |
//|                                  Copyright 2026, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#define testing (bool)MQLInfoInteger(MQL_TESTER)
#define total0 (ushort)PositionsTotal()
#include "TradeAlt.mqh"

CTradeAlt         trade;
CPositionInfo     position;
input double iCloseTarget = 45.02; //  Initial Closing Target
input double iProfitInc   = 3;     //  Profit Target
input double       iDrawdown           = 5;         // Comfortable Drawdown
input bool         iVerbose            = true;     // Show more Trade details
input int      iMagicNumber   = 227;    // Magic Number
input int          iSlippage           = 1;       // Slippage (in pips)
input int          Spread              = 7;       // Spread (in pips)

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
void OnTick(void)
  {
//---
//   static double closeTarget, __ct; //, lowTarget, __lt;
//   static bool hitLow;
   static int total;
   static double hiEquity, lowEquity, bestDD, worstDD, dd, equ0, pnt0;
   static double _hiEquity, _lowEquity, _bestDD, _worstDD;
   equ0 = AccountInfoDouble(ACCOUNT_EQUITY);
   total = PositionsTotal();
   if(total >= 1 && equ0>iCloseTarget*1000)
      {CloseTrades();
      return;}
   dd = AccountInfoDouble(ACCOUNT_PROFIT);
   pnt0 = SymbolInfoDouble(_Symbol,SYMBOL_POINT);
   static bool report = false;
   if(dd < worstDD-1)
     {
      _worstDD = worstDD;
      report = _worstDD>dd;
      worstDD = dd;
     }
   if(dd > bestDD+1 || bestDD == 0)
     {
      _bestDD = bestDD;
      report = _bestDD<dd;
      bestDD = dd;
     }
   if(equ0 > pnt0 && (lowEquity <= 0 || equ0 < lowEquity-1))
     {
      _lowEquity = lowEquity;
      report = _lowEquity>equ0;
      lowEquity = equ0;
     }
   if(equ0 > hiEquity+1)
     {
      _hiEquity = hiEquity;
      report = _hiEquity<equ0;
      hiEquity = equ0;
     }
   if(report)
     {
      PrintFormat("----");
      PrintFormat(DoubleToString(hiEquity, 2)+(_hiEquity<hiEquity?" *** ":"  ")+"Highest Equity");
      PrintFormat(DoubleToString(lowEquity, 2)+(_lowEquity>lowEquity?" *** ":"  ")+"Lowest Equity");
      PrintFormat(DoubleToString(bestDD, 2)+(_bestDD<bestDD?" *** ":"  ")+"Best DD");
      PrintFormat(DoubleToString(worstDD, 2)+(_worstDD>worstDD?" *** ":"  ")+"Worst DD");
      PrintFormat("----");
      report = false;
      _lowEquity = lowEquity;
      _hiEquity = hiEquity;
      _worstDD = worstDD;
      _bestDD = bestDD;
     }
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   if(testing != false || !iVerbose)
      trade.LogLevel(LOG_LEVEL_NO);
   else
      trade.LogLevel(LOG_LEVEL_ALL);
   trade.SetExpertMagicNumber(iMagicNumber);
   trade.SetDeviationInPoints(iSlippage);
   trade.SetMarginMode();
   trade.SetTypeFillingBySymbol(Symbol());
   trade.SetAsyncMode(true);
//---
   return (INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void CloseTrades()
  {
   for(int k = total0 - 1; k >= 0; k--)
     {
      if(position.SelectByIndex(k))
        {
         int otyp = position.Type();
         if(otyp != POSITION_TYPE_SELL && otyp != POSITION_TYPE_BUY)
            continue;
         ulong tk = position.Ticket();
         trade.PositionClose(tk,-1);
        }
     }
   if(PositionsTotal()<1)
      ExpertRemove();
  }
//+------------------------------------------------------------------+
 
Michael Charles Schefe:

my code below just prints best/worst equity when these are breached. And when equity hits X then closes all trades.

This worked fine for oever 3 months til my lateest update 5833. i dont know if this is a bug or is my amateur coding. Here's my coding below. If you see anything that is wrong or can be done better, PLEASE TELL ME.

klast week and today, it didnt close all trades when it was supposed to do so. and then, even when i manually right clicked on a trade and selected : Bulk Positions >> Close all positions, i had to do this 4 times before all my 100 trades had closed! i am suspicious about both my coding AND 5833.

I don't know if -1 is proper for a Deviation parameter:

         trade.PositionClose(tk,-1);

I do know that I don't have a problem in Build 5833, using 0 instead of -1.

 
Ryan L Johnson #:

I don't know if -1 is proper for a Deviation parameter:

I do know that I don't have a problem in Build 5833, using 0 instead of -1.

i have been using that line for years, no issues, but i have SetDeviation in my OnInit, so i never understood what i was meant to put on that Deviation parameter on the trade.PositionClose line.
 
Michael Charles Schefe #:
i have been using that line for years, no issues, but i have SetDeviation in my OnInit, so i never understood what i was meant to put on that Deviation parameter on the trade.PositionClose line.

I see that you have:

input int          iSlippage           = 1;       // Slippage (in pips)
   trade.SetDeviationInPoints(iSlippage);

But I don't see any pips to points conversion math.

TBH, I have no idea how your SetDeviaton code interacts with an attempt to override it with -1... in Build 5833.


On an unrelated note, your event handler header comments for OnInit() and OnTick() appear to be transposed.

 

Also, PositionsTotal() is an int.

Your custom define shows a ushort:

#define total0 (ushort)PositionsTotal()
   for(int k = total0 - 1; k >= 0; k--)
Documentation on MQL5: PositionsTotal / Trade Functions
Documentation on MQL5: PositionsTotal / Trade Functions
  • www.mql5.com
Returns the number of open positions. Return Value Value of int type. Note For the "netting" interpretation of positions (...
 
Ryan L Johnson #:

Also, PositionsTotal() is an int.

Your custom define shows a ushort:

BIG OOPS. great catch! that could be the whole problem. thanks!