A Python-based MetaTrader strategy tester for the MetaTrader5 module
Project description
StrategyTester5
StrategyTester is a Python-based backtesting and strategy testing framework built specifically for the MetaTrader5 (MT5) Python API.
Getting started
Ensure you have the MetaTrader 5 desktop application then proceeed to install the dependencies used in this project in your Python virtual environment
pip install strategytester5
Running your First Robot in The Strategy Tester
Step 1: Initialize the desired MetaTrader 5 terminal right after importing its module, alongside other useful Python modules for this project.
import MetaTrader5 as mt5
from strategytester5.tester import StrategyTester
from strategytester5.trade_classes.Trade import CTrade
import json
import os
import config
if not mt5.initialize(): # Initialize MetaTrader5 instance
print(f"Failed to Initialize MetaTrader5. Error = {mt5.last_error()}")
mt5.shutdown()
quit()
See examples the examples https://github.com/MegaJoctan/StrategyTester5/tree/main/examples
Step 2: Load configurations from a JSON file. In this case configs/tester.json file.
try:
with open(os.path.join(config.CONFIGS_DIR,'tester.json'), 'r', encoding='utf-8') as file: # reading a JSON file
# Deserialize the file data into a Python object
tester_configs = json.load(file)
except Exception as e:
raise RuntimeError(e)
Step 3: Initialize the Tester class, giving it configurations and an initialized MetaTrader 5 instance.
tester = StrategyTester(tester_config=tester_configs["tester"], mt5_instance=mt5) # very important
Optionally, instantiate the CTrade class to make life much easier.
m_trade = CTrade(simulator=tester, magic_number=magic_number, filling_type_symbol=symbol, deviation_points=slippage)
Step 4: Write some trading strategy
# ---------------------- inputs ----------------------------
symbol = "EURUSD"
timeframe = "PERIOD_H1"
magic_number = 10012026
slippage = 100
sl = 1000
tp = 100
# ---------------------------------------------------------
symbol_info = tester.symbol_info(symbol=symbol) # symbol information
def pos_exists(magic: int, type: int) -> bool:
for position in tester.positions_get():
if position.type == type and position.magic == magic:
return True
return False
def on_tick():
tick_info = tester.symbol_info_tick(symbol=symbol)
ask = tick_info.ask
bid = tick_info.bid
pts = symbol_info.point
if not pos_exists(magic=magic_number, type=mt5.POSITION_TYPE_BUY): # If a position of such kind doesn't exist
m_trade.buy(volume=0.1, symbol=symbol, price=ask, sl=ask-sl*pts, tp=ask+tp*pts, comment="Tester buy") # we open a buy position
if not pos_exists(magic=magic_number, type=mt5.POSITION_TYPE_SELL): # If a position of such kind doesn't exist
m_trade.sell(volume=0.1, symbol=symbol, price=bid, sl=bid+sl*pts, tp=bid-tp*pts, comment="Tester sell") # we open a sell position
Step 5: Call the main trading function into action on every tick, similarly to the OnTick function in MQL5
tester.OnTick(ontick_func=on_tick) # very important!
https://github.com/user-attachments/assets/09ed6921-8f00-4b49-8bcf-c6ac6ae9cdb3
More information about the project
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file strategytester5-1.0.0.tar.gz.
File metadata
- Download URL: strategytester5-1.0.0.tar.gz
- Upload date:
- Size: 46.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0rc2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8cbfcb7e74330eefeaaa25b45db2e66737df0b5e69629d174ddb5c6fe9f4f4a0
|
|
| MD5 |
7176f6f5db46af0af411facdc030ae0a
|
|
| BLAKE2b-256 |
3f2335cf3313f5d8daf1f740d1664974ccf21566d32623834df505ca70d88c63
|
File details
Details for the file strategytester5-1.0.0-py3-none-any.whl.
File metadata
- Download URL: strategytester5-1.0.0-py3-none-any.whl
- Upload date:
- Size: 52.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0rc2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b1ced559dee1c2d7c5014594fad63b0217953a561e68b138b890ce91cf904d19
|
|
| MD5 |
ad92787cba4b6d1ccfcab36bf0279129
|
|
| BLAKE2b-256 |
11782becfd27cca90059b414f8215f66e5a113fbd695e50767b185fdd2d1ad7d
|