Tuesday, 29 December 2009

Time to change - Moving from Java to Scala

The end of the year is a good time to look backwards and ask yourself a few questions. The question I'm asking myself every year is whether or not it's a good moment to change my core programming language. The last time I answered 'yes' was eight years ago.

That time I was working for a software company where the lion's share of software code was written in C/C++ and nobody wanted to hear about Java. There were two major reasons for that, first of all, people generally were against any changes, secondly almost everyone used to think that Java is slow and not appropriate programming language for developing large scale and distributed computer systems. Finally choosing Java for developing next-gen company products was a bull's eye shot.

Now, eight years later, I feel again that it's a good moment to go forward. This time I'm moving from Java to Scala. In reference to scala-lang.org:
Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages, enabling Java and other programmers to be more productive. Code sizes are typically reduced by a factor of two to three when compared to an equivalent Java application [1]
A good way to present the difference between two programming languages is to show the same code written in both of them. The code below presents following steps:
  1. Create list of three decimal prices [2].
  2. Convert prices to probabilities, probability = 1/price.
  3. Print all probabilities to the console.
Scala code:
//Convert prices to probabilities
//and display them to the console
object PriceToProbability extends Application {
val prices = List(2d,3d,4d)
val probabilities = prices.map(s => 1/s)

Java code:
//Convert prices to probabilities
//and display them to the console
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PriceToProbability {

public static void main(String[] args) {

List prices = Arrays.asList(2d,3d,4d);
List probabilities = new ArrayList();

for(Double price: prices) {

for(Double probability: probabilities) {

Nice, isn't it? Although it's a very simple example, it illustrates how neat and concise Scala could be. Is a Scala for you? it depends on the business domain that you want to apply it for. My domain is mostly represented by risk management and algorithmic trading and many aspects of Scala fit there perfectly. For example functional programming is very useful for implementing various calculations and mathematical algorithms.

For those, who want to learn more about Scala, I'd like to recommend very good book, actually one of the best books I read this year. Despite the fact it's a technical book I would compare it to reading Harry Potter. Simple language, easy to understand, short chapters and can't stop reading it. Here is the link: Programming in Scala by Martin Odersky


Friday, 4 December 2009

Bio heat map of the Barclays Premier League - Winner 2009/10

The picture below presents a bio heat map of a betting exchange market.

Market: Barclays Premier League - Winner 2009/10 market.
Time: 04 December 2009 19:51

Function: tv = f(r,p,t)
  • tv - total traded volume on the given runner in a particular market
  • r - market runner
  • p - price
  • t - time
Color - green to black to red
  • green - 0 traded volume
  • black - middle between min and max
  • red - max traded volume

Sunday, 29 November 2009

Bio heat map of a betting market

It's a beginning of my research on a lifetime of a betting exchange market and my preliminary goal is to underatand how market grows and evolves during its lifetime.

One of my first thoughts was to present a market in a form suitable for analysis using a sense of sight and image recognition. In the first instance I decided to try a bio heat map that is commonly used in a science to present gene, weather or biological maps.

The picture below presents a bio heat map for a betting exchange market: tv = f(r,p,t)
  • tv - total traded volume on the given runner in a particular market
  • r - market runner
  • p - probability
  • t - time
Color - green to black to red
  • green - 0 traded volume
  • black - middle between min and max
  • red - max traded volume

Having that, I'm going to record a lifetime of a market and replay it with an increased speed and make some initial analyses.

Wednesday, 26 August 2009

AndroBet - The Betting Exchange simulation game

Andro Bet is a betting exchange simulation game. The aim is to make the highest profit by backing and laying the same selection in the market. Game starts with the screen presenting market view for a single selection that player can place bets on. After 3 minutes of the game the runner outcome (winner or loser) is calculated and the profit&loss is displayed on the screen.

On top of market view there is a status bar. On the left side of a status bar there is a profit for outcomes (winner and loser) that is based on all player's matched bets. On the right side of a status bar there is a remaining time of a game. In the centre of a market view there is a ladder price widget. The middle column contains all unmatched bets for all users on a betting exchange. Unmatched back bets are on the blue background, whereas unmatched lay bets are on the pink background. Each cell contains two numbers, on top of a cell there is a price, whereas on the bottom of a cell there is total value of unmatched bets for all users on this price. The left and right columns present all unmatched back and lay bets placed by a player. The player's unmatched back bets are in the left column, whereas the player's unmatched lay bets are in the right column. When the player’s bet is matched then it disappears from the cell.

Player may place back and lay bets, but once bet is placed, it can’t be cancelled. Bets are placed by tapping the cells, in the left and right columns of a market view. The size of one bet is 2, whereas the price is determined by the row tapped by a player. To place a back bet, tap the cell in the left column. To place a lay bet, tap the cell in the right column. The left and right columns display only player’s bets that remain unmatched.

After game is finished the result is presented to a player. It contains starting price that is used to calculate selection’s outcome and the player’s profit.


AndroBet on Google Code: http://code.google.com/p/androbet/

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.



Saturday, 14 March 2009

Automated betting with BetFair, Business Intelligence and Eclipse BIRT

When you place a single bet on a tennis match (market), you need to decide, which tennis player (runner) to bet on. Usually it’s based on your intelligence. By your intelligence I mean experience, knowledge, common sense, etc. When you use automated betting, then you place a lot of bets every day and the decision process becomes much more complicated. I this case you need something more than your intelligence, what you need is a Business Intelligence (BI).

Regarding to Wikipedia, Business Intelligence (BI) refers to skills, technologies, applications and practices used to help a business acquire a better understanding of its commercial context. In my betting application I use Business Intelligence for:
  • Historical data analysis – e.g. create reports/charts to show profit for sports/regions over the time.
  • Data mining – e.g. to recognize some particular situations when I win or lose.
  • Automated decision making system - based on evolutionary algorithms, e.g. eliminate wrong betting strategies during evolution process.
In this post I describe how I analyse historical data using Eclipse Business Intelligence and Reports Tools (BIRT) technology. Eclipse BIRT is an open source Java project from IBM and it’s a powerful tool, especially to create custom multi dimensional reports and charts. It provides easy to use GUI designer to create and run reports. The GUI designer can be used as a stand alone application or a plugin in Eclipse IDE. Also a web based reports viewer is provided, which in my opinion isn’t very good. Finally there is a very well documented API, that allows to integrate BIRT charts and reports into a thirt-party applications.

The examples below present BIRT chart and table report integrated into web console of my betting application.

Figure 1 Profit per week over the time.

Figure 2 Profit per sport/region over the time.

How I use Eclipse BIRT?

In my betting application BIRT reports and charts are integrated into a web console, which is based on a Wicket framework. The figure 3 presents all components involved in designing, generating and presenting reports to the user.

Figure 3 Design of Birt reports.

Diagram details:
  • ReportsViewer - Allows to choose report and values for report parameters and displays report to the user as html page. List of available reports is obtained from reports directory on a file system.
  • Reports files - BIRT reports created by reports designer.
  • Reports Designer - GUI reports designer provided by Eclipse BIRT (see Figure 4). It's a powerful tool and allows to create almost all types of reports and charts you can ever think about. The best is that it can be used by a non technical person, e.g. business intelligence specialist. Also it is possible to parametrize reports, e.g. create report to show profit per sport/report with two input parameters: sport and region.
  • BirtEngine - Generates report in a html/pdf format based on a report file and reports data from data source, e.g. from database.
  • Database - Repository of betting history, e.g. account statement, markets, bets, prices.

Figure 4 BIRT Designer.


Business Intelligence is a crucial element of every automated betting application. Correctly used can significantly improve performance of betting. Moreover, it discovers new horizons and ways of betting hidden behind a huge amount of historical data.



Thursday, 26 February 2009

BetFair Java API with Apache CXF, Spring and Maven

In this post I present how to login and place a bet on BetFair in Java. Betfair provides a webservice API to get events, markets, place a bet and do many other things. There are several ways to use this API in Java but personally I prefer one of the easiest and quickest of them, which is based on Apache CXF technology.

Apache CXF is a professional open-source Java library to create webservice clients and servers. It generates a webservice java client automatically from a wsdl file, that works very well with BetFair API. Thus calling most of a BetFair operations such a login, getEvents, placeBet is very simple and is just a couple of lines.

The diagram below presents how BetfairService is designed and used in my betting application:

Diagram details:
  • BFGlobalService/BFExchangeService - Betfair webservices generated by Apache CXF. GlobalService contains login operation and others, which don't require logging in. Whereas ExchangeService allows to get market data, place bets, get account information and others, which require user to be logged in.
  • BettingServiceImpl - BetFair Facade, that simplifies access to the BetFair operations from a BettingEngine.
  • BettingEngine - Module of my application responsible for executing betting strategies.
How to login to a Betfair account?

The login operation is provided by a BFGlobalService. To login to a Betfair account three parameters are required: user, password and productId. For Betfair free API the productId is 82.
//Login to a BetFair account.
public void login(String userName, String password, int productId) {

//Creating login command which contains all login parameters.
LoginReq req = new LoginReq();

//Calling BetFair webservice login operaton.
LoginResp resp = globalWebService.login(req);

//Checking login result and storing session token, which is necessary to call BFExchangeService operations, e.g. getMarkets, placeBet.
if (resp.getErrorCode().equals(LoginErrorEnum.OK))
state.setSessionToken(resp.getHeader(). getSessionToken());
} else {
throw new BetFairException("Login error. Error code: " + resp.getErrorCode() + ", api code: "
+ resp.getHeader().getErrorCode());

How to place a bet?

The placeBet operation is provided by a BFExchangeService. There are many different bet types available. This example show how to place a back/lay bet on a betting exchange. The code snippet below presents a placeBet method with parameters:
  • marketId/selectionId - Which market runner the bet is placed on, e.g. draw in a football match, tennis player in a tennis match or horse in a horse race.
  • betType - Back or lay , e.g. back bet means that something will happen, e.g. draw in a football match, lay means that it will not happen, e.g. tennis player will not win the match.
  • betCategory - Exchange or SP(starting price) bet.
  • price - Binary odds of a bet, e.g. 2.0
  • size - Stake of a bet, e.g. 20 dollars.
//Place a bet on a BetFair
//betting exchange.
public void placeBet(int marketId, int selectionId, BetTypeEnum betType, BetCategoryTypeEnum betCategory,
double price, double size) {

//Creating request header with a session token returned by login operation.
APIRequestHeader requestHeader = new APIRequestHeader();

//Creating placeBet command, which contains all required parameters.
PlaceBetsReq req = new PlaceBetsReq();

ArrayOfPlaceBets arrayOfBets = new ArrayOfPlaceBets();
PlaceBets placeBets = new PlaceBets();
placeBets.setBetPersistenceType( BetPersistenceTypeEnum.NONE);

//Calling placeBet operation on a BetFair webservice.
PlaceBetsResp resp = exchangeWebService.placeBets(req);
if (resp.getErrorCode().equals(...) {
//Checking placing bet result.

How to generate BFGlobal and BFExchange services with Maven and cxf-codegen-plugin?

Because my betting tool uses a Maven build system, I use a cxf-codegen-plugin to generate java classes for BetFair webservices. There are two plugin executions to generate java classes for BFGlobal and BFExchange services. Java classes are generated from the wsdl files. See lines 17 and 37.



${basedir}/target/generated-sources/src/main/ java

${basedir}/src/main/wsdl/ BFGlobalService.wsdl



${basedir}/target/generated-sources/src/main/ java

${basedir}/src/main/wsdl/ BFExchangeService.wsdl


How to configure BFGlobal and BFExchange services with Spring?

The BFGlobal and BFExchange services are created by a JaxWsProxyFactory bean, which is configured using the class name of generated webservice and the url of a BetFair webservice. Afterwards BetFair services are injected into a BetFair facade, which is used by my betting application.