Search code examples
mql4metatrader4

Metatrader 4 | No trades to be placed on EA re-initialization


I have an EA which places a trade - considering all other conditions are met - at the beginning of the candlestick bar & based on the "datetime time[0]`.

Naturally, if I were to reinitialize the EA, the EA would "re-recognise" the conditions and execute another trade. How would I stop that from happening on the oninit section of my EA?

Thanks


Solution

  • Declare global variable bool initTrade = true;

    and:

    int OnInit()
     {
       if(OrdersTotal() > 0){
          if(OrderSelect(OrdersTotal()-1, SELECT_BY_POS)){
             if(Minute() == TimeMinute(OrderOpenTime())  && Hour() == TimeHour(OrderOpenTime()) && Day() == TimeDay(OrderOpenTime())){
                initTrade = false;
             }
             if(initTrade){
                //Open init order logic...
             }
          }
       }
     return(INIT_SUCCEEDED);
    }