Discussing the article: "Python-MetaTrader 5 Strategy Tester (Part 06): MQL5-Style Backtesting for Python Expert Advisors"

 

Check out the new article: Python-MetaTrader 5 Strategy Tester (Part 06): MQL5-Style Backtesting for Python Expert Advisors.

Code and build Python-based trading robots just like MQL5 Expert Advisors (EAs). In this article, we develop a Python-based replica of the MetaTrader 5 Python package, providing methods that closely resemble those of MetaTrader 5 during simulation. This allows us to backtest Python EAs in a simplified environment, using an approach similar to developing and testing Expert Advisors in MQL5.

When you are developing trading robots for MetaTrader 5 in Python and face a familiar split: for live trading you call the MetaTrader5 package, while for backtesting you must build and maintain a separate tester layer with different initialization, data sources and APIs. The result is duplicated logic, brittle synchronization between two code paths, and wasted time every time you change your strategy. What we want instead is an MQL5-like workflow in Python: a single EA-style main() (OnTick) implementing trading logic once, and the ability to run that same code against historical data or in the live market without rewriting it.

This article shows how the StrategyTester5 framework achieves that: it provides a VirtualMetaTrader5 that mirrors the MetaTrader5 API, a simple mt5 variable swap to switch environments, and a run_backtesting() function that replays market data and returns a TesterStats object. The goal is practical: write your strategy once, then choose whether to run it on history or on a live account.


Author: Omega J Msigwa

 

Nice approach. Reusing the same strategy logic for both historical testing and live execution is a big advantage because it reduces duplicated code and inconsistencies.

The VirtualMetaTrader5 concept is especially interesting. I’d also be curious how the framework models real-world differences such as variable spreads, slippage, execution latency, and broker conditions when comparing backtest results with live trading.

 
Bhavesh #:
I’d also be curious how the framework models real-world differences such as variable spreads, slippage, execution latency, and broker conditions when comparing backtest results with live trading.

Your comment makes it look like you’ve read the articles. Your question makes it look like you haven’t. I have two follow-up questions for you:

  • Is there any mention of what you asked about in any of the six articles?
  • Is it possible to figure this out from the source code described in those articles?
 
Bhavesh #:

Nice approach. Reusing the same strategy logic for both historical testing and live execution is a big advantage because it reduces duplicated code and inconsistencies.

The VirtualMetaTrader5 concept is especially interesting. I’d also be curious how the framework models real-world differences such as variable spreads, slippage, execution latency, and broker conditions when comparing backtest results with live trading.

Great, some of your concerns have been implemented in prior articles, check them out.