Matlab / Markov - Matlab, R project and Python | futures io social day trading
futures io futures trading


Matlab / Markov
Updated: Views / Replies:8,127 / 39
Created: by mokodo Attachments:3

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 3  
 
Thread Tools Search this Thread
 

Matlab / Markov

  #11 (permalink)
 Vendor: www.indicatorx.com 
Bridgwater, UK
 
Futures Experience: Beginner
Platform: Ninjatrader
Broker/Data: MB Trading
Favorite Futures: Forex
 
mokodo's Avatar
 
Posts: 384 since Jun 2011
Thanks: 525 given, 344 received

Update

So I've been squeezing in what work I can on this project and I think I have something useful- or at least a starting point.

This post is about the HMM (Hidden Markov Model) I've worked up. The other side of this project was a simple probability matrix for sequences of highs of day, lows of day, double tops and double bottoms. That's finished (methodology detailed in my previous post).

I did break my first version. I was sampling some intraday data before it was available - price vs the extreme of price for the day. This is obviously only available once the day is over. So I have recoded the NT strategy that collects the source data so that states and emissions are sampled every 4 hours, at 00:00, 03:30, 07:30: 11:30, 15:30, 19:30.

There are 3 different states:

i) Is price greater than 0.1% than it was 4 hours ago (state 2)
ii) Is price less than 0.1% than it was 4hrs ago (state 1)
iii) Is price between these two boundaries, as it was 4 hours ago (state 3)

The price is sample from a 2 period 5min EMA in an endeavor to catch the meaningful shifts in the price without the spikes.

There are 6 different emissions:

Emissions are as they were before, 3 for movement below yesterday's mid line and 3 for movement above it:

i) Is price less than 0.8% below yesterdays mid line (emission 1)
ii) Is price between 0.8-0.4% below yesterday's mid line (emission 2)
iii) Is price between 0.4% below and yesterday's mid line (emission 3)
(these ranges are reversed for price above and make up emission 4,5,6)

Price is sampled as a 2 period 30 minute EMA.

The two files are then imported to Matlab and the HMM applied.

What I am trying to explore is whether there is a relationship between current price in relation to price 4hrs ago, in light to what has come over the previous n days.

The HMM model applies probability algorithms to generate the next most probable states based on the states and corresponding emissions that are fed in. Some code comes with the statistics toolbox and others I have hacked together.

My model does this:

Takes the first 480 data points for emissions (6 samples per day so 80 days worth of data) and applies the hmmestimate Matlab function. There are two outputs: a probability matrix (3x3) for the maximum likelihood estimate of each states transitioning to any other state. There is also a probability matrix (3x6) for the maximum likelihood estimate of each emissions being generated by each state.

The next step is to use the hmmtrain function to train on / estimate the transmission and emission probabilities for the original 480 data emission sequence. I feed the output from the hmmestimate in, so the model is training on the most up to date real position.

The last step is to run the Viterbi algorithm which generates the most likely state path through the HMM (using the probability matrices produced in the second step) based on the actual sequence of emissions. The output of this step also predicts the next most probable state.

The model loops through the entire data set (c.6000 points covering about 3.5 years) with a moving window of 480 data points and I harvest the next predicted state each time. The final output is a matrix the same size and shape as the input of original states but shifted back by one state, so that I can compare the actual state at a particularly sampling point with the prediction made 4hrs earlier.

I tested the steps and think it is sound, insofar that the code and model is doing what I intended and not generating erroneous output.

There is more work to do in trialling different lengths of window and initial parameters for the state and emission inputs. I have gone with a common sense approach to these first to see what popped out. I am also limited by computing power, a full run for a 3 state, 6 emission, 6000 data point model takes 1 hour and my development machine is no slouch.

I have completed a few runs and had a chance to feed back the Matlab output to an NT strategy where a state change to 2 goes long, state change to 1 goes short (ignores states 3 which is ranging). My initial comments on the hmm and the initial back tests are:

- Accuracy of any given data point being correctly predicted is no better (and no worse) than a random 33%.

- The output of states does not exactly resemble the distribution of the actual states, it has a slightly different character. The output states tend to come in waves where say a move from state 3 to 2 will bleed in as a trend up is predicted and then takes hold

- Based on the feature above a dominant 'regime' can very often be identified, which could be used as a bias overlay for intraday trading

- The output of predicted states does have a moving average feel to it and I will do some comparisons with an MA crossover to see if in fact the HMM model is any better. I have run a back test with the model pulled nearer by a further 4hours and the results were stellar, which i) gives me some confidence that the model is not just a random, but ii) led me to think it's just another MA.

- OOS performs as the in sample does.

My aim is not to end up with an automated strategy, just a way to use a bias filter for my few discretionary setups. So next steps are to keep get into the detail of the outputs, back test and find out if I can incorporate the state predictions in my trading.

Apologies in advance for not posting any code up (yet). I am wary that the method / code may be flawed and would be more comfortable working on it a while longer. However my intention is to post both the Matlab and NT files in due course so others can have a play too.

Hope to have some backtests stats and other findings available over the next few weeks.

know thyself
Reply With Quote
The following 2 users say Thank You to mokodo for this post:
 
  #12 (permalink)
Elite Member
Atkinson, NH USA
 
Futures Experience: Intermediate
Platform: NinjaTrader 8/TensorFlow
Broker/Data: NinjaTrader Brokerage
Favorite Futures: Futures, CL, ES, ZB
 
NJAMC's Avatar
 
Posts: 1,925 since Dec 2010
Thanks: 2,962 given, 2,294 received


mokodo View Post
My aim is not to end up with an automated strategy, just a way to use a bias filter for my few discretionary setups. So next steps are to keep get into the detail of the outputs, back test and find out if I can incorporate the state predictions in my trading.

Apologies in advance for not posting any code up (yet). I am wary that the method / code may be flawed and would be more comfortable working on it a while longer. However my intention is to post both the Matlab and NT files in due course so others can have a play too.

Hope to have some backtests stats and other findings available over the next few weeks.

Hi @mokodo,

I have been working with Accord .NET and might be able to get an On-line version of you logic running when you are ready. Accord has a HMM built in so you might be able to run the model within an indicator. Instructions on setting up Accord:

https://futures.io/elite-circle/24495-setting-up-accord-net-ninjatrader.html

Nil per os
-NJAMC [Generic Programmer]

LOM WIKI: NT-Local-Order-Manager-LOM-Guide
Artificial Bee Colony Optimization
Reply With Quote
The following user says Thank You to NJAMC for this post:
 
  #13 (permalink)
 Vendor: www.indicatorx.com 
Bridgwater, UK
 
Futures Experience: Beginner
Platform: Ninjatrader
Broker/Data: MB Trading
Favorite Futures: Forex
 
mokodo's Avatar
 
Posts: 384 since Jun 2011
Thanks: 525 given, 344 received

Thanks



NJAMC View Post
Hi @mokodo,

I have been working with Accord .NET and might be able to get an On-line version of you logic running when you are ready. Accord has a HMM built in so you might be able to run the model within an indicator. Instructions on setting up Accord:

https://futures.io/elite-circle/24495-setting-up-accord-net-ninjatrader.html

Just taken a quick look at this and it is open source awesomeness. Great signpost, thanks. If I can prove to myself that my model/concept contributes to profit, this integration via C# with NT is about as smooth at it could get.

know thyself
Reply With Quote
 
  #14 (permalink)
 Vendor: www.indicatorx.com 
Bridgwater, UK
 
Futures Experience: Beginner
Platform: Ninjatrader
Broker/Data: MB Trading
Favorite Futures: Forex
 
mokodo's Avatar
 
Posts: 384 since Jun 2011
Thanks: 525 given, 344 received

On Hold For Now

I've decided to put this project on hold for now. I can sense there is something in there but winkling it out is taking a lot of time - which I simply do not have spare right now. Plan to tidy up the code for Matlab and NT and post up in the new year to see if anyone wants to take it on.

know thyself
Reply With Quote
 
  #15 (permalink)
Elite Member
Atkinson, NH USA
 
Futures Experience: Intermediate
Platform: NinjaTrader 8/TensorFlow
Broker/Data: NinjaTrader Brokerage
Favorite Futures: Futures, CL, ES, ZB
 
NJAMC's Avatar
 
Posts: 1,925 since Dec 2010
Thanks: 2,962 given, 2,294 received


mokodo View Post
I've decided to put this project on hold for now. I can sense there is something in there but winkling it out is taking a lot of time - which I simply do not have spare right now. Plan to tidy up the code for Matlab and NT and post up in the new year to see if anyone wants to take it on.

I understand, I have a shortage of time right now as well. Best wishes, look forward to your progress on this topic. I believe there is some hope as well, just takes time and patients to get through the research side.

Nil per os
-NJAMC [Generic Programmer]

LOM WIKI: NT-Local-Order-Manager-LOM-Guide
Artificial Bee Colony Optimization
Reply With Quote
The following 2 users say Thank You to NJAMC for this post:
 
  #16 (permalink)
 Vendor: www.indicatorx.com 
Bridgwater, UK
 
Futures Experience: Beginner
Platform: Ninjatrader
Broker/Data: MB Trading
Favorite Futures: Forex
 
mokodo's Avatar
 
Posts: 384 since Jun 2011
Thanks: 525 given, 344 received

Hang On...

Well I was just about to put this project on the shelf, mostly due to lack of time (but I wasn't making the progress I thought I would, so was perhaps looking for an excuse), and I saw through one of the issues I was coming up against - and have identified a way around it.

The idea I am trying to investigate is that there are hidden patterns in price, which HMM's are supposed to be good at uncovering. My initial dead-end with my first raft of modelling was that the two variables I used were very closely correlated, both sampled price, simply at different time intervals. As a result my model generated something like a lagging moving average.

What I think I needed were two uncorrelated variables but which had strong co-integration, i.e. there is a strong relationship between them, but one that is not the same step by step. Google co-integration and there is a well used analogy of a drunk walking home with his dog, they both wander around, circle back, etc but not in unison. But they leave the same place and end up in the same and are closely related through out there journey although it looks pretty random at times.

So I have tested out using price as one variable and a bounded indicator (in my case Williams R) as the other. I chose WilliamsR as it's very reactive and as a consequence jumps around a fair bit (stochastics would also be an option for the same reason).

The model is very similar to the previous version, although now I have categorised WilliamsR between 0 -100 into 6 states and the model aims to predict the state for the indicator to be in. I've sampled hourly and have a run 40 day moving training window to return the next hour's prediction.

Maybe progress, but have to run tests to see what's what. Feels like I am on a more fruitful path though.

know thyself
Reply With Quote
The following user says Thank You to mokodo for this post:
 
  #17 (permalink)
Trading Apprentice
Lahore Pakistan
 
Futures Experience: Intermediate
Platform: NT
Favorite Futures: GOLD, oil , stocks
 
Posts: 40 since Aug 2012
Thanks: 12 given, 6 received


mokodo View Post
Well I was just about to put this project on the shelf, mostly due to lack of time (but I wasn't making the progress I thought I would, so was perhaps looking for an excuse), and I saw through one of the issues I was coming up against - and have identified a way around it.

The idea I am trying to investigate is that there are hidden patterns in price, which HMM's are supposed to be good at uncovering. My initial dead-end with my first raft of modelling was that the two variables I used were very closely correlated, both sampled price, simply at different time intervals. As a result my model generated something like a lagging moving average.

What I think I needed were two uncorrelated variables but which had strong co-integration, i.e. there is a strong relationship between them, but one that is not the same step by step. Google co-integration and there is a well used analogy of a drunk walking home with his dog, they both wander around, circle back, etc but not in unison. But they leave the same place and end up in the same and are closely related through out there journey although it looks pretty random at times.

So I have tested out using price as one variable and a bounded indicator (in my case Williams R) as the other. I chose WilliamsR as it's very reactive and as a consequence jumps around a fair bit (stochastics would also be an option for the same reason).

The model is very similar to the previous version, although now I have categorised WilliamsR between 0 -100 into 6 states and the model aims to predict the state for the indicator to be in. I've sampled hourly and have a run 40 day moving training window to return the next hour's prediction.

Maybe progress, but have to run tests to see what's what. Feels like I am on a more fruitful path though.


Every thing derived from price will have a correlation with it, they are not independently drawn , i.e the price and william's R. my suggestion In case try like or better experiment like. 100 possible states (rounded to 0 decimal places) and from that calculate the estimates of transition and emission probabilities . Better use RSI , as if your price remapped.

Reply With Quote
 
  #18 (permalink)
Elite Member
Atkinson, NH USA
 
Futures Experience: Intermediate
Platform: NinjaTrader 8/TensorFlow
Broker/Data: NinjaTrader Brokerage
Favorite Futures: Futures, CL, ES, ZB
 
NJAMC's Avatar
 
Posts: 1,925 since Dec 2010
Thanks: 2,962 given, 2,294 received


mokodo View Post
Well I was just about to put this project on the shelf, mostly due to lack of time (but I wasn't making the progress I thought I would, so was perhaps looking for an excuse), and I saw through one of the issues I was coming up against - and have identified a way around it.

The idea I am trying to investigate is that there are hidden patterns in price, which HMM's are supposed to be good at uncovering. My initial dead-end with my first raft of modelling was that the two variables I used were very closely correlated, both sampled price, simply at different time intervals. As a result my model generated something like a lagging moving average.

What I think I needed were two uncorrelated variables but which had strong co-integration, i.e. there is a strong relationship between them, but one that is not the same step by step. Google co-integration and there is a well used analogy of a drunk walking home with his dog, they both wander around, circle back, etc but not in unison. But they leave the same place and end up in the same and are closely related through out there journey although it looks pretty random at times.

So I have tested out using price as one variable and a bounded indicator (in my case Williams R) as the other. I chose WilliamsR as it's very reactive and as a consequence jumps around a fair bit (stochastics would also be an option for the same reason).

The model is very similar to the previous version, although now I have categorised WilliamsR between 0 -100 into 6 states and the model aims to predict the state for the indicator to be in. I've sampled hourly and have a run 40 day moving training window to return the next hour's prediction.

Maybe progress, but have to run tests to see what's what. Feels like I am on a more fruitful path though.

@mokodo,

I have experienced similar effects when dealing with SVMs. I need more than a few SMA's, I need those "othoginal" type of parameters. Too much of the same thing doesn't and much more knowledge to the system. Need to look at the propblem from different angles/perspectives.

Nil per os
-NJAMC [Generic Programmer]

LOM WIKI: NT-Local-Order-Manager-LOM-Guide
Artificial Bee Colony Optimization
Reply With Quote
 
  #19 (permalink)
Elite Member
Atkinson, NH USA
 
Futures Experience: Intermediate
Platform: NinjaTrader 8/TensorFlow
Broker/Data: NinjaTrader Brokerage
Favorite Futures: Futures, CL, ES, ZB
 
NJAMC's Avatar
 
Posts: 1,925 since Dec 2010
Thanks: 2,962 given, 2,294 received


asimsaim View Post
Every thing derived from price will have a correlation with it, they are not independently drawn , i.e the price and william's R. my suggestion In case try like or better experiment like. 100 possible states (rounded to 0 decimal places) and from that calculate the estimates of transition and emission probabilities . Better use RSI , as if your price remapped.

@asimsaim,

I would say, most every indicator is dependant upon price action and/or volume. They all need to be derived from the raw data available which is Level I/II data. Order book is still closely related to price action, you could consider it the cause (precedes price). What I have found from machine learning it digesting this limited data to "features" to present to machine learning systems. I believe this is what happens in the mind of the expert trader, they convert what they see into features and determine the future likelyhood of something occuring. I would say, most expert traders don't know they are doing this, it just happens as it is something they have conditioned their mind to do.

Nil per os
-NJAMC [Generic Programmer]

LOM WIKI: NT-Local-Order-Manager-LOM-Guide
Artificial Bee Colony Optimization
Reply With Quote
 
  #20 (permalink)
Trading Apprentice
Lahore Pakistan
 
Futures Experience: Intermediate
Platform: NT
Favorite Futures: GOLD, oil , stocks
 
Posts: 40 since Aug 2012
Thanks: 12 given, 6 received



NJAMC View Post
@asimsaim,

I would say, most every indicator is dependant upon price action and/or volume. They all need to be derived from the raw data available which is Level I/II data. Order book is still closely related to price action, you could consider it the cause (precedes price). What I have found from machine learning it digesting this limited data to "features" to present to machine learning systems. I believe this is what happens in the mind of the expert trader, they convert what they see into features and determine the future likelyhood of something occuring. I would say, most expert traders don't know they are doing this, it just happens as it is something they have conditioned their mind to do.

Orderbook only seems to me like ..... some guys waiting for their Limits / STops to be hit.. do you agree ?
Still its not the cause for price to follow the orderbook..
The best indicator is price action itself nodoubt about that.

Reply With Quote

Reply



futures io > > > > Matlab / Markov

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Linda Bradford Raschke: Reading The Tape

Elite only

Adam Grimes: TBA

Elite only

NinjaTrader: TBA

January

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
matlab and hidden markov model for stock market alchemist74 Matlab, R project and Python 3 November 9th, 2012 05:21 PM
matlab c# wh The Elite Circle 3 June 14th, 2011 09:22 PM
Rithmic and matlab tradersheldon Matlab, R project and Python 1 January 28th, 2011 01:16 PM
Matlab and ESignal MXASJ Matlab, R project and Python 3 July 29th, 2010 08:43 AM


All times are GMT -4. The time now is 08:48 PM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts
Page generated 2017-12-10 in 0.18 seconds with 20 queries on phoenix via your IP 54.83.122.227