How to prevent multiple pending order at a time?

 

Hi, experts

New to mql5 scripting here. I come across this issue where pending order keep on activated because I cannot seem to stop it using OrdersTotal() function.
The script should be, pending order can only open 1 at a time. It would be nice to have workaround for this issue.

Thanks,
Amir H

 
M. Amir :

Hi, experts

New to mql5 scripting here. I come across this issue where pending order keep on activated because I cannot seem to stop it using OrdersTotal() function.
The script should be, pending order can only open 1 at a time. It would be nice to have workaround for this issue.

Thanks,
Amir H

Show your MQL5 code, please.

 
Vladimir Karputov #:

Show your MQL5 code, please.

My mistake here is the code

#include  <Trade\Trade.mqh>
CTrade trade;
input double lotSize=0.1;
input int StopLoss=150;
input int TakeProfit=100;
input string StartTradingTime="02:00";
input string StopTradingTime="05:00";
string CurrentTime;
bool TradingIsAllowed=false;
double Resistance=99999;

void OnTick()
  {
  //initialize time
  datetime time=TimeTradeServer();
  CurrentTime=TimeToString(time,TIME_MINUTES);
  
  //initialize Close & Open
  double close[];                                                                     
  ArraySetAsSeries(close,true);                                                       
  CopyClose(_Symbol,_Period,0,3,close); 
  double open[];                                                                      
  ArraySetAsSeries(open,true);                                                        
  CopyOpen(_Symbol,_Period,0,3,open);
  
  //Detect resistance
  if(close[1]<close[2] && open[1]>open[2])
  {
   Resistance=close[2];
  }
  
  //Trade 
  if(CheckTradingTime()==true)
   if(close[1] > Resistance && OrdersTotal()==0)//Breakout with no pending order open
   {
     trade.SellStop(lotSize,Resistance,_Symbol,Resistance+StopLoss*_Point,Resistance-TakeProfit*_Point,ORDER_TIME_GTC,0,NULL);
   }
  
  
  }

bool CheckTradingTime()
  {
   if (StringSubstr(CurrentTime,0,5)==StartTradingTime)
   TradingIsAllowed=true;
   if (StringSubstr(CurrentTime,0,5)==StopTradingTime)
   TradingIsAllowed=false;
   return TradingIsAllowed;
  }
  
 

I confirm. After a while, OrdersTotal stops seeing pending orders!!!

MetaTrader 5 x64 build 3333 started for MetaQuotes Software Corp.
Windows 11 build 22621, 12 x Intel Core i7-9750H  @ 2.60GHz, 23 / 31 Gb memory, 706 / 946 Gb disk, UAC, GMT+2
C:\Users\barab\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075
Files:
1.mq5  3 kb
 

This exactly what happened through the tester which the code only work until the 5th order.
May I ask is there another workaround to disable any pending order to activate when there is already a pending order?

 
M. Amir # :

This exactly what happened through the tester which the code only work until the 5th order.
May I ask is there another workaround to disable any pending order to activate when there is already a pending order?

There is no workaround as the 'OrdersTotal' system function is broken :(

 
Vladimir Karputov #:

There is no workaround as the 'OrdersTotal' system function is broken :(

oh well that is a bummer. I will try find another way then. also should I report this bug?

Thanks in advance Mr Vladimir, appreciate the answer

 

For Future Readers, This code is flawed. too many order makes the OrdersTotal() overwhelmed. I found another workaround.

Sincerely,

M Amir

 
M. Amir #:

For Future Readers, This code is flawed. too many order makes the OrdersTotal() overwhelmed. I found another workaround.

Sincerely,

M Amir

Are you not going to share this "workaround"?

 
Of Course, add a trade counter for when order is placed and make sure to reset it when necessary. Here is some of my code.
  //Trade Time:Sell stop when breakout happen
  if(TimeCheck()==true && close[1] > Resistance && OrdersTotal()==0 && TradeCounter<1)
  {
   trade.SellStop(lotSize,Resistance,_Symbol,Resistance+StopLoss*_Point,Resistance-TakeProfit*_Point,ORDER_TIME_DAY,0,NULL);
   TradeCounter=TradeCounter+1;
  }

  //No Trade Time : Reset Parameter & Close Order 
  if ( TimeCheck() == false )
  {
   Resistance=99999;
   ulong  order_ticket=OrderGetTicket(0);
   trade.OrderDelete(order_ticket);
   TradeCounter=0;
  }
 
M. Amir :


Build 3335 is out - looks like the bug with 'OrdersTotal' has been fixed.

Reason: