Download MetaTrader 5
To add comments, please log in or register
Have you published a program in the Market? Place it in your blog as well - the widget will help!
Guillermo
75
Guillermo 2016.10.04 08:19 
Hello, 

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())
            continue;

        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.
/
To add comments, please log in or register