Solving Non Closure of Trades due to a FIFO Violation MT4 EA - page 2

 
hayseed:
//----

hey mladen..... yes, there needs to be a '150' error code alert... ERR_TRADE_PROHIBITED_BY_FIFO.

//-----

until this week, ibfx had not been implementing fifo in the states..... this may be just specific to ibfx, but if 2 or more orders have the same orderopentime, the fifo tie breaker is the ticket number....

orderopentime's smallest increment is in minutes.... so it's quite possible for many orders to be opened in that same minute.....

OrderSelect(i, SELECT_BY_POS) should return the smallest ticket, but for some reason it did not when orders had the same orderopentime.... it never once returned the proper ticket if 2 or more orders had same opentime....

the 150 error code would lockup the platform....

my solution was to add an additional function to select the pairs smallest ticket. and include that number as a condition...... thereby assuring both time and ticket conditions were met......

and again, this might be relative to ibfx only..... h

//-----

while(Count() > 0)

{

datetime oldest = Time[0]+Period()*60+1;

int ticketToClose = -1;

double closePrice;

int lowticket;

for(int cnt = OrdersTotal()-1; cnt>=0; cnt--)

{

lowticket = ticket(); //--- ticket() is an external fuction that returns lowest ticket number

if (!OrderSelect(cnt, SELECT_BY_POS)) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderOpenTime() < oldest && OrderTicket() == lowticket)

{

oldest = OrderOpenTime();

ticketToClose = OrderTicket();

RefreshRates();

if (OrderType() == OP_BUY)

closePrice = Bid;

else closePrice = Ask;

}

}

hayseed

Be careful with the lowest ticket number

According to metaquotes, lower ticket number does not mean (and does not guarantee) that the time of the order opened is lower too (that is their official statement). So, after all, we should make an array with lowest open time that are equal and try one by one until we do not get an error 150 (if we are to honor what metaquotes people are saying about the ticket numbers and time relation)

PS: how is that that the order open time is in minutes? It should be in seconds. Is that specific to the broker you are using?

 
mladen:
hayseed

Be careful with the lowest ticket number

PS: how is that that the order open time is in minutes? It should be in seconds. Is that specific to the broker you are using?

//---

hey mladen.... the time error was mine.... actually as you said, the smallest increment is seconds..... for some reason i had minutes in my mind....

figured out the time issue... it has to do with pending orders.... if a script sends several pending orders quickly, it is possible for them all to have the same orderopentime down to the same second..... see the picture below.... there are 3 groups with same opentime......

last week i had probably a dozen such instances.....

i have been testing opening and closing since market opening..... without the extra ticket() function i always get the error 150 code.... with it, they close properly.....

the safest solution, for me, is to rewrite everything that sends orders and include a sleep() between them.....h

//-----

Files:
mladen.png  136 kb
 
hayseed:
//---

hey mladen.... the time error was mine.... actually as you said, the smallest increment is seconds..... for some reason i had minutes in my mind....

figured out the time issue... it has to do with pending orders.... if a script sends several pending orders quickly, it is possible for them all to have the same orderopentime down to the same second..... see the picture below.... there are 3 groups with same opentime......

last week i had probably a dozen such instances.....

i have been testing opening and closing since market opening..... without the extra ticket() function i always get the error 150 code.... with it, they close properly.....

the safest solution, for me, is to rewrite everything that sends orders and include a sleep() between them.....h

//-----

Yes, but that will not solve the problem if you are using pending orders to open orders (then they can open at the same time). Looks like the FIFO rule will always demand some extra coding when it comes to closing orders