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


Matlab / Markov
Updated: Views / Replies:8,147 / 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

  #1 (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

Matlab / Markov

Hi,

I'm having a play with MATLAB for the first time with the intention of pattern hunting.

Wondered if anyone has experience with Markov approaches using MATLAB. Initially keen just to make contact with anyone with relevant practical experience. Very much a side project that I am using to get familiar with the software /code.

Great to hear back from anyone active in this dark corner of futures.io (formerly BMT).

know thyself
Reply With Quote
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (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

A journey into Markov...


Thought I would document the side project I'm undertaking with Matlab / Markov. I've asked around this and other forums to see what work has been done by us little guys and have drawn a blank so far. Seems to be the scholarly, hedge fundy types that get their teeth into it.

A disclaimer: I am a total novice with this stuff, just driven by an inquiring mind and a little spare time.

So what's a nice guy like me doing in a (mental) place like this? Well I've figured out that I will not trade unless I have worked up a 'system' that I can empirically test. I will then trade it discretionary and more confidently than I would without putting in the hard miles of coding and testing. Without putting in the back testing groundwork my mind just gets too paranoid and the whole thing falls in on itself.

I got here because I am working up a system based on market structure/volatility. I have watched 15 min charts on forex pairs pretty much full time for 2 years and I can sense patterns in there. One of my main indicators is ADX and I can intuitively get close to predicting trend days and consolidation days in the context of the last few days PA. I also have been spotting repetitions in price extremes at key times of the day, London and US open for example (these are well known).

So my starting point is to work out whether a trade idea such as, go long off a double bottom in the Asian late and London open session can be safer knowing than the probability that today is more likely to be an up day.

So I was looking for a way to underpin the intuition with a decent bit of back testing and who knows maybe I'll go so far as putting a some trade on as result.

There are then two elements to the approach, i) profiling 'typical' price structure in the context of 24hrs of forex price action from UK midnight to midnight and ii) developing a hidden Markov model than can make a prediction on the direction of price for the day.

The first part is relatively straight forward. I split the day into the obvious (to me) trading session. Remember I am in the UK. Asian Late, London Open, Morning, US Open, Afternoon, Asian early. The sessions aim to have a similar amount of activity, so the London and US opens are short (2 hours or so), the morning and afternoon a little longer and the Asian sessions taking up around half the day from 19:00-006:30.

I then identify in an NT strategy in which sessions the low of the day (LOD), high of the day (HOD) occur and in which sessions any double tops / bottoms occur with LOD/HOD. That output can then easily be manipulate to provide a probability for price extremes as the day progresses. I figure that the same market participants get up, brush their teeth and play out the whole game day after day after day. So there will inevitably be routines in their trading decisions.

So that deals with the PA intra day day, what about the PA across days?

I've opted for using an Markov approach. I first became aware of this in a great book called 'Trading Regime Analysis - The Probability of Volatility' by Murray Gunn. Mr Gunn's idea is all systems work some of the time, the key is knowing when they will, which is down to volatility - you are either trending or range bound.

So my take on this is that there is some structure in the markets all rooted in human emotion and whilst the observations look random, they mask an underlying structure. Markov modeling deals exactly with this. And it does so based only on the last observation. In this way the approach is not like a (lagging) indicator. You only need to know the current state to predict the next state and you train you prediction on the window of the last n days to come up with a maximum likelihood estimate.

So for this part I have extracted two sets of data from my price series (I am using EURUSD). The first is the 'state' which I have determined as whether yesterday was an up day or a down down - as I am trying to work out if today is going to be an up or down day. The other set are the 'emissions', i.e. those things that we can see. For this I have chosen price as related to yesterday's mid price, 3 for the price being higher at advancing degrees and the same inversely for lower prices. Matlab has some built in function/algorithms that get to work on the relationship between the states and emissions and output the most probable next state and their confidence weights. In my case: is today predicted to be an up or down day and how confident is that prediction.

For back testing purposes the goal is to train the model on test data and then apply predictions of up/down days to out of sample data as a filter to, say, 'go long off a double bottom in the Asian late and London open session'.

And that is where I am at now. I have previously been bitten by the ogre of curve fitted strategies so I am skeptical of anything written in computer code. But this Markov stuff less vulnerable to those issues. And it's a whole lot of fun too.

So I have a draft of the model done and am just starting the grind of testing. I'll post up the preliminary output for anyone that is weird enough to care.

know thyself
Reply With Quote
The following 6 users say Thank You to mokodo for this post:
 
  #4 (permalink)
Elite Member
glostrup, denmark
 
Futures Experience: Advanced
Platform: Custom platform
Broker/Data: CQG
Favorite Futures: Futures, Options, Stocks
 
ktrader's Avatar
 
Posts: 249 since Aug 2011
Thanks: 151 given, 274 received


mokodo View Post
Thought I would document the side project I'm undertaking with Matlab / Markov. I've asked around this and other forums to see what work has been done by us little guys and have drawn a blank so far. Seems to be the scholarly, hedge fundy types that get their teeth into it.

Sounds like a very interesting project you are undertaking, I am looking forward to follow the progress here

Reply With Quote
 
  #5 (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

Other (non-trading) work has dominated my time over the last month so I haven't made as much progress as I had planned, but I have some developments to post up.

The intra day PA side of the strategy is done and I have started backtesting that. And yes a simple long of a double bottom registers as mildly profitable for my in sample data on EURUSD (April '07 - Jan '11).

The Markov model I drafted out in Matlab is great fun. Here's where I am with that.

I've settled for a three state system, determined by the daily range. State 1 is a down day with a close 45 pips or more below the open, state 2 is a range day with the close within 45 pips of the the open (either up or down), state 3 is an up day with the close more than 45 above the open. Why these settings? From the sample data this gave me a fairly good spread of days of each type. I have not run tests with any other settings, primarily to avoid curve fitting.

The emissions side of the model is sampled 6 times across each day. The emissions are the the distance from yesterday's mid price. There are 6 of these, 3 for increasing degrees above and in reverse for below. Those are numbered 1-6.

So a sample output may look like:

1 1 1 1 1 1 (this is a down day, so each of the 6 states are 1)
5 4 4 3 2 1 (pa during the day went lower through the day, and the emissions report that)

An output like the above is generated by the NT strategy and written to a text file which is made available to Matlab. In my sample I have a 860 x 6 matrix for the states, each row is a day and will be the same (either all 1s,2s or 3s); and an 860 x 6 row for the emissions where on each row there can be different numbers representing the 'emission' at each of the 6 intra day time slots.

So the variables in the Markov code in Matlab just determine at which point in the matrix to start training the model and at which point to stop. I tested on a fixed and moving window and the moving window performs better - and makes more sense to me. Too many or too few days and the model fails to capture the underlying structure. Training the model on 480 data points (80 days) provides a really stable result.

So the Matlab code I've hacked together trains on the first 80 days and then applies that model to the input data and outputs the most probable next state, it then shifts along by one data point and continues the process outputting the next most probable state until it reached the end of the sample. The output is written in to a separate file which can then be compared to what actually happen at each next point.

So the states which were predicted in the first time slot (Asian Late) which runs to 07.30GMT are shown below. The practical side of this is so I can consult the model at the beginning of my trading day and see what's it's predicting - up day, down day or range day.

The model predicted from the 860 day sample:

132 down days from 284 actual down days, 46.48% accuracy
197 range days from 324 actual range days, 60.18% accuracy
118 up days from 252 actual up days, 46.83% accuracy
127 days the model predicted a trend day but got the direction wrong, 23.69% all trend days

I also ran outputs of random 60 days sets and the std dev is very tight so I have some faith in the consistency.

My bench mark to compare these figures to would be random accuracy for a three output probability, i.e. 33%. So it's around 50% or more accurate than random on this first run for all three states.

Next step is to keep chugging out the tests to try and break it. I'll put it to work on other currency pairs as I have decent tick data going back to 07 for several of those and do the out of sample comparisons for EURUSD and then for these others. Days predicted to be up/down/range can be integrated in to NT backtesting code very easily.

I will reiterate a disclaimer here:
I am a novice and possibly do not know what I am doing. I am self taught with NT and Matlab coding and only have a year's experience with the former and about 6 weeks with the later. All these results may well just be the product of chance. I have no training with probability at all (although I do have a few books on probability and statistics). I would welcome anyone to ask questions who does know a thing or two about this stuff to blow my ideas and methodology up.

know thyself
Reply With Quote
The following user says Thank You to mokodo for this post:
 
  #6 (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

Hi @mokodo,

Looks like a great thread and you are off and running! I don't have any direct experience with Matlab and HMM but some self learning about Machine Learning. I will help where I can and hope to learn as you have a good connection to the market which I feel I am lacking in my efforts. For others interested in the detail of the Hidden Markov Model, Wikipedia has a good page:

Hidden Markov model - Wikipedia, the free encyclopedia

Also, in general as you learn things about Machine learning, I have a general thread:
https://futures.io/elite-circle/23861-machine-learning-ai-discussion-generic.html

Right now, I have some of the learning issues covered, Under/Over fitting of your solution/parameter space.

I look forward to watching your progress with this approach and how you apply your knowledge of the market into a Machine Learning application.

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:
 
  #7 (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

I found a similar thread and there are a few PDF docs I don't know if you saw:
https://futures.io/matlab-r-type/18053-matlab-hidden-markov-model-stock-market.html#post195083

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:
 
  #8 (permalink)
Elite Member
Milan (I)
 
Futures Experience: Advanced
Platform: Ninjatrader
Broker/Data: Kinetick
Favorite Futures: FDAX,6E,CL,YM,NQ,ES
 
redratsal's Avatar
 
Posts: 1,653 since Oct 2010
Thanks: 1,217 given, 2,068 received

Find attached the Markov's toolbox 4.0 latest version for Matlab (entirely compatible with GNU Octave freeware ), Documentation can be viewed insed the zip file via Matlab. I do use Matlab as a passive user, I am still in the learning process and can't help any further, you can compare it with your model

Attached Files
Register to download File Type: zip MDPtoolbox.zip (231.5 KB, 57 views)
Reply With Quote
The following 3 users say Thank You to redratsal for this post:
 
  #9 (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


NJAMC View Post
Hi @mokodo,

Looks like a great thread and you are off and running! I don't have any direct experience with Matlab and HMM but some self learning about Machine Learning. I will help where I can and hope to learn as you have a good connection to the market which I feel I am lacking in my efforts. For others interested in the detail of the Hidden Markov Model, Wikipedia has a good page:

Hidden Markov model - Wikipedia, the free encyclopedia

Also, in general as you learn things about Machine learning, I have a general thread:
https://futures.io/elite-circle/23861-machine-learning-ai-discussion-generic.html

Right now, I have some of the learning issues covered, Under/Over fitting of your solution/parameter space.

I look forward to watching your progress with this approach and how you apply your knowledge of the market into a Machine Learning application.

Thanks for the feedback, I was not following the thread, but now am


NJAMC View Post
I found a similar thread and there are a few PDF docs I don't know if you saw:
https://futures.io/matlab-r-type/18053-matlab-hidden-markov-model-stock-market.html#post195083

Yes, I have that one and there are a few more than I collated as I trawled the web. Once I have time to review them I'll drop in some links for others to get as confused as I did!


redratsal View Post
Find attached the Markov's toolbox 4.0 latest version for Matlab (entirely compatible with GNU Octave freeware ), Documentation can be viewed insed the zip file via Matlab. I do use Matlab as a passive user, I am still in the learning process and can't help any further, you can compare it with your model

Very many thanks for this. I was not aware of this toolbox and have had to hack some code together from samples at the Matlab file exchange. Not yet taken a look at it, but will soon. Again very many thanks for sharing.

know thyself
Reply With Quote
The following 2 users say Thank You to mokodo for this post:
 
  #10 (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
Thanks for the feedback, I was not following the thread, but now am



Yes, I have that one and there are a few more than I collated as I trawled the web. Once I have time to review them I'll drop in some links for others to get as confused as I did!



Very many thanks for this. I was not aware of this toolbox and have had to hack some code together from samples at the Matlab file exchange. Not yet taken a look at it, but will soon. Again very many thanks for sharing.

@mokodo,

Any progress, I am interested to watch your steps with the HMM.

Nil per os
-NJAMC [Generic Programmer]

LOM WIKI: NT-Local-Order-Manager-LOM-Guide
Artificial Bee Colony Optimization
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)

Jigsaw Trading: TBA

Elite only

FuturesTrader71: TBA

Elite only

NinjaTrader: TBA

Jan 18

RandBots: TBA

Jan 23

GFF Brokers & CME Group: Futures & Bitcoin

Elite only

Adam Grimes: TBA

Elite only

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 10:59 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-14 in 0.19 seconds with 20 queries on phoenix via your IP 54.90.207.75