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.1a0.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: ml_backtest-0.1.1a0.tar.gz
  • Upload date:
  • Size: 18.7 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.1a0.tar.gz
Algorithm Hash digest
SHA256 e6d0dba2f03043f86456a01648793d9fd71e5cde8f600a8ccef30de10ca8d943
MD5 cc50a4f998a0d9b426981266dd23c3cf
BLAKE2b-256 d15f81541f9640d68369b5473b41c5795fb24a1a04455cbe1c364ac38895ff0d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ml_backtest-0.1.1a0-py3-none-any.whl
Algorithm Hash digest
SHA256 c9e87774f8d89d3235bd1a78bad5bfa396bc0ea99f2488d245ea9cbe1af240b8
MD5 60364340e5ef4000cb2f7ef1d01cff1f
BLAKE2b-256 c2e034bf753a9c5868bd76bcbc0ea24d1edc1c14c7af98290bc4e5f4bdde8d82

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