vdemydiuk / mtapi

MetaTrader API (terminal bridge)
http://mtapi4.net/
MIT License
521 stars 281 forks source link

MT5: Improve testing speed with optional Events #144

Closed KptKuck closed 3 years ago

KptKuck commented 5 years ago

In order to increase the speed during testing, it makes sense to make the events individually switchable. Below is an example of how I use it myself. Or a better solution. `

input bool Enable_OnBookEvent = false; // for faster testing input bool Enable_OnTickEvent = false; // for faster testing input bool Enable_OnTradeTransaction = false; // for faster testing void OnTick() { string symbol = Symbol();

bool lastbar_time_changed = false; long lastbar_time = SeriesInfoInteger(symbol, Period(), SERIES_LASTBAR_DATE); if (_last_bar_open_time != lastbar_time) { if (_last_bar_open_time != 0) { MqlRates rates_array[]; CopyRates(symbol, Period(), 1, 1, rates_array);

     MtTimeBarEvent* time_bar = new MtTimeBarEvent(symbol, rates_array[0]);
     SendMtEvent(ON_LAST_TIME_BAR_EVENT, time_bar);
     delete time_bar;

     lastbar_time_changed = true;
  }

  _last_bar_open_time = lastbar_time;

} if (Enable_OnTickEvent) { MqlTick last_tick; SymbolInfoTick(Symbol(),last_tick);

  MtOnTickEvent * tick_event = new MtOnTickEvent(symbol, last_tick);
  SendMtEvent(ON_TICK_EVENT, tick_event);
  delete tick_event; 

}

if (IsTesting()) { if (BacktestingLockTicks == LOCK_EVERY_TICK || (BacktestingLockTicks == LOCK_EVERY_CANDLE && lastbar_time_changed)) { _is_ticks_locked = true;

     MtLockTickEvent * lock_tick_event = new MtLockTickEvent(symbol);
     SendMtEvent(ON_LOCK_TICKS_EVENT, lock_tick_event);
     delete lock_tick_event;
  }

  OnTimer();

} }

void OnTradeTransaction( const MqlTradeTransaction& trans, // trade transaction structure const MqlTradeRequest& request, // request structure const MqlTradeResult& result // result structure

) {

ifdef __DEBUG_LOG__

PrintFormat("%s:", FUNCTION);

endif

if(Enable_OnTradeTransaction) { MtOnTradeTransactionEvent* trans_event = new MtOnTradeTransactionEvent(trans, request, result); SendMtEvent(ON_TRADE_TRANSACTION_EVENT, trans_event); delete trans_event;}

}

void OnBookEvent(const string& symbol) {

ifdef __DEBUG_LOG__

  PrintFormat("%s: %s", __FUNCTION__, symbol);

endif

if(Enable_OnBookEvent) { MtOnBookEvent * book_event = new MtOnBookEvent(symbol); SendMtEvent(ON_BOOK_EVENT, book_event); delete book_event; } } `

vdemydiuk commented 5 years ago

Okay. I will think about this.