OrdersHistoryTotal() and OrdersTotal returns nothing

 

Hi All,

I've spent the better part of a day trying to do something I thought would be extremely simple - getting my account's closed trades using a very basic EA snippet. I've made sure all of my account history is showing on the 'Account History' tab, my broker is Oanda and my account was recently migrated to the v-20 servers. 

This is what is returned in the Journal after running the EA. The tester log gives the same information:

**********************************************************************************************************************************************************************

2018.12.02 18:36:42.648 EURUSD-4,H1: 529 tick events (1504 bars, 2032 bar states) processed in 0:00:00.000 (total time 0:00:00.000)

2018.12.02 18:36:42.648 2018.11.01 00:00:00  ORDER HISTORY test started

2018.12.02 18:36:42.648 TestGenerator: spread set to 13

**********************************************************************************************************************************************************************

Likely I've missed something obvious, and if I have, apologies. Any help would be appreciated. The code I am using below:


**********************************************************************************************************************************************************************

int start()

{

   int orderPos;

   // GET CLOSED TRADES

   for (orderPos = 0; orderPos < OrdersHistoryTotal(); orderPos++) 

   {

      if(OrderSelect(orderPos, SELECT_BY_POS, MODE_HISTORY))

      {

         OrderPrint();

      }

   }

   return(0);

  }

*************************************************************************************************************************************************************************

Thanks again for any help.

 

I've got an Oanda account (forced upgrade to v20 :-/) and your code works fine on my end.

I tried it on both an EA and a Script.

All I did was place your code that's inside

int start {}

to the OnInit() function for the EA (it's the weekend so OnTick() doesn't work) and, for the Script, inside the OnStart() function.

It didn't work in the Strategy Tester and I'm guessing (can't recall, just got back into MQL) that it's because it has to be placed inside the OnTester() function of the EA.

 

Hi,before call OrdersHistoryTotal you need to load the orders to memory using the method HistoryOrderSelect.

https://www.mql5.com/en/docs/trading/historyorderselect

Documentation on MQL5: Trade Functions / HistoryOrderSelect
Documentation on MQL5: Trade Functions / HistoryOrderSelect
  • www.mql5.com
Selects an order from the history for further calling it through appropriate functions. It returns true if the function has been successfully completed. Returns false if the function has failed. For more details on error call GetLastError(). Do not confuse orders of a trading history with current pending orders that appear on the "Trade" tab of...
 
Bilal Said:

Hi,before call OrdersHistoryTotal you need to load the orders to memory using the method HistoryOrderSelect.

https://www.mql5.com/en/docs/trading/historyorderselect

I think that you will find that the OP's code is mql4

 
Keith Watford:

I think that you will find that the OP's code is mql4

Posted in mql5 session. 
 
Bilal Said:
Posted in mql5 session. 

Moving to the correct section.....

 

Thanks Alexander, I'll use what you've given me here and go from there. I'm also just getting back into it - it's mostly like riding a bike, but some of the finer points still obviously elude me!

Apologies Bilal and Keith for my rookie mistake.

Lee