Rellocation

My name is Daniel Korzekwa and I've been running this blog for a one year as Jessica Mong. I have good and bad news. The bad news is that Jessica won't write anything more and the good news is that I will continue her work as me under new location:
http://bettingexchangeresearch.blogspot.com/ I moved all posts from here to the new location, also there are already two posts not available here: I hope you will enjoy reading my blog, even though I'm not Jessica anymore:). Bye, Daniel

Tuesday, 21 April 2009

Trading at BetFair betting exchange with Apache SCXML

The main concept of trading is to buy at low price and to sell at high price, e.g. to buy/sell shares at a stock exchange. Similar idea is behind a betting exchange, but instead of shares issued by companies there are horse racing, tennis and football markets, instead of buying/selling shares, bets are placed. For example if two bets are placed on the same horse in a horse race, one bet at a lower price that the horse will lose and second bet at a higher price that the horse will win ,then whether or not a horse is a winner there is always a profit from these two bets.

How to trade at BetFair trading exchange?

At BetFair betting exchange (Figure 1) there are markets, e.g. tennis match or horse race. On each market there are runners, e.g. horses in a horse race. On runners the back/lay bets are placed. Back bet means that the runner will win, lay bet when it will lose. Bets are placed on a given price, which represents the chances of winning, e.g. price 2.0 is a 50% of chances (1/2=0.5), price 1.01 is a 99% of chances (1/1.01=0.99), 1000 is a 0.1% of chances (1/1000=0.001). For example when a back bet is placed for 3$ at price of 2 and horse wins, then the profit is (3*2)-3 = 3$.

Figure 1. Horse racing market with runners and prices (source: betfair.com).

On the same time the price to lay (pink color) is always higher than the price to back (blue color), and placing two bets (back and lay) on the same time never gives a profit. Example:

Bets on a first runner (Shemoli):
  • Back bet for 2$ at price 2.12
  • Lay bet for 2$ at price 2.14
When the runner is a winner then the profit/loss is: 2*(2.12-1) -2(2.14-1) = 2.24 - 2.28 = - 0.04
When the runner is a looser then the profit/loss is: -2 + 2 = 0

But firstly, when the back bet is placed, e.g, on a price 3.0 and some time later the lay bet is placed on a price 2.12 then the profit/loss is never negative. Example:

Bets on a first runner (Shemoli):
  • Back bet for 2$ at price 3.0
  • Lay bet for 2$ at price 2.14
When the runner is a winner then the profit/loss is: 2*(3-1) -2(2.14-1) = 4 - 2.28 = 1.72
When the runner is a looser then the profit/loss is: -2 + 2 = 0

The chart below (Figure 2) presents the price over the time for Shemoli runner. The key point of trading is to predict the trend of price. It allows to back on a high price and lay at a low price and to make a profit regardless of a market outcome.

Figure 2. Price chart for horse runner (source: betfair.com).

How I use Apache SCXML to trade at BetFair?

The simplest scenario of trading is to place two bets on the same runner, one back bet and one lay bet. It can be described by the following states and transitions between them (Figure 3):
  • No bets - When the runner price is increasing (price.slope>15), then a back bet is placed on it (placeBet betType='B' size=2 price=runner.priceToBack). The 'price.slope' parameter determines the angle of a price trend over last 15 minutes. When the angle is bigger than 0 then price is increasing, when is less than 0 then it is decreasing.
  • Back matched - When the best available price to lay on the runner is lower than the price that the back bet was matched on, then a lay bet is placed. It assures the profit regardless of a market outcome.
  • Lay matched - Back and lay bets are matched. Do not place any more bets on the runner.

Figure 3. Trading strategy (States machine) GUI editor.

My betting application allows to trade exactly in the same way as described above and usually it's a several minutes to prepare and execute new betting strategy:
  • Creata a state machine diagram using GUI editor.
  • Export state machine to SCXML file (see Figure 4) - SCXML is an xml format from W3C consortium, that describes a state machine. Because of a generic nature of SCXML, I extended it with many features, e.g. placeBet, cancelBet, variables used in conditions: runner.slope, runner.bwinPrice, runner.oddsCheckerPrice.
  • Import SCXML file to my betting application, where it's executed by betting engine based on Apache SCXML - Apache SCXML is a Java library to parse and execute state machines in a SCXML format.



















Figure 4. State machine for trading strategy in a SCXML format.

The presented example of state machine for betting strategy is very simple, however complex state machines can be created as well. The diagram below presents state machine for trading strategies on Soccer and Horse Racing markets.

Figure 4. State machine for trading strategies at Soccer and Horse Racing markets.

Conclusion

Adopting state machine to design and execute trading strategies is a powerful technique. First of all it's an easy and quick to design any kind of strategy and present it in a simple graphical format. More over it doesn't require any special skills and can be performed by trading specialist, who doesn't know to much about computers or programming languages. Also describing trading strategy as a state machine allows to prepare very useful reports, e.g. what is the profit/loss per states over the time. Such report provides information that is essential to eliminate wrong states and evolve toward better design. In the end I'd like to mention that state machine can evolve itself, empowered by Business Intelligence and Machine Learning techniques.

References

Bye,
Jessica

24 comments:

  1. Great article Jessica - really interesting stuff! Thanks for writing it up.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This is very impressive!
    How long did it take you to develop your application? Are you a professional developer?
    If you don't mind I have just a few questions:
    1. In your state machine definition you have the state transition condition - "price.slope gt 15" is "price" an field of the class which extends AbstractStateMachine? Linkewise is "runner" also a field of the same class?
    2. Also, it is possible for bets to be partially matched on Betfair, however in your state machine I don't see a contingency state for partial matches. Perhaps this isn't a problem as the bet is being placed at betfair's minimum stake of 2?
    Thanks!

    ReplyDelete
  4. Hi,

    Thanks for a good word and your questions. I wouldn't continue with my blog If no one read it:).

    1)Yes, I'm a professional developer.

    2)It doesn't extend AbstractStateMachine. All extra variables used in conditions and actions, e.g. runner.slope, market.isInPlay, etc., are passed in a SCXML context. More details at:
    http://commons.apache.org/scxml/guide/contexts-evaluators.html.

    3)Good point. Of course it is possible for bets to be partially matched. I presented a simple (ideal) state machine to not complicate my post and make it relatively easy to understand. But in a reality it's important to take into account partially matched bets. E.g. the placeBet action could be:
    placebet bettype="L" size="lastBetBack.sizeMatched" price="runner.priceToLay"

    Notice that the size is not fixed anymore.

    ...or even better:
    size="lastBetLay.sizeMatched*lastBetLay.avgPriceMatched)/runner.priceToLay"

    Feel free to ask more questions. I'm always eager to answer.

    Bye.
    Jessica

    ReplyDelete
  5. Hi Jessica
    Thanks for your response.
    I'll have a look at the apache scxml link.
    I think there may be a problem with the blogspot comment system when using Firefox and a google account to sign in. Maybe that's why less people have commented this time. I found I had to use internet explorer in order to post a comment. On your previous months posts I was able to use firefox.
    Thanks

    ReplyDelete
  6. Hi Jessica,

    Great blog! I have been writing my own automated Betfair trading application in C#, which uses a neural network to make its selections. I blog about it here Dynamic Notions.

    I'm really glad you are making money too, as I know how hard it is to get this stuff right.

    I'm I right in thinking that you monitor your markets and build up a price history for each selection, and then analyse this data?

    Keep up the good work.

    John

    ReplyDelete
  7. Hi John,

    I had a look at your blog and it's very interesting. I will put a link to your blog. It's very difficult to find some useful articles about betting/trading as 99% of them is a spam with a lot of adverts.

    Answering your question about building up a runner history:
    Yes, this is a one of the things I'm doing.

    ReplyDelete
  8. Hi Jessica,

    Your article is really interesting!

    When you calculating the "price slope", how do you get the historical price information? Are you analyzing the graph, or there is an other way to get the pure data?

    When you have the price data, by which algorithm do you calculate the slope?

    cheers,
    lajos

    ReplyDelete
  9. Hello Jessica,
    is it your own SCXML editor?

    ReplyDelete
  10. Hi Stefan,

    The SCXML editor is created by my friend. Currently it's for internal use only. It consists of two parts: generic SCXML editor + plugin for my betting application to support custom actions (placeBet, etc.) and custom context variables, e.g. price.slope.

    ReplyDelete
  11. Hi Jakab,

    I keep the history of prices for all runners that I analyze, then I use it for price prediction.

    To predict the price in a nearest future usually I use multiple linear regression (http://en.wikipedia.org/wiki/Linear_regression)

    ...but it also depends on market type and few other variables.

    ReplyDelete
  12. Hey Jessica,

    Nice article. Using a state machine is a nice way to separate the logic from the core functionality of downloading race data.

    I've written a Java app that auto-trades on Betfair, and I'm looking at refactoring my trade execution functionality to use the Specification Pattern to determine if a trade on a horse should be made.

    good luck!

    Aidan

    ReplyDelete
  13. Hi Jessica ... I've been with Betfair for about 8 years and in that time I've won some and lost less! Your blog has reminded me of some thoughts I had a few years back but was let down by a developer, they revolved around hitching up totally automatic betting systems based on form (and statistical) selections made by reference to various parameters with a bet placement engine. I would like to have a chat with you about them if you would be interested.

    ReplyDelete
  14. Hi Farmer,

    Contact me by email please.

    Bye.
    Jessica.

    ReplyDelete
  15. No more articles Jessica???

    ReplyDelete
  16. Hi

    My name is Emily Walker. I've just visited your website and I was wondering if you'd be interested in exchanging links with my website. I can offer you a HOME PAGE link back from 2 of my Gambling Guide websites which are:

    http://anthonygreenmusic.com/ with page rank 5
    http://itcontrolling.info/ with page rank 2

    If you are interested, please add the following information to your
    website and kindly let me know when it's ready. I'll do the same for
    you in less than 24 hours, otherwise you can delete my link from your
    site.

    Title: Horse Betting
    URL: http://www.freebettingonline.co.uk/Horse-Racing-Betting/
    Description: View all the latest horse racing information online.



    I hope you have a nice day and thank you for your time.

    Best regards;

    Emily Walker
    emily.walker@anthonygreenmusic.com
    Web Marketing Consultant

    ReplyDelete
  17. Interesting information. Many people aren't aware of betting exchange, let me explain you about it in simple words. Betting exchanges are emerging as the best sportsbetting sites because they offer better odds than traditional bookmakers and you don't see as much of your betting profit disappearing into the bookie's pockets.

    ReplyDelete
  18. One way to deal with State Machine complexsity is to seperate Data from Behavior. Examples for gaming can be viewed at State Machine Gallery
    www.StateSoft.org.

    ReplyDelete
  19. This comment has been removed by a blog administrator.

    ReplyDelete
  20. You have wonderful site I've seen. Thank you for letting me post in here. and more power.

    Football Betting Online is an online forum for sports.

    ReplyDelete
  21. I'm sorry but I don't get this.

    Considering your 1st example:

    #########################

    Bets on a first runner (Shemoli):
    Back bet for 2$ at price 2.12
    Lay bet for 2$ at price 2.14

    #########################

    The way I see it is the following:

    When the runner is a winner then your profit is: (2*2.12)-2= 2.24

    But you've also wasted $2 on the Lay bet, so your net profit is 2.24-2 = 0.24

    When the runner is a looser then your profit is: (2*2.14)-2=2.28

    But you've also wasted $2 on the Back bet, so your net profit is 2.28-2=0.28

    Am I missing something?

    Thanks

    ReplyDelete
  22. This comment has been removed by a blog administrator.

    ReplyDelete
  23. Hi Jessica/Dan,

    You have really good thoughts, I am also one of Betfair fan and I also have an automated betting application, it is not as advanced as yours as I am not a professional software developer but it is making money from last 18 months now, I now want to spend more time to make it even more profitable but I followed a different approach, I decide odds based on form factors rather then just price movement, I only bet on horse racing. I was never sure that only price movement can be used to make money, but price movement coupled by form analysis can really do wonders, I think I am in very early stages...Thanks for sharing your thoughts with others, I also wanted to start a blog but to be honest you have already shared most of information I wanted to share

    ReplyDelete
  24. Hi,
    The more data your trading decisions are based on the better. I started with simple price movements, but it was 2,3 years ago. Read my other posts at http://blog.danmachine.com/. Contact me in person by email or linkedin and I would be happy to chat with you.

    ReplyDelete