13.08.2022 Views

advanced-algorithmic-trading

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

213

highly volatile state. Subsequent to 2011 the model reverts to switching between Regime #2 and

Regime #3.

It is clear that choosing the initial number of states to apply to a real returns stream is a

challenging problem. It will depend upon the asset class being utilised, how the trading for that

asset is carried out as well as the time period chosen.

14.4 Next Steps

In a later chapter the Hidden Markov Model will be used by a RiskManager subclass in QSTrader.

It will determine when to veto and close signals generated by a Strategy class in an attempt to

improve profitability over the case of no risk management.

14.5 Bibliographic Note

An overview of Markov Models (as well as their various categorisations), including Hidden Markov

Models (and algorithms to solve them), can be found in the introductory articles on Wikipedia[8],

[5], [7], [9], [6], [14], [18].

A highly detailed textbook mathematical overview of Hidden Markov Models, with applications

to speech recognition problems and the Google PageRank algorithm, can be found in

Murphy (2012)[71]. Bishop (2007)[22] covers similar ground to Murphy (2012), including the

derivation of the Maximum Likelihood Estimate (MLE) for the HMM as well as the Forward-

Backward and Viterbi Algorithms. The discussion concludes with Linear Dynamical Systems

and Particle Filters.

Regime detection has a long history in the quant blogosphere. Quantivity (2009, 2012)[83,

82, 84] replicates the research of Kritzman et al (2012)[65] using R to determine US equity

"regimes" via macroeconomic indicators. Slaff (2015)[93] applies the depmixS4 HMM library in

R to EUR/USD forex data to detect volatility regimes.

Systematic Investor (2012, 2015)[56, 57] initially uses simulated data and the RHmm package

in R to determine regime states, but then applies these methods to SPY using a rolling window

approach. A later post[58] reintroduces the methods using the depmixS4 package.

Gekkoquant (2014, 2015)[44, 43, 42, 45] provides a four-part series on applying HMM for

regime detection, using the RHmm package. The first two posts concentrate solely on the mathematics

of the model along with the derivation of the Viterbi algorithm. The third post considers

two approaches to using HMM: One HMM with each state representing a regime and another

with multiple HMM, one per regime. The final post applies this to a trend-following strategy,

ultimately leading to a Sharpe Ratio of 0.857.

Wiecki (2013)[100] presents a Guassian HMM in the Quantopian framework, although this is

not directly applied to a trading strategy.

14.6 Full Code

# Import the necessary packages and set

# random seed for replication consistency

install.packages(’depmixS4’)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!