<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4079211241768361336</id><updated>2012-01-12T05:57:59.031-08:00</updated><category term='Eclipse BIRT'/><category term='CXF'/><category term='Spring'/><category term='Java'/><category term='BetFair'/><category term='Maven'/><category term='Betfair API'/><category term='Betting exchange'/><category term='Apache SCXML'/><title type='text'>Automated betting with Betfair, Java and Jessica</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://jessicalovesbetfair.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4079211241768361336/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://jessicalovesbetfair.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Daniel Korzekwa</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/-qaYDIeP-WTs/TVmjUTkyrdI/AAAAAAAAALw/dy3C423tsig/s220/IMG_2655.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4079211241768361336.post-6540245011818180993</id><published>2009-04-21T14:00:00.000-07:00</published><updated>2009-04-21T14:01:16.551-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Betting exchange'/><category scheme='http://www.blogger.com/atom/ns#' term='BetFair'/><category scheme='http://www.blogger.com/atom/ns#' term='Apache SCXML'/><title type='text'>Trading at BetFair betting exchange with Apache SCXML</title><content type='html'>&lt;div style="text-align: justify;"&gt;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.&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;How to trade at BetFair trading exchange?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;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$.&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: center;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_TV098I9EDPs/SeohxbTXUQI/AAAAAAAAABM/mCX1F-jXr08/s1600-h/horse+market+panel"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 247px;" src="http://1.bp.blogspot.com/_TV098I9EDPs/SeohxbTXUQI/AAAAAAAAABM/mCX1F-jXr08/s400/horse+market+panel" alt="" id="BLOGGER_PHOTO_ID_5326106642345316610" border="0" /&gt;&lt;/a&gt;Figure 1. Horse racing market with runners and prices (source: betfair.com).&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;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:&lt;br /&gt;&lt;br /&gt;Bets on a first runner (Shemoli):&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Back bet for 2$ at price 2.12&lt;/li&gt;&lt;li&gt;Lay bet for 2$ at price 2.14&lt;/li&gt;&lt;/ul&gt;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&lt;br /&gt;When the runner is a looser then the profit/loss is: -2 + 2 = 0&lt;br /&gt;&lt;br /&gt;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:&lt;br /&gt;&lt;br /&gt;Bets on a first runner (Shemoli):&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Back bet for 2$ at price 3.0&lt;/li&gt;&lt;li&gt;Lay bet for 2$ at price 2.14&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;When the runner is a winner then the profit/loss is: 2*(3-1) -2(2.14-1) =  4 - 2.28 = 1.72&lt;br /&gt;When the runner is a looser then the profit/loss is: -2 + 2 = 0&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: center;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_TV098I9EDPs/SeoiFbwth1I/AAAAAAAAABU/XryBNvAPuF0/s1600-h/LoadRunnerInfoChartAction.do.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 350px; height: 255px;" src="http://3.bp.blogspot.com/_TV098I9EDPs/SeoiFbwth1I/AAAAAAAAABU/XryBNvAPuF0/s400/LoadRunnerInfoChartAction.do.jpg" alt="" id="BLOGGER_PHOTO_ID_5326106986065790802" border="0" /&gt;&lt;/a&gt;Figure 2. Price chart for horse runner (source: betfair.com).&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;How I use Apache SCXML to trade at BetFair?&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;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):&lt;br /&gt;&lt;/div&gt;&lt;ul style="text-align: justify;"&gt;&lt;li&gt;No bets - When the runner price is increasing (price.slope&gt;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.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul style="text-align: justify;"&gt;&lt;li&gt;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.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Lay matched - Back and lay bets are matched. Do not place any more bets on the runner.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;div style="text-align: center;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_TV098I9EDPs/Se4hleUIznI/AAAAAAAAACk/JPOypfx0UMk/s1600-h/jessica1d.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 340px;" src="http://2.bp.blogspot.com/_TV098I9EDPs/Se4hleUIznI/AAAAAAAAACk/JPOypfx0UMk/s400/jessica1d.jpg" alt="" id="BLOGGER_PHOTO_ID_5327232336902278770" border="0" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;div style="text-align: center;"&gt;Figure 3. Trading strategy (States machine) GUI editor.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;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:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Creata a state machine diagram using GUI editor.&lt;/li&gt;&lt;/ul&gt;&lt;ul style="text-align: justify;"&gt;&lt;li&gt;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.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;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.&lt;/li&gt;&lt;/ul&gt;&lt;pre class="brush: xml"&gt;&lt;br /&gt;&lt;br /&gt;&lt;scxml xmlns="http://www.w3.org/2005/07/scxml" my="http://my.custom-actions/CUSTOM" version="1.0" initialstate="noBets"&gt;&lt;br /&gt;&lt;br /&gt;&lt;state id="noBets"&gt;&lt;br /&gt;&lt;transition target="backMatched" cond="price.slope gt 15"&gt;&lt;br /&gt;  &lt;my:placebet bettype="B" size="2" price="runner.priceToBack"&gt;&lt;/my:placebet&gt;&lt;br /&gt;&lt;/transition&gt;&lt;br /&gt;&lt;/state&gt;&lt;br /&gt;&lt;br /&gt;&lt;state id="backMatched"&gt;&lt;br /&gt;&lt;transition target="layMatched" cond="price.priceToLay lt lastBackBet.avgPriceMatched"&gt;&lt;br /&gt;  &lt;my:placebet bettype="L" size="2" price="runner.priceToLay"&gt;&lt;/my:placebet&gt;&lt;br /&gt;&lt;/transition&gt;&lt;br /&gt;&lt;/state&gt;&lt;br /&gt;&lt;br /&gt;&lt;state id="layMatched"&gt;&lt;/state&gt;&lt;br /&gt;&lt;br /&gt;&lt;/scxml&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div style="text-align: center;"&gt;Figure 4. State machine for trading strategy in a SCXML format.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: center;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_TV098I9EDPs/Se4UZMq_lsI/AAAAAAAAACc/p8ShMcHta_U/s1600-h/jessica2d.jpg"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 273px;" src="http://4.bp.blogspot.com/_TV098I9EDPs/Se4UZMq_lsI/AAAAAAAAACc/p8ShMcHta_U/s400/jessica2d.jpg" alt="" id="BLOGGER_PHOTO_ID_5327217832356714178" border="0" /&gt;&lt;/a&gt;Figure 4. State machine for trading strategies at Soccer and Horse Racing markets.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;Conclusion&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;/div&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;&lt;br /&gt;References&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Betting_exchange"&gt;Betting exchange&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Betfair"&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_24"&gt;Betfair&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.eclipse.org/birt/phoenix/"&gt;&lt;/a&gt;&lt;a href="http://en.wikipedia.org/wiki/SCXML"&gt;SCXML&lt;/a&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_25"&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://commons.apache.org/scxml/"&gt;Apache SCXML&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;Bye,&lt;br /&gt;Jessica&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4079211241768361336-6540245011818180993?l=jessicalovesbetfair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jessicalovesbetfair.blogspot.com/feeds/6540245011818180993/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://jessicalovesbetfair.blogspot.com/2009/04/trading-at-betfair-betting-exchange.html#comment-form' title='24 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4079211241768361336/posts/default/6540245011818180993'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4079211241768361336/posts/default/6540245011818180993'/><link rel='alternate' type='text/html' href='http://jessicalovesbetfair.blogspot.com/2009/04/trading-at-betfair-betting-exchange.html' title='Trading at BetFair betting exchange with Apache SCXML'/><author><name>Jessica Mong</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_TV098I9EDPs/SeohxbTXUQI/AAAAAAAAABM/mCX1F-jXr08/s72-c/horse+market+panel' height='72' width='72'/><thr:total>24</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4079211241768361336.post-1098391887043731777</id><published>2009-03-14T14:05:00.000-07:00</published><updated>2009-03-25T16:02:57.553-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='BetFair'/><category scheme='http://www.blogger.com/atom/ns#' term='Eclipse BIRT'/><title type='text'>Automated betting with BetFair, Business Intelligence and Eclipse BIRT</title><content type='html'>&lt;div style="text-align: justify;"&gt;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).&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;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:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Historical  data analysis – e.g. create reports/charts to show profit for sports/regions over the time.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Data mining – e.g. to recognize some particular situations when I win or lose.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Automated decision making system - based on evolutionary algorithms, e.g. eliminate wrong betting strategies during evolution process.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;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.&lt;br /&gt;&lt;br /&gt;The examples below present BIRT chart and table report integrated into web console of my betting application.&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: center;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_TV098I9EDPs/ScLROxP4kZI/AAAAAAAAAAU/ZWaZ5hTNpP0/s1600-h/profit_per_week.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 232px;" src="http://4.bp.blogspot.com/_TV098I9EDPs/ScLROxP4kZI/AAAAAAAAAAU/ZWaZ5hTNpP0/s400/profit_per_week.png" alt="" id="BLOGGER_PHOTO_ID_5315040561918939538" border="0" /&gt;&lt;/a&gt;Figure 1 Profit per week over the time.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: center;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_TV098I9EDPs/ScPq5v5dUcI/AAAAAAAAAAk/PWe2nXWweZA/s1600-h/_1237576372064.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 375px; height: 400px;" src="http://1.bp.blogspot.com/_TV098I9EDPs/ScPq5v5dUcI/AAAAAAAAAAk/PWe2nXWweZA/s400/_1237576372064.png" alt="" id="BLOGGER_PHOTO_ID_5315350263058223554" border="0" /&gt;&lt;/a&gt;Figure 2 Profit per sport/region over the time.&lt;/div&gt;&lt;/div&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;How I use Eclipse BIRT?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;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.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: center;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_TV098I9EDPs/ScUoBlwtsFI/AAAAAAAAAA0/5qvXow2kBxc/s1600-h/BI.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 230px;" src="http://3.bp.blogspot.com/_TV098I9EDPs/ScUoBlwtsFI/AAAAAAAAAA0/5qvXow2kBxc/s400/BI.png" alt="" id="BLOGGER_PHOTO_ID_5315698942962151506" border="0" /&gt;&lt;/a&gt;Figure 3 Design of Birt reports.&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;Diagram details:&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;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.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Reports files - BIRT reports created by reports designer.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;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.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;BirtEngine - Generates report in a html/pdf format based on a report file and reports data from data source, e.g. from database.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Database - Repository of betting history, e.g. account statement, markets, bets, prices.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_TV098I9EDPs/ScUuaGeFllI/AAAAAAAAAA8/rr3r_N70eZk/s1600-h/birt+designer.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 233px;" src="http://1.bp.blogspot.com/_TV098I9EDPs/ScUuaGeFllI/AAAAAAAAAA8/rr3r_N70eZk/s400/birt+designer.png" alt="" id="BLOGGER_PHOTO_ID_5315705961129023058" border="0" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;Figure 4 BIRT Designer.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;Conclusion&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt; 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.&lt;br /&gt;&lt;/div&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;&lt;br /&gt;References&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Betting_exchange"&gt;Betting exchange&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Betfair"&gt;Betfair&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.eclipse.org/birt/phoenix/"&gt;Eclipse BIRT&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;Bye,&lt;br /&gt;Jessica&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4079211241768361336-1098391887043731777?l=jessicalovesbetfair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jessicalovesbetfair.blogspot.com/feeds/1098391887043731777/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://jessicalovesbetfair.blogspot.com/2009/03/automated-betting-with-betfair-business.html#comment-form' title='15 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4079211241768361336/posts/default/1098391887043731777'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4079211241768361336/posts/default/1098391887043731777'/><link rel='alternate' type='text/html' href='http://jessicalovesbetfair.blogspot.com/2009/03/automated-betting-with-betfair-business.html' title='Automated betting with BetFair, Business Intelligence and Eclipse BIRT'/><author><name>Jessica Mong</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_TV098I9EDPs/ScLROxP4kZI/AAAAAAAAAAU/ZWaZ5hTNpP0/s72-c/profit_per_week.png' height='72' width='72'/><thr:total>15</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4079211241768361336.post-5367629782622813880</id><published>2009-02-26T14:33:00.001-08:00</published><updated>2009-03-21T11:43:10.333-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Betfair API'/><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='CXF'/><category scheme='http://www.blogger.com/atom/ns#' term='Maven'/><category scheme='http://www.blogger.com/atom/ns#' term='Spring'/><title type='text'>BetFair Java API with Apache CXF, Spring and Maven</title><content type='html'>&lt;div style="text-align: justify;"&gt;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.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;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.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;The diagram below presents how BetfairService is designed and used in my betting application:&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_vHBR1L4_xvs/SZdCSzkPchI/AAAAAAAAABE/4HQKtZZIFP0/s1600-h/BettingService.png"&gt;&lt;img id="BLOGGER_PHOTO_ID_5302779977099342354" style="margin: 0px auto 10px; display: block; width: 400px; cursor: pointer; height: 208px; text-align: center;" alt="" src="http://4.bp.blogspot.com/_vHBR1L4_xvs/SZdCSzkPchI/AAAAAAAAABE/4HQKtZZIFP0/s400/BettingService.png" border="0" /&gt;&lt;/a&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:0;"&gt;&lt;span style="font-size:78%;"&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Diagram details:&lt;br /&gt;&lt;ul style="text-align: justify;"&gt;&lt;li&gt;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.&lt;/li&gt;&lt;/ul&gt;&lt;ul style="text-align: left;"&gt;&lt;li style="text-align: left;"&gt;BettingServiceImpl - BetFair Facade, that simplifies access to the BetFair operations from a BettingEngine.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul style="text-align: left;"&gt;&lt;li style="text-align: left;"&gt;BettingEngine - Module of my application responsible for executing betting strategies.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;How to login to a Betfair account?&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;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.&lt;br /&gt;&lt;/div&gt;&lt;pre class="brush: java"&gt;//Login to a BetFair account.&lt;br /&gt;//&lt;br /&gt;public void login(String userName, String password, int productId) {&lt;br /&gt;&lt;br /&gt;//Creating login command which contains all login parameters.&lt;br /&gt;LoginReq req = new LoginReq();&lt;br /&gt;req.setUsername(userName);&lt;br /&gt;req.setPassword(password);&lt;br /&gt;req.setProductId(productId);&lt;br /&gt;req.setLocationId(0);&lt;br /&gt;req.setVendorSoftwareId(0);&lt;br /&gt;&lt;br /&gt;//Calling BetFair webservice login operaton.&lt;br /&gt;LoginResp resp = globalWebService.login(req);&lt;br /&gt;&lt;br /&gt;//Checking login result and storing session token, which is necessary to call BFExchangeService operations, e.g. getMarkets, placeBet.&lt;br /&gt;if (resp.getErrorCode().equals(LoginErrorEnum.OK))&lt;br /&gt;{&lt;br /&gt;state.setSessionToken(resp.getHeader(). getSessionToken());&lt;br /&gt;} else {&lt;br /&gt;throw new BetFairException("Login error. Error code: " + resp.getErrorCode() + ", api code: "&lt;br /&gt;+ resp.getHeader().getErrorCode());&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;span style="font-size:130%;"&gt;How to place a bet?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: justify;"&gt;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:&lt;br /&gt;&lt;/div&gt;&lt;ul style="text-align: justify;"&gt;&lt;li&gt;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.&lt;/li&gt;&lt;/ul&gt;&lt;ul style="text-align: justify;"&gt;&lt;li&gt;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.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;betCategory - Exchange or SP(starting price) bet.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;price - Binary odds of a bet, e.g. 2.0&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;size - Stake of a bet, e.g. 20 dollars.&lt;/li&gt;&lt;/ul&gt;&lt;pre class="brush: java"&gt;//Place a bet on a BetFair&lt;br /&gt;//betting exchange.&lt;br /&gt;public void placeBet(int marketId, int selectionId, BetTypeEnum betType, BetCategoryTypeEnum betCategory,&lt;br /&gt;double price, double size) {&lt;br /&gt;&lt;br /&gt;//Creating request header with a session token returned by login operation.&lt;br /&gt;APIRequestHeader requestHeader = new APIRequestHeader();&lt;br /&gt;requestHeader.setSessionToken(sessionToken);&lt;br /&gt;&lt;br /&gt;//Creating placeBet command, which contains all required parameters.&lt;br /&gt;PlaceBetsReq req = new PlaceBetsReq();&lt;br /&gt;req.setHeader(requestHeader);&lt;br /&gt;&lt;br /&gt;ArrayOfPlaceBets arrayOfBets = new ArrayOfPlaceBets();&lt;br /&gt;PlaceBets placeBets = new PlaceBets();&lt;br /&gt;placeBets.setAsianLineId(0);&lt;br /&gt;placeBets.setBetCategoryType(betCategory);&lt;br /&gt;placeBets.setPrice(price);&lt;br /&gt;placeBets.setSize(size);&lt;br /&gt;placeBets.setBspLiability(0d);&lt;br /&gt;placeBets.setBetPersistenceType( BetPersistenceTypeEnum.NONE);&lt;br /&gt;placeBets.setBetType(betType);&lt;br /&gt;placeBets.setMarketId(marketId);&lt;br /&gt;placeBets.setSelectionId(selectionId);&lt;br /&gt;arrayOfBets.getPlaceBets().add(placeBets);&lt;br /&gt;req.setBets(arrayOfBets);&lt;br /&gt;&lt;br /&gt;//Calling placeBet operation on a BetFair webservice.&lt;br /&gt;PlaceBetsResp resp = exchangeWebService.placeBets(req);&lt;br /&gt;if (resp.getErrorCode().equals(...) {&lt;br /&gt;//Checking placing bet result.&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;span style="font-size:130%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;How to generate &lt;/span&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-size:130%;"&gt;BFGlobal and BFExchange services with Maven and cxf-codegen-plugin?&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;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.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;pre class="brush: xml"&gt;&lt;br /&gt;&lt;plugin&gt;&lt;br /&gt;&lt;groupid&gt;org.apache.cxf&lt;/groupid&gt;&lt;br /&gt;&lt;artifactid&gt;cxf-codegen-plugin&lt;/artifactid&gt;&lt;br /&gt;&lt;version&gt;2.0.5-incubator&lt;/version&gt;&lt;br /&gt;&lt;executions&gt;&lt;br /&gt;&lt;!-- Generate java classes for a BFGlobalService--&gt;&lt;br /&gt;&lt;execution&gt;&lt;br /&gt;&lt;id&gt;generate-sources_BFGlobalService&lt;/id&gt;&lt;br /&gt;&lt;phase&gt;generate-sources&lt;/phase&gt;&lt;br /&gt;&lt;configuration&gt;&lt;br /&gt;&lt;sourceroot&gt;&lt;br /&gt;${basedir}/target/generated-sources/src/main/ java&lt;br /&gt;&lt;/sourceroot&gt;&lt;br /&gt;&lt;wsdloptions&gt;&lt;br /&gt;&lt;wsdloption&gt;&lt;br /&gt;&lt;wsdl&gt;&lt;br /&gt;${basedir}/src/main/wsdl/ BFGlobalService.wsdl&lt;br /&gt;&lt;/wsdl&gt;&lt;br /&gt;&lt;/wsdloption&gt;&lt;br /&gt;&lt;/wsdloptions&gt;&lt;br /&gt;&lt;/configuration&gt;&lt;br /&gt;&lt;goals&gt;&lt;br /&gt;&lt;goal&gt;wsdl2java&lt;/goal&gt;&lt;br /&gt;&lt;/goals&gt;&lt;br /&gt;&lt;/execution&gt;&lt;br /&gt;&lt;!-- Generate java classes for a BFGlobalService--&gt;&lt;br /&gt;&lt;execution&gt;&lt;br /&gt;&lt;id&gt;generate-sources_BFExchangeService&lt;/id&gt;&lt;br /&gt;&lt;phase&gt;generate-sources&lt;/phase&gt;&lt;br /&gt;&lt;configuration&gt;&lt;br /&gt;&lt;sourceroot&gt;&lt;br /&gt;${basedir}/target/generated-sources/src/main/ java&lt;br /&gt;&lt;/sourceroot&gt;&lt;br /&gt;&lt;wsdloptions&gt;&lt;br /&gt;&lt;wsdloption&gt;&lt;br /&gt;&lt;wsdl&gt;&lt;br /&gt;${basedir}/src/main/wsdl/ BFExchangeService.wsdl&lt;br /&gt;&lt;/wsdl&gt;&lt;br /&gt;&lt;/wsdloption&gt;&lt;br /&gt;&lt;/wsdloptions&gt;&lt;br /&gt;&lt;/configuration&gt;&lt;br /&gt;&lt;goals&gt;&lt;br /&gt;&lt;goal&gt;wsdl2java&lt;/goal&gt;&lt;br /&gt;&lt;/goals&gt;&lt;br /&gt;&lt;/execution&gt;&lt;br /&gt;&lt;/executions&gt;&lt;br /&gt;&lt;/plugin&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;How to configure &lt;/span&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;BFGlobal and BFExchange services with Spring?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span style="font-size:100%;"&gt;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.&lt;/span&gt;&lt;span class="content"&gt;&lt;span class="block" style="margin-left: 0px;"&gt;&lt;code class="string"&gt;&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="content"&gt;&lt;span class="block" style="margin-left: 0px;"&gt;&lt;code class="string"&gt;&lt;/code&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;pre class="brush: xml"&gt;&lt;br /&gt;&lt;beans&gt;&lt;br /&gt;&lt;!-- BetFair facade--&gt;&lt;br /&gt;&lt;bean class="jm.flexibet.betfairservice. BetFairServiceImpl" id="betFairService"&gt;&lt;br /&gt;&lt;property ref="exchangeWebService" name="exchangeWebService"&gt;&lt;/property&gt;&lt;br /&gt;&lt;property ref="globalWebService" name="globalWebService"&gt; &lt;/property&gt;&lt;br /&gt;&lt;/bean&gt;&lt;br /&gt;&lt;br /&gt;&lt;!-- BFGlobalService: factory and bean --&gt;&lt;br /&gt;&lt;bean class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean" id="proxyFactoryGlobalWebService"&gt;&lt;br /&gt;&lt;property name="serviceClass" value="com.betfair.publicapi.v3.bfglobalservice. BFGlobalService"&gt; &lt;/property&gt;&lt;br /&gt;&lt;property name="address" value="https://api.betfair.com/global/v3/ BFGlobalService"&gt; &lt;/property&gt;&lt;br /&gt;&lt;/bean&gt;&lt;br /&gt;&lt;bean class="com.betfair.publicapi.v3.bfglobalservice. BFGlobalService" id="globalWebService" method="create" bean="proxyFactoryGlobalWebService"&gt;&lt;br /&gt;&lt;/bean&gt;&lt;br /&gt;&lt;br /&gt;&lt;!-- BFExchangeService: factory and bean --&gt;&lt;br /&gt;&lt;bean class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean" id="proxyFactoryExchangeWebService"&gt;&lt;br /&gt;&lt;property name="serviceClass" value="com.betfair.publicapi.v5.bfexchangeservice. BFExchangeService"&gt; &lt;/property&gt;&lt;br /&gt;&lt;property name="address" value="https://api.betfair.com/exchange/v5/ BFExchangeService"&gt; &lt;/property&gt;&lt;br /&gt;&lt;/bean&gt;&lt;br /&gt;&lt;bean class="com.betfair.publicapi.v5.bfexchangeservice. BFExchangeService" id="exchangeWebService" method="create" bean="proxyFactoryExchangeWebService"&gt;&lt;br /&gt;&lt;/bean&gt;&lt;br /&gt;&lt;br /&gt;&lt;/beans&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight: bold;font-size:130%;" &gt;References&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Betting_exchange"&gt;Betting exchange&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Betfair"&gt;Betfair&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://bdp.betfair.com/"&gt;Betfair API&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Apache_CXF"&gt;Apache CXF&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Spring_Framework"&gt;Spring Framework&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Apache_Maven"&gt;Apache Maven&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;Bye,&lt;br /&gt;Jessica&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4079211241768361336-5367629782622813880?l=jessicalovesbetfair.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jessicalovesbetfair.blogspot.com/feeds/5367629782622813880/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://jessicalovesbetfair.blogspot.com/2009/02/betfair-java-api-with-apache-cxf-spring.html#comment-form' title='19 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4079211241768361336/posts/default/5367629782622813880'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4079211241768361336/posts/default/5367629782622813880'/><link rel='alternate' type='text/html' href='http://jessicalovesbetfair.blogspot.com/2009/02/betfair-java-api-with-apache-cxf-spring.html' title='BetFair Java API with Apache CXF, Spring and Maven'/><author><name>Jessica Mong</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_vHBR1L4_xvs/SZdCSzkPchI/AAAAAAAAABE/4HQKtZZIFP0/s72-c/BettingService.png' height='72' width='72'/><thr:total>19</thr:total></entry></feed>
