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.


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.