AN INTRODUCTION TO BACKTESTING WITH PYTHON AND PANDAS



Similar documents
Algorithmic Trading Session 1 Introduction. Oliver Steinki, CFA, FRM

Design of an FX trading system using Adaptive Reinforcement Learning

Pattern Recognition and Prediction in Equity Market

White Paper Electronic Trading- Algorithmic & High Frequency Trading. PENINSULA STRATEGY, Namir Hamid

TSL TRADING SYSTEM DESCRIPTION FOREX TRADING SYSTEM

Stock Market Trends...P1. What Is Automated Trading...P2. Advantages & Disadvantages...P3. Automated Trading Conclusion...P4

PITFALLS IN TIME SERIES ANALYSIS. Cliff Hurvich Stern School, NYU

Algorithmic Trading Session 6 Trade Signal Generation IV Momentum Strategies. Oliver Steinki, CFA, FRM

Contents Risk Disclaimer... 3 Introduction... 4 Choosing the Right Broker... 4 The Financial Markets... 5 The Forex Market...

Take it E.A.S.Y.! Dean Malone 4X Los Angeles Group - HotComm January 2007

(Part.1) FOUNDATIONS OF RISK MANAGEMENT

Haksun Li MY EXPERIENCE WITH ALGORITHMIC TRADING

The Merits of Absolute Return Quantitative Investment Strategies

Mean Reversion - Illustration of Irregular Returns in US Equity and Fixed Income Markets

GMO WHITE PAPER. The Capacity of an Equity Strategy. Defining and Estimating the Capacity of a Quantitative Equity Strategy. What Is Capacity?

The Lee Kong Chian School of Business Academic Year 2014 /15 Term 2

Asset Liability Management / Liability Driven Investment Optimization (LDIOpt)

Contents. List of Figures. List of Tables. List of Examples. Preface to Volume IV

Pairs Trading STRATEGIES

Performance of pairs trading on the S&P 500 index

High-frequency trading, flash crashes & regulation Prof. Philip Treleaven

Whether you re new to trading or an experienced investor, listed stock

MATLAB as an Automated Execution System

Stock Market Dashboard Back-Test October 29, 1998 March 29, 2010 Revised 2010 Leslie N. Masonson

Nest Plus StocksIQ User Manual

Rotational Trading using the %b Oscillator

My EA Builder 1.1 User Guide

Data Technologies for Quantitative Finance

Quantitative Trading. How to Build Your Own Algorithmic Trading Business. Wiley Trading

UNUSUAL VOLUME SYSTEM

Using Order Book Data

Algorithmic Trading Course Module 2: Backtesting & Quantitative Trading

CONTENTS MODULE 1: INDUSTRY OVERVIEW 4 MODULE 2: ETHICS AND REGULATION 6 MODULE 3: INPUTS AND TOOLS 8 MODULE 4: INVESTMENT INSTRUMENTS 12

ETF Total Cost Analysis in Action

Fast Trading and Prop Trading

Society of Actuaries in Ireland

Multiple Kernel Learning on the Limit Order Book

Algorithmic Trading Present and Future in Korea

From thesis to trading: a trend detection strategy

Risk Visualization: Presenting Data to Facilitate Better Risk Management

Commodity Trading Advisors. AQF 2005 Nicolas Papageorgiou

Algorithmic Trading: A Quantitative Approach to Developing an Automated Trading System

JUST SIT BACK AND LET THE GIRTH MODEL MAKE MONEY FOR YOU. Elham Negahdary

Machine Learning and Algorithmic Trading

Enterprise Risk Management

Intraday FX Trading: An Evolutionary Reinforcement Learning Approach

Understanding Managed Futures

Department of Accounting and Finance

Value, size and momentum on Equity indices a likely example of selection bias

Long/Short Equity Strategy

Risks Involved with Systematic Investment Strategies

CURRENCY TRADING Using Swing Breakouts in Forex Trading

Finanzdienstleistungen (Praxis) Algorithmic Trading

Replicating Life Insurance Liabilities

Financial Text Mining

MSc Financial Economics - SH506 (Under Review)

Day Trading Stock Controller

Scientific Programming in Python

International Investments

Guidance for the Development of a Models-Based Solvency Framework for Canadian Life Insurance Companies

Analyzing an Electronic Limit Order Book

Intraday Trading Invariance. E-Mini S&P 500 Futures Market

Understanding Currency

Software support for economic research at CNB

Sentiment Analysis of Twitter Feeds for the Prediction of Stock Market Movement

Portfolio Management for Banks

Master of Business Administration Program in the Faculty of Business Administration and Economics

Online tools for demonstration of backtest overfitting

Master of Business Administration Program in the Faculty of Business Administration and Economics

Master of Science in Finance (MSF) (Courses option)

Machine Learning Applications in Algorithmic Trading

Survivorship Bias in the Development of Equity Trading Systems

Quantitative Methods for Finance

Does trend following work on stocks? Part II

Algorithmic trading facts & fantasies

CRASH COURSE PYTHON. Het begint met een idee

Calculation of Valu-Trac Statuses

INFERRING TRADING STRATEGIES FROM PROBABILITY DISTRIBUTION FUNCTIONS

Business Analytics Syllabus

Turk s ES ZigZag Day Trading Strategy

Managing Risks and Optimising an Energy Portfolio with PLEXOS

Scalping Trade Strategies April 14, 2009

Distributed Computational Finance Infrastructure for Algorithmic and Proprietary Trading

Marketing Mix Modelling and Big Data P. M Cain

Black Box Trend Following Lifting the Veil

The Future of Algorithmic Trading

Certification Program on Corporate Treasury Management

Schroders Investment Risk Group

Simpler Options. Indicator guide. An informative reference for John Carter s commonly used trading indicators.

ELECTRONIC TRADING GLOSSARY

INCORPORATION OF LIQUIDITY RISKS INTO EQUITY PORTFOLIO RISK ESTIMATES. Dan dibartolomeo September 2010

The Master of Science in Finance (English Program) - MSF. Department of Banking and Finance. Chulalongkorn Business School. Chulalongkorn University

DATA SCIENCE CURRICULUM WEEK 1 ONLINE PRE-WORK INSTALLING PACKAGES COMMAND LINE CODE EDITOR PYTHON STATISTICS PROJECT O5 PROJECT O3 PROJECT O2

Exercise 0. Although Python(x,y) comes already with a great variety of scientic Python packages, we might have to install additional dependencies:

Measuring downside risk of stock returns with time-dependent volatility (Downside-Risikomessung für Aktien mit zeitabhängigen Volatilitäten)

DELTA Dashboards Visualise, Analyse and Monitor kdb+ Datasets with Delta Dashboards

ATTITUDE TO RISK. In this module we take a look at risk management and its importance. MODULE 5 INTRODUCTION PROGRAMME NOVEMBER 2012, EDITION 18

ALGORITHMIC TRADING USING MACHINE LEARNING TECH-

Transcription:

AN INTRODUCTION TO BACKTESTING WITH PYTHON AND PANDAS Michael Halls-Moore - QuantStart.com

WHAT S THIS TALK ABOUT? A talk of two halves! In the first half we talk about quantitative trading and backtesting from a theoretical point of view. In the second half we show how to use modern Python tools to implement a backtesting environment for a simple trading strategy.

QUANTITATIVE TRADING Creates a set of rules for trade order generation and risk management of positions with minimal subsequent manager intervention. Attempts to identify statistically significant and repeatable market behaviour that can be exploited to generate profits. Low-frequency (weekly, daily) through to high-frequency (seconds, milliseconds...) Carried out both at the retail level and at the large quantitative hedge funds.

TAXONOMY OF TRADING STRATEGIES Forecasting methods attempt to predict the direction or value of an instrument in subsequent future time periods based on certain historical factors. Mean Reversion trades on the deviation of a spread between two or more instruments. Utilises cointegration tests to ascertain mean reverting behaviour. Momentum or trend following. Trades on the basis of the slow diffusion of information (in direct contrast to Efficient Market Hypothesis). High Frequency Trading or HFT. Specifically referring to exploitation of sub-millisecond market microstructure. FPGAs, Infiniband networks, lots of dirty tricks!

WHAT IS BACKTESTING? A simulation designed to test the performance of a set of trading and risk management rules on historical data. Provides quantified performance of a strategy that can be used for comparison with other strategies. Outlines likely capital requirements, trade frequency and risk to a portfolio. Arguably a significant improvement beyond guessing!

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

DIFFERENT TYPES OF BACKTESTER Rapid prototyping Research Many strategies/parameters can be tested quickly. Identifying statistical relationships Vectorised (pandas, MatLab or R). Often unrealistic (inflated) performance Implementation Extensive development and testing time. Full Order Management System (OMS). Often event-driven or CEP. Code-reuse between live implementation and backtesting. More realistic performance.

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

PYTHON TOOLS FOR BACKTESTING 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. IbPy - Pythonic wrapper for Interactive Brokers proprietary market/order API. ZipLine - All-in-one Python backtesting framework powering Quantopian.com.

MOVING AVERAGE CROSSOVER The Hello World of quantitative trading! A very basic momentum strategy, but useful for calibrating backtesters. Strategy Rules: - 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

NOW PLEASE SHOW ME SOME PYTHON!

OBTAINING FREE FINANCIAL DATA Use the Quandl data service (www.quandl.com): $ pip install Quandl Easy to obtain daily financial market data (returns a pandas DataFrame): >>> import datetime >>> import pandas as pd >>> import Quandl >>> ibm = Quandl.get( GOOG/NYSE_IBM ) # Use Google Finance as data source Or with Yahoo Finance: >>> start_date = datetime.datetime(2009,1,1) >>> end_date = datetime.datetime(2014,1,1) >>> amzn = pd.io.data.datareader("amzn", "yahoo", start_date, end_date)

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. Example Strategy abstract base class: from abc import ABCMeta, abstractmethod class Strategy(object): metaclass = ABCMeta @abstractmethod def generate_signals(self): raise NotImplementedError("Should implement generate_signals()!")

MOVING AVERAGE CROSS IN PYTHON generate_signals creates a signals DataFrame used by the Portfolio 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

MARKET ON CLOSE PORTFOLIO class MarketOnClosePortfolio(Portfolio):.. def generate_positions(self): # Generate a pandas DataFrame to store quantity held at any bar timeframe positions = pd.dataframe(index=signals.index).fillna(0.0) positions[self.symbol] = 100 * signals['signal'] # Transact 100 shares on a signal return positions def backtest_portfolio(self): # Create a new DataFrame portfolio to store the market value of an open position portfolio = self.positions * self.bars['adj Close'] pos_diff = self.positions.diff() # Create a holdings Series that totals all open position market values # and a cash column that stores remaining cash in account portfolio['holdings'] = (self.positions*self.bars['adj Close']).sum(axis=1) portfolio['cash'] = self.initial_capital - (pos_diff*self.bars['adj Close']).sum(axis=1).cumsum() # Sum up the cash and holdings to create full account equity, then create the percentage returns portfolio['total'] = portfolio['cash'] + portfolio['holdings'] portfolio['returns'] = portfolio['total'].pct_change() return portfolio

TYING IT ALL TOGETHER Download the data, create the strategy, backtest the portfolio... if name == " main ": # Obtain daily bars of Amazon from Yahoo Finance # for the period 1st Jan 2009 to 1st Jan 2014 symbol = 'AMZN' bars = DataReader(symbol, "yahoo", datetime.datetime(2009,1,1), datetime.datetime(2014,1,1)) # Create a Moving Average Cross Strategy instance # with short and long moving average windows mac = MovingAverageCrossStrategy(symbol, bars, short_window=40, long_window=100) signals = mac.generate_signals() # Create a portfolio of AMZN, with $100,000 initial capital portfolio = MarketOnClosePortfolio(symbol, bars, signals, initial_capital=100000.0) returns = portfolio.backtest_portfolio() # Plot the performance with Matplotlib..

PERFORMANCE What next? - Calculate a Sharpe Ratio - Calculate a Maximum Drawdown - Many other metrics, e.g. - CAGR - Risk/Reward Ratios - Distribution of returns - Trade-level metrics - All very straightforward with pandas

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

WHERE CAN I FIND OUT MORE? Visit QuantStart to find the complete code and the slides from the talk: - http://www.quantstart.com/articles/my-talk-at-the-london-financial-python-user-group Make sure to investigate these fantastic free tools: - 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/ Email: mike@quantstart.com, Twitter: @mhallsmoore

THANK YOU!