Skip to main content

Backtesting of trading strategies with machine learning.

Project description

Backtesting with Machine Learning

Available Patterns

Only bullish candlestick patterns are available right now and they are:

  • Inverted Hammer
  • Hammer
  • Bullish Engulfing
  • Bullish Harami
  • Morning Star
  • Morning Star Doji
  • Piercing Pattern
  • Dragon Fly Doji

This software backtest a trading strategy and runs the results through a user defined machine learning, with feature engineering to optimize the strategy as much as possible. An example output of this software would look like this.

The initial backtest

The backtest with the machine learning optimization

How to use

Install all dependencies.

pip install ml-backtest

First you need to import all the required classes.

from ml_backtest import Backtest, MachineLearning
from ml_backtest.machine_learning import CandleStickDataProcessing
from ml_backtest.strategies import InvertedHammer
from ml_backtest.models import RandomForestRegressorTrainer
import pandas as pd

After that make sure you rename your data-frame columns to these names if they are not already named that.

df = pd.read_csv('YOUR FILE NAME.csv')
df = df.rename(columns={'Time': 'date', 'Open': 'open', 'Close': 'close', 'High': 'high', 'Low': 'low'})

After you have your data-frame prepped you can make an instance of the strategy you want and pass it into a Backtest instance.

strategy = InvertedHammer()
backtest = Backtest(df, strategy)
print(backtest.results())

After that, we have all the data we need for machine learning to take place. Just declare an instance of the machine learning class and pass the need info into it. The machine learning takes place when the run function is called on the class. We can dump the model (saving the model to be used as a standalone file) with the dump_model function.

ml = MachineLearning(ml_class=RandomForestRegressorTrainer,
                         df=df,
                         results=backtest.get_trades())
ml.run(dp_pattern=CandleStickDataProcessing.calculate_inverted_hammer_features)
ml.dump_model(filename='YOUR FILE NAME')

After we trained the model, we want to backtest the model and see the results! The fun part! Two importnant functions you need to call for the backtest, get_util function and get_data function. The get_util will return a tuple of important values to be passed into the backtest class. The get_data will just be the data-frame as before but it includes all necessary added features during the call of feature_engineering function of the desired machine learning class.

model, columns, rows = ml.get_util()
data = ml.get_data()

ml_backtest = Backtest(data, strategy, model=model, columns=columns, rows=rows, cs_pattern=True)
print(ml_backtest.results())

Thats it! You should see similar output text wise as the outputs provided above. A more in depth how to use guide to customize your machine learning and strategy can be found below.

Project details


Download files

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

Source Distribution

ml_backtest-0.1.1.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

ml_backtest-0.1.1-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file ml_backtest-0.1.1.tar.gz.

File metadata

  • Download URL: ml_backtest-0.1.1.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for ml_backtest-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5eb7f81183ed8281b609d831cb6edbe355db870069cb057b3dac9a0ae8ff3c51
MD5 366fb532d88bf7047c26ee14f9549516
BLAKE2b-256 1b726ddada2cfe978b60615b840d3a51318cc933d479e56a8154b08624336592

See more details on using hashes here.

File details

Details for the file ml_backtest-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: ml_backtest-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 21.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for ml_backtest-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 62ec2b51f11e9772d59a430201d1efc69a846db71e0889ec43a7105297489bba
MD5 74efd66bf6f54d22b5773fda643d210d
BLAKE2b-256 7a83c6854845a1e2eabb890f447f67e62178a6e4e585c5fad3419295c54ae65c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page