Close orders by OrderOpenTime()

TradeChaser

Active Trader
May 12, 2020
161
6
34
32
Having trouble writing an EA in FIFO land... I am trying to close all open orders once a set PT is hit... say all orders average a total of 50 pips --> close trade...

I am getting FIFO violations in my journal. I have played with different things in my code... I can successfully scrape the OrderOpenTimes() and sort them Ascending, which is awesome. But I do not know how to use that data to then iterate through open orders and close them.

could it be as simply as changing the order in which I iterate through OrdersTotal();?

For example, my standard iteration through orders looks like this:

for(int i=OrdersTotal();i>=0;i--)


If I change to the below, will the orders be in chronological order?
for(int i = 0 ; i < OrdersTotal(); i++)
 

hayseed

Master Trader
Jul 27, 2010
1,046
262
149
usa
Having trouble writing an EA in FIFO land... I am trying to close all open orders once a set PT is hit... say all orders average a total of 50 pips --> close trade...

I am getting FIFO violations in my journal. I have played with different things in my code... I can successfully scrape the OrderOpenTimes() and sort them Ascending, which is awesome.
//-----

usually people use ,

Code:
for(int i = (OrdersTotal() - 1); i >= 0; i--)
   {



closing orders under fifo with dozens or hundreds of orders will be difficult..... it has to do with , 'when is a order actually a order and what to do when 4 same sized orders have the same orderopentime()"....

there will be times when the oldest order can not be closed first..... it will generate a fifo error......

the best solution, which will still fail at times, is to first search for the oldest ticket and then close that order using select_by_ticket.....

continue that till all are closed.....

thought about running the glitch by roeder over on mq4 site but never got around to it.....h
 

TradeChaser

Active Trader
May 12, 2020
161
6
34
32
Thanks for the reply hayseed!

As it stands, my EA will only open on the bar open, so as far as datetime data goes, it should definitely be something that can be iterated and sorted.

I suppose my only option will be to create two arrays, one for ticket#, and one that sorts the difference between the current time and OrderOpenTime.

I can then sort the second away by duration, then run a logical test through the array of ticket # and when the ticket number matches the oldest order by duration, close that order, and move on.

Just seems so clunky
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,607
1,366
144
Odesa
www.earnforex.com
What I do normally is I create a two-dimensional array where the first dimension is the sorting parameter (e.g., time) and the second dimension is the ticket. Then I record all orders into this array, sort it, and do what I need further with the sorted orders.