Monday, 19 April 2010

The Betting AI (Artificial intelligence) - domain model, components, deployment

Previous part of the betting ai design.

Domain model


  • Betting Exchange - "...is a form of bookmaking in which the operator offsets its risk perfectly through technology, such that the effect to the customer is that customers are seen to bet between themselves..." - from Wikipedia.
  • Market - "...The market is where you place a bet on an event. Any given event, such as a soccer match, may have a number of different markets: for example, Match Odds, a Handicap and First Goalscorer markets. This is the place to view the odds and have a bet..." - from Betfair help.
    • market id - Unique  market id.
    • market name - e.g. Match Odds or Place market
    • event name - e.g. Mat Utd vs Arsenal
    • market state - active/in play/settled
    • selection id - Unique selection id, e.g. 100 for Manchester United and 101 for Arsenal.
    • selection name - e.g. Arsenal
  • Bet - "...A ‘Back’ bet is a bet on something to happen – a horse to win a race, a football team to win, a person to be evicted from Big Brother etc. It’s just like the bet you’d regularly place with a traditional bookmaker or tote, or even with your mates..." - from Betfair help. "...Laying a bet means you are betting on something not to happen – a match not to end in a draw, a horse not to come in first, a rugby team not to win by over 7 pts etc..." - from Betfair help
    • bet price - Bet odds. Chances of winning written in a decimal format = 1/probability, e.g. 1/0.5 = 2 ( fifty-fifty).
    • bet size, e.g. 20£
    • bet type - Back or lay.
    • market id/selection id - Unique id of a particular selection in a given market.
    • bet status - Unmatched, matched, cancelled (by user), lapsed (by a betting exchange, e.g. when market is turned in play), settled.
  • Historical market data - Represents the whole market life cycle: market creation, bet placement, turning market in play, bet placement in play and market settlement. It allows to replay a market from the past.
  • Trader - Responsible for analysing market and taking appropriate bet placement decision. The aim of bet placement is to guarantee the highest profit with the lowest risk. There are two types of traders, a real trader that is represented by human and artificial trader that is represented by computer software. The main aim of this project is to provide a simulation environment for testing artificial traders.
  • Expected profit - This is a measure that tells, what would be the profit in a long term if the same market was processed many times. For instance if the expected market profit is 10, then it means that for 100 'exactly the same' markets the profit would be 1000. It may happen that for the first market the profit will be -23 and for the next market it will be 12, but in total, based on a theory of probability it should be approximately 1000.

Component design



Components: 
  • User - A person who is using the market simulation. Usually it's a market analyst or a real trader.
  • Simulator App - Provides a command line interface to the market simulation. Read the configuration of the historical market data and the trader implementation, runs simulation and displays expected profit report on the screen.
  • Simulator - Does exactly the same as a simulator application, but provides application programming interface instead of command line interface.
  • Market Event Processor - Parses historical market event in a string format, e.g. CREATE_MARKET and calls appropriate method on a betting exchange, e.g. createMarket(market:Market)
  • Trader - Analyses markets on a betting exchange and places bets.
  • Expected Profit Calculator - Reads matched bets for user (e.g. trader) from a betting exchange and calculates expected market profit report.
  • Betex - It's a betting exchange that provide peer-to-peer betting services.

Flow of control:
  1. User starts the market simulator giving the path to the file with the historical market data and the name of the trader implementation class. After the market simulation is finished the market expected profit report for the trader is printed on the screen.
  1. The Simulator Application calls the Simulator component with the input parameters provided by the user.
  1. The Simulator component reads the file with historical market data and for each historal market event, calls both MarketEventProcessor and Trader. After all historical market events are processed, it calls the ExpectedProfitCalculator to get the market expected profit report.

Deployment design


  • Java VM - The Betting AI application will be written in the Scala programming language that is running within the Java virtual machine.
  • Scala - The scala language distribution, contains scala libraries that are required to run scala applications.
  • Betting AI application - Binaries of the betting ai application, contains start script to run market simulation.
  • Historical market data file - This file contains historical market data that is used for market simulation.
  • Trader implementation class - This file contains trader implementation that market simulation is executed for.

References

Project home page: http://code.google.com/p/betting-ai/

Wednesday, 14 April 2010

The Betting AI (Artificial intelligence) - user interface and input data

Previous part of the betting ai design.
Next part of the betting ai design.

User interface

As a trading analyst I want to run the market simulation from a command line interface as presented below:

app_name marketData=[market_data_file] traderImpl=[trader_impl_class]

  • app_name - Name of the application, e.g. market_simulator

  • marketData - Text file with market data that will be used for the simulation. Go to the next chapter 'Input data' for more details.

  • traderImpl - Fully classified name of the trader implementation class that the simulation will be executed for. Go to the next chapter 'Input data' for more details.

After simulation is started, the following output is presented on the screen:

[BEGINNING Of THE OUTPUT]

Simulation is started.
Simulation progress: 1% 2% 3% 4% 5% 6% ....................................................... .................................................................................................................
..................................100%
Simulation is finished in 23 sec.

Expected profit report for trader com.dk.bettingai.trader.SimpleTraderImpl:

Catt 7th Apr: 14:10 7f Claim Stks    expProfit=28 expAggrProfit=28  mBets=34 uBets=17
Catt 7th Apr: 14:40 1m6f Hcap       expProfit=4   expAggrProfit=32  mBets=27 uBets=2
...
Nott 7th Apr: 17:20 1m Hcap         expProfit=-6  expAggrProfit=136 mBets=45 uBets=57
-------------------------------------------------------------------------------------
TotalExpectedProfit=136 TotalMatchedBets=457 TotalUnmachedBets=262
[END Of THE OUTPUT]

Definitions:
  • expProfit - Expected market profit based on all matched bets on the market placed by trader.

  • expAggrProfit- Expected profit for the market and all prior markets. 

  • mBets - Number of matched bets on the market placed by trader.

  • uBets - Number of unmatched bets on the market placed by trader.

  • TotalExpectedProfit - Expected profit for all markets and all matched bets placed by trader.

  • TotalMatchedBets - Number of all matched bets for all markets placed by trader.

  • TotalUnmatchedBets - Number of all unmatched bets for all markets placed by trader.

Input data

To run the market simulation, user has to provide the historical market data and the trader implementation class.

The historical market data is provided as a text file, that contains the number of events for the market management and the bet placement. The list below presents all possible event types:
  • CREATE_MARKET
    • Market id, market name, event name, number of winners, market time.
    • List of selections: selection id, selection name.

  • TURN_MARKET_INPLAY
    • Market id.

  • SETTLE_MARKET
    • Market id, list of selection ids for winners.

  • PLACE_BET
    • Bet size, bet price, bet type (back/lay), market id, selection id.

  • CANCEL_BET
    • Bet id.

The trader implementation class implements the following interface:

Interface name: Trader
Interface operations: 
  • execute():void

  • setBetexPublicApi(betexPublicApi: BetexPublicApi):void

BetexPublicApi:
  • getActiveMarkets
    • Input - None.
    • Output -  All markets that are not closed. The following attributes are returned: market id, market name, event name, market time.

  • getMarketData
    • Input - Market id.
    • Output - All back and lay stakes for each price on the exchange for a given market. 
    • Output - Market id, market name, event name, selection ids/names, number of winners, market status (active/in play/settled), market time.
    • Output - matched amounts at each price on all of the runners.

  • placeBet - Place a bet on a betting exchange. 
    • Input: Bet size, bet price, bet type (back/lay), market id, selection id

  • cancelBet - Cancel a bet.
    • Input - Bet id.
    • Output - None.

  • getBets - Returns bets for a given user on a betting exchange
    • Input: User id, bet status (matched, unmatched, matched_unmatched, settled, cancelled, lapsed)
    • Output: List of bets (bet id, bet size, bet price, bet type, market id, selection id, bet status).

References


    Friday, 9 April 2010

    The Betting AI (Artificial intelligence) - requirements

    Previous part of the betting ai design.
    Next part of the betting ai design.

    Functional requirements

    Use case scenario:
    As a trading analyst

    I want to choose historical market data for the simulation, e.g. all market data for the period from 01.02.2010 to 10.02.2010

    • Historical market data represents the whole market life cycle: market creation, bet placement, turning market in play, bet placement in play and market settlement.

    Then I want to choose trader implementation that the bet placement intelligence should be tested for. 

    • Trader implementation should be executed every time the historical market event is processed. For instance when market is created or when bet is placed or market is turned in play. When trader is executed it can check the market state, runner prices, etc. and take appropriate trading decisions.

    Then I want to run simulation.

    • The aim of the simulation is to test, how a trader implementation would behave if it was running live on the betting exchange. Of course the market simulation will never return the same results as testing trader on a real betting exchange, however they both should be similar as far as possible.


    Finally I want to see expected trader profit based on matched bets placed by trader and market probabilities derived from market prices before market is turned in play.
    • Expected trader profit for market tells us what would be the profit in a long term if this market was processed many times. For instance if the expected market profit is 10, then it means that for 100 exactly the same markets the profit would be 1000. It may happen that for the first market the profit will be -23 and for next market it will be 12, but in total, based on a theory of probability it should be approximately 1000.


    Non-functional requirements

    • Performance - Running the market simulation for 10 markets with 10 runners each and collected data for 10 minutes with 1 second interval should take maximum 1 second.

    • Command line interface - The market simulation should be started from the command line interface.

    • Multi-platform - It should be possible to run the market simulation on Windows and Linux operating systems.

    • Historical market data - All historical market data for the market simulation should be read from a single file on a file system. 

    • Installation - The application for the market simulation should be delivered as a zip file. To install and run the market simulation the following steps should be required:
      • Unzip the application.
      • Go to the application folder.
      • Run start script and provide file with the historical market data and the trader implementation.
      • Print the simulation report to the output screen.

    • Easy to run the market simulation for different traders - Running the market simulation for different traders should require only to change the name of the trader provided on the command line interface.

    References

    Monday, 5 April 2010

    The Betting AI (Artificial intelligence) - project initiation

    Next part of the betting ai design.

    Background

    In 2008 I started a research project that was to analyse and understand behaviours on a betting exchange. On the beginning of my research I created FlexiBet, automated betting system based on state machines. For the purpose of the Flexibet project I developed betting-scxml, domain specific language for creating automated betting strategies based on SCXML state machines. 

    Having that, I created a few betting strategies and I put them live to get some data to analyse. In the next step I analysed collected data, then I created new betting strategy and I put it live again to get more data to analyse.

    I was evolving betting strategies and analysing collected data for one year or so, until it became time consuming and difficult to continue. It was because of using manual data analysis, mostly based on SQL and Eclipse Birt Business Intelligence. Despite of being simple and handy for creating simple reports, I found it difficult to analyse complex data.

    Vision

    The vision is to apply neural network technique to analyse markets on a betting exchange. I want to create a trader based on a neural network that can take appropriate bet placement decisions. Then, trader should be tested on a historical market data to measure how efficient the trader's intelligence is.

    Use case scenario:
    I want to choose historical market data for the simulation, e.g. all market data for the period from 01.02.2010 to 10.02.2010

    Then I want to choose trader implementation that the bet placement intelligence should be tested for.

    Then I want to run simulation.

    Finally I want to see expected trader profit based on matched bets placed by trader and market probabilities derived from market prices before market is turned in play.

    References

    Project home page: http://code.google.com/p/betting-ai/