Skip to main content

tradetestlib: A MetaTrader5 backtesting tool

License: MIT PyPI version

tradetestlib is a backtesting library built to integrate with MetaTrader5, with the purpose being able to provide a broad overview of a trading strategy/idea, more specifically, an evaluation of a strategy. Some of the main evaluation metrics used in this project are the sharpe ratio, and profit factor.

tradetestlib also provides the option to optimize a strategy by using a Grid Search algorithm for hyperparameter tuning.

Currently, hyperparameters are limited to position sizing, and exposure.

A demonstration can be found here

Installation

tradetestlib can be installed with pip

pip install tradetestlib

Usage

A simulation instances can be created by calling the Simulation class.

symbol and timeframe are only used for naming conventions, for comparing a basket of assets.

train_raw and test_raw are dataframes that contain Open, High, Low, Close, signal, and true_signal columns.

lot is the lot size used to trade

starting_balance is the initial deposit of the simulation in USD.

from tradetestlib import Simulation

sim = Simulation(
    symbol = symbol,
    timeframe = tf,
    train_raw = train,
    test_raw = test,
    lot = 1, 
    starting_balance = 100000    
)

Optimization can be used by creating a params dictionary with the required hyperparameters.

run_grid_search returns the optimal configuration, and overall testing set.

Optimized hyperparameters may also be accessed as attributes, which can then be used to create a final simulation instance, to verify result with the test set.

from tradetestlib import Optimize

params = {
    'lot' : [1,2],
    'hold_time': [5, 10],
    'max_loss': [0.005, 0.01]
}

o = Optimize(symbol = symbol, 
    timeframe = tf, 
    train = train, 
    test = test,
    metric = 'sharpe_ratio',
    how = 'maximize')

best, df = o.run_grid_search(params)

o.optimized_lot # best lot 
o.optimized_holdtime # best holdtime
o.optimized_max_loss # best exposure 

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tradetestlib-1.2.0.tar.gz (22.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tradetestlib-1.2.0-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file tradetestlib-1.2.0.tar.gz.

File metadata

  • Download URL: tradetestlib-1.2.0.tar.gz
  • Upload date:
  • Size: 22.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for tradetestlib-1.2.0.tar.gz
Algorithm Hash digest
SHA256 804ba02895f27ecedb27084c256304bf604c5db19f29bc6b90ea57525cb198fa
MD5 1e8267a3a750d69932013d57e7485c53
BLAKE2b-256 685dc5c202b460b9441ed5b93369836c2b14ef9f1ab8e3e50b8bb399ee0b6f34

See more details on using hashes here.

File details

Details for the file tradetestlib-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: tradetestlib-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for tradetestlib-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d890dee68f996fbfc02a4a5f4ad1811a1694e4ff128a3237bd4cf8ea426efe62
MD5 29c32447abf8c45544ffbafb2481e07b
BLAKE2b-256 43cac390c8546f2545c0f3ca3897f1f9a0db5b505209bdba6697f91bd3017874

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 files

1.1.0

2 files

1.0.1

2 files

1.0.0

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page