introduction to algorithmic trading strategies lecture 2 · 2015-08-14 · introduction to...

43
Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li [email protected] www.numericalmethod.com

Upload: others

Post on 14-Jan-2020

5 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Introduction to Algorithmic Trading StrategiesLecture 2

Hidden Markov Trading Model

Haksun [email protected]

www.numericalmethod.com

Page 2: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Outline Carry trade Momentum Valuation CAPM Markov chain Hidden Markov model

2

Page 3: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

References Algorithmic Trading: Hidden Markov Models on Foreign Exchange Data. Patrik Idvall, Conny Jonsson. University essay from Linköpingsuniversitet/Matematiska institutionen; Linköpingsuniversitet/Matematiska institutionen. 2008.

A tutorial on hidden Markov models and selected applications in speech recognition. Rabiner, L.R. Proceedings of the IEEE, vol 77 Issue 2, Feb 1989.

3

Page 4: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

FX Market FX is the largest and most liquid of all financial markets – multiple trillions a day.

FX is an OTC market, no central exchanges. The major players are: Central banks Investment and commercial banks Non‐bank financial institutions Commercial companies Retails

4

Page 5: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Electronic Markets Reuters EBS (Electronic Broking Service) Currenex FXCM FXall Hotspot Lava FX

5

Page 6: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Fees Brokerage Transaction, e.g., bid‐ask

6

Page 7: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Basic Strategies Carry trade Momentum Valuation

7

Page 8: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Carry Trade Capture the difference between the rates of two currencies.

Borrow a currency with low interest rate. Buy another currency with higher interest rate. Take leverage, e.g., 10:1. Risk: FX exchange rate goes against the trade. Popular trades: JPY vs. USD, USD vs. AUD Worked until 2008.

8

Page 9: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Momentum FX tends to trend. Long when it goes up. Short when it goes down.

Irrational traders Slow digestion of information among disparate participants

9

Page 10: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Purchasing Power Parity McDonald’s hamburger as a currency. The price of a burger in the USA = the price of a burger in Europe

E.g., USD1.25/burger = EUR1/burger EURUSD = 1.25

10

Page 11: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

FX Index Deutsche Bank Currency Return (DBCR) Index A combination of Carry trade Momentum Valuation

11

Page 12: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

CAPM Individual expected excess return is proportional to the market expected excess return.

,  are geometric returns is an arithmetic return

Sensitivity

,

12

Page 13: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Alpha Alpha is the excess return above the expected excess return.

For FX, we usually assume  .

13

Page 14: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Bayes Theorem Bayes theorem computes the posterior probability of a hypothesis H after evidence E is observed in terms of the prior probability,  the prior probability of E,  the conditional probability of  |

|

14

Page 15: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Markov Chain

s2: MEAN‐

REVERTING

s1: UP s3: DOWN

a22 = 0.2

a11 = 0.4 a33 = 0.5

a12 = 0.2

a21 = 0.3

a23 = 0.5

a32 = 0.25

a13 = 0.4

a31 = 0.25

15

Page 16: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Example: State Probability What is the probability of observing Ω , , ,

0.25 0.4 0.4

16

Page 17: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Markov Property Given the current information available at time 

, the history, e.g., path, is irrelevant.

Consistent with the weak form of the efficient market hypothesis.

17

Page 18: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Hidden Markov Chain Only observations are observable (duh). World states may not be known (hidden). We want to model the hidden states as a Markov Chain.

Two assumptions: Markov property | , ⋯ , , , ⋯ , |

18

Page 19: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Markov Chain

s2: MEAN‐

REVERTING

s1: UP s3: DOWN

a22 = ?

a11 = ? a33 = ?

a12 = ?

a21 = ?

a23 = ?

a32 = ?

a13 = ?

a31 = ?

19

Page 20: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Problems Likelihood Given the parameters, λ, and an observation sequence, Ω, compute  Ω| .

Decoding Given the parameters, λ, and an observation sequence, Ω, determine the best hidden sequence Q.

Learning Given an observation sequence, Ω, and HMM structure, learn λ.

20

Page 21: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Likelihood Solutions

21

Page 22: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Likelihood By Enumeration

But… this is not computationally feasible.

22

Page 23: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Forward Procedure

the probability of the partial observation sequence until time t and the system in state  at time t.

Initialization

: the conditional distribution of 

Induction ∑

Termination Ω| ∑ , the likelihood

23

Page 24: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Backward Procedure

the probability of the system in state  at time t, and the partial observations from then onward till time t

Initialization 1

Induction ∑

24

Page 25: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Decoding Solutions

25

Page 26: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Decoding Solutions Given the observations and model, the probability of the system in state  is:

, | |

|

26

Page 27: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Maximizing The Expected Number Of States

This determines the most likely state at every instant, t, without regard to the probability of occurrence of sequences of states.

27

Page 28: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Viterbi Algorithm The maximal probability of the system travelling these states and generating these observations:

28

Page 29: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Viterbi Algorithm Initialization

Recursion max

the probability of the most probable state sequence for the first t observations, ending in state j

= argmax the state chosen at t

Termination ∗ max ∗= argmax

29

Page 30: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Learning Solutions

30

Page 31: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

As A Maximization Problem Our objective is to find λ that maximizes  . For any given λ, we can compute  . Then solve a maximization problem. Algorithm: Nelder‐Mead.

31

Page 32: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Baum‐Welch the probability of being in state  at time  , and state 

at time  , given the model and the observation sequence

32

Page 33: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Xi

, , | |

|

33

Page 34: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Estimation Equation By summing up over time, ~ the number of times  is visited ~ the number of times the system goes from state  to state 

Thus, the parameters λ are: , initial state probabilities

∑ ,∑ , transition probabilities

∑ ,

∑ , conditional probabilities

34

Page 35: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Estimation Procedure Guess what λ is. Compute λ using the estimation equations. Practically, we can estimate the initial λ by Nelder‐Mead to get “closer” to the solution.

35

Page 36: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Conditional Probabilities Our formulation so far assumes discrete conditional probabilities.

The formulations that take other probability density functions are similar. But the computations are more complicated, and the solutions may not even be analytical, e.g., t‐distribution.

36

Page 37: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Heavy Tail Distributions t‐distribution Gaussian Mixture Model a weighted sum of Normal distributions

37

Page 38: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Trading Ideas Compute the next state. Compute the expected return. Long (short) when expected return > (<) 0. Long (short) when expected return > (<) c. c = the transaction costs

Any other ideas?

38

Page 39: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Experiment Setup EURUSD daily prices from 2003 to 2006. 6 unknown factors. Λ is estimated on a rolling basis. Evaluations: Hypothesis testing Sharpe ratio VaR Max drawdown alpha

39

Page 40: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Best Discrete Case

40

Page 41: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Best Continuous Case

41

Page 42: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

Results More data (the 6 factors) do not always help (esp. for the discrete case).

Parameters unstable.

42

Page 43: Introduction to Algorithmic Trading Strategies Lecture 2 · 2015-08-14 · Introduction to Algorithmic Trading Strategies Lecture 2 Hidden Markov Trading Model Haksun Li haksun.li@numericalmethod.com

TODOs How can we improve the HMM model(s)? Ideas?

43