Skip to main content

Backtesting of trading strategies with machine learning.

Project description


description: >- Trading strategy backtesting with machine learning optimizing best exit point (aka highest point of trade) for each trade, maximizing profit.

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 -r requirements.txt

First you need to import all the required classes.

import pandas as pd
from backtest.backtest import Backtest
from strategies.invertedhammer import InvertedHammer
from machine_learning.wrapper import MachineLearning
from models.rfr import RandomForestRegressorTrainer
from machine_learning.data import CandleStickDataProcessing

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()
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)
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.0.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

ml_backtest-0.1.0-py3-none-any.whl (21.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ml_backtest-0.1.0.tar.gz
Algorithm Hash digest
SHA256 032828d6317e30ae12d6194df212c56bf37939eee3fd4b34417f4251a91ffc2f
MD5 b96027f9636e6b9c75ce77a5677512cc
BLAKE2b-256 3f9891a2c9cc87d52cccc7af4e5c5a876c8670b06c8c3c95a5b9aff8a7ee86bc

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ml_backtest-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ebd0499b1c102e54ee76d9adb949fd12b707f4cf85d777db04e7786852a34428
MD5 75f4fa40abeb054ab61df1c231d83098
BLAKE2b-256 16a0c0ca1327531182f0c2c5b5af331f310a4652fda2cbd0be4d2b0c59f05ca6

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