Download MetaTrader 5
To add comments, please log in or register
How to launch your trading signal and become a provider
gangsta1
1117
gangsta1 2014.12.08 23:40 

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();
        }
    }
Keith Watford
Moderator
7891
Keith Watford 2014.12.09 01:00  
You could check that the history total >0, otherwise return.
gangsta1
1117
gangsta1 2014.12.09 10:08  
GumRai:
You could check that the history total >0, otherwise return.
Thank you, Could you show me an example please?
Keith Watford
Moderator
7891
Keith Watford 2014.12.09 10:56  
    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
whroeder1
13602
whroeder1 2014.12.09 13:41  
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.
/
To add comments, please log in or register