Accessing Opened orders in the order in which they were opened

To add comments, please log in or register
pwakhungu
36
pwakhungu  

Hi, I am still trying to understand MQl4, so my question may be basic. My EA has ability of opening upto 10 orders, and I need to place a trailing stop to all trades depending on the order in which they were opened. Looking at topics like  https://www.mql5.com/en/forum/191264 , I note that the orders are not always ordered from 0-(n-1) where n is the total number of orders. How can I access these orders starting from the first to the last say in a loop?

Why can't this code below work?

for(int position=0; position<=number_of_trades-1; position++){
      if(OrderSelect(position,SELECT_BY_POS,MODE_TRADES)==true){
        //Some code to place trailing stop loss depending on the order in which it was opened
        }
}

Close the first order in multiple buys or sells
Close the first order in multiple buys or sells
  • 2017.04.30
  • www.mql5.com
Hi guys, I was hoping someone could help me out. Coding is not a strong point for me but I am trying...
Carl Schreiber
Moderator
7130
Carl Schreiber  
  • Make it as a habit to count down! Otherwise if you e.g. close the first order of the list your index moves on to 2 but the second order is now the first and you'll miss it:
for(int positionnumber_of_trades-1; position>=0; position--){...
  • After this code line you have to select:
if ( !OrderSelect(position) ) continue; // or print the error
Just look in the reference: place the cursor on e.g. OrderSelect() and press F1. Search there for examples, there are plenty for to copy and paste :)
pwakhungu
36
pwakhungu  
Carl Schreiber:
  • Make it as a habit to count down! Otherwise if you e.g. close the first order of the list your index moves on to 2 but the second order is now the first and you'll miss it:
  • After this code line you have to select:
Just look in the reference: place the cursor on e.g. OrderSelect() and press F1. Search there for examples, there are plenty for to copy and paste :)
I get your point. However, for this specific scenario, I want to use OrderModify() on the orders starting with the first order to the last. There is a separate function to close the orders where I have used count down.
Carl Schreiber
Moderator
7130
Carl Schreiber  
Then assign the ticket numbers (or the timestamp of the opening) of all open positions to an integer or datetime array and sort this array and the access the the open position according to the place in the array.
pwakhungu
36
pwakhungu  
Carl Schreiber:
Then assign the ticket numbers (or the timestamp of the opening) of all open positions to an integer or datetime array and sort this array and the access the the open position according to the place in the array.
So in other words the order at position 0 is not automatically the first order that was opened? This is the clarification that I need. My orders are opened at least two seconds apart, after a certain condition is met.
To add comments, please log in or register