Close the first order in multiple buys or sells - page 2

 


Hi Jimdandy,

I really appreciate your contribution. Your approach has helped me understand whroeder1's suggestion of using time as a way to select my first opened order. Thank you for commenting your code too, it really helps to get my head around the steps required to extract the information I need. 

I guess where I was initially falling short was the fact I didn't understand why I would need to use time or even a loop for that matter considering I know that I want to target the order in position 0. It's pretty clear I haven't got the logic behind MQL down yet and have a long way to go.

Hopefully I'll be able to use to use the above to achieve what I want to do. 

Thank you all for helping me. It really is very much appreciated!

 
technoteze: I know that I want to target the order in position 0.
Position zero could be any order from any chart. Don't you want it to work correctly, when you place it on multiple charts, or with other EAs or with manual trading?
 
whroeder1:
Position zero could be any order from any chart. Don't you want it to work correctly, when you place it on multiple charts, or with other EAs or with manual trading?


Hey whroeder1,

I sure do. I thought I would filter the positions using _Symbol and the magic number. I had quite gotten there yet. I have it working more or less at the moment albeit with much needed work before I can confirm things are working as they should be.

I really do appreciate all the helpful feedback and suggestions. You guys have been great!

 
James Hodges:

  I think there are as many different ways to write this as their are coders... this is just one way to do it. I just typed it into the browser so their could be some syntax errors.
  In my example I use the process of elimination to get to the order we are interested in obtaining the time and most importantly... the ticket number of.  Mr. Roeder makes a good point about how you are initializing your EarliestSellTime to zero. That equates to the first second of 1970 and therefore you are NEVER going to have a trade older than that. Thus it will never select an order...
  He also rightly points out that, in your example, even if you were to somehow manage to select an older order, you never get the ticket number of it. It is always going to be returning the -1 to which you initialize it. In my example I simply use the current server time as a base to start from. Therefore the first order that passes the test of all the filters is going to be older than the current time. After that it will only save information about an order with an opening time older than the currently saved order time.
  Keep in mind this is just way it could be written. You an put it all together into one big if statement if you like. Of course to make the 'buy order' version of this function you simply change the names of the function and variables and throw out the orders that are not buy orders.

Hope this helps.. Pip Pip... Jimdandy

how to tweak this to get the 2nd earliest?
 
The Village: how to tweak this to get the 2nd earliest?
int ticketFirst=EMPTY, ticketSecond=EMPTY;
for(int iPos=OrdersTotal()-1; iPos >= 0; --iPos) if(
   OrderSelect(iPos, SELECT_BY_POS)
&& OrderMagicNumber() == …
&& OrderSymbol()      == _Symbol
   ⋮
){ ticketSecond = ticketFirst; ticketFirst = OrderTicket(); }

When in doubt, think!


Don't double post! You already had this thread open.
          General rules and best pratices of the Forum. - General - MQL5 programming forum (2017.07.19)