Back to Posts
Listen to Thread

Example inspired by a scikit-learn example. I used this during my talk at the NYSE meetup earlier this week.

The idea is to predict hidden states in the daily price fluctuations and trading volume using a Hidden Markov Model (see the graphic). This model essentially assumes the existence of discrete hidden states. Each hidden state is associated with a certain probability of moving to another state at the next time point (thus, the current state is dependent on the previous one -- that's the Markov property). In addition, each hidden state is associated with emitting an observable event (in this case, fluctuation and volume) with a certain probability. This example tries to infer the hidden state transition probabilities, the observable events emitted, and then try to predict the hidden state of the current market.


Since we are continuously recomputing the HMM I set the previously learned means as a prior for the next model. So we are using the observed states we already learned for the next model.

Finally, this is so far just an analysis. Turning this into a trading strategy would require inferring what specific states mean and then place orders in response to that. This might not be quite as easy (you might want to look at the inferred means for this). But the Markov assumption that the current state depends on the previous will almost certainly be violated at least for the price fluctuations -- it is a well known fact that returns have almost 0 autocorrelation. Volume does, however, have autocorrelation so it might work better there. In addition, it's not clear that there are discrete states underneath. The state space could be continuous. If that's the case, a Kalman filter might be interesting to explore.

Clone Algorithm
Backtest from to with initial capital ( data)
Cumulative performance:
Algorithm Benchmark
Custom data:
Total Returns
Information Ratio
Benchmark Returns
Max Drawdown
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Information Ratio 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Sorry to revive an old thread, but is their a video accompanying the NYSE link? I would love to see it!

Hi Thomas, I agree with Brandon, it would be nice to look at the video, or at least to have access to the material of the presentations.

Hi guys,

Unfortunately there was no video recording of this talk. The notebook I used also has less descriptions than what can be found here. I'm happy to try and do a better job explaining what's going on (but note that there are good resources on HMMs online as well).

Related, Jess and I have been talking about how to extend this strategy so if something results from that we'll update it and hopefully provide a better description.


Log in to reply to this thread.
Not a member? Sign up!