Master’s Thesis
Algorithmic Trading
Hidden Markov Models on Foreign Exchange Data
Patrik Idvall, Conny Jonsson
Master’s Thesis: 30 hp
Level: A
Supervisor: J. Blomvall,
Department of Mathematics, Link¨opings Universitet
Examiner: J. Blomvall,
Department of Mathematics, Link¨opings Universitet
Link¨oping: January 2008Contents
1 Introduction 1
1.1 The Foreign Exchange Market . . . . . . . . . . . . . . . . . . . . . 1
1.1.1 Market Structure . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Shifting to Electronic Markets . . . . . . . . . . . . . . . . . . . . . 3
1.2.1 Changes in the Foreign Exchange Market . . . . . . . . . . . 4
1.3 Algorithmic Trading . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.1 Different Levels of Automation . . . . . . . . . . . . . . . . 5
1.3.2 Market Microstructure . . . . . . . . . . . . . . . . . . . . . 6
1.3.3 Development of Algorithmic Trading . . . . . . . . . . . . . 7
1.4 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4.1 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4.2 Purpose Decomposition . . . . . . . . . . . . . . . . . . . . 8
1.4.3 Delimitations . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.4.4 Academic Contribution . . . . . . . . . . . . . . . . . . . . . 9
1.4.5 Disposal . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2 Theoretical Framework 11
2.1 Foreign Exchange Indices . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1.1 Alphas and Betas . . . . . . . . . . . . . . . . . . . . . . . . 12
2.2 Hidden Markov Models . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2.1 Hidden Markov Models used in Finance . . . . . . . . . . . . 13
2.2.2 Bayes Theorem . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.2.3 Markov Chains . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2.4 Extending the Markov Chain to a Hidden Markov Model . . . 16
2.2.5 Three Fundamental Problems . . . . . . . . . . . . . . . . . 17
2.2.6 Multivariate Data and Continuous Emission Probabilities . . . 26
2.3 Gaussian Mixture Models . . . . . . . . . . . . . . . . . . . . . . . . 26
2.3.1 Possibilities to More Advanced Trading Strategies . . . . . . 28
2.3.2 The Expectation Maximization Algorithm on Gaussian Mixtures 28
2.4 The ExponentiallyWeighted Expectation Maximization Algorithm . . 30
2.4.1 The Expectation Maximization Algorithm Revisited . . . . . 30
2.4.2 Updating the Algorithm . . . . . . . . . . . . . . . . . . . . 31
2.4.3 Choosing η . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.5 Monte Carlo Simulation . . . . . . . . . . . . . . . . . . . . . . . . 34
2.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Idvall, Jonsson, 2008. xiii
xiv Contents
3 Applying Hidden Markov Models on Foreign Exchange Data 37
3.1 Used Input Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.2 Number of States and Mixture Components and Time Window Lengths 38
3.3 Discretizing Continuous Data . . . . . . . . . . . . . . . . . . . . . . 38
3.4 Initial Parameter Estimation . . . . . . . . . . . . . . . . . . . . . . 39
3.5 Generating Different Trading Signals . . . . . . . . . . . . . . . . . . 40
3.5.1 Trading Signal in the Discrete Case . . . . . . . . . . . . . . 40
3.5.2 Standard Signal in the Continuous Case . . . . . . . . . . . . 41
3.5.3 Monte Carlo Simulation Signal in the Continuous Case . . . . 42
3.6 Variable Constraints and Modifications . . . . . . . . . . . . . . . . . 42
3.7 An Iterative Procedure . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.8 Evaluating the Model . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.8.1 Statistical Testing . . . . . . . . . . . . . . . . . . . . . . . . 43
3.8.2 Sharpe Ratio . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.8.3 Value at Risk . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.8.4 Maximum Drawdown . . . . . . . . . . . . . . . . . . . . . 45
3.8.5 A Comparative Beta Index . . . . . . . . . . . . . . . . . . . 46
4 Results 49
4.1 The Discrete Model . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
4.1.1 Using Only the Currency Cross as Input Data . . . . . . . . . 50
4.1.2 Adding Features to the Discrete Model . . . . . . . . . . . . 50
4.2 The Continuous Model . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.2.1 Prediction Using a Weighted Mean of Gaussian Mixtures . . . 54
4.2.2 Using Monte Carlo Simulation to Project the Distribution . . 55
4.3 Including the Spread . . . . . . . . . . . . . . . . . . . . . . . . . . 60
4.3.1 Filtering Trades . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.4 Log-likelihoods, Random Numbers and Convergence . . . . . . . . . 63
5 Analysis 67
5.1 Effects of Different Time Windows . . . . . . . . . . . . . . . . . . . 67
5.2 Hidden States and Gaussian Mixture Components . . . . . . . . . . . 68
5.3 Using Features as a Support . . . . . . . . . . . . . . . . . . . . . . 68
5.4 The Use of Different Trading Signals . . . . . . . . . . . . . . . . . . 69
5.5 Optimal Circumstances . . . . . . . . . . . . . . . . . . . . . . . . . 70
5.6 State Equivalence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
5.7 Risk Assessment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
5.8 Log-likelihood Sequence Convergence . . . . . . . . . . . . . . . . . 71
6 Conclusions 73
6.1 Too Many Factors Brings Instability . . . . . . . . . . . . . . . . . . 73
6.2 Further Research Areas . . . . . . . . . . . . . . . . . . . . . . . . . 74