Lesson 2: Backtest your trading strategy with only 3 lines of code
Learn how to assess the performance of an SMAC strategy on JFC
- fastquant Lesson 2 - Backtest your trading strategy with only 3 lines of code
- fastquant package Updates
- Setup
- Recap from Lesson 1
- Lesson 2: Backtesting with fastquant
#!pip install fastquant
fastquant Lesson 2 - Backtest your trading strategy with only 3 lines of code
fastquant package Updates
-
get_stock_data
has been added, which now includes all Yahoo Finance data (on top of PSE) -
backtest
function is now ready to use! - README has been updated to reflect the above
Setup
- Google Colab (limitted plotting)
- Jupyter notebook (complete plotting)
Google Colab Lesson 2 link
Colab notebook on this link
How to use Jupyter notebooks
1. Go to your terminal and install jupyter
pip3 install jupyter
2. Clone fastquant
git clone https://github.com/enzoampil/fastquant.git
cd fastquant
3. Run jupyter
jupyter notebook
lessons/fastquant_lesson2_backtest_your_trading_strategy.ipynb
4. Open lesson at from matplotlib import pyplot as plt
from fastquant import backtest, get_stock_data
%matplotlib inline
jfc = get_stock_data("JFC", "2018-01-01", "2019-01-01")
jfc.close.plot(figsize=(10, 6))
plt.title("Daily Closing Prices of JFC\nfrom 2018-01-01 to 2019-01-01", fontsize=20)
Analyze with a simple moving average (SMA) trading strategy
In this section, we will attempt to visually assess the performance of a SMA crossover strategy. There are many ways to do this strategy, but we will go with a “price crossover” approach with a 30 day SMA.
In this case, it’s considered a “buy” signal when the closing price crosses the simple moving average from below, and considered a “sell” signal when the closing price crosses the simple moving average from above.
So how do we know our SMA price crossover strategy is effective? Visually, we can assess this by seeing if the “sell” signal happens right before the stock price starts going down, and if the “buy” signal happens right before the stock price starts going up.
import pandas as pd
ma30 = jfc.close.rolling(30).mean()
close_ma30 = pd.concat([jfc.close, ma30], axis=1).dropna()
close_ma30.columns = ['Closing Price', 'Simple Moving Average (30 day)']
close_ma30.plot(figsize=(10, 6))
plt.title("Daily Closing Prices vs 30 day SMA of JFC\nfrom 2018-01-01 to 2019-01-01", fontsize=20)
from fastquant import backtest, get_stock_data
- Get stock data in a date, close, volume format (DCV)
Here, we get DCV (date, closing, volume) data from JFC using the get_pse_data
function
jfc = get_stock_data("JFC", "2018-01-01", "2019-01-01")
jfc
- Backtest a simple moving average crossover (
smac
) strategy on the JFC data
We perform the backtest using a 15 day moving average as the "fast" moving average, and a 35 day moving average as the "slow" moving average. If we want to change these parameter values, we can just replace the numbers in the backtest
function.
We call these the strategy level arguments since they are unique to a specific strategy.
Do note that by default, the backtesting algorithm assumes that you start out with PHP 100,000 as cash (init_cash
), while using all of that cash during a buy signal (buy_prop
), and selling all of your current stock holdings during a sell signal (sell_prop
). We call these the global level arguments since they can be shared across companies.
%matplotlib
backtest('smac', jfc, fast_period=15, slow_period=35)
%matplotlib
backtest('smac', jfc, fast_period=1, slow_period=30)
%matplotlib
backtest('smac', jfc, fast_period=30, slow_period=50)
We can conclude that across all the parameter combinations we've tried, the best performing one is the one where fast_period
= 15, while slow_period
= 40.
%matplotlib
backtest('smac', jfc, fast_period=15, slow_period=40)
Relevant resources
- Medium article
- Backtrader repo
How to contribute more trading strategies
fastquant/strategies.py
Checkout - New strategies can be added with as little as 20 lines of code (excluding docs)
- Please let me know if you're intrested in contributing! :)
- Happy to walk you through the process personally, still in the process of writing a tutorial
Planning to add the following TA strategies next:
- SMAC [DONE]
- RSI [DONE]
- Bollinger Bands
- Moving Average Convergence Divergence
- EMAC (exponential moving average crossover)
- On-Balance-Volume
- Ichimoku Kinko Hyo
- Average Directional Index
- Parabolic Stop and Reverse (SAR)
- Stochastic