Using VB2008 to acccess the Betfair API: A tutorial

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gavvy
    Junior Member
    • Nov 2011
    • 4

    #766
    Hi,Bigsprout!
    Thank you for quick answer.

    I have other code to write to a file:

    Code:
    logfile = "C:\scachki\new\" & MarketId & ".txt"
    My.Computer.FileSystem.WriteAllText(logfile, allttr & vbCrLf, True)
    Probably, I will give a bigger piece of code. Can anyone see a non-optimal code.I am no programmer, wrote as he could.
    Code:
     
    Private Sub BFindMarket_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BFindMarket.Click
            With FindTimer
                If .Enabled Then
                    .Stop()
                    BFindMarket.ForeColor = Color.Black
                Else
                    .Interval = 60000
                    .Start()
                    BFindMarket.ForeColor = Color.Green
                End If
            End With
        End Sub
    
    Private Sub FindTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindTimer.Tick
            Print("*** Find(timer) ***")
            FindMatch(BetFairUK.getMarketPrices(MpricesReq))
    End Sub
    
    Sub FindMatch(ByVal MpriceResp As BFUK.GetMarketPricesResp)
            Dim result As String
            Dim oMarketsReq As New BFUK.GetAllMarketsReq
            Dim oMarketsResp As BFUK.GetAllMarketsResp
            Dim ttl As Double
            Dim strtarget, nott As Integer
            With oMarketsReq
                .header = oHeaderUK()
                ReDim .eventTypeIds(0) : .eventTypeIds(0) = 7
                ReDim .countries(1) : .countries(0) = "GBR" : .countries(1) = "IRL"
                .fromDate = Today
                .toDate = Today.AddDays(1)
            End With
            Print("proces=" & proces)
            If (proces = 0) Then
                Print("*** WE SEEK A NEW MATCH ***")
                oMarketsResp = BetFairUK.getAllMarkets(oMarketsReq)
                With BetFairUK.getAllMarkets(oMarketsReq)
                    CheckHeader(.header)
                    Print("ErrorCode = " & .errorCode.ToString)
                    If .errorCode = BFUK.GetAllMarketsErrorEnum.OK Then
                        Dim AllMarkets As New UnpackAllMarkets(.marketData)
                        Dim Names As String(), TodaysCard As New List(Of MarketDataType)
                        With AllMarkets
                            For i = 0 To .marketData.Length - 1
                                Names = .marketData(i).menuPath.Split("\")
                                If Names.Length = 4 AndAlso Not Names(3).StartsWith("Daily") Then
                                    If .marketData(i).noOfWinners = 1 Then
                                        TodaysCard.Add(.marketData(i))
                                    End If
                                End If
                            Next
                        End With
                        TodaysCard.Sort(New CompareMarketTimes)
                        For Each Race In TodaysCard
                            With Race
                                ttl = .totalAmountMatched
                                result = .marketId
                                Print("ttl=" & ttl & "  proces=" & proces & " result=" & result)
                                Dim reader = My.Computer.FileSystem.ReadAllText("C:\scachki\new\UsedMarket.txt")
                                    strtarget = reader.IndexOf(result)
                                    Print("strtarget=" & strtarget & "  proces=" & proces)
                                    If (strtarget = -1) Then
                                        My.Computer.FileSystem.WriteAllText("C:\scachki\new\UsedMarket.txt", result & vbCrLf, True)
                                        If ttl > 2000 Then
                                            MarketId = .marketId
                                            proces = 1
                                            find_match = 1
                                           strk = .marketName
                                            strt = .eventDate.ToLocalTime.AddHours(1).TimeOfDay.ToString
                                            With FindTimer
                                                If .Enabled Then
                                                    .Stop()
                                                    BFindMarket.ForeColor = Color.Black
                                                End If
                                            End With
                                            With PriceTimer
                                                If .Enabled Then
                                                    nott = 0
                                                Else
                                                    .Interval = 2000
                                                    .Start()
                                                    BPrices.ForeColor = Color.Green
                                                End If
                                            End With
                                        End If
                                    End If
                           End With
                        Next
                    End If
                End With
            End If
    End Sub
    
    Private Sub BPrices_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BPrices.Click
            With PriceTimer
                If .Enabled Then
                    .Stop()
                    BPrices.ForeColor = Color.Black
                Else
                    .Interval = 2000
                    .Start()
                    BPrices.ForeColor = Color.Green
                End If
            End With
    End Sub
    
    Private Sub PriceTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PriceTimer.Tick
            Print("*** Prices(timer) ***")
            Dim oMPCreq As New BFUK.GetMarketPricesCompressedReq
            With oMPCreq
                .header = oHeaderUK()
                .marketId = MarketId
            End With
            StateCount += 1
            BetFairUK.getMarketPricesCompressedAsync(oMPCreq, StateCount)
    End Sub
    
    Private Sub BetFairUK_getMarketPricesCompressedCompleted(ByVal sender As Object, ByVal e As BFUK.getMarketPricesCompressedCompletedEventArgs) Handles BetFairUK.getMarketPricesCompressedCompleted
            Dim Lay, Back, allttr, yy, logfile, nott As String
            Dim oMarketsReq As New BFUK.GetAllMarketsReq
            Dim back1, lay1 As Double
            Dim dly As Integer
            Dim yy1, yy2 As DateTime
            allttr = ""
              
            '...............................................................................................................        
            If (proces = 0) Then
                If (find_match = 1) Then
                    Print("*** WE SEEK A NEW MATCH ***")
                    With PriceTimer
                        If .Enabled Then
                            .Stop()
                            BPrices.ForeColor = Color.Black
                        End If
                    End With
                    With FindTimer
                        If .Enabled Then
                            nott = 0
                        Else
                            .Interval = 60000
                            .Start()
                            BFindMarket.ForeColor = Color.Green
                        End If
                    End With
                Else
                    Print("*** BOT STOPPED ***")
                    With PriceTimer
                        If .Enabled Then
                            .Stop()
                            BPrices.ForeColor = Color.Black
                        End If
                    End With
                End If
            Else
                Try
                    If Not e.Cancelled Then
                        With e.Result
                            CheckHeader(.header)
                            yy = .header.timestamp.ToLocalTime.AddHours(1).ToString
                            Print("ErrorCode = " & .errorCode.ToString)
                            If .errorCode = BFUK.GetMarketPricesErrorEnum.OK Then
                                Dim oMarketPrices As New UnpackMarketPricesCompressed(.marketPrices)
                                With oMarketPrices
                                    Print("MarketID = " & .marketId & " in play=" & .delay)
                                    dly = .delay
                                    '-----------------    Prepare           --------------------------------------------------
                                    allttr = allttr & yy & "   "
                                    For i = 0 To .runnerPrices.Length - 1
                                        With .runnerPrices(i)
                                            Back = ""
                                            For j = 0 To .bestPricesToBack.Length - 1
                                                With .bestPricesToBack(j)
                                                    Back = Back & "  " & .price & "/" & Int(.amountAvailable)
                                                End With
                                            Next
                                            If .bestPricesToBack.Length > 0 Then
                                                back1 = .bestPricesToBack(0).price
                                            Else
                                                Print("CURVE DATA")
                                                back1 = 0.999
                                                proces = 0
                                                Print("*** MATCH CLOSED ***")
                                            End If
                                            allttr = allttr & i + 1 & "=  " & back1
                                            Lay = ""
                                            For j = 0 To .bestPricesToLay.Length - 1
                                                With .bestPricesToLay(j)
                                                    Lay = Lay & "  " & .price & "/" & Int(.amountAvailable)
                                                End With
                                            Next
                                            If .bestPricesToLay.Length > 0 Then
                                                lay1 = .bestPricesToLay(0).price
                                            Else
                                                lay1 = 999
                                            End If
                                            allttr = allttr & "-" & lay1 & "  "
                                            Print(i + 1 & " Back = " & Back & "   Lay = " & Lay)
                                        End With
                                    Next
                                    If My.Computer.FileSystem.FileExists(logfile) Then
                                        nott = ""
                                    Else
                                        pred_yy = "23.09.2011 5:02:57"
                                        logfile = "C:\scachki\new\" & MarketId & ".txt"
                                        My.Computer.FileSystem.WriteAllText(logfile, strt & " " & strk & vbCrLf, True)
                                        Print2(strt & " " & strk)
                                    End If
                                    yy1 = DateTime.Parse(pred_yy)
                                    yy2 = DateTime.Parse(yy)
                                     '--------------------         main   part     -----------------------------------------------
                                    If yy2 > yy1 Then
                                        allttr = allttr & " " & dly
                                        logfile = "C:\scachki\new\" & MarketId & ".txt"
                                        My.Computer.FileSystem.WriteAllText(logfile, allttr & vbCrLf, True)
                                        pred_yy = yy
                                    End If
                                    '------------------------------------------------------------------------------------------------- 
                                End With
                             End If
                        End With
                    End If
                Catch ex As ApplicationException
                    Print(e.Error.Message)
                End Try
            End If
    End Sub

    Comment

    • gavvy
      Junior Member
      • Nov 2011
      • 4

      #767
      The problem is solved. The reason was a too frequent use of the procedure " PRINT".

      Comment

      • cpcGiorgi
        Junior Member
        • Nov 2011
        • 3

        #768
        How to Get the Total Match Points

        Hi All,

        how i can to get the "Total match points" Value for basketball,Tennis,Ice hokey, ...
        I mean for example Total Match Points - Under 142.5pts, Over 142.5pts

        I Use the "BetFairUK_getMarketPricesCompressedCompleted" method but i can't find any field to hold this information.

        Thanks

        Comment

        • cpcGiorgi
          Junior Member
          • Nov 2011
          • 3

          #769
          Is anybody alive in this forum?

          Comment

          • Dingo Bongo
            Junior Member
            • Jul 2009
            • 6

            #770
            Patience is a virtue...

            Comment

            • cpcGiorgi
              Junior Member
              • Nov 2011
              • 3

              #771
              Fine, Long Live Dingo Bongo !

              Comment

              • Dingo Bongo
                Junior Member
                • Jul 2009
                • 6

                #772
                I hope so.

                Anyways, your question regarding markets looks quite straightforward unless Ive misunderstood what you are after. You seem to be asking for the labels in the over/unders markets, eg over/under 2.5 goals. As they are in the same place that runners in horseracing occupy, I would imagine that a call to GetMarket would return you the info that you are after.

                Live long and prosper

                Comment

                • ahn
                  Junior Member
                  • Jul 2011
                  • 12

                  #773
                  selection removed problem

                  hi to all,

                  i have a strange problem! using the market id, asian lite id and selection id i get the live prices for the total goals event. In case that the score in a match is 0-0 then my algorithm can succesfully place back/lay bets. The problem arises after at least a goal has been scored. Then every time i try to place a bet i receive a selection removed error from the server. Does enyone know why do i get this error and how can i overcome it!!! thanks

                  Comment

                  • JayBee
                    Junior Member
                    • Oct 2010
                    • 114

                    #774
                    Matching win and RFC market prices...

                    Hello,

                    Is there a process or algorithm for matching runners in win/place horse racing markets with the 1-2/2-1 etc. in the RFC market?

                    The runnerIds in the RFC market do not match the win/place market runnerIds.

                    I assume I need to use runner numbers.

                    Can someone clarify which data in win/place markets matches against data in the RFC market so that I may link horse pairs in the win/place markets with bets in the RFC market.

                    Thank you.

                    Comment

                    • monkeymagix
                      Junior Member
                      • Jul 2010
                      • 105

                      #775
                      Best way of calculating how long a market has been inplay for

                      Hello

                      I have a Windows Service/BOT that uses the API to place bets (purely automated) which is working fine however I came up with a strategy for horse race betting that involves betting "inplay" but only up to a certain time within the event - as I don't want to place a LAY bet on a horse who is half a furlong from the line and 10 lengths ahead of the crowd.

                      As I don't get to watch the races and the BOT is purely automated I have accumulated for every racecourse the min, max and average running time for each distance and going (ground type). Using this data I can set a max time for me to place inplay bets up to.

                      For some reason I thought that the delay property of UnpackCompleteMarketPricesCompressed would return the actual no of seconds the event had been "inplay" for but it's value is only ever 1 or 0 and is only good for telling you whether the market has turned in-play or not.

                      Therefore it seems I am going to have to constantly poll up coming races looking for markets turning inplay and then once that value has switched from 0 to 1 use my own timer to work out the event running time.

                      OR is there another way of finding out how long the horse race has been running for?

                      Obviously there are a few things to think about such as
                      -False starts - I was going to wait 5-15 seconds before starting to bet to handle these but it looks like I will have to check for market suspension with my own timer
                      -Accurate timings, I don't want my timer to be out of sync with the actual event start time

                      Maybe I am missing something or there is another property in an object somewhere I could use to detect the time the event has been "inplay"?

                      I am using the free API.

                      Any help would be much appreciated. Thanks

                      Comment

                      • Dingo Bongo
                        Junior Member
                        • Jul 2009
                        • 6

                        #776
                        I might be wrong but Im pretty sure youre initial assumptions are correct. Youd need to check for the in-play indicator and then time it yourself. Id give yourself a bit of a buffer too. You may also want to consider how promptly Betfair switch the markets over from pre-market to in-play. It may only be a matter of seconds which wouldnt impact on a 3 mile chase but might be important on a 5f sprint.

                        Comment

                        • monkeymagix
                          Junior Member
                          • Jul 2010
                          • 105

                          #777
                          Hi Dingo,

                          Yes it seems that I have to log the time myself and I have come up with a threaded system that locks a race about to turn inplay using the race start date (with a few minutes either side to handle time differences on the PC running the app, the database server and the actual race off time) and then I repeatedly poll the market for that race checking the InPlay flag.

                          It seems that the market will first turn into SUSPENDED mode before going InPlay at which point I use the time that my app has recorded as the actual race start time and use the difference from the current date (within my app) as the no of seconds the race has been in action for.

                          As I only place bets up until 2/3rds of the minimum recorded duration I have logged for that course/distance/going I am giving myself a good third before the end of the race to place the bets.

                          Thanks for replying.

                          Comment

                          • Dingo Bongo
                            Junior Member
                            • Jul 2009
                            • 6

                            #778
                            As regards time you might want to synch the PCs clock inline with Betfair. I think there might be something in the API guide about it somewhere. There are are number of different internet time services that you can log in to and they update your PC's clock for you. It also helps you ensure that you dont exceed throttle limits too.

                            Comment

                            • RuiGomes
                              Junior Member
                              • Mar 2012
                              • 1

                              #779
                              How can I get the Football games occuring today?
                              I already know that the Football ID is, but I can't figure out how to get football games. I know how to get all the markets, but not each single game.

                              Help would be appreciated!

                              Comment

                              • Dingo Bongo
                                Junior Member
                                • Jul 2009
                                • 6

                                #780
                                How can I get the Football games occuring today?
                                I already know that the Football ID is, but I can't figure out how to get football games. I know how to get all the markets, but not each single game.

                                Help would be appreciated!
                                Dime bar? Dime........bar.

                                When making your market call set the date item to return only markets happening that day. Read this thread if you're not sure how.

                                If you catch the response and examine it in a text editor you'll begin to see a pattern in the event hierarchy, eg leagues have a number associated with them so you can start to filter out wheat from chaff.

                                Hope that helps.

                                Comment

                                Working...
                                X