Is this code FIFO compliant?

Back to topics list To post a new topic, please log in or register
Flood19 2016.10.04 10:19

I'm writing an expert advisor and I want it to be FIFO compliant. Note that I can have various instances of the EA running on different pairs at the same time. Also, there could be various instances running on the same pair (on different charts), each with a different magic number.

When I need to close an order, I call "oldestTicket()" and close that order:

// An order is valid if its from the current symbol and has the EA parameters
//  magic number
bool isOrderValid() {
    if ((OrderMagicNumber() != magic_number) || (OrderSymbol() != Symbol()))
        return false;

    return true;

// Returns the ticket of the oldest opened order, in order to compliant with FIFO
int oldestTicket() {
    int res = -1;
    datetime oldest = -1;

    for (int i = OrdersTotal() - 1; i >= 0; i--) {
        if (!OrderSelect(i, SELECT_BY_POS))
            continue; // Error selecting order

        if (!isOrderValid())

        if (OrderOpenTime() < oldest || (OrderOpenTime() == oldest && OrderTicket() < res) || oldest == -1) {
            oldest = OrderOpenTime();
            res = OrderTicket();

    return res;

I am worried about when two orders were opened at the same time by the same instance, which would possibly trigger that condition: "(OrderOpenTime() == oldest && OrderTicket() < res)".

Also I'm not sure if it there is something I am missing.

Any clarification would be appreciated.

Thank you.
Back to topics list  

To add comments, please log in or register