This is the 2nd post of the series “Creating A Betfair Bot With C#”.
In the first blog post of this series I mentioned all the tools and software I am using when working on betting bots based on .NET C#.
Before I will start I just want to mention that you are using the given information and software on your own risk. I am not responsible for any problems and issues.
Today we will step into development and I will write about the following functionalities every bot of mine has:
- Local Database
- Saving Settings in Registry
- Bot Handling (Start/Stop)
You will also see the structure of the bot, but most of the classes are not yet finished, especially the ones which are related to the Betfair API.
It might be useful to download the Visual Studio 2005 project first and look at the source code and the descriptions in this series simultaneously.
I will now go through a few classes and files first and give information about important things to consider:
This file contains the main entry of our application which means if you start the application the first call is the so called “main method”. Generally this main method calls the first windows form.
To make things easier I always add the following line as the first command in the main method:
This is our main windows form with all the controls we need to show market information or any information we want. To make it easier I only will place a few controls for debugging information.
This windows form is the form we are using for login into Betfair. Currently there is no functionality implemented. I will update this form in the next article, which is related to Betfair API.
This class will handle all the connectivity to Betfair API. Currently it is not yet implemented, but I added it already to give you an overview of the structure of the bot.
I also already added web references to Betfair API. We will need this in the next article.
I only added a reference to global service (for login functionality etc) and a reference to UK exchange (for getting prices, markets etc). If your bot should also work for AUS markets you need to add a reference to AUS exchange too.
1. Logging Functionality
When developing an application it is always useful to know what is going on in your application. Therefore I am always creating logging functionality in every application I am working on. The logging in this bot consists of two parts:
a) General logging into a console:
This is done via method “LogIntoConsole” in form frmMain.
Use the call
to log something into console. This logging is working non-blocking, that means even if you log many large messages or variable contents the user interface is still responsive.
b) Logging into a log file:
This is done via LoggingTask. To perform a log into a log file use call
There will be created a log file in the path where the executable is. For each day you will get another file, so the file name contains a part of the date, something like “debug_11_30.log” for 30th November. This logging is done in a background thread, so again if you are logging huge data the application is still responsive an not blocking.
2. Local Database
Using local databases is really important, especially if you want to be able to load data into your bot from external data sources. For example you can create a bot which places bets on horse racing depending on various criteria from Adrian Massey’s Rating from http://www.adrianmassey.com/28/rating.php.
You can let your bot scrap the webpage or save it manually into MS Excel and save it as comma separated value file and then let the bot read the file and place the bets for you, if certain criteria fit your strategy. For saving data like this you can use a local database in your bot. Therefore I have created a SQL Manager which does all the work for you:
For demonstration how to work with a database I added a SQL table called “Settings” for saving settings for the bot. So you can save settings like stake, max odds, min odds etc and every time you start the bot all the settings will be loaded from the database.
I only created a sample of this SQL Manager. To get your settings working you need to add some functionality. The same, if you want to save horse racing data. The steps to add something like this are:
1. Write a method for creating the SQL table like I did in method SQLManager.createSettingTable().
2. Call this method in SQLManager.Initialize(). So now your table will be created at start of application, if it is not yet present.
3. Write a method for saving objects into database like I did in method SQLManager.SaveSetting(string key, string value).
4. Write a method for getting objects from database like I did in method SQLManager.GetSetting(string key).
So now you can save settings for example like this anywhere in the bot with
and get it back with
I am not using a database for saving settings, but just want to give an example. I am saving settings via Registry:
3. Saving Settings in Registry
This is done via class Data.CRegistry.cs. All settings will be saved in HKEY_CURRENT_USER\Software\MyBot.
I added a few methods in CRegistry for demonstration. So with this example you can save stake size, min odds and max odds. If you want to add further settings, just copy one of the methods and modify it accordingly. Saving stake sizes for example within registry call CRegistry.saveStakeSize(10.00); and get it from registry call double stakeSize = CRegistry.getStakeSize();.
It is up to you to decide whether you choose database or registry for saving settings.
4. Bot Handling
What do I mean with “bot handling”? When using an automated betting system it is always useful to start and stop the bot. Therefore I created an enumeration called BotStatusEnum, which indicates whether the bot is running or not.
Another important thing to consider is, that starting the application via clicking on the executable does not automatically mean, that the bot is running. To start the bot itself you need to click on start button in the top left menu. If the bot then is running you can stop it by clicking on stop button. I also added a closing form handler which means if the bot is running and you click on the X on top right unintentionally, you will always get a message that the bot is still running an can not be closed. The is really important if your bot trades automatically on Betfair. So you can implement rules for automatically closing trades on application exit to keep your liability as small as possible.
Wow, what a huge blog post. Hopefully I did not miss any information. If so, feel free to drop an email or leave a comment. In the next two blog posts of this series we will step into Betfair API handling. This will be really interesting, especially login/logout, market handling (add and remove markets), getting prices and placing bets.