Friday, 27 August 2010

Horse racing market simulation

At this point of time, the Betting AI provides both market data collector and market simulator tools. Market data collector provides history of a market on a betting exchange in a form of market events, where as market simulator allows for testing trading strategies based on a historical market data. After simulation is finished the following report is generated (simulation for one day on UK horse racing markets, 21 markets):

***********************************************************************************

*Market Simulator Copyright 2010 Daniel Korzekwa(http://danmachine.com)           *
*Project homepage: http://code.google.com/p/betting-ai/                           *
*Licenced under Apache License 2.0(http://www.apache.org/licenses/LICENSE-2.0)    *
***********************************************************************************

Simulation is started. Simulation progress: 0% 4% 9% 14% 19% 23% 28% 33% 38% 42% 47% 52% 57% 61% 66% 71% 76% 80% 85% 90% 95% 100%
Simulation is finished in 65907 milliseconds.

Expected profit report for trader dk.trader.Trader:
7f Mdn Stks: /GB/Folk 22nd Aug minProfit/prob=-92.58/0.63 maxProfit/prob=345.0/0.01 expProfit=9.05 expAggrProfit=9.05 mBets=128 uBets=7
2m6f Hcap Hrd: /GB/Newt 22nd Aug minProfit/prob=-173.4/0.14 maxProfit/prob=80.4/0.14 expProfit=1.84 expAggrProfit=10.89 mBets=23 uBets=4
1m6f Hcap: /GB/Muss 22nd Aug minProfit/prob=-1182.9/0.19 maxProfit/prob=522.0/0.06 expProfit=24.83 expAggrProfit=35.73 mBets=226 uBets=17
1m Hcap: /GB/Muss 22nd Aug minProfit/prob=-1036.8/0.27 maxProfit/prob=615.0/0.01 expProfit=-11.99 expAggrProfit=23.74 mBets=222 uBets=9
3m2f Hcap Chs: /GB/Newt 22nd Aug minProfit/prob=-968.55/0.21 maxProfit/prob=1153.65/0.18 expProfit=-25.4 expAggrProfit=-1.66 mBets=184 uBets=12
2m5f Nov Chs: /GB/Newt 22nd Aug minProfit/prob=-1812.6/0.24 maxProfit/prob=1659.15/0.33 expProfit=-105.13 expAggrProfit=-106.8 mBets=468 uBets=14
2m3f Nov Hrd: /GB/Newt 22nd Aug minProfit/prob=-729.3/0.16 maxProfit/prob=738.36/0.31 expProfit=-3.33 expAggrProfit=-110.13 mBets=178 uBets=10
5f Mdn: /GB/Folk 22nd Aug minProfit/prob=-932.6/0.29 maxProfit/prob=591.4/0.11 expProfit=-8.61 expAggrProfit=-118.74 mBets=233 uBets=12
1m Hcap: /GB/Muss 22nd Aug minProfit/prob=-1687.7/0.16 maxProfit/prob=1637.55/0.34 expProfit=46.21 expAggrProfit=-72.53 mBets=421 uBets=24
7f Sell Stks: /GB/Folk 22nd Aug minProfit/prob=-3.0/0.09 maxProfit/prob=7.65/0.28 expProfit=0.07 expAggrProfit=-72.46 mBets=3 uBets=5
5f Sell Stks: /GB/Muss 22nd Aug minProfit/prob=-1699.8/0.28 maxProfit/prob=1462.06/0.3 expProfit=-112.54 expAggrProfit=-185.0 mBets=384 uBets=11
2m6f Hcap Hrd: /GB/Newt 22nd Aug minProfit/prob=-1088.4/0.2 maxProfit/prob=454.8/0.08 expProfit=-8.51 expAggrProfit=-193.51 mBets=156 uBets=8
2m1f NHF: /GB/Newt 22nd Aug minProfit/prob=-1068.0/0.2 maxProfit/prob=1105.8/0.29 expProfit=38.09 expAggrProfit=-155.42 mBets=230 uBets=16
7f Mdn Stks: /GB/Muss 22nd Aug minProfit/prob=-822.6/0.45 maxProfit/prob=1197.9/0.1 expProfit=-26.43 expAggrProfit=-181.85 mBets=333 uBets=12
1m4f Hcap: /GB/Muss 22nd Aug minProfit/prob=-750.72/0.4 maxProfit/prob=561.0/0.03 expProfit=-13.69 expAggrProfit=-195.55 mBets=207 uBets=7
7f Hcap: /GB/Folk 22nd Aug minProfit/prob=-849.45/0.25 maxProfit/prob=471.0/0.07 expProfit=-20.03 expAggrProfit=-215.57 mBets=167 uBets=5
5f Hcap: /GB/Muss 22nd Aug minProfit/prob=-564.3/0.18 maxProfit/prob=219.0/0.04 expProfit=7.55 expAggrProfit=-208.02 mBets=102 uBets=5
6f Hcap: /GB/Folk 22nd Aug minProfit/prob=-1268.7/0.2 maxProfit/prob=1548.0/0.16 expProfit=35.25 expAggrProfit=-172.78 mBets=361 uBets=22
2m1f Mdn Hrd: /GB/Newt 22nd Aug minProfit/prob=-922.5/0.18 maxProfit/prob=684.15/0.23 expProfit=53.31 expAggrProfit=-119.47 mBets=280 uBets=18
1m1f Hcap: /GB/Folk 22nd Aug minProfit/prob=-875.91/0.32 maxProfit/prob=2577.9/0.16 expProfit=-19.39 expAggrProfit=-138.86 mBets=489 uBets=20
1m4f Hcap: /GB/Folk 22nd Aug minProfit/prob=-589.23/0.35 maxProfit/prob=1018.5/0.22 expProfit=-10.18 expAggrProfit=-149.05 mBets=249 uBets=11
------------------------------------------------------------------------------------
TotalExpectedProfit=-149.05 TotalMatchedBets=5044 TotalUnmachedBets=249


The list below presents a few market events for a horse racing market:

{"time":1282481934744,"eventType":"CREATE_MARKET","marketId":101655582,"marketName":"7f Mdn Stks","eventName":"/GB/Folk 22nd Aug","numOfWinners":1,"marketTime":"2010-08-22 14:10:00","runners": [{"runnerId":4848193,"runnerName":"Dubawi Gulf"},{"runnerId":4818333,"runnerName":"Apace"},{"runnerId":4932434,"runnerName":"Menha"},{"runnerId":4932435,"runnerName":"Flying Arch"},{"runnerId":4883524,"runnerName":"Hello Tomorrow"},{"runnerId":4932436,"runnerName":"Mazagee"}]}
{"time":1282481934744,"eventType":"PLACE_BET","betSize":38.0,"betPrice":5.9,"betType":"LAY","marketId":101655582,"runnerId":4818333}
...
{"time":1282482515736,"eventType":"PLACE_BET","betSize":314.0,"betPrice":1.61,"betType":"BACK","marketId":101655582,"runnerId":4848193}
{"time":1282482515736,"eventType":"CANCEL_BETS","betsSize":5.0,"betPrice":18.0,"betType":"LAY","marketId":101655582,"runnerId":4932435}
{"time":1282482515736,"eventType":"PLACE_BET","betSize":17.0,"betPrice":20.0,"betType":"LAY","marketId":101655582,"runnerId":4932435}

Example of trader implementation:

/**Executes trader implementation so it can analyse market on a betting exchange and take appropriate bet placement decisions.
*
* @param ctx Provides market data and market operations that can be used by trader to place bets on a betting exchange market
*/
def execute(ctx: ITraderContext) = {

for(runner <- ctx.runners) {
val bestPrices = ctx.getBestPrices(runner.runnerId)

if(bestPrices._1>2) {
ctx.placeBet(2,bestPrices._1,BACK,runner.runnerId)
ctx.placeBet(2,bestPrices._1 - 0.02,LAY,runner.runnerId)
}
if(bestPrices._2<2.2) {
ctx.placeBet(2,bestPrices._2,LAY,runner.runnerId)
ctx.placeBet(2,bestPrices._2 + 0.02,BACK,runner.runnerId)
}
}
}

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

10 comments:

  1. Hello Daniel,

    good work. I was thinking about creating a betfair data scraper myself (by modifying Lignite: http://forum.bdp.betfair.com/showthread.php?t=565 ) but then I found your blog.

    So I've just created a small app for data conversion from marketcollector format to ninjatrader instrument format so I can test various strategies. I've encountered a minor bug. Occasionally there are some strange prices in market logs (screenshot: http://img153.imageshack.us/img153/5466/marketlog.jpg ). Maybe some rounding error?

    I've also noticed that the minimum refresh rate is 1s. Is it possible to get this value down to 0.5s by using these: http://bdp.betfair.com/index.php?option=com_content&task=view&id=36&Itemid=64 redundant free api queries? (e.g. Get Detail Available Market Depth and Get Complete Market Prices Compressed)? Or does their usage count to the same limit?

    Thank you in advance for your reply and good luck with your project!

    ReplyDelete
  2. Hi ____

    Thanks for your response and interest in my little project.

    1)There is no problem to change refresh rate. I just set the minimum interval to 1 sec as I'm not using any lower value for now.

    2)In terms of strange prices, this may happen when a runner was withdrawn and reduction factor was applied to other runners. If you find strange prices for other cases then please report a bug.

    Please report issues that you've found here: http://code.google.com/p/betting-ai/issues/list

    Cheers.
    Daniel

    ReplyDelete
  3. Hi,

    its quite amusing that I found your blog, after creating my own data scraper. But that's live. I develop some "trading strategies" based on machine learning.

    It would appreciate it, if we compare our trading strategies, e.g. prediction accuracy, and share some thoughts.

    Please contact me( I couldnt find a "contact button" in your blog.

    Cheers

    ReplyDelete
  4. Hi WerlstWer,

    Currently, I'm focused on using neural networks for market analysis and this isn't something that could be easily presented in a shape of trading strategy.

    PS. The best way to contact me is to use LinkedIn profile (http://www.linkedin.com/in/DanielKorzekwa)

    Regards.
    Daniel

    ReplyDelete
  5. Hi Daniel.
    I have been looking for information regarding neural networks applied to Betfair. this is how I found your blog.
    I haven't tried your software yet but will do it soon.

    How is your investigation going?
    I find it hard to define a good data structure for market representation in a useful form for the neural network.
    Would you have some thoughts on this?

    Thanks and let's keep in touch.
    Bruno

    ReplyDelete
  6. Hi BV,

    I was struggling to present a market data in a proper format to neural networks too. Two neural networks I was playing with are back propagation and self organizing map. I found them quite useful for the following scenarios:

    - analyzing long term historical data to learn how market behaves in a particular situations, e.g. what happens to a runner price when derivative of traded volume over time is moving up or down
    - classifying markets, e.g. by steamers, drifters

    I'm not a master of AI but what I already understood is that it's quite difficult to model problems using AI in a domain that human brain doesn't understand well yet, therefore, recently I redefined my research method to apply more statistics, search algorithms, complex event processing and analyze output data with a human brain and eventually later to apply more AI.

    Additionally, I'm not analyzing a long term data anymore, but instead I'm focus on a short term behavior of a market, e.g. last 10 minutes before market is turned in play. In the past, to perform such analysis I had to place real bets on a betfair betting exchange, but since I developed event based market simulator I can run plenty of simulations not risking any money.

    To summarize, the more I know the more I see unknowns, but anyway it's a great fun to do such research.

    ReplyDelete
  7. Daniel, BV

    Take a look at differential evolution or even possibly PSO (particle swarm optimisation). both very simple yet efficient vector (ie: array) based optimisation techniques.

    google 'essentials of metaheuristics', an excellent free book that outlines those algorithms + many others.

    you could use a vector based learner to optimise your neural nets in terms of;

    1) feature selection

    if (v[i] > .5) then turn input node on

    2) node input weight optimisation

    and even auto-generated network topologies.

    ReplyDelete
  8. Hi ras,

    Thanks for pointing me into a good direction, I've just got my new Kindle DX and 'essentials of metaheuristics' will be the first book I will read with it. After I finish I will post review on my blog.

    ReplyDelete
  9. Hi Daniel,

    do you know any program for football markets?

    Regards,

    ReplyDelete
  10. Hi Nuno,

    What sort of program are you referring to?

    ReplyDelete