58
Al ith i T di Algorithmic T rading By: Avi Thaker

Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Embed Size (px)

Citation preview

Page 1: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Al ith i T diAlgorithmic TradingBy: Avi Thaker

Page 2: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

What is Algorithmic Tradingg g

Th f l i l f The use of electronic platforms for entering trading orders with an algorithm which executes

d t di pre-programmed trading instructions whose variables may include timing, price, or quantity of the orderquantity of the order.

http://neverlosstrading.com/Algorithmic%20Trading.htmlp g g g

Page 3: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Motivation

Computers can process larger amounts of data than humans and make decisions faster than humans

Algorithms do what they are told, takes the human emotion out of trading

Trillions of $$$ traded daily - highly paid employees

Bleeding edge of sciences; math, engineering, computer science, etc.

Page 4: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Introduction: The Efficient Market Hypothesis

I ibl t “b t th k t” Impossible to “beat the market”

Market efficiency causes prices to incorporate and reflect all relevant information

Prices adjust quickly to new information

Prices should reflect all available information Prices should reflect all available information

Securities always trade at their fair value

The only way to obtain higher returns is to purchase riskier investments

Cannot predict trends history does not indicate future Cannot predict trends, history does not indicate future

Warren Buffett? Citadel? Two Sigma?

Are they lucky

The market is becoming increasingly more efficient The market is becoming increasingly more efficient

Page 5: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Index

DataCollection.jar DataCollection.jar

Python Pandas

Hello World – Algo Trading Hello World Algo Trading

Kinds of Trading Strategies

Backtestingg

System Architecture

Appendixpp

Page 6: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

DataCollection.jarj

Collects intra-day data from Google Finance

Writes data to current directory Writes data to current directory

Snapshots

Can collect multiple stocks

Type in ticker hit collect data Type in ticker, hit collect data

Works on Macs and PC’s with Java

Example Output (tab delimited)

127 35 4:00PM EDT 127.35 4:00PM EDT

Run me please? See how I work.

Page 7: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

A programming introduction

Page 8: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Python Pandasy

Developed for data manipulation and analysis by Wes Mckinney at AQR

Extremely fast vector driven manipulation

Dataframe manipulation, integrated indexing

Data alignment and integrated handling of Data alignment and integrated handling of missing data

Time series-functionality Date range generation Date range generation

Frequency conversion

Regressions, moving window statistics

And so much more! And so much more!

Page 9: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Pandas Example: Getting Historical Datap g

#pandas_example.py# Importsimport datetimeimport pandas as pdimport pandas.io.datafrom pandas import Series, DataFrameimport matplotlib.pyplot as pltimport matplotlib as mplmpl.rc('figure', figsize=(8, 7))

I am interactive(zoom into me)

# Get Dataaapl = pd.io.data.get_data_yahoo('AAPL', 

start=datetime.datetime(2006, 10, 1),end=datetime.datetime(2015, 4, 7) )

aapl.head()# Do some time series manipulation# Do some time series manipulationaapl['SMA50'] = pd.rolling_mean(aapl['Adj Close'],50)aapl['EMA50'] = pd.ewma(aapl['Adj Close'], 50)

# Plotplt.figure()plot(aapl.index, aapl['Adj Close'])p ot(aap de , aap [ dj C ose ])plot(aapl.index, aapl['SMA50'])plot(aapl.index, aapl['EMA50'])plt.legend(('ADJ Close', 'SMA50', 'EMA50'))

Page 10: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Pandas Example: DataFramep

Data Arranged In columns indexed b d tby date

Resembles Excel/R

Handles NaN data extremely well

Great for data manipulation

# view the first few elements aapl.head()

Open High Low Close Volume Adj Close SMA50 EMA50 Great for data manipulation

Common functions

Df.head()

Df.tail()

Open High Low Close Volume Adj Close SMA50 EMA50Date10/2/2006 75.1 75.87 74.3 74.86 178159800 10.09NaN 10.0910/3/2006 74.45 74.95 73.19 74.08 197677200 9.98NaN 10.0344610/4/2006 74.1 75.46 73.16 75.38 207270700 10.16NaN 10.0771410/5/2006 74.53 76.16 74.13 74.83 170970800 10.08NaN 10.07787 Df.tail()

Df.index()

Df.ffill()

Align and fill missing data

10/5/2006 74.53 76.16 74.13 74.83 170970800 10.08NaN 10.0778710/6/2006 74.42 75.04 73.81 74.22 116739700 10NaN 10.06168

Page 11: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Pandas Example: Data Visualizationp

df = pd.io.data.get_data_yahoo(symbols=['AAPL', 'GE', 'GOOG', 'IBM', 'KO', 'MSFT', 'PEP'])['Adj Close']

rets = df.pct_change()

a = plt.figure()

plt.scatter(rets.PEP, rets.KO)

plt.xlabel('Returns PEP')

plt.ylabel('Returns KO')

pd.scatter_matrix(rets, diagonal='kde', figsize=(10, 10));

Page 12: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Pandas Example: Data Visualization 2p

pd.scatter_matrix(rets, diagonal='kde', figsize=(10, 10));

Page 13: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Pandas Example: Data Visualization 3p

# Di l C l ti f t# Display a Correlation of returns

corr = rets.corr()

plt.figure()

plt.imshow(corr, cmap='hot', interpolation='none')

plt.colorbar()

lt ti k ( (l ( )) plt.xticks(range(len(corr)), corr.columns)

plt.yticks(range(len(corr)), corr.columns)

Page 14: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Pandas Example: Data Visualization 4p

plt.scatter(rets.mean(), rets.std())

plt.xlabel('Expected returns')

plt.ylabel('Risk')

for label, x, y in zip(rets.columns, rets.mean(), rets.std()):

plt.annotate(label, xy = (x, y), xytext = (20, -20),textcoords = 'offset points', ha = ' ight' 'b tt ' bb di t(b t l 'right', va = 'bottom', bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5), arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))

http://nbviewer.ipython.org/github/twiecki/financial-analysis-python-tutorial/blob/master/1.%20Pandas%20Basics.ipynb

Page 15: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Hello World – Algorithmic TradingBacktesting a Moving Average Crossover Strategy

http://www.quantstart.com/articles/Backtesting-a-Moving-Average-Crossover-in-Python-with-pandas, Zipline

Page 16: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Strategy: Moving Average Crossovergy g g

M t St t ( l t ) Momentum Strategy (more later)

Enter long, when longer MA crosses over the shorter faster MA

Enter short (or exit) when shorter faster Enter short (or exit), when shorter faster MA crossover longer MA

Believe that market will strongly move in one direction

Prone to whiplash

We will build and backtest this!

Page 17: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Getting Startedg Prerequisites

Python - 2.7+

NumPy

Pandas

Matplotlib

I suggest using pythonXY/Spyder – does everything for you

We will now create this strategy and run it on AAPL

Take ma_cross.py and backtest.py and place them into the same directory

Run the files

Change the Parameters to Short=20, Long=200, on ARMH from 2011-Now

Page 18: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Code Details and QuestionsQ

Only trades long, creates two simple moving averages

MovingAverageCrossStrategy is subclassed from Strategy MovingAverageCrossStrategy is subclassed from Strategy

MarketOnClosePortfolio is subclassed from Portfolio

What is misleading with the data we are using?

Hint look at AAPL’s price.

How can we improve on this code?

How do we measure strategy performance?

How can we tell if a strategy is good?

What are some other kinds of trading strategies?

Page 19: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Outputp

Page 20: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Python: Quantopiany Q p

Extremely Slow, but provides a decent backtesterbacktester

Direct Python interface to IB Live and Paper Trading

Hosts competitions if you win will be Hosts competitions, if you win, will be awarded with $100,000 to trade with All profits go to you

https://www.quantopian.com/open

Open source community https://www.quantopian.com/home

https://www.quantopian.com/postsp q p p

Page 21: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

A sample momentum algorithm from Quantopian Quantopian.

Very simple coding interface to use, and push to live trading with IB, or paper trade

This system can directly trade with your Interactive Brokers account

Must give privileges, have cash, etc.

Page 22: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Sample results from a paper trade, with statistics and an equity curve

Page 23: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Kinds of StrategiesA Brief Introduction

Page 24: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Terms

Depth of Market (DOM, Limit Order Book, Level 2) Best Bid – price to instantly sell Best Bid price to instantly sell Best Ask – price to instantly buy Bid Size

A k Si Ask Size Alpha

The abnormal rate of return in excess of what would be predicted by an equilibrium model like the capital asset pricing model (CAPM).

Tick Size – Interval between possible prices Slippage – Difference between signaled price and

trade price

Page 25: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

HFT vs Non HFT

HFT or High Frequency Trading, relies on quick algorithmic trading to capture small spreads or small g g p palpha (less than 1 minute holds)

Difficult for the retail investor to get into

Extremely competitive

Fastest usually takes all profits

Makes up 70%+ of all trading volume

Non HFT (trades of longer periods)( g p )

Easier for the retail investor to get into

Not as competitive

Smarter people make profits p p p

This is our focus

Page 26: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Strategy: Alpha Takinggy p g

Believes the market will

http://marcoagd.usuarios.rdc.puc-rio.br/revers.html

move in one direction Mean Reverting – price

reverts back to a “average” price, Bollinger Bands Stochastic Math

Trend following / Momentum Stockcharts com g price will move in one

direction

We tested/built one of these

Stockcharts.com

We tested/built one of these

Impulse response filters

Page 27: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Strategy: Market Makinggy g

Capture the Spread Bids Price Asks Increase Liquidity On both sides of the

market

100.03 2,1

100.02 3,7,8

100.01 5,2,15

Risky during times of volatility

Must be fast and have

100 1,2,5

1,2 99.99

2,5,8 99.98 Must be fast and have

good queue position Math is generally more

complicated

3,8,1,5,3 99.97

2,3 99.85

Lots of machine learning, think Bayesian complicatednetworks, and neural networksThe focus is on market microstructure

Page 28: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Strategy: Pair Tradinggy g

Assumes two contracts move together

Revert moves when one moves against the other

C b hi hl i k Can be highly risky 2007 Crash? Spreads blow up

Commonly used by Hedgefunds Commonly used by Hedgefunds High usage of Kalman Filters,

CADF (Co-integrated Augmented Dickey Fuller) tests, autocorrelation etc http://www.investopedia.com/university/autocorrelation, etc. p p y

guide-pairs-trading/pairs-trade-example.asp

Page 29: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Strategy Detail: Common Signalsgy gSimple Moving Average: SMA

Page 30: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Strategy Detail: Common Signalsgy gExponential Moving Average: EMA

Page 31: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Trading The EMAg

Enter Long: Close > EMA & Prev_Close > EMA_PrevEnter Short: Close < EMA & Prev_Close < EMA_Prev

Page 32: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Strategy Detail: Common Signalsgy gBollinger Bands

V l tilit B d Volatility Bands

Based upon standard deviation

Identifies points of reversion

Middl B d 50 D SMA Middle Band = 50-Day SMA

Upper Band = 50-Day SMA + 50-Day SD of Price

Lower Band = 50-Day SMA - 50-Day SD of Price

Page 33: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Example: Trading the Bollinger Bandsp g g

Page 34: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

More Examples / Technical Indicatorsp

T h i l i di t tl RSI Relative Strength Index Technical indicators are mostly useless on their own

Must identify something that

RSI Relative Strength Index

Parabolic SAR – Parabolic Stop and Reverse

Price Channels

VWAP – Volume Weighted Average Pricehappens in the market, and use the indicators (or come up with your own) to represent that something

VWAP Volume Weighted Average Price

ZigZag

MACD – Moving Average Convergence Divergence

PPO – Percentage Price Oscillatorsomething Data visualization is crucial Simplicity is usually better

PPO Percentage Price Oscillator

KST - Know Sure Thing

Ultimate Oscillator

Vortex Indicator

The list goes on forever … The list goes on forever

Page 35: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

BacktestingReally again? How much is there to backtesting?

Page 36: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Backtesting a Strategyg gy

Backtesting with Limit order Execution Simulate by placing limit orders Simulate by placing limit orders

Need to check for fills

Complex and requires time

Does not perfectly model slippage

Backtesting with Close execution Orders filled on close of bar Orders filled on close of bar

Subject to bid/ask bounce

Must subtract slippage numbers

More than 2 ticks? More than 2 ticks?

Event Driven

Page 37: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Limit Order BookBid Size Ask Size

Volume By Price

B t A k

Best Bid

Best Ask

http://www.trade2win.com/articles/1750-understanding-liquidity-market-pullbacks

Page 38: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Limit Order Execution

For an order to be executed a trade must cross below your buy or a trade happens at your priceFor an order to be executed, a trade must cross below your buy, or a trade happens at your price,when you have been filled in the queue

Page 39: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Limit Order Execution – Place Order

Bids Price Asks

100 03 2 1100.03 2,1

100.02 3,7,8

100.01 5,2,15

100 1,2,5100 1,2,5

1,2 99.99

2,5,8 99.98

3,8,1,5,3 99.97, , , ,

2,3 99.85

Place limit order of 2 lots order of 2 lots at 99.99

Page 40: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Limit Order Execution – Book Movement

Bids Price Asks

100 03 2 1100.03 2,1

100.02 3,7,8

100.01 5,2,15

100 1,2,5100 1,2,5

1,2,5 99.99

2,5,8 99.98

3,8,1,5,3 99.97, , , ,

2,3 99.85

Fill at 99.99, this becomes removed,

Another order is placed behind

People cancel their ordersbecomes removed,

and position advances. A trade happens

placed behind you

their orders

Page 41: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Limit Order Execution – Order Fill

Bids Price Asks

100 03 2 1100.03 2,1

100.02 3,7,8

100.01 5,2,15

100 1,2,5100 1,2,5

2,5 99.99

2 99.98

3,8,1,5,3 99.97, , , ,

2,3 99.85

After an order is filled you move up in the queue, until you either are filled or cancel the orderWe are now first in the queue

Page 42: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Backtesting a Strategy/Riskg gy Provide evidence of profitability

Curve fitting/ optimization bias

In-sample vs out-of-sample

Forward looking bias

Risk tolerance

Key StatisticsAverage wins :: 0.637 USDAverage loss    :: ‐0.438 USD# Wins :: 214# Wins          :: 214# Losses      :: 210# Neutrals    :: 3Win Rate  :: 0.501PPC :: 0.104USDPPC           :: 0.104USD# Traded      :: 427.0Ann. Sharpe   :: 2.335

Page 43: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Backtesting a Strategyg gy Does the strategy work across many assets?

How many years does it work for? Overall: 1980 2016Sharpe: 2 12

Does it escape the bid-ask bounce?

Risk Tolerance?

Maximum Drawdown?

Sharpe: 2.12PPC: 0.13Wins: 12634Losses: 10527Trades: 23666

Fees? Trading frequency?

Would you trade me?

Sharpe:1.299PPC:0.338Wins:255

In Sample: SPY 2004-2010Out of Sample: Assets Randomly Selected:ADBE XLNX BBBY CFN EMC ADP AFL DE T SPLS DG ADSALL MET CL PX WYN

Losses:202Trades:463.0

Page 44: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Biases and Pitfalls

These can be done unintentionally

Curve Fitting Bias Curve Fitting Bias

Adjusting/adding parameters until the strategy looks attractive in backtest

Forward looking bias

Program looks at future due to bug in code Program looks at future due to bug in code

Calculating optimal parameters, optimizations

Looking at the data!

Survivorship Biasp

Not including full universe (pre 2008 crash, 2007 algo trading blow up)

Psychological Bias Can you tolerate a 5 month drawdown? Lose half your portfolio

Your backtests will suggest possible severity

Page 45: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

System ArchitectureHow do I actually build an autonomous trading system?

Page 46: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

System Architecture Overviewy

Strategy BacktesterClient (IB)

RSI

Multi-Threaded

Can instantiate multiple strategies

C++, Java, Python

Communication RSI

Event Driven Backtester

Eliminates errors

Can use the same strategy for trading and

over interfaces

gbacktesting

Page 47: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Visualizing an Intraday Strategyg y gy

Exit Long

Entry Long

Page 48: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

General Tipsp This is not a get rich quick scheme

Finding alpha is hard, do not get discouraged

Drawdown are painful

Trust your alpha (if you have some)

Performance

Out of sample performance is generally ½ of in sample performance

Live trading performance is generally ¼ of in sample performance

Due to curve fitting, unexpected slippage, etc.

Make sure you account for transaction fees and slippage

Fun and exciting way to learn not only the markets but also computer science and math

Data is your friend

Build your own backtester/execution environment

Page 49: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Questions?Questions?

Page 50: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Further Readingsg

Best guide to starting algo trading (intro/backtester taken from here)

http://www.quantstart.com/p q

Execution Environment/Backtester/Community

https://www.quantopian.com/

Cheap trading platform with API

https://www.interactivebrokers.com/ind/en/main.php

Stellar documentation on how to do execution

Technical Analysis Library TA-Lib

http://ta-lib.org/

https://pypi.python.org/pypi/TA-Lib

Data:

Free: Yahoo Finance Google Finance error prone Free: Yahoo Finance, Google Finance – error prone

Cheap: Pi Trading, Kibot, Tickwrite

Page 51: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Appendix

Page 52: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Appendix: Candlestick/Bar Datapp

Open – price at start of bar p p

High – highest price

Low – lowest price

Close – price at end of bar

Volume – number traded during barbar

Can be on any timescale: seconds to monthly

htt // fi i l dhttp://www.financial-spread-betting.com/course/candle-stick-charting.html

Page 53: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Appendix: Order Sizingpp g

Page 54: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Appendix: Sharpe Ratiopp p

Page 55: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Appendix: Profit Per Contract (PPC)pp ( )

Page 56: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Appendix: CAPMppCapital Asset Pricing Model

Page 57: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Appendix: Machine Learningpp g

Neural Networks (with and without reinforcement), support vector machines, clustering, Bayesian networks…

G b g i G b g t Garbage in == Garbage out

You cannot expect this tool to give you good results from nothingg

You are data mining!

Be careful of over fitting data

Be sure to test in-sample out- Be sure to test, in sample, outof-sample and out-out-of-sample

Page 58: Al ith i T diAlgorithmic Trading - HMC Courses Taught by ...palmislandtraders.com/econ136/thakeralgotradings15.pdfType in ticker hit collect dataType in ticker, hit collect data Works

Appendix: RSIppRelative Strength Index