Path: blob/master/examples/Stock_NeurIPS2018_3_Backtest.ipynb
726 views
Stock NeurIPS2018 Part 3. Backtest
This series is a reproduction of paper the process in the paper Practical Deep Reinforcement Learning Approach for Stock Trading.
This is the third and last part of the NeurIPS2018 series, introducing how to use use the agents we trained to do backtest, and compare with baselines such as Mean Variance Optimization and DJIA index.
Other demos can be found at the repo of FinRL-Tutorials.
Part 1. Install Packages
Part 2. Backtesting
To backtest the agents, upload trade_data.csv in the same directory of this notebook. For Colab users, just upload trade_data.csv to the default directory.
Then, upload the trained agent to the same directory, and set the corresponding variable to True.
Load the agents
Trading (Out-of-sample Performance)
We update periodically in order to take full advantage of the data, e.g., retrain quarterly, monthly or weekly. We also tune the parameters along the way, in this notebook we use the in-sample data from 2009-01 to 2020-07 to tune the parameters once, so there is some alpha decay here as the length of trade date extends.
Numerous hyperparameters – e.g. the learning rate, the total number of samples to train on – influence the learning process and are usually determined by testing some variations.
Part 3: Mean Variance Optimization
Mean Variance optimization is a very classic strategy in portfolio management. Here, we go through the whole process to do the mean variance optimization and add it as a baseline to compare.
First, process dataframe to the form for MVO weight calculation.
Helper functions for mean returns and variance-covariance matrix
Calculate the weights for mean-variance
Use PyPortfolioOpt
Part 4: DJIA index
Add DJIA index as a baseline to compare with.
Part 5: Backtesting Results
Backtesting plays a key role in evaluating the performance of a trading strategy. Automated backtesting tool is preferred because it reduces the human error. We usually use the Quantopian pyfolio package to backtest our trading strategies. It is easy to use and consists of various individual plots that provide a comprehensive image of the performance of a trading strategy.
Now, everything is ready, we can plot the backtest result.