Warm tip: This article is reproduced from serverfault.com, please click

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

发布于 2021-01-26 08:12:28

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

Questioner
Johannes Schulz-Bauer
Viewed
0
tomgny 2021-02-03 22:35:11

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);
}