Bot Betting Edge

Bot Betting Edge

The Blog

This blog will contain my reflections and progress within the field of modelling and predicting sports probabilities, especially for soccer matches inplay. Although placing bets on different matches I don't consider myself as a bettor, I see myself as an investor placing money where I have edge and finds value. Sports betting is fantastic!

PostgreSQL

DataPosted by Tobias Bramhed Wed, January 13, 2016 11:08:11
Lately I have been working on improving my data handling and storage process. The previous set up I used was to export all data into .csv files, and then import it to SAS and from there creating SAS tables from where I could do my analysis.

This was an easy set up, but not very efficient.

So, now I have set up a proper SQL database. I chose the PostgreSQL as its free and supports unlimited size tables. Now my bot speak directly to the SQL database, inserting and retrieving data directly.

So far so good, it seems to be working without any issues - and it seems to keep the tables smaller (in size) than the SAS tables.

The improvement continues on all parts! Good data is essential for good analysis.

  • Comments(0)//blog.bramhed.se/#post34

Betfair API NG Session

DataPosted by Tobias Bramhed Thu, June 18, 2015 12:55:35
Argh! As you might know from previous posts programming is not my cup of tea, I only have the basic skills in Visual Basic to get stuff working. As soon as something "out of the ordinary" happens I have to spend hours and hours to get it right again.

This weekend Betfair decided that I needed to refresh my session every hour instead of every 12 hours....

The effect on my bot was that it could retrieve odds data and place bets but it couldnt get betstatus and it couldnt cancel bets. The result was that my bot placed several similar bets, where it should place only one! So instead of risking 3 % on a single market I ended up risking up to 15% in some markets....

LUCKILY those bets went my way, and I could shot my bot down before serious damage were made to my portfolio. Many hours later I now have implemented safety routines and fixed the session issue in my bot.

Realy shitty when I try so hard to rule out the effect of luck in my models, and some bad coding is close to destroy everything in a couple of minutes.



  • Comments(0)//blog.bramhed.se/#post25

VB Betfair API-NG, How to part 1

DataPosted by Tobias Bramhed Tue, October 28, 2014 20:47:27
Now you have some classes that you can use to objectify your json return strings. I will give you an example of how I use CancelOrder in my code (some of this code is not created by me, parts of it found on different forums etc).


Sub CancelBetNG(ByVal betId As String, ByVal marketid As String)
Dim CancelOrderResponse As Object = Nothing
Dim strRequest As String = ""
Try
strRequest = MakeJsonRpcRequestString("cancelOrders", CancelOrdersRequestString(marketid, betid))
CancelOrderResponse = CreateRequest(app_key, session_token, strRequest, "betting")
Dim objJson = JsonConvert.DeserializeObject(Of CancelOrder)(CancelOrderResponse.ToString)

Catch ex As Exception
Print_log(Now & ":Error in CancelBetNG :" & ex.Message)
End Try
End Sub



Function CancelOrdersRequestString(ByVal MarketId As String, ByVal betId As String) As String
CancelOrdersRequestString = "{""marketId"":""" & MarketId & """,""instructions"":[{""betId"":""" & betId & """}],""customerRef"":null}"
End Function



Function MakeJsonRpcRequestString(ByVal Method As String, ByVal RequestString As String) As String
MakeJsonRpcRequestString = "{""jsonrpc"": ""2.0"", ""method"": ""SportsAPING/v1.0/" & Method & """, ""params"": " & RequestString & ", ""id"": 1}"
End Function



Function CreateRequest(AppKey As String, SessToken As String, postData As String, type As String)
Dim Url As String = "https://api.betfair.com/exchange/" & type & "/json-rpc/v1/"
Dim request As WebRequest = Nothing
Dim dataStream As Stream = Nothing
Dim response As WebResponse = Nothing
Dim strResponseStatus As String = ""
Dim reader As StreamReader = Nothing
Dim responseFromServer As String = ""

Try
request = WebRequest.Create(New Uri(Url))
request.Method = "POST"
request.ContentType = "application/json-rpc"
request.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8")
request.Headers.Add("X-Application", AppKey)
request.Headers.Add("X-Authentication", SessToken)
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
request.ContentLength = byteArray.Length
dataStream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
response = request.GetResponse()
'~~> Display the status below if required
'~~> Dim strStatus as String = CType(response, HttpWebResponse).StatusDescription
strResponseStatus = CType(response, HttpWebResponse).StatusDescription
'~~> Get the stream containing content returned by the server.
dataStream = response.GetResponseStream()
'~~> Open the stream using a StreamReader for easy access.
reader = New StreamReader(dataStream)
'~~> Read the content.
responseFromServer = reader.ReadToEnd()
'~~> Display the content below if required
'~~>Dim strShowResponse as String = responseFromServer '~~>If required
Catch ex As Exception
'~~> Show any errors in this method for an error log etc Just use a messagebox for now
Print_log(Now & ":CreateRequest Error:" & ex.Message)
End Try
Return responseFromServer '~~> Function Output
'~~> Clean up the streams.
reader.Close()
dataStream.Close()
response.Close()
End Function


  • Comments(0)//blog.bramhed.se/#post21

VB Betfair API-NG ListCurrentOrder

DataPosted by Tobias Bramhed Tue, October 28, 2014 20:32:38
The Visual Basic Class implementation of ListCurrentOrder for Betfair API-NG:

Imports Newtonsoft.Json
Imports Newtonsoft.Json.JsonTextReader
Imports Newtonsoft.Json.JsonTextWriter

Public Class ListCurrentOrder
Private m_result As ListOrderC
<JsonProperty(PropertyName:="result")> _
Public Property Result() As ListOrderC
Get
Return m_result
End Get
Set(value As ListOrderC)
m_result = value
End Set
End Property
End Class

Public Class ListOrderC
Private m_currentOrders As List(Of OrderDetail)
<JsonProperty(PropertyName:="currentOrders")> _
Public Property currentOrders() As List(Of OrderDetail)
Get
Return m_currentOrders
End Get
Set(value As List(Of OrderDetail))
m_currentOrders = value
End Set
End Property

Private m_moreAvailable As String
<JsonProperty(PropertyName:="moreAvailable")> _
Public Property moreAvailable() As String
Get
Return m_moreAvailable
End Get
Set(value As String)
m_moreAvailable = value
End Set
End Property
End Class

Public Class OrderDetail
Private m_selectionId As String
<JsonProperty(PropertyName:="selectionId")> _
Public Property selectionId() As String
Get
Return m_selectionId
End Get
Set(value As String)
m_selectionId = value
End Set
End Property

Private m_betId As String
<JsonProperty(PropertyName:="betId")> _
Public Property betId() As String
Get
Return m_betId
End Get
Set(value As String)
m_betId = value
End Set
End Property

Private m_averagePriceMatched As Decimal
<JsonProperty(PropertyName:="averagePriceMatched")> _
Public Property averagePriceMatched() As Decimal
Get
Return m_averagePriceMatched
End Get
Set(value As Decimal)
m_averagePriceMatched = value
End Set
End Property
Private m_sizeMatched As Decimal
<JsonProperty(PropertyName:="sizeMatched")> _
Public Property sizeMatched() As Decimal
Get
Return m_sizeMatched
End Get
Set(value As Decimal)
m_sizeMatched = value
End Set
End Property
Private m_sizeRemaining As Decimal
<JsonProperty(PropertyName:="sizeRemaining")> _
Public Property sizeRemaining() As Decimal
Get
Return m_sizeRemaining
End Get
Set(value As Decimal)
m_sizeRemaining = value
End Set
End Property
End Class


  • Comments(0)//blog.bramhed.se/#post20

VB Betfair API-NG CancelOrder

DataPosted by Tobias Bramhed Tue, October 28, 2014 20:30:22
VB Class implementation of CancelOrder for Betfair API-NG:

Imports Newtonsoft.Json
Imports Newtonsoft.Json.JsonTextReader
Imports Newtonsoft.Json.JsonTextWriter

Public Class CancelOrder
Private m_result As CancelInfo
<JsonProperty(PropertyName:="result")> _
Public Property Result() As CancelInfo
Get
Return m_result
End Get
Set(value As CancelInfo)
m_result = value
End Set
End Property
End Class
Public Class CancelInfo
Private m_status As String
<JsonProperty(PropertyName:="status")> _
Public Property status() As String
Get
Return m_status
End Get
Set(value As String)
m_status = value
End Set
End Property
End Class


  • Comments(0)//blog.bramhed.se/#post19

VB Betfair API-NG PlaceOrder

DataPosted by Tobias Bramhed Tue, October 28, 2014 20:28:48
Implements the class of PlaceOrder for Betfair API-NG:

Imports Newtonsoft.Json
Imports Newtonsoft.Json.JsonTextReader
Imports Newtonsoft.Json.JsonTextWriter

Public Class PlaceOrder
Private m_result As OrderInfo
<JsonProperty(PropertyName:="result")> _
Public Property Result() As OrderInfo
Get
Return m_result
End Get
Set(value As OrderInfo)
m_result = value
End Set
End Property
End Class
Public Class OrderInfo
Private m_status As String
<JsonProperty(PropertyName:="status")> _
Public Property status() As String
Get
Return m_status
End Get
Set(value As String)
m_status = value
End Set
End Property
Private m_instructionReports As List(Of ReportInfo)
<JsonProperty(PropertyName:="instructionReports")> _
Public Property instructionReports() As List(Of ReportInfo)
Get
Return m_instructionReports
End Get
Set(value As List(Of ReportInfo))
m_instructionReports = value
End Set
End Property
End Class

Public Class ReportInfo
Private m_betId As String
<JsonProperty(PropertyName:="betId")> _
Public Property betId() As String
Get
Return m_betId
End Get
Set(value As String)
m_betId = value
End Set
End Property

End Class


  • Comments(0)//blog.bramhed.se/#post18

VB Betfair API-NG MarketBook

DataPosted by Tobias Bramhed Tue, October 28, 2014 20:26:55
This is the class implementation of Betfair API-NG MarketBook for VB:

Imports Newtonsoft.Json
Imports Newtonsoft.Json.JsonTextReader
Imports Newtonsoft.Json.JsonTextWriter

Public Class MarketBook
Private m_result As List(Of Bookinfo)
<JsonProperty(PropertyName:="result")> _
Public Property Result() As List(Of Bookinfo)
Get
Return m_result
End Get
Set(value As List(Of Bookinfo))
m_result = value
End Set
End Property
End Class
Public Class Bookinfo
Private m_isMarketDataDelayed As Boolean
<JsonProperty(PropertyName:="isMarketDataDelayed")> _
Public Property isMarketDataDelayed() As Boolean
Get
Return m_isMarketDataDelayed
End Get
Set(value As Boolean)
m_isMarketDataDelayed = value
End Set
End Property
Private m_status As String
<JsonProperty(PropertyName:="status")> _
Public Property status() As String
Get
Return m_status
End Get
Set(value As String)
m_status = value
End Set
End Property
Private m_betDelay As Integer
<JsonProperty(PropertyName:="betDelay")> _
Public Property betDelay() As Integer
Get
Return m_betDelay
End Get
Set(value As Integer)
m_betDelay = value
End Set
End Property
Private m_inplay As Boolean
<JsonProperty(PropertyName:="inplay")> _
Public Property inplay() As Boolean
Get
Return m_inplay
End Get
Set(value As Boolean)
m_inplay = value
End Set
End Property
Private m_totalMatched As Decimal
<JsonProperty(PropertyName:="totalMatched")> _
Public Property totalMatched() As Decimal
Get
Return m_totalMatched
End Get
Set(value As Decimal)
m_totalMatched = value
End Set
End Property
Private m_totalAvailable As Decimal
<JsonProperty(PropertyName:="totalAvailable")> _
Public Property totalAvailable() As Decimal
Get
Return m_totalAvailable
End Get
Set(value As Decimal)
m_totalAvailable = value
End Set
End Property
Private m_runners As List(Of SelectionsBook)
<JsonProperty(PropertyName:="runners")> _
Public Property Runners() As List(Of SelectionsBook)
Get
Return m_runners
End Get
Set(value As List(Of SelectionsBook))
m_runners = value
End Set
End Property

End Class
Public Class ex_runners
Private m_availableToBack As List(Of ex_available)
<JsonProperty(PropertyName:="availableToBack")> _
Public Property availableToBack() As List(Of ex_available)
Get
Return m_availableToBack
End Get
Set(value As List(Of ex_available))
m_availableToBack = value
End Set
End Property
Private m_availableToLay As List(Of ex_available)
<JsonProperty(PropertyName:="availableToLay")> _
Public Property availableToLay() As List(Of ex_available)
Get
Return m_availableToLay
End Get
Set(value As List(Of ex_available))
m_availableToLay = value
End Set
End Property

End Class
Public Class ex_available
Private m_price As Decimal
<JsonProperty(PropertyName:="price")> _
Public Property price() As Decimal
Get
Return m_price
End Get
Set(value As Decimal)
m_price = value
End Set
End Property
Private m_size As Decimal
<JsonProperty(PropertyName:="size")> _
Public Property size() As Decimal
Get
Return m_size
End Get
Set(value As Decimal)
m_size = value
End Set
End Property

End Class

Public Class SelectionsBook
Private m_selectionId As Long
<JsonProperty(PropertyName:="selectionId")> _
Public Property SelectionId() As Long
Get
Return m_selectionId
End Get
Set(value As Long)
m_selectionId = value
End Set
End Property
Private m_status As String
<JsonProperty(PropertyName:="status")> _
Public Property status() As String
Get
Return m_status
End Get
Set(value As String)
m_status = value

End Set
End Property
Private m_lastPriceTraded As Decimal
<JsonProperty(PropertyName:="lastPriceTraded")> _
Public Property lastPriceTraded() As Decimal
Get
Return m_lastPriceTraded
End Get
Set(value As Decimal)
m_lastPriceTraded = value
End Set
End Property
Private m_totalMatched As Decimal
<JsonProperty(PropertyName:="totalMatched")> _
Public Property totalMatched() As Decimal
Get
Return m_totalMatched
End Get
Set(value As Decimal)
m_totalMatched = value
End Set
End Property
Private m_ex As ex_runners
<JsonProperty(PropertyName:="ex")> _
Public Property ex() As ex_runners
Get
Return m_ex
End Get
Set(value As ex_runners)
m_ex = value
End Set
End Property
End Class


  • Comments(0)//blog.bramhed.se/#post17

VB Betfair API-NG MarketCatalogue

DataPosted by Tobias Bramhed Tue, October 28, 2014 20:23:37
Hi,

Promised some code that might help you write your own bot. The code is in Visual Basic, and the first start is example of Class Implementation of MarketCatalogue.

Code Starts here:

Imports Newtonsoft.Json
Imports Newtonsoft.Json.JsonTextReader
Imports Newtonsoft.Json.JsonTextWriter

Public Class MarketCatalogue
Private m_result As List(Of MarketInfo)
<JsonProperty(PropertyName:="result")> _
Public Property Result() As List(Of MarketInfo)
Get
Return m_result
End Get
Set(value As List(Of MarketInfo))
m_result = value
End Set
End Property
End Class
Public Class MarketInfo
Private m_marketId As String
<JsonProperty(PropertyName:="marketId")> _
Public Property MarketId() As String
Get
Return m_marketId
End Get
Set(value As String)
m_marketId = value
End Set
End Property
Private m_marketName As String
<JsonProperty(PropertyName:="marketName")> _
Public Property marketName() As String
Get
Return m_marketName
End Get
Set(value As String)
m_marketName = value
End Set
End Property
Private m_marketStartTime As Date
<JsonProperty(PropertyName:="marketStartTime")> _
Public Property MarketStartTime() As Date
Get
Return m_marketStartTime
End Get
Set(value As Date)
m_marketStartTime = value
End Set
End Property
Private m_totalMatched As Decimal
<JsonProperty(PropertyName:="totalMatched")> _
Public Property totalMatched() As Decimal
Get
Return m_totalMatched
End Get
Set(value As Decimal)
m_totalMatched = value
End Set
End Property
Private m_runners As List(Of Selections)
<JsonProperty(PropertyName:="runners")> _
Public Property Runners() As List(Of Selections)
Get
Return m_runners
End Get
Set(value As List(Of Selections))
m_runners = value
End Set
End Property
Private m_events As MeetingsInfo
<JsonProperty(PropertyName:="event")> _
Public Property EventInfo() As MeetingsInfo
Get
Return m_events
End Get
Set(value As MeetingsInfo)
m_events = value
End Set
End Property
Private m_competition As CompetitionInfo
<JsonProperty(PropertyName:="competition")> _
Public Property competitionInfo() As CompetitionInfo
Get
Return m_competition
End Get
Set(value As CompetitionInfo)
m_competition = value
End Set
End Property
End Class
Public Class Selections
Private m_selectionId As Long
<JsonProperty(PropertyName:="selectionId")> _
Public Property SelectionId() As Long
Get
Return m_selectionId
End Get
Set(value As Long)
m_selectionId = value
End Set
End Property
Private m_runnerName As String
<JsonProperty(PropertyName:="runnerName")> _
Public Property RunnerName() As String
Get
Return m_runnerName
End Get
Set(value As String)
m_runnerName = value

End Set
End Property
Private m_sortPriority As Integer
<JsonProperty(PropertyName:="sortPriority")> _
Public Property SortNumber() As Integer
Get
Return m_sortPriority
End Get
Set(value As Integer)
m_sortPriority = value
End Set
End Property
End Class
Public Class MeetingsInfo
Private m_name As String
<JsonProperty(PropertyName:="name")> _
Public Property MeetingName() As String
Get
Return m_name
End Get
Set(value As String)
m_name = value
End Set
End Property
Private m_id As Integer
<JsonProperty(PropertyName:="id")> _
Public Property id() As Integer
Get
Return m_id
End Get
Set(value As Integer)
m_id = value
End Set
End Property
Private m_countryCode As String
<JsonProperty(PropertyName:="countryCode")> _
Public Property countryCode() As String
Get
Return m_countryCode
End Get
Set(value As String)
m_countryCode = value
End Set
End Property

End Class
Public Class CompetitionInfo
Private m_name As String
<JsonProperty(PropertyName:="name")> _
Public Property CompetitionName() As String
Get
Return m_name
End Get
Set(value As String)
m_name = value
End Set
End Property
Private m_id As Integer
<JsonProperty(PropertyName:="id")> _
Public Property Competitionid() As Integer
Get
Return m_id
End Get
Set(value As Integer)
m_id = value
End Set
End Property
End Class

- > End Code


  • Comments(0)//blog.bramhed.se/#post16
Next »