Metadata-Version: 2.1
Name: ml_backtest
Version: 0.1.0
Summary: Backtesting of trading strategies with machine learning.
Home-page: https://github.com/MaxwellMendenhall/ml-backtest
Author: Maxwell Mendenhall
Author-email: mendenhallmaxwell@gmail.com
License: MIT
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pandas~=2.2.1
Requires-Dist: scikit-learn~=1.3.2
Requires-Dist: numpy~=1.26.4
Requires-Dist: tqdm~=4.66.2
Requires-Dist: joblib~=1.3.2

---
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.

<figure><img src=".gitbook/assets/Screenshot 2024-03-18 at 12.15.50 AM.png" alt=""><figcaption><p>The initial backtest</p></figcaption></figure>

<figure><img src=".gitbook/assets/Screenshot 2024-03-18 at 12.15.54 AM.png" alt=""><figcaption><p>The backtest with the machine learning optimization</p></figcaption></figure>

## How to use

Install all dependencies.

```
pip install -r requirements.txt
```

First you need to import all the required classes.

```python
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.

```python
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.&#x20;

```python
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.&#x20;

```python
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.

```python
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.

* [Machine Learning Class Guide](backtesting-with-machine-learning/machine-learning-class-guide.md)
* [Trading Strategy Class Guide](backtesting-with-machine-learning/strategy-creation-class-guide.md)
