Programming for Betfair

Programming for Betfair, a guide to creating sports trading applications, is now available on Amazon. You do not need any previous programming experience to follow the book, just a logical mind. Both beginner and expert programmers have completed the book. Programming for Betfair teaches you all you need to know about programming your own trading applications.

Why Would I Want This Book?

Yes, there is plenty of third-party software out there but a lot of this software is geared to manual trading and even then, trading in a certain style. You may have your own trading strategies, which cannot be implemented with third-party software. Algorithmic trading is best done with software that you have crafted yourself. Also, you may not want third-party software to be able to implement your ideas otherwise other traders will happen upon your strategies.

In the long-term there is a great cost-saving to be had from producing your own trading software. Betfair does charge a £299 one-off access fee for receiving live data through API-NG. However, you can receive free delayed data (with a one to sixty seconds delay) whilst you develop your own software and strategies. Betting functions are always in realtime whether you use live data or not. Also, with the delayed key you will still have access to the lastMatchTime so you will know at which time the lastPriceMatched was struck.

Most third-party trading software has a subscription fee that has to be paid every month and eventually you will pay a lot more than Betfair's API-NG live access charge. You can always work through this book, test some ideas and then decide if you want live access to the data at a later date.

What will the book teach me?

Using the freely available Visual Studio programming environment I show the reader how to build an application that gets prices from and places bets automatically into Betfair's exchange.

Also, the reader can build databases from Betfair data for offline analysis. Data can be saved whilst the trading application is running and then converted into a CSV format for a spreadsheet where it can be manipulated, charted and analysed for the creation of trading rules.

After working through the book the reader should have all the tools needed to start building their own trading bots.

There is also a chapter on improving access to the charts on Betfair's website. Charts can be grouped together for comparison and updated automatically to keep the trader up to date with the latest trends in the market place. The final chapter touches on some advanced techniques such as the creation of trading indicators, volume analysis (specifically VWAP - volume weighted average price), low-latency optimisation, arbitrage, machine learning, Monte Carlo methods and more.

With this book you will be able to create trading bots using your own trading systems. You won't have to give away any of your trading secrets through asking a third-party to include the exact functionality that you require in their software.

Some Screen Captures from the Book

Betfair's visualisers are used to demonstrate how JSON is used to communicate between the reader's computer and Betfair's servers. The reader is then gently led through the creation of JSON request strings and the processing of JSON response strings into raw data for use by the application.

The price engine with a bet placement control for experimenting with various bet types. The user is then shown a non-graphical way of inserting bets into the exchange for automated trigger betting with bots.

A ChartBot can easily be created for multiple views of Betfair charts, side by side. They can also be set to auto-update.

The applications created in the book can build databases of data for offline analysis in a spreadsheet so that the user can analyse the data and build trading rules.



Chapter 1 - A BASIC Approach
Join the Revolution
Microsoft Visual Basic
Google Chrome

Chapter 2 - Logging into Betfair
Session Tokens and Application Keys
The ToolBox
Changing a Control's Properties
Adding Another Form
The WebBrowser Control
The Betfair Visualisers

Chapter 3 - Understanding JSON
JavaScript Object Notation
JSON Parser
Serialization and Deserialization

Chapter 4 - Off to Market
Sending a Request
Serializing a Visual Basic Object into JSON
Referencing Newtonsoft.Json.dll
Building a Visual Basic Request Object
Deserializing JSON into a Visual Basic Object
Running The Application

Chapter 5 - A GUI Feeling
Displaying the Contents of a JSON Response
The DataGridView Control
Building a Standalone Application
Further Work

Chapter 6 - Gathering Prices
Using the listMarketBook Method
Processing ListMarketBook Responses
The Timer Control
Further Work

Chapter 7 - Place Your Bets
Types of Trader
Placing a Bet Through the Visualiser
Sub £2 Bets
Greening Up (Hedging)
Betting Experiments
Monitoring Bets
Automated Trading
Further Work

Chapter 8 - Saving Data for Offline Analysis
Saving Data to Your Hard Drive
Processing Data for Offline Analysis
Data Visualisation with a Spreadsheet

Chapter 9 - Automating Betfair Charts
Creating a Form Programmatically
Using ChartBot
Further Work

Chapter 10 - Making The Most of It
Quantitative Analysis
Creating You Own Indicators
Volume Analysis
Trend Following
Fundamental Analysis
Money Management
Optimising for Speed (Low Latency)
Machine Learning
Monte Carlo Methods
Concluding Remarks

Recommended Reading

Complete Listings

Is there an eBook? There will not be an eBook. I am a self-publisher with no facilities for creating eBooks. My book is printed on demand with no advance royalties from a publisher therefore I am dependent on all future sales. As I started coding in the early 1980s I am used to entering code from magazines and books. I feel that I learned more from that than today's generation who cut and paste their knowledge. There is a lot to be said for doing things slowly.

Need help with the book?

If you are looking for help on this page then you have not read the book properly. Start again from the very first page, read carefully and you will find the URL for the book's support page, where I provide updates and help beginners to find their typos.

With over 1000 satisfied readers the code in this book (as of February 2017) still permits a trader to create their own applications for algo-trading and historic data capture.


  1. Hi James,
    Betfair has recently made some headway in the USA, opening it's service to New Jersey residents (horse-racing only at the moment). Do you know if Betfair's API would allow for the development of software for the US flavor of the exchange (

    I've tried a couple of commercial apps that errored when I tried to login. I'm not certain though if this is due to those apps being hardcoded for the UK exchange or if there is an inherent API limitation at the moment for US access. Any insight would be greatly appreciated, I'd love to order and begin development of my own app using your teachings.

    1. I shall look into that and get back to you.

      Although, I have had plenty of US buyers for the book so maybe they know something we don't.

    2. Thank you very much James, that's encouraging. I'd be very grateful if you are indeed able to confirm somehow.

    3. I can get no answers from anyone I know in the UK.

      You should try Betfair support at

      You may need to register but you will be speaking directly to Betfair technical support.

      If you give them the specifics of your problem; your account details, name of exchange server you want to access and that you want to use API-NG then they are the people to give you the answer.

      I hope this helps.

    4. Many thanks for your efforts, I'll do that straight away =)

  2. Hi James,

    I just purchased both of your books from Amazon I am very much looking forward to them arriving. I am a Computer Scientist myself and I am wondering if there are any tools that I will require? I know you use MS Visual Studio, is the Language some flavour of C? (I am guessing because you chose to use MS VS) does your book use scripts(Python or Perl) to report to the bot, kind of in a RESTful manner.

    Thank you so much for your time,

    This is a burning interest of mine, My final year project on my UnderGraduate Degree was "Using machine learning to predict football matches based in historical events."

    1. You should have all the tools and skills required to make best use of the book.

  3. Hi James, is it easy enough to convert the horse racing app to football? I tried changing eventTypeIds.Add("7") to eventTypeIds.Add("1") to no avail. It continues to show horse racing. Is it alot more complicated than that? Thanks for the great book!

    1. Yes, it's easy enough. Beyond the scope of my book though.

      You will need to experiment with the Visualiser and look at the JSON strings it produces. Replicate them in code and you will have what you need.

  4. Hello James,

    Regarding L MC's comment, I have a football "lay the draw" system that is price specific. What I would like to do is build a bot to scan the markets and alert me to trades sole'ly based on prices within the varios football markets. Would this be possible?

    Thank you.

    1. Yes. The book is horse racing specific but if you are an experienced software programmer then conversion to your needs should be easy enough.

  5. Hey James, I have both your books - great works. I year or so ago I completed the coding on the first book and it was working fine. I moved to another project, but I have time now to get back to BF.

    One issue I now find running the program is that after getting the BF login page and entering my credentials I get a series of pop up boxes saying :
    Script Error

    And several of these boxes keep appearing then the ap hangs. Are there any updates to your code due to changes at BF?

    1. Yes, reread the book. Find the support web page. Use the web page to change manual login to certificate login.

  6. Hello James,
    My aim is to build my own Androin app which should be automated to a certain degree. Will the book be useful for me or it dedicatede for Windows programming only?

    1. If you are an experienced Android coder then porting should not be a problem. Your problem is usage. Is this for personal or commercial use. If personal then use a desktop. I can't see how you can trade effectively with a phone. If this is for commercial use then Betfair will stop you, quite easily. You need a digital certificate to use the Betfair back end.

  7. Hi James.

    Im done with p.64 in your book but when running the script I get the message:

    "Newtonsoft.Json.JsonReaderException: 'JSON integer 2548203097 is too large or small for an Int32. Path '[0].result[0].version', line 1, position 388.'".

    this is the value of "Version" that is to big for being a Integer. Do you know what I can do to fix the problem or is there another problem with my code (typo)?

    1. Change integer to long.

      In future, can you post questions to the support web page (you'll find it near the beginning of the book) so that others can benefit from questions and answers.

      Thank you.

  8. Hi james,

    does the "ChartBox" section of your code still work? The mine not (only as concern displaying charts). Probably the problem is the url of the charts, but i am not sure...The code works greatly in the other sections ( i am using it for football). Thanks

    1. I'm away on holiday for sometime. I suggest you go to the Betfair website, right click on a chart and display it on its own. You'll see its URL at the top of your browser. From there you'll be able to see how the URL has changed and alter your code accordingly.

    2. Ok don't worry...i noticed it is a problem of italian APING...