An Introduction To Backtesting
An Introduction To Backtesting
Wednesday, 19 March 14
Wednesday, 19 March 14
QUANTITATIVE TRADING
Creates a set of rules for trade order generation and risk management of
positions with minimal subsequent manager intervention.
Wednesday, 19 March 14
Wednesday, 19 March 14
WHAT IS BACKTESTING?
A simulation designed to test the performance of a set of trading and risk
management rules on historical data.
Wednesday, 19 March 14
BACKTESTING PITFALLS
Market regime shift - Regulatory change, macroeconomic events, black swans
Transaction costs - Unrealistic handling of slippage, market impact and fees
Liquidity constraints - Ban of short sales (e.g. finance stocks in 2008)
Optimisation Bias - Over-fitting a model too closely to limited data
Survivorship Bias - Only using instruments which still exist (incorrect sample)
Lookahead Bias - Accidental introduction of future information into past data
Interference - Ignoring strategy rules just this once because I know better
Wednesday, 19 March 14
Implementation
Rapid prototyping
tested quickly.
COMPONENTS OF A BACKTESTER
Data Handler - An interface to a set of historic or live market data.
Strategy - Encapsulates signal generation based on market data.
Portfolio - Generates orders and manages of Profit & Loss PnL
Execution Handler - Sends orders to broker and receives fills.
...and many more depending upon complexity
Wednesday, 19 March 14
NumPy/SciPy - Provide vectorised operations, optimisation and linear algebra routines all needed for
certain trading strategies.
Pandas - Provides the DataFrame, highly useful for data wrangling of time series data. Takes a lot of the
work out of pre-processing financial data.
Scikit-Learn - Machine Learning library useful for creating regression and classification models, that
are used in forecasting strategies.
Statsmodels - Statistical library (contains packages similar to R). Highly useful for time series analysis
for mean-reversion/momentum detection.
Wednesday, 19 March 14
Create two separate simple moving averages (SMA) of a time series with differing
lookback periods, e.g. 40 days and 100 days.
If the short moving average exceeds the long moving average then go long
If the long moving average exceeds the short moving average then exit
Wednesday, 19 March 14
Wednesday, 19 March 14
import datetime
import pandas as pd
import Quandl
ibm = Quandl.get(GOOG/NYSE_IBM)
Wednesday, 19 March 14
CLASS HIERARCHIES
Create Strategy and Portfolio class hierarchies
Abstract base classes enforce interface for subclasses
Strategies and Portfolios can be swapped out easily and are loosely coupled to
data and execution modules.
Wednesday, 19 March 14
class MovingAverageCrossStrategy(Strategy):
..
def generate_signals(self):
# Create DataFrame and initialise signal series to zero
signals = pd.DataFrame(index=self.bars.index)
signals['signal'] = 0
# Create the short/long simple moving averages
signals['short_mavg'] = pd.rolling_mean(bars['Adj Close'], self.short_window, min_periods=1)
signals['long_mavg'] = pd.rolling_mean(bars['Adj Close'], self.long_window, min_periods=1)
# When the short SMA exceeds the long SMA, set the signals Series to 1 (else 0)
signals['signal'][self.short_window:] =
np.where(signals['short_mavg'][self.short_window:] >
signals['long_mavg'][self.short_window:], 1, 0)
# Take the difference of the signals in order to generate actual trading orders
signals['positions'] = signals['signal'].diff()
return signals
Wednesday, 19 March 14
Wednesday, 19 March 14
PERFORMANCE
What next?
Wednesday, 19 March 14
IMPROVEMENTS?
Multi-symbol portfolios, by adding more columns to a pandas DataFrame.
Risk management framework (much more important than signal generation!)
True event-driven backtesting helps mitigate lookahead bias
Realistic handling of transaction costs - fees, slippage and possible market impact
Optimisation routines to find best parameters (be careful of curve-fitting!)
GUI via PyQT or other libraries
Wednesday, 19 March 14
http://www.quantstart.com/articles/My-Talk-At-The-London-Financial-Python-User-Group
Pandas - http://pandas.pydata.org/
Scikit-Learn - http://scikit-learn.org/
Statsmodels - http://statsmodels.sourceforge.net/
ZipLine - https://github.com/quantopian/zipline
Canopy - https://www.enthought.com/products/canopy/
Quandl - http://www.quandl.com/
THANK YOU!
Wednesday, 19 March 14