2011-03-10

Update

Hello everybody,

I am still alive! A really long time since my last blog post. There are two reason for not posting that often in the moment: Firstly time is really limited. I am still working from 9 to 5 and doing the horses as well, so every free minute is appreciated.
The second one is that I do not have to say anything interesting. I am posting my profit/loss figure on a daily bases on the right side via twitter, just to keep you updated.
The horses are doing really well. March is already with 96.48 points in profit and with Cheltenham in front, I am really excited what March in total will bring.
On 2nd of April Muxor and me will attend a football trading seminar in Manchester. We will arrive on Friday and we will stay there until Sunday. It is the first time that we get in touch in real life with other traders and I am really excited about other people’s background, motivation and ideas about sports trading and betting. 
That’s it so far. I will keep you updated about the trading seminar and maybe other stuff, too ;-)

Cheers,

Loocie

2010-12-11

Betfair Bot: Basic Functionality

Hi,

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:

  1. Logging
  2. Local Database
  3. Saving Settings in Registry
  4. 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.

DOWNLOAD-LINK Betfair Betting Bot Project

I will now go through a few classes and files first and give information about important things to consider:

Program.cs
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:

AppDomain.CurrentDomain.UnhandledException +=
                new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

This means we are listening to the event “UnhandledException”. So if we forgot to insert some exception handling anywhere in our application we do not have search why our application throws exceptions and crashes. We will get a message box with some debugging information and we can locate the error much faster, especially in threading related source code. In addition we need to implement a method “CurrentDomain_UnhandledException” which will show the message box. This is already done. 

GUI.frmMain.cs
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.

GUI.frmLogin.cs
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.

Data.Betfair.BetfairContainer.cs
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.

web_references (web references in project explorer)

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

GUI.frmMain.LogIntoConsole(“Testmessage”);

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

GUI.frmMain.LogIntoFile(“Testmessage”);

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:
Database.SQLManager.cs:
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
SQLManager.SaveSetting(“stake”, “10.00”);
and get it back with 
SQLManager.GetSetting(“stake”);

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();.

registry(Example: Settings saved in registry)

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.

Cheers, Loocie

2010-11-28

Betting in the Zone

Hi folks,

Long time since the last blog post. I am still there and working on the betting front.
Generally, when betting related bloggers publish blog posts less frequently than earlier there are two common reasons: Their betting/trading is not successful as expected or everything is fine. Luckily I am one of the latter one.
To give you an idea about the results of the last months here is an overview:

  • Sept: +225.29 pts
  • Oct: +370.52 pts
  • Nov so far: +73.92 pts

All the profit comes from betting on horse races with the semi automated version of HorstBecker.
The change of the weather conditions and the change into the jump season had an effect of our results for November. During the first week we were 50 pts down but our confidence is still big enough to handle those down swings and currently we are looking at a solid profit of 73.92 points.
I mentioned in the last blog post, that Muxor and me enjoyed a part of our profit with a short vacation in Malta at the end of October. Here are some pictures (Click on images to enlarge):

SAM_1049Our Private Extra-Large Terrace 

SAM_1089 Looking for a speed boat to hire

SAM_1109 The Westin Dragonara Resort Malta – Our Hotel

SAM_1136 Blue Lagoon – View from the speed boat

We really enjoyed the trip to Malta. This was our first big reward we had from our betting and I am hoping for more of that.

The move into my new flat is already done, but there are still things to work on.

Today only a meeting in Kempton takes place, so I will have some time to work on the next article of the series “Creating A Betfair Bot With C#”.

That’s it so far. Have a nice Sunday!

Cheers, Loocie

2010-10-27

Preparing for Malta

Hi folks,

last few months have been fantastic. Manual version of HorstBecker performs really great. I am twittering (does this word exists??) the results on a daily bases. It is really comfortable to publish a small statement rather than writing a blog post. I can do it with my mobile, too.
malta-blaue-lagune-3

Next week I will start moving in my new flat. A lot of work to do, but before Muxor and me will have a short vacation in Malta. Weather will be great and much better than in Germany. I will post some photos and I am sure it will be really exciting.

I have not forgotten my blog series about the bots. I think I can finish it after my move.

Cheers, Loocie

2010-09-26

Busy Times

Hello,

Busy Sorry for not posting that often on the blog at the moment. I know there are a lot people out there waiting for the next blog post of the betting bot series.
Currently I have not enough time for anything. I am working on a job change and I am preparing for moving in a new flat. Really busy times at the moment.

Cheers, Loocie

2010-09-05

Summary August 2010

Hello,

Today I will give a small update about how I am progressing at the moment.
Currently I have not much time to write in detail about my betting, because I have a lot to do at work.
However, I am still progressing with the semi automated version of HorstBecker. Finding the bug I mention earlier was really helpful.

august_2010

August produced a total profit of 305.59 points!! This was the first month where I made more money from betting than money I get from work.
A few days ago I also checked the source code of the full automated version of HorstBecker and the bug was also present. I fixed that, but we need to wait for end of September to see if this will improve results or not.

I am also still working on the next section of the series “Creating A Betfair Bot With C#”. It is not yet finished, but hopefully I will get some time to finish work on that.

That’s it so far.

Cheers, Loocie

2010-08-23

Betfair Bot: Environment/Setup

Hi folks,

here is the first blog post of the series “Creating A Betfair Bot With C#”.

This section will give some information about the environment and setup of our work. This section is not a tutorial about using the presented tools, because there are also other tools available and giving a complete overview of all developer tools would cost too much time.

Hardware Environment:
There is no special hardware required. Make sure you are using a computer with Microsoft Windows. I started working on the bots under Windows XP and now I am working under Windows 7.

Software Environment:
I am working with Microsoft Visual Studio Professional 2005. vs_screenshot I am sorry, but my version of Visual Studio is in German and I can not change the language so far. If there are any questions feel free to ask.

Visual Studio is not only a code editor. It has also a forms designer which let you create windows user interfaces via drag and drop, which saves you a lot of time.

An alternative for Visual Studio might be Microsoft Visual Studio Express. I must admit I do not have any experiences with VS Express, but I am sure there are many tutorials out there about this tool.

You also need to have Microsoft .NET Framework installed on those computers you are running your bot. When installing Visual Studio I think the framework is installed too. But if you want to run your bot on another machine, maybe a Virtual Private Server, make sure that the framework is installed. To download the framework just visit Google and enter “.NET Framework”. You will get a lot of results for downloading. It is free to use.

Generally my bots are using local databases. Therefore I am using Microsoft SQL Server Compact 3.5. This is an embedded database, which means you will have a single database file with file extension SDF. You can use this database like a common and well known database like Oracle or MySQL. The advantage is you do not need to care about database management handling like table spaces and user privileges etc. You just need to create your database tables and save your data.
It is really easy to use. You can save your matched bets and prices of single selections for tracking etc.
I am using the database for saving matched selections, so I can start and stop my bot without losing data, because they are stored in my local database. If those data are only available in memory in variables an objects I would lose them if my bot crashes or if I would stop the bot. It is not required, but it is really helpful.

When using Microsoft SQL Server Compact 3.5 you need to be able to have a look at your SDF files. I am doing this via SDF-Viewer. You can also use Visual Studio for that, but for me it is much more comfortable to use SDF-Viewer.

Today’s section did not help much about getting really in touch with Betfair, but I think it is necessary to give an overview which tools I am using. If there is anybody also working on betting bots feel free to add your thoughts and comments about the tools I am using and also about the tools you are using which are not mentioned in this blog post.

The next section “Basic Functionality” will handle all the stuff I am using for logging, data management (database), settings and bot handling. We will also see how I am organising my bot projects, which will help us to use the same source code easily in another bot again without recoding the same stuff again and again. At the end of the next section we will have our first self created software. We will be able to start and stop the bot, create logging entries, we will use registry settings and we will get in touch with threading and event handling.

That’s it so far.
If there are any comments or questions feel free to drop an email or leave a comment.

Cheers, Loocie