OrdersTotal() function not getting open orders on startup

 

Hi the OrdersTotal() function is not getting open orders on startup. I am trying to check for open orders in case of a lost internet connection or restart of the EA due to power issues. But the code below is returning 0 for open orders...

int OnInit()

{

        int totalOrders = OrdersTotal(); 
	Print("Total  orders: ", totalOrders); 

}



void OnTick()

{

        Comment("totalOrders ", totalOrders); //is printing 0 but there is an open order 

}



help is appreciated thanks
Documentation on MQL5: Integration / MetaTrader for Python / order_calc_margin
Documentation on MQL5: Integration / MetaTrader for Python / order_calc_margin
  • www.mql5.com
order_calc_margin - MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

interesting.


How are you going to change your trade if you lost connection?

Are you using a VPS?   If so, Why do you need to know if the internet connection is lost?

I think you also want to use PositionTotal() which will give you the positions that are open.  An Order is a finished position. 

You might want to read this article. 

Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles


Hope this helps.

Chris

Orders, Positions and Deals in MetaTrader 5
Orders, Positions and Deals in MetaTrader 5
  • www.mql5.com
Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.
 
If you use MQL5, then OrdersTotal() shows total count of pending orders. You should use PositionsTotal() to find out how many market positions are opened.
 
sarahfoxwell:

Hi the OrdersTotal() function is not getting open orders on startup. I am trying to check for open orders in case of a lost internet connection or restart of the EA due to power issues. But the code below is returning 0 for open orders...

The start of an EA is performed by several parallel threads. So it can happen that you EA sends the order OrdersTotal() to the server and moves on without waiting for the answer. You need to repeat it in OnTick().

 

thanks all it works if I put PositionsTotal() in the OnTick() function, now trying to get the order type whether buy or sell

and save it to a ticket. 

 
sarahfoxwell #: it works if I put PositionsTotal() in the OnTick() function, now trying to get the order type whether buy or sell

There is no order type; there is only a position type.

Reason: