OrdersTotal() not updating when I place an order!

 

I am trying to backtest an expert advisor and limit the number of pending orders to 1. However, when I place an order using the CTrade class, OrdersTotal() is still returning 0. I found this forum post but to no avail. 

Here's the function I'm running and the output:

void Buy() {
   double sl = iOpen(Symbol(), Period(), 2);
   double ocur = iOpen(Symbol(), Period(), 0);
   double tp = (ocur - sl) * 2 + ocur;
   if (tp < sl || OrdersTotal() > 1) {
      return;
   }
   trade.Buy(Volume, Symbol(), symbol.Ask(), sl, tp);
   Print(CountOrders());
   DebugBreak();
}
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Figured out that I'm using the wrong function. For anyone else who is having this problem see:  PositionsTotal - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Documentation on MQL5: Trade Functions / PositionsTotal
Documentation on MQL5: Trade Functions / PositionsTotal
  • www.mql5.com
PositionsTotal - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Macintyre Sunde #:
Figured out that I'm using the wrong function. For anyone else who is having this problem see:  PositionsTotal - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

You have to select the order first 

 
Select the order first
 
Macintyre Sunde :


Carefully study the basic terms: what is a "pending order" (OrdersTotal) and what is a "position" (PositionsTotal)!

PositionsTotal

Returns the number of open positions

OrdersTotal

Returns the number of orders

Documentation on MQL5: Trade Functions / PositionsTotal
Documentation on MQL5: Trade Functions / PositionsTotal
  • www.mql5.com
PositionsTotal - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5