Algorithmic trading. Automated trading in financial markets. Tutorial

CIFER 2014 Algorithmic trading Automated trading in financial markets Tutorial Aistis Raudys Vilnius University, Faculty of Mathematics and Informati...
Author: Nigel Thomas
0 downloads 0 Views 2MB Size
CIFER 2014

Algorithmic trading Automated trading in financial markets Tutorial Aistis Raudys Vilnius University, Faculty of Mathematics and Informatics Naugarduko st. 24, LT-03225 Vilnius, Lithuania [email protected]

2014 February, London, UK

Summary • Background • Algorithmic trading methods – Technical (Trend, Mean reversion, Seasonality) – Arbitrage – Statistical arbitrage (Statarb) – Fundamental – High frequency trading (HFT)

• Optimizations 2

Algorithmic/automated trading • Classic way – Analyst analyses the market and makes a decision to buy or sell some specific asset – Trader executes the trade

• Automated trading way – Analyst finds some reoccurring trading opportunities and codes the logic into the algorithm – Computer performs analysis and executes the trade 3

Algorithmic/automated trading • Computer program makes a decision: buys and sells without human intervention • Decision depends on – Human entered order (big order execution) – Single instrument market data (trend, TA, HFT) – Multiple instrument market data (statarb, trend) – Fundamental data (global macro, ratios) – News feed (event arbitrage) – Weather patterns ? Moon phases ? 4

Algorithmic trading Long term trend following

High frequency trading Trading frequency

5

Also known as • • • • • • • • • • • •

Algorithmic trading Automated trading Robot trading / trading robots Program trading Mechanical trading Systematic trading High frequency trading Low latency trading Ultra low latency trading Black-box trading Trading models Quant trading 6

Individual • • • • •

Trading strategy Trading system Trading robot Trading algorithm (algo) A model

7

Individual • • • • •

Trading strategy Trading system Trading robot Trading algorithm (algo) A model

?

8

Pros / Cons of Algorithmic Trading • Pros – Speed – can react quicker than human – Can be infinitely replicated – Emotionless – Will not quit or get sick – Can be tested using >20 years of historical data

• Cons – Cannot react to unknown changes – Is not able to see the big picture 9

People - Quants • • • • • • •

Mathematics Statistics Signal processing Game theory Machine learning Computer science Finance and Economics 10

Quant ?

11

Quants and Models

12

13

Strategy: One Moving Average ONE MA (9) Period = 9 14

15

Algorithmic trading examples • Execution of big orders • Technical analysis – Trend Following – Mean reversion or contra-trend – Chanel breakout

• HFT, Market making, scalping • Arbitrage

• Statistical arbitrage (pairs trading) • Seasonality • Fundamental analysis • Event arbitrage / news trading • Many unknown • Moon phases / weather

16

Arbitrage • • • •

Buy low in one place, sell high in other place Speed is the main factor Arbitrage opportunities exist for a very short time Arbitrage opportunities usually makes small profit – Hence big quantities

• I.E. Forex brokers, if one sells lower then other buys • I.E. same stock traded in US and EU (LSE: BP, NYSE: BP) • I.E. wheat traded in EU ir US – Keep in mind transportation, tax and other fees

• Moves liquidity from one market to another 17

Arbitrage: illustration Broker #1

Broker #2

sell

Price

Price

Price Profit

Price buy

Time

t

Time

t 18

Arbitrage: 2 way example • Suppose Walmart is selling the DVD of Shaft in Africa for $10. However, I know that on eBay the last 20 copies of Shaft in Africa on DVD have sold for between $25 and $30. Then I could go to Walmart, buy copies of the movie and turn around and sell them on eBay for a profit of $15 to $20 a DVD. It is unlikely that I will be able to make a profit in this manner for too long, as one of three things should happen: • •

1. Walmart runs out of copies of Shaft in Africa on DVD 2. Walmart raises the price on remaining copies as they've seen an increased demand for the movie • 3. The supply of Shaft in Africa DVDs skyrockets on eBay, which causes the price to fall.

19

Arbitrage: 3 way example • The basic formula for the relationship of three related currency pairs, having 3 different currencies, is as follows. •

AAA/BBB x CCC/AAA = CCC/BBB

• Chance of triangular arbitrage occurs whenever this equation goes wrong. A triangle arbitrator buys BBB spending AAA, then buys CCC spending BBB and lastly returns to AAA selling CCC, capturing a small profit. The chance of profit is maximized by utilizing margin from brokers and trading with higher amounts.

• For example take exchange rates EUR/USD = 0.6522, EUR/GBP = 1.3127 and USD/GBP = 2.0129. With $500,000 one can buy 326100 Euros, using that he can buy 248419.29 Pounds. He can now sell the pounds for $500043.19. Thus he can earn a profit of $43.19. 20

Long/Short equity (StatArb) • • • •

AKA “pairs trading” Buy one and sell short the other Can trade more than 2 instruments Mostly known for equities – Similar companies prices move together – Difference moves mean reverting

• Suitable to correlated instruments : (crude/heating oil corn/wheat) • Moves liquidity from one asset to another 21

Statistical Arbitrage sell

or or

Price

Stock 1 Stock 2

buy

Time 22

correlation: 0.7824 80 70

Cullen/frost Bankers Commerce Bancshares Inc

60 50 40 30 20 10

0 1992

1995

1997

2000

2002

2005

2007

2010

2012

2015

23

Mean reversion • Price moves around the mean • One instrument or spread between the two • If the price deviates from the mean too much take the reverse direction position • Similar to contra-trend type trading

24

sell sell sell

Price

me a

n buy

buy Time 25

Cullen/frost Bankers vs. Commerce Bancshares Inc 25 price diff 252d average 20

15

10

5

0

-5 2002

2004

2006

2008

2010

2012

2014 26

Index arbitrage • Stock index is composed of weighted stocks • Some stocks lead, some lag the index • If you can identify the lagger you can buy the stock and short the index • Or you can short the leader and buy the index

27

Index arbitrage: illustration sell

INDEX Lagger

sell

Price

Leader

buy

buy

Time 28

Volatility arbitrage • All comes to the fact that one can predict future volatility more accurately than others • Usually one trades an option and its underlier • Profit comes from the difference of implied volatility and realised volatility • Usually one has immunity to market movements

29

Market makers • • • • • •

A.k.a. specialists Always quotes bid and offer Supplying liquidity to the market Gets some advantages for their work Generates profit from small market moves Moves liquidity from one time to another

30

Market makers

Price

Investor 1 wants to sell MM buys from investor Investor 2 wants to buy sell MM sells to investor Ask buy

liquidity

Bid

sell

bid/ask spread

buy Time 31

32

Market maker

33

High frequency trading / scalping • Similar to market makers but no obligation always stay in the market • Position last sometimes only several sec/ms • Profit per trade very small & volume is huge • Sometimes profit is just liquidity rebate • Very valuable as has low risk • Problem - limited capacity • Very crowded now - profits are shrinking 34

Source: http://www.nanex.net/FlashCrash/CCircleDay.html

HFT 08-03-10 "Boston Shuffle". 1250 quotes in 2 seconds, cycling the ask price up 1 penny a quote for a 1.0 rise, then back down again in a single quote (and drop the bid size at that time for a few cycles).

35

36

Simple HFT logic in easylanguage • input: e(0),x(0); • var: tick(minmove/pricescale); • if marketposition = 0 then buy next bar close e*tick limit; • if marketposition = 1 then sell next bar close + x*tick limit;

37

38

39

Trend following /momentum • • • •

Classic technical analysis example Buy if prices are rising Sell short if prices are falling The idea is the anticipation that trend will continue for a while • The art is to detect the beginning of a trend as early as possible

40

Trend following sell

Price

sell

buy

buy Time 41

42

43

Counter-trend / Contra-trend • Classic technical analysis example • The idea behind is that market moves in waves or in a channel • A trading system must identify top of the wave/channel and take opposite position • Close after market goes back to trend

44

Counter-trend sell sell buy

Price

sell buy buy Time

45

46

tren

Price

Trend following and counter-trend can work at the same time

d

Time

• Both can work but on different time frames • For example: – Counter-trend on the daily basis – Trend following on 3 month basis

47

Channel breakout • A type of mean reversion • Most famous example is Bollinger bands • Around the price one draws a channel where prise sits most of the time • If price brakes out of the channel: – Take the trade in the same direction – Or take the trade in opposite direction

48

Bollinger

49

Bollinger

50

News trading • Thomson Reuters, Dow Jones, and Bloomberg provide news feed (some claim low latency) • Algorithm can analyse text, content, keywords stocks tickers and then buy or sell • Sources: Facebook, Twitter, Google, ... • Voice recognition from TV announcements • Speed matters a lot here ...

51

Sesonality • • • •

End of Year effect Harvesting effect Heating oil and heating season effect Political seasonality : 4 year US presidential election • Other seasonality – During the day – During the month

• End of tax year effect 52

Corn – yearly average

53

5

volume by business day in the year (in 9604 stocks)

x 10

actual 21d average

9.5 9 8.5

average volume

8 7.5 7 6.5 6 5.5 5 4.5 1

2

3

4

5

6

7 month

8

9

10

11

12

54

4

x 10

AAPL AIG ALTR AMAT AMD AMGN AMZN AXP BA BBBY BIIB

9 8 7 6 5 4 3 2 1 10:00

11:00

12:00

13:00

14:00

15:00

16:00

55

Fundamental analysis • Company accounts report analysis • Country economy analysis • Typical/classic investment type • If fundamental data is available then process can be automated • Reuters and Bloomberg and others provide fundamental numbers 56







• • •



1 . Discounted Cash Flow (DCF): While the concept behind discounted cash flow analysis is simple, its practical application can be a different matter. The premise of the discounted cash flow method is that the current value of a company is simply the present value of its future cash flows that are attributable to shareholders. Its calculation is as follows: If we know that a company will generate $50 per share in cash flow for shareholders every year into the future; we can calculate what this type of cash flow is worth today. This value is then compared to the current value of the company to determine whether the company is a good investment, based on it being undervalued or overvalued. 2. Ratio Analysis: Financial ratios are mathematical calculations using figures mainly from the financial statements, and they are used to gain an idea of a company’s valuation and financial performance. Each valuation ratio uses different measures in its calculations. For example, price-tobook compares the price per share to the company’s book value. The calculations produced by the valuation ratios are used to gain some understanding of the company’s value. Valuation ratios are also compared to the historical values of the ratio for the company, along with comparisons to competitors and the overall market itself. 3. Price- Earning Ratio (P/E Ratio): A valuation ratio of a company’s current share price compared to its per-share earnings. Calculated as: In general, a high P/E suggests that investors are expecting higher earnings growth in the future compared to companies with a lower P/E. It’s usually more useful to compare the P/E ratios of one company to other companies in the same industry, to the market in general or against the company’s own historical P/E. The P/E is sometimes referred to as the “multiple”, because it shows how much investors are willing to pay per dollar of earnings. If a company were currently trading at a multiple (P/E) of 20, the interpretation is that an investor is willing to pay $20 for $1 of current earnings.

57

Other Quant Application Areas • Another Big area is Pricing • Used mostly by major investment banks • Quant construct models to price options and other derivative instruments • Must do it right: credit default swaps, collateralized debt obligations and synthetic CDOs played major role in 2007-2008 crisis 58

Trading strategy optimization • Optimization is very important process in automated trading • a.k.a. calibration, tuning • For example: what periods of 2MA to use to maximize the profit ? • One needs to check different combinations to find the best ones • Brute force and Genetic are most popular • Simulated annealing and other heuristics

59

2 Moving average heatmap 0.6 10 0.4

20 30 40

0

50 60

-0.2

Sharpe Ratio

Slow period

0.2

70 80

-0.4

90 -0.6 100 20

40

60

Fast period

80

100

60

In sample / Out of Sample • • • • • •

The same as in pattern recognition Optimise your model on one set (older) Test performance on new/unseen data Results in out of sample are worse Optimization process is important The best result in sample is not the best in out of sample (aka past performance is not necessarily indicative of future results) 61

Typical Trap 7

x 10

in sample out of sample

5

profit

4

3

2

1

0

2004

2006

2008 time

2010

2012 62

Diversification r1, sharpe=1.6, dd=11

r1 + r2 + r3 + r4, sharpe=3.2537, dd=3.1

r2, sharpe=1.6, dd=10

40

20

40

35

30

30

20

25

0

10

20 -20

0

100

200

300

0

0

100

200

300

15 r3, sharpe=1.6, dd=9.2

r4, sharpe=1.6, dd=9.1

40

40

10 20

20

0

0

-20

0

100

200

300

-20

5 0

0

100

200

300

-5

0

50

100

150

200

250

300

63

Algorithmic trading funds • Altegris 40 Index – http://www.managedfutures.com

64

Algorithmic trading funds • Barclay Systematic Traders Index – http://www.barclayhedge.com

65

Literature • Trading and Exchanges: Market Microstructure for Practitioners by Larry Harris • Inside the Black Box: The Simple Truth About Quantitative Trading by Rishi K Narang • New Trading Systems and Methods New Trading Systems and Methods by Perry J. Kaufman • Options, Futures & Other Derivatives by JOHN C HULL • Algorithmic Trading: Winning Strategies and Their Rationale by Ernie Chan, 2013 • Tradeworx, Inc. Public Commentary on SEC Market Structure Concept Release, 2010 66

Thank You

67

68

Additional Slides

69

Metatrader

70

71

72

73

Deltix

74

Other vendos • • • • •

QuantHouse OpenQuant Ninja Trader Multicharts ...

75

Ultra Fast Databases • Fast data need to be processed fast • In memory databases are wildly used • CSQL - http://www.csqldb.com/ • SQLite – can work in memory rc = sqlite3_open(":memory:", &db); • Some commercial – TimesTen – ORACLE – kdb+ KX systems • Non SQL • Uses it own Q query language

– TimeScape - Xenomorph

• Many more 76

Aistis Raudys • • • • •

Msc in Computer Science PhD in Neural Networks Work experience in finance Now practitioner Now academic at Vilnius University, Lithuania – Lectures – Research

77

S&P MidCap 400

78

What can be automated • Can one program it? • Possible if – Structural data is available – Decision is clear

• Qualitative vs. Quantitative

79

May 6, 2010

80

Flash Crash • Sell of e-mini SP500 75000 contracts– 4.1 billion USD • Drives prices down • Arbitrages moved stock prices down • Market makers scared switched off systems • Liquidity dried • Avalanche effect • Some stocks traded at 1 cent  (PG) • Prices returned to previous level • All took several minutes 81

Flash Crash

82