Created by Weidi Zhang
Python 3.7.4
Python Packages:
- joblib 0.14.1
- graphviz 0.13.2
- matplotlib 3.1.2
- numpy 1.18.0
- pandas 0.25.3
- scikit-learn 0.22
Other Software:
Exact versions are not required but correct behavior is not guaranteed on differing versions.
backtest_strategy.py: Strategies implemented for backtest simulationbacktest.py: Visualization and backtest logicdata_display.py: Visualization of processed datadata_processor.py: Process datasets of an assetextrema.py: Calculate extrema points for data processingindicators.py: Calculate indicator data for data processingpredict.py: Predict using decision tree model and post-processing of model prediction for improvementtrader.py: Abstract class that should be used a base in creating a live trader using model predictionstrain.py: Train processed data to produce decision tree modelvisualizer.py: Base to streamline visualizations for all data
- Run
data_display.pyto visualize the processed version of a dataset - Run
train.pyto generate and visualize the decision tree model - Run
backtest.pyto visualize result of backtesting using the trained model
From data_display.py:
From backtest.py:
Given a data set containing the date/time, prices (open, high, low, close), and volume of an asset over a fixed time span, we compute the following:
Indicator Data:
- HLC (high, low, close) (average used as price)
- EMA30
- EMA30 Directional Crossings
- MACD
- MACD Signal Line
- MACD Directional Crossings
- OBV
- RSI
Extrema Data (relating to price):
- Local Minima
- Local Maxima
Data is condensed and standardized into fixed ranges of either [-1, 1] for MACD indicator or [0, 100] for other data such that it can be applied to any asset or model given that their data is also condensed into this fixed range.
The shape of the data is important; the original values are not.
A decision tree is trained and used in the model for predicting extremas at a given point.
It classifies the "Extrema" attribute in the data based off of the following chosen indicators:
- HLC Average
- RSI
- EMACrossDifference
- EMACrossDirection
- MACDCrossDifference
- MACDCrossDirection
For the EMA and MACD calculations, the direction of a crossing and distance of a crossing past a signal line are more standardized and have higher bearing than the value of an indicator itself - thus only these two attributes are used. Additionally, correlation to how how a price is and the strength of its movement (RSI) is also used as feature attributes in training the decision tree. Any form of OBV data has been left out in this model -- all these attributes and any settings can be modified in train.py.
Note that the decision tree is generated using Gini impurity and not entropy (information gain).
The decision tree model is used to generate predicted extremas based on a given data set (a pandas dataframe).
The predictions of these extrema is then post-processed with two heuristics to correct for accuracy:
- Remove predicted extrema with greater than a specified number of maximum conflicting opposing extremas in a fixed search region
- Remove predicted extrema with less than a specified number of agreeing extrema predictions in a fixed search region
The search region is a fixed distance backwards or before a search point, as in a real world situation only backwards data search is possible.
Backtesting using predictions is done to verify how this asset trader would perform based on its predicted extremas, or buy and sell signals, in a real world situation.
The current backtesting strategy assumes that:
- Buy and sell transactions must alternate, i.e. it will not act on multiple buy signals in a row prior to selling its assets
- Profits from sell transactions are immediately reinvested for future buy transactions
- It will end the simulation or backtest with a sell transaction to gauge profits
An abstract class trader.py has been provided to help in creating live automated trading software solutions by using predictions from the model generated.
This class implements the following features in assisting this goal:
- Base structure to follow in creating this solution
- Update data and adapt data sources into a processable dataframe
- Easily predict current extrema based on latest point in time
- Sample logic for making transactions using the model
The provided trained model found at models/model.joblib may not work on your system - the model representation is architecture specific.
It would be best to re-generate it on your own system.
See: https://scikit-learn.org/stable/modules/model_persistence.html

