Using Bayesian Statistics and Probability of Bar Direction

 

Hi

I have been interested in using Bayesian probability to try and predict the direction of the bar based on the direction of the bar before it.

If you apply bayesian probability then you might get a good idea of the likelihood of winning pips based on the opening position of the new bar and the previous bars behaviour.

The idea would be to enter on the start of the new bar in a higher timeframe such as H1 or H4.

Profit would be taken at the closing of the second bar or could in fact be left to roll for the next bar based on the Bayesian probability value.

I figure that there are 8 possible variations in behaviour from one bar to the next...

1 1st Bar Up 2nd bar open lower than first close and 2nd bar close lower than first close

2 1st Bar Up 2nd bar open lower than first close and 2nd bar close higher than first close

3 1st Bar Up 2nd bar open higher than first close and 2nd bar close lower than first close

4 1st Bar Up 2nd bar open higher than first close and 2nd bar close higher than first close

5 1st Bar Down 2nd bar open lower than first close and 2nd bar close lower than first close

6 1st Bar Down 2nd bar open lower than first close and 2nd bar close higher than first close

7 1st Bar Down 2nd bar open higher than first close and 2nd bar close lower than first close

8 1st Bar Down 2nd bar open higher than first close and 2nd bar close higher than first close

From a conditional probability perspective this list can easily be described by the following eight probability states (sorry if the above list does not match the following list line by line) ...

1 P(2U|(1U&2H)) or P(bar 2 up | (bar 1 is up AND bar 2 opens higher than the close of bar 1) )

2 P(2U|(1U&2L)) or P(bar 2 up | (bar 1 is up AND bar 2 opens lower than the close of bar 1) )

3 P(2U|(1D&2H)) or P(bar 2 up | (bar 1 is down AND bar 2 opens higher than the close of bar 1) )

4 P(2U|(1D&2L)) or P(bar 2 up | (bar 1 is down AND bar 2 opens lower than the close of bar 1) )

5 P(2D|(1U&2H)) or P(bar 2 down | (bar 1 is up AND bar 2 opens higher than the close of bar 1) )

6 P(2D|(1U&2L)) or P(bar 2 down | (bar 1 is up AND bar 2 opens lower than the close of bar 1) )

7 P(2D|(1D&2H)) or P(bar 2 down | (bar 1 is down AND bar 2 opens higher than the close of bar 1) )

8 P(2D|(1D&2L)) or P(bar 2 down | (bar 1 is down AND bar 2 opens lower than the close of bar 1) )

The first four states would equate to a win for the trader taking a long position.

The last four states would equate to a win for the trader taking a short position.

So if these probabilities for the eight states can be calculated then the trader will potentially have a better undertsnading of the behaviour for the newly opening bar given its open price in relation to the previous bars close and also the up or down nature of the previous bar.

 

Sooo continuing on a bit ...... if we set a period number of bars to look at so that we get a reasonable snapshot of the current markets statistical behaviour ( being non linear and dynamic in its nature) then we can count the frequency of the various events happening over PERIOD number of bars.

The relationship of every bar to the bar before it.

ie in pseudo code working backwards from the most recent bar...

for i = 0 to PERIOD step -1

(

if( CLOSE(bar (i+1)) > OPEN(bar(I+1)) ) /* Bar 1 Up

AND ( OPEN(bar(i)) > CLOSE(bar(i+1)) ) /* Bar 2 OPEN > Bar 1 CLOSE

AND ( CLOSE(bar(i)) > OPEN(bar(i)) ) /* Bar 2 CLOSE > Bar 2 OPEN

) then P(2U|(1U&2H)) = P(2U|(1U&2H)) +1

if( CLOSE(bar (i+1)) > OPEN(bar(I+1)) ) /* Bar 1 Up

AND ( OPEN(bar(i)) > CLOSE(bar(i+1)) ) /* Bar 2 OPEN > Bar 1 CLOSE

AND ( CLOSE(bar(i)) < OPEN(bar(i)) ) /* Bar 2 CLOSE < Bar 2 OPEN

) then P(2D|(1U&2H)) = P(2D|(1U&2H)) +1

if( CLOSE(bar (i+1)) > OPEN(bar(I+1)) ) /* Bar 1 Up

AND ( OPEN(bar(i)) < CLOSE(bar(i+1)) ) /* Bar 2 OPEN < Bar 1 CLOSE

AND ( CLOSE(bar(i)) > OPEN(bar(i)) ) /* Bar 2 CLOSE > Bar 2 OPEN

) then P(2U|(1U&2L)) = P(2U|(1U&2L)) +1

if( CLOSE(bar (i+1)) > OPEN(bar(I+1)) ) /* Bar 1 Up

AND ( OPEN(bar(i)) > CLOSE(bar(i+1)) ) /* Bar 2 OPEN < Bar 1 CLOSE

AND ( CLOSE(bar(i)) < OPEN(bar(i)) ) /* Bar 2 CLOSE < Bar 2 OPEN

) then P(2D|(1U&2L)) = P(2D|(1U&2L)) +1

if( CLOSE(bar (i+1)) < OPEN(bar(I+1)) ) /* Bar 1 Down

AND ( OPEN(bar(i)) > CLOSE(bar(i+1)) ) /* Bar 2 OPEN > Bar 1 CLOSE

AND ( CLOSE(bar(i)) > OPEN(bar(i)) ) /* Bar 2 CLOSE > Bar 2 OPEN

) then P(2U|(1D&2H)) = P(2U|(1D&2H)) +1

if( CLOSE(bar (i+1)) < OPEN(bar(I+1)) ) /* Bar 1 Down

AND ( OPEN(bar(i)) > CLOSE(bar(i+1)) ) /* Bar 2 OPEN > Bar 1 CLOSE

AND ( CLOSE(bar(i)) < OPEN(bar(i)) ) /* Bar 2 CLOSE < Bar 2 OPEN

) then P(2D|(1D&2H)) = P(2D|(1D&2H)) +1

if( CLOSE(bar (i+1)) < OPEN(bar(I+1)) ) /* Bar 1 Down

AND ( OPEN(bar(i)) < CLOSE(bar(i+1)) ) /* Bar 2 OPEN < Bar 1 CLOSE

AND ( CLOSE(bar(i)) > OPEN(bar(i)) ) /* Bar 2 CLOSE > Bar 2 OPEN

) then P(2U|(1D&2L)) = P(2U|(1D&2L)) +1

if( CLOSE(bar (i+1)) < OPEN(bar(I+1)) ) /* Bar 1 Down

AND ( OPEN(bar(i)) > CLOSE(bar(i+1)) ) /* Bar 2 OPEN < Bar 1 CLOSE

AND ( CLOSE(bar(i)) < OPEN(bar(i)) ) /* Bar 2 CLOSE < Bar 2 OPEN

) then P(2D|(1D&2L)) = P(2D|(1D&2L)) +1

)

Then you can work out the frequencies for each event by dividing the counts by the PERIOD value

 

On we go ....

OOOPS the frequency counts that were calculated in the previous pseudo code should be divided by PERIOD to get the base probabilities for each of the eight possible states.

I understand that coders out there are going to look at the pseudo code above and think how sub-optimal the code structure is but hey it makes it easy to understand... please be patient as better structured code will follow (I hope lol) in later posts as I develop this thread and subsequently the conditional probability maths a bit more.

So now we have a set of eight probability outcomes and an accurate figure on the probability of each state based on the previous PERIOD bars.

Applying these numbers to real trading ... it would be nice to know what the probability for an up bar or a down bar is likely to be .. and this can be calculated using the probabilities calculated for each state in the previous pseudo code.

ie

you notice that the last bar is UP and you are thinking of a BUY ...

therefore P(2U|1U) will be the sum of P(2U|(1U&2L)) + P(2U|(1U&2H))

This should be the probability of an UP bar for the current bar based on previous performance and given that the previous bar was UP.

you notice that the last bar is UP and you are thinking of a SELL ...

therefore P(2D|1U) will be the sum of P(2D|(1U&2L)) + P(2D|(1U&2H))

This should be the probability of an DOWN bar for the current bar based on previous performance and given that the previous bar was UP.

you notice that the last bar is DOWN and you are thinking of a BUY ...

therefore P(2U|1D) will be the sum of P(2U|(1D&2L)) + P(2U|(1D&2H))

This should be the probability of an UP bar for the current bar based on previous performance and given that the previous bar was DOWN.

you notice that the last bar is DOWN and you are thinking of a SELL ...

therefore P(2D|1D) will be the sum of P(2D|(1D&2L)) + P(2D|(1D&2H))

This should be the probability of an UP bar for the current bar based on previous performance and given that the previous bar was DOWN.

This can be further rationalised into some pseudo code based on whether you are looking to sell or buy.

I will post that code in the next post.

Cheers

mylorman

 

Hi Mylorman ,

Which software do you use for Bayes?

 
nevar:
Hi Mylorman , Which software do you use for Bayes?

Hiya Nevar ... I am not using anything other than MT4 and I guess the goal is to construct a probabilistic indicator that gives me reasonably improved odds on the higher time frames like the 4H and daily. I am just applying my grey and faded engineering student memories from years ago to see if I can come up with something different I guess.

Do you have any suggestions .. matlab or ??? btw if you have any suggestions on what to look at then feel free to contribute ... am ghonna get some code together to try and look for visual patterns .. or maybe this could be the basis of taking quant type trades based on probabilities ?>? dunno .. just interested to see where it goes and if it is at all useful

Cheers

Mylorman

 

Hi anyone that reads this

Onwards with this thinking ...

Ok so now we have counts and relative frequencies for each of the possible outcomes based on a two bar sample length we can start to think about what is the probability of an up bar or a down bar based on the observation of the open of the bar with relationship to the previous bars behaviour .

Bayes theorem is a good way to estimate the probability of an event happening based on an observation.

In this case our events are current Bar UP or current Bar Down and our observation is the open position of this bar relative to the behaviour of the last bar.

Bayes theorem states

P(B1/A) = P(B1)P(A/B1)

__________________________

SUM(i = 1 to n) of P(Bi)P(A/Bi)

what we are looking for is ...... P(UP) = P(UP/this bar OPEN is UP) + P(UP/this bar open is DOWN)

The only thing here is that I am not sure whether the bar open is up and the bar open is down states constitute two separate observations which would void the basic Bayes theorem or whether I can consider the state of this bar (UP or DOWN) as a single observation for the A term in the probability theorem above ?????

 

Ok I am going to assume that the observation of the open of the current bar is a single observation.

that means it is either up or down but nonetheless is a single observation.

the other option is to have two sets of probabilities ... one for each current bar open .. higher or lower ?

I will code and see what happens anyway

stay tuned and I will post some pics of the indicators progress .. if anyone is interested that is

 
I found your work interesting, I am currently studying bayes. Although I still understand little and the translation does not help I will try some things that you have proposed. If you've had any breakthrough I'd like to know.
Reason: