Order Select History Missing When MT4 restarted

 

Hi,

My EA always needs to check the last close trade but whenever mt4 is closed and restarted it does not read the last trade. Any ideas how I can code my EA to check the last trades result even when the terminal is restarted?

    double profit = 0;
    datetime lastCloseTime = 0;
    int cnt = OrdersHistoryTotal();
    for (int i=0; i < cnt; i++)
    {
        if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && lastCloseTime < OrderCloseTime())
        {
            lastCloseTime = OrderCloseTime();
            profit = OrderProfit();
        }
    }
 
You could check that the history total >0, otherwise return.
 
GumRai:
You could check that the history total >0, otherwise return.
Thank you, Could you show me an example please?
 
    int cnt = OrdersHistoryTotal();
    if(cnt<1)
       return;
    for (int i=0; i < cnt; i++)
    {
If the history has not loaded, it will then return and wait for the next tick without doing anything
 
gangsta1: Any ideas how I can code my EA to check the last trades result even when the terminal is restarted?
Are you doing it in init? wait for a tick.
Reason: