Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Trading Strategy Using Markov Chains

This is my first Quantopian algo, so be easy on me :).

The idea is to bin consecutive stock movements based on the price differences and to mark those frequencies in a probability transition matrix, where the Pij entry denotes the movement from state i to state j. The matrix is then divided by the total number of transitions to obtain a probability of moving states. If the rowsum, for j>i, is a decently large probability (aka the probability of it transitioning upwards, or stock price increasing), then buy shares, and vice versa.

Perceived errors:
I believe the main issue with this algorithm is the binning method that I chose in line 34. Instead of binning the stock movements based on the average, perhaps it would be better to bin it by dividing by the closing price or current stock price. The rowsums of the probability transition matrix don't appear to sum of 1. I also did not compute any μ values because I did not know how to utilize them, as used in some identity like Pij * μ = μ. Would implement a bigger matrix if the run time wasn't so slow as is.

Any feedback or helpful insight would be greatly appreciated.

2 responses

Hi Daniel,

Welcome to Quantopian and thanks for sharing -- intriguing algorithm. At the core it seems to be a pattern detection algorithm. A couple of suggestions:

  • Implementing Markov Chains by hand is tricky business because of the normalization, as you note.
  • It seems you are just looking at each stock individually. It would be cool to infer one state for all stocks.
  • Here is an example of using a Hidden Markov Model to infer latent states. The problem with that algo is that it's not clear what the states actually mean. I like that your algorithm directly associates meaning to each state which allows you to advise a trading strategy on top of it.

Anyway, just some thoughts. Would be curious to learn if you had any other ideas to improve the strategy?

Thomas

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Hmm, I would have to do some research on that hidden markov model after I bang out my financial math midterm, seems interesting. I wasn't quite sure exactly how to normalize it/what it does, or how to even code it for that matter, so i sort of decided to go off on my own tangent and try a strategy that bought some shares if the probability of increasing states was reasonable based on the matrix.

I'm still thinking about how to improve the algorithm aside from fixing the binning/buying shares part. There may be an error somewhere because -100% returns seems a little farfetched lol. I would say my main intention of the algorithm was more of getting the hang of Quantopian though rather than having a beastly strategy, although positive returns wouldn't hurt :P