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.get_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(),
rows=10,
columns=['EMA_Diff', 'SMA_Diff', 'MACD_hist'])
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.get_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.
Metadata
Release files for ml-backtest 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ml_backtest-0.1.3.tar.gz | 19.0 kB | Without provenance | Details for ml_backtest-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ml_backtest-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for ml_backtest-0.1.3-py3-none-any.whl |
Total release size: 41.4 kB
Release files / ml_backtest-0.1.3.tar.gz
| Download URL | ml_backtest-0.1.3.tar.gz |
|---|---|
| Size | 19.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9804dcc3d56087c7ca1d2788c230b7dd246f6f0cedc16928035169ff63d08987
|
|
BLAKE2b-256 checksum How to use checksums |
c8159734f7698b304424d785e05f67f69cfb9f857cc0af78ac5bd16b3e0ef8de
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.19
|
Release files / ml_backtest-0.1.3-py3-none-any.whl
| Download URL | ml_backtest-0.1.3-py3-none-any.whl |
|---|---|
| Size | 22.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
57eb1b2fef30a415dfac2493adc79b951e2f3b77f065fcd4235fcd6c163bb42b
|
|
BLAKE2b-256 checksum How to use checksums |
f9e92fa5bbb130a20dc4dce6f74a9a893e01f84a201ebe1f5d248c312ae9b1b4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.19
|
Security for ml-backtest 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.