How to Use Calc Macros for Horse Race Analysis

Analyzing Race Results in OpenOffice

© Mark Alexander Bain

Nov 27, 2008
Calc Spreadsheets and Horse Racing Results, Mark Alexander Bain
Analyzing horse race results can be very complicated, but the task is greatly simplified by doing the analysis in a Calc spreadsheet

Every few minutes there is a horse race starting somewhere in the world, and the details for all of those races are on the Internet; so, the initial questions must be

  • how easy is it to analyze that data?
  • can betting systems be tested - for instance, what is the profitablity in backing every horse in the race?

And the answer? The answer is to load the data into OpenOffice.org Calc and let macros do all of the hard work.

A Real Horse Race: Wetherby - 26th November 2008 12:20

An example of the results of a real race horse are the 12:20 at Wetherby on the 26th November from easyodds.com:

1st Albany C I Gillies 15/8 f
2nd Mirjan J P O'Farrell 9/2
3rd Monolith F Davis 11/1
4th Just Waz J Halliday 66/1
5th Something Gold George Strickland 18/1
6th Fair Spin Liam Reeves 100/1
7th Three Lions J W Farrelly 5/1
8th Bawnafaugh M O'Connell 200/1
9th Ad Murum Mathew Dickinson 14/1
10th Willie The Fish Gary Rutherford 12/1
11th Amjad T Messenger 100/1
12th Mr Twins A R Adams 40/1
13th Nayodabayo D N Cullinane 18/1
14th Oops Oliver Williams 125/1
15th Minster Lake Paul Callaghan 300/1
Fell Alecia J Kington 18/1

Obtaining the Data

Before the data can be analyzed it must be loaded into Calc, however, that's not quite as easy as it sounds:

  • some sporting web sites have the download facility disabled
  • the HTML in each web site will vary greatly and may be very complex
  • the HTML making up any web page is likely to change without warning

With that in mind it's probably easier to copy and paste the information directly from the web page and into Calc; however, that conversion process may turn some of the odds into dates:

1st 15/8 f
2nd 09/02/08
3rd 11/01/08
4th 66/1

The job of the first macro, therefore, is to format the odds correctly.

A Macro for Formatting Odds

When Calc imports the horse race information it will take some of the odds (such as 9/2) to be a day and a month; the macros task, therefore, is to turn the dates back into odds:

Sub convert_odds (SheetNo as Integer)
Dim odds_cell
Dim c, r As Integer
Dim odds
r = 0
c = 3 'Column D
odds_cell = thisComponent.Sheets(SheetNo).getCellByPosition(c, r)
while odds_cell.String <> ""
odds = split (odds_cell.String, "/")
odds_cell.String = odds(0) & "/" & odds(1)
r = r + 1
odds_cell = thisComponent.Sheets(SheetNo).getCellByPosition(c, r)
Wend

If the macro is run then the odds column will be formatted correctly:

1st 15/8 f
2nd 09/02
3rd 11/01
4th 66/1

The next stage is to calculate how much can be won in the horse race.

A Macro for Calculating Winning Returns

The next macro takes the odds for each horse running and applies them to a stake (in this case $1 bet on each horse) thereby calculating what the winnings will be for each horse:

Sub calculate_winning_returns (SheetNo as Integer, Stake as Double)
Dim odd, favorite, odds_cell, winnings_cell
Dim odds_column, winnings_column, r As Integer
odds_column = 3 'Column D
winnings_column = 4 'Column E
r = 0 'Row 1
odds_cell = thisComponent.Sheets(SheetNo).getCellByPosition(odds_column, r)
while odds_cell.String <> ""
odds = split (odds_cell.String, "/")
'Remove the f from the favorite odds
favorite = split (odds(1), " ")
If ubound(favorite) = 1 Then
odds(1) = favorite(0)
End If
winnings_cell = thisComponent.Sheets(SheetNo).getCellByPosition(winnings_column, r)
winnings_cell.value = Stake * odds(0) / odds(1) + Stake
r = r + 1
odds_cell = thisComponent.Sheets(SheetNo).getCellByPosition(odds_column, r)
Wend
End Sub

A Macro to Analyze the Race Horse Results

With all of the data processed the next stage is to create a new sheet that contains a summary of the analysis:

Sub create_summary (no_sheets As Integer)
Dim cell, sheet
dim c, r as Integer
sheet = thisComponent.createInstance ("com.sun.star.sheet.Spreadsheet")
thisComponent.Sheets.insertByName("Summary", sheet)
c = 0
r = 0
For r = 1 to no_sheets
cell = sheet.getCellRangeByName("A" & r)
cell.Formula = "=Counta(Sheet" & r & ".A1:A40)"
cell = sheet.getCellRangeByName("B" & r )
cell.Formula = "=Sheet" & r & ".E1"
Next r
r = r + 1
cell = sheet.getCellRangeByName("A" & r)
cell.Formula = "=SUM(A1:A" & r - 1 & ")"
cell = sheet.getCellRangeByName("B" & r)
cell.Formula = "=SUM(B1:B" & r - 1 & ")"
End Sub

Running all of the Macros

The races at Wetherby on the 26th November 2008 were:

  • 12:20
  • 12:50
  • 13:20
  • 13:50
  • 14:25
  • 15:00
  • 15:35

And so, if the results of these is each placed in its own sheet in the spreadsheet then one final macro is required analyze the data; it will process all 7 sheets and then create the summary sheet:

Sub Main
Dim s as Integer
For s = 0 To 6
convert_odds (s)
calculate_winning_returns (s, 1)
Next s
create_summary (s)
End Sub

Conclusion

At the end of the process the summary sheets will contain the results of the days betting of $1 on every horse, and this is:

  • $88 spent
  • $47.13 won

giving an overall loss of $40.87; which goes to show that creating macros to analyze horse races is easy, but making a profit with them is not.


The copyright of the article How to Use Calc Macros for Horse Race Analysis in Computer Programming is owned by Mark Alexander Bain. Permission to republish How to Use Calc Macros for Horse Race Analysis in print or online must be granted by the author in writing.


Calc Spreadsheets and Horse Racing Results, Mark Alexander Bain
Analyze Horse Races in a Spreadsheet, Mark Alexander Bain
A Macro Generated Spreadsheet, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo