How to solve the problem of repeated open orders ?

 

In OnTick(), if i want sell gold in $ 3230 , due to delays in broker,the next tick not find 3230 Position,repeated sell,i used Sleep(),but not much effect.

Is there any other way?

 

Reproducing the situation (use autotranslate).


All details on how to solve the problem can be found at this link.

Проверка на наличие дублей ордеров/позиций в MT5
Проверка на наличие дублей ордеров/позиций в MT5
  • 2021.09.12
  • www.mql5.com
Появление дублей ордеров/позиций в MT5 - архитектурная особенность платформы, с которой многие сталкиваются. Данная неприятность вызывает серьезные перекосы в торговых рисках, ломает логику, усложняет
 

there could be two different kinds of undesired cases:

- new orders getting executed on every tick

or

- more than one order getting executed in the same bar


Which is your undesired case? It should be clarified. There are different solutions for different cases

 
Conor Mcnamara #:

there could be two different kinds of undesired cases:

- new orders getting executed on every tick

or

- more than one order getting executed in the same bar


Which is your undesired case? It should be clarified. There are different solutions for different cases

for example: the gold rise in $ 3220 ,the first tick find not position in 3220,it send sell order,due to delays in broker,the second tick also find not position in 3220, open sell again,so i have the 2 position in $ 3220, but i just want to one position.

 
fxsaber #:

Reproducing the situation (use autotranslate).


All details on how to solve the problem can be found at this link.

sorry , I don't understand

 
c327515 #:

for example: the gold rise in $ 3220 ,the first tick find not position in 3220,it send sell order,due to delays in broker,the second tick also find not position in 3220, open sell again,so i have the 2 position in $ 3220, but i just want to one position.

Maybe you should reveal the code for how you execute orders.

with this condition, it would ensure only one order is placed:

if(OrdersTotal() == 0 && PositionsTotal() == 0){

   //Place order
}

because it only allows an order to place when there are currently zero orders...when one order executes, this condition will become false so then it can't execute another order

I don't recommend using Sleep() function


another option is to delete extra orders when one of the orders turns into a position:

void OnTick(){
 //
 //
  if(OrdersTotal() > 0 && PositionsTotal() >= 1){

    DeleteOrders();
  }
}

void DeleteOrders(){

   for(int i = 0; i < OrdersTotal(); i++){
       ulong order = OrderGetTicket(i);
       if(order > 0){
           trade.OrderDelete(order);
       }
   }  
}
 
Conor Mcnamara #:

Maybe you should reveal the code for how you execute orders.

with this condition, it would ensure only one order is placed:

because it only allows an order to place when there are currently zero orders...when one order executes, this condition will become false so then it can't execute another order

I don't recommend using Sleep() function


another option is to delete extra orders when one of the orders turns into a position:

so,executed process of Buy() or Sell() in CTrade class :Order -> Position ?

 
Conor Mcnamara #:

Maybe you should reveal the code for how you execute orders.

with this condition, it would ensure only one order is placed:

because it only allows an order to place when there are currently zero orders...when one order executes, this condition will become false so then it can't execute another order

I don't recommend using Sleep() function


another option is to delete extra orders when one of the orders turns into a position:

void OnTick()
  {
//---
   Print("Order :"+OrdersTotal());
   m_trade.Sell(0.01,Symbol(),0,0,0);
   
  }

no, i tested,OrdersTotal() always equal 0

 
c327515 #:

no, i tested,OrdersTotal() always equal 0

Orders and Positions are different. 

Positions:

m_trade.Sell(....);
m_trade.Buy(....);

Orders:

m_trade.BuyStop(....);
m_trade.SellStop(....);
m_trade.BuyLimit(....);
m_trade.SellLimit(....);


In MQL5 the word "orders" is meaning pending orders only. But everything was "order" in MQL4.


 

If your signal is based on OHLC then you can execute one position like this

datetime barOpenTime = iTime(_Symbol, _Period, 0);
static datetime signalTime = 0;  

if(signalTime != barOpenTime){

    trade.Buy(tradeVolume, _Symbol, Ask, slBuy, tpBuy);   
    signalTime = barOpenTime;  
}

logic means - execute one position in that bar

 

Forum on trading, automated trading systems and testing trading strategies

How to solve the problem of repeated open orders ?

Conor Mcnamara, 2025.04.15 14:59

with this condition, it would ensure only one order is placed:

if(OrdersTotal() == 0 && PositionsTotal() == 0){

   //Place order
}

because it only allows an order to place when there are currently zero orders...when one order executes, this condition will become false so then it can't execute another order

This condition does not guarantee that only one position will be opened.