Using VB2008 to acccess the Betfair API: A tutorial

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mumbles0
    Junior Member
    • Jan 2009
    • 240

    #376
    Price Increments

    Waldjunge,

    The table of price increments is here.

    From this you will see that 5.9 is a valid price, but 7.9 is not.
    Try 7.8 or 8.0 instead.

    Comment

    • gvigliani
      Junior Member
      • Jul 2010
      • 17

      #377
      Placebets with low stakes

      Hello Mumbles0 and friends,
      I changed the placebet to accept even lower stakes in 2 ($ or €). I have developed only to back but it will be easy to modify for the Lay bet:
      Code:
      Private Sub bPlaceBet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPlaceBet.Click
      
              Dim Market As Integer = Int(Val(Mark_id.Text))    'The Id of your market
              Dim Selection As Integer = Nothing    'The Id of your selection
              Dim BetType As BFUK.BetTypeEnum                         'For back or lay bet
              Dim tempIdBet As Long
              Dim secIdBet As Long = 0
              For i As Byte = 0 To 30
                  If C_chk(i).Checked = True Then
                      Selection = Int(Val(M_id(i).Text))
                  End If
              Next
              If rbBack.Checked Then BetType = BFUK.BetTypeEnum.B Else BetType = BFUK.BetTypeEnum.L 'Select Back or Lay bet
              BetPrice = nudPrice.Value    'The selected price
              BetSize = nudSize.Value      'The selected bet size
              If nudSize.Value < 2 And rbBack.Checked Then
                  Dim prev_odd = nudPrice.Value               'memorize value nudprice per future use
                  Dim Prev_stake = nudSize.Value              ''memorize value nudsize per future use
                  nudPrice.Value = 1000                       ' place nudprice to odd 1000,00 
                  nudSize.Value = 2                           ' place nudsize to stake 2
                  TheBetId = 0                                ' reset Thebetid variable
                  PlaceBet(Market, Selection, BetType, nudPrice.Value, nudSize.Value)    'place the bet . it will not be placed
                  While tempIdBet = 0                         'Placebet is too fast. I expect the variable to be matched
                      tempIdBet = TheBetId                   'I store the id to cancel the bet after
                  End While
                  nudPrice.Value = 1000           ' I confirm Odd to 1000 
                  BetPrice = 1000                 ' Odd to 1000 should not change
                  BetSize = nudSize.Value         'Old value = nudsize
                  nudSize.Value = nudSize.Value + Prev_stake  ' New value= oldvalue + prev_stake 
      
                  TheBetId = 0 : secIdBet = 0
                  UpdateBet(tempIdBet, nudPrice.Value, BetPrice, nudSize.Value, BetSize)        ' Update the bet at new values
                  'Now I will have two bets. The first bet to odd=1000 with stakes= 2 Euro and the second Bet to Odd=1000 but with right stakes
                  While secIdBet = 0
                      secIdBet = TheBetId ' stores the id of the bet to change it later
                  End While
                  nudPrice.Value = prev_odd        ' new odd = prev_odd (the initial odd)
                  BetPrice = 1000                  ' Odd to modify
                  nudSize.Value = Prev_stake       ' original stake
                  UpdateBet(secIdBet, nudPrice.Value, BetPrice, nudSize.Value, BetSize) ' I change the bet at the right Odd and the right stake
                  CancelBet(tempIdBet)        ' cancel the first bet
                  'all done
                  Exit Sub
              End If
              If nudSize.Value >= 2 And rbBack.Checked Then
                  PlaceBet(Market, Selection, BetType, BetPrice, BetSize)    'piazzo la scommessa che non verra' accettata .
              End If
          End Sub

      Comment

      • gvigliani
        Junior Member
        • Jul 2010
        • 17

        #378
        More market togheter

        Dear Mumbles0,

        I wanted to know if you can insert and manage multiple markets on the same page.

        Thank's

        Comment

        • gvigliani
          Junior Member
          • Jul 2010
          • 17

          #379
          Ladder

          I wanted to know if you can insert and manage multiple markets on the same page.


          and.... if is possible to access to Odds over the third level in order to prepare a ladder.

          Comment

          • gvigliani
            Junior Member
            • Jul 2010
            • 17

            #380
            Look my code

            Comment

            • gvigliani
              Junior Member
              • Jul 2010
              • 17

              #381
              Originally posted by waldjunge View Post
              I have tried to write code for betting less then the minimum stake for Lay betting, according to this guideline.

              'Documentation will not help you with this issue. Trick is to do it in few steps.
              'If you want, for example, to place £0.1 bet then place regular £2 bet on highest quote (1000 if you want to back)
              'or lowest quote (1.01 if you want to lay). The bet will not be matched.
              'Change its amount to 2.1£ and as result you will have two bets: one with £2 and the other with £0.1 amount.
              'Cancel first one, change £0.1 bet quote on the desired value and that's it

              But I can not really update the bet to get a new betID and a new st
              Look my code.
              Best Regards

              Comment

              • WStecher
                Junior Member
                • Aug 2010
                • 2

                #382
                Get results of all matches?

                Hello,

                first at all I want to say THANKS to this gread Thread. It helped me very much building my own Betfair software. At the end I still have one question:

                Is there any possibility in the betfair API to get the results (I only want to know which team or player has won) of all matches in a market?

                With getAccountStatement() I can get the results for each match I have placed a bet. But I also want to know the winning player/team for the matches I don't have placed a bet. So I can build up a history of what I could have had won if I would have placed a bet.

                Thanks for your ideas.

                Comment

                • granted
                  Junior Member
                  • Jun 2009
                  • 14

                  #383
                  how would i add sorting by times to this sub?

                  Private Sub bInPlay_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bInPlay.Click
                  Print("*** InPlay Markets ***")
                  Dim oMarketsReq As New BFAU.GetAllMarketsReq
                  With oMarketsReq
                  .header = oHeaderAU()
                  .fromDate = Now
                  .toDate = Now.AddDays(1) 'Specifies next 24 hour period
                  End With
                  With BetFairAU.getAllMarkets(oMarketsReq) 'Call the AU API
                  CheckHeader(.header)
                  Print("ErrorCode = " & .errorCode.ToString)
                  If .errorCode = BFAU.GetAllMarketsErrorEnum.OK Then
                  Dim AllMarkets As New UnpackAllMarkets(.marketData) 'Unpack .marketData
                  With AllMarkets
                  For i = 0 To .marketData.Length - 1
                  With .marketData(i)
                  If .turningInPlay Then 'This market will become InPlay
                  Print(.eventDate.ToLocalTime.TimeOfDay.ToString & " " & .marketId & " " & .menuPath & "\" & .marketName)
                  End If
                  End With

                  Next
                  End With
                  End If
                  End With
                  End Sub

                  i want the printed list to be sorted by earliest to latest...but cant figure it out

                  thanks
                  christian

                  Comment

                  • WStecher
                    Junior Member
                    • Aug 2010
                    • 2

                    #384
                    @granted: how would i add sorting by times to this sub?

                    Hey granted,

                    I think you have to put the data in a datatable and create a dataview on it.
                    The dataview can be sorted. This will bring the data in a sorted tableview to you. If you don't want to have a DataGridView on your form, you can set visible = false to the datagridview.

                    Just one idea. I would do it like this:
                    First place a datagridview to your form and name it "DataGrid1"

                    Add the following code to your class (not the sub):
                    Code:
                    Private dtInPlay As New DataTable("InPlay")
                    
                    With dtInPlay.Columns
                       .Add("EventDate", GetType(System.DateTime))
                       .Add("MarketID", GetType(System.String))
                       .Add("Match", GetType(System.String))
                       .Add("MarketName", GetType(System.String))
                    End With
                    
                    DataGrid1.DataSource = dtInPlay
                    Add the following code to your "bInPlay_Click_1" Sub
                    Code:
                    Dim matchdata(4) As Object
                    Replace the code from
                    - With AllMarkets
                    to
                    - End With
                    in your "bInPlay_Click_1" Sub with the following code:
                    Code:
                    With AllMarkets
                    
                    For i = 0 To .marketData.Length - 1
                    
                       With .marketData(i)
                    
                          If .turningInPlay Then
                    
                             matchdata(0) = .eventDate.ToLocalTime
                             matchdata(1) = .marketID
                             matchdata(2) = .menuPath
                             matchdata(3) = .marketName
                    
                             dtInPlay.Rows.Add(matchdata)
                    
                          End If
                    
                       End With
                    
                    Next
                    
                    DataGrid1.Sort(DataGrid1.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
                    
                    End With
                    
                    'the data will be displayed sorted by EventDate in the DataGridView  
                    'if you want to print it with the function "print()", add the following code:
                    
                    For Each DataGridRow As DataRow In DataGrid1.Rows
                    Print(DataGridRow.Item(0).ToString & " " & DataGridRow.Item(1).ToString & " " & DataGridRow.Item(2).ToString & "\" & DataGridRow.Item(3).ToString)
                    Next

                    Comment

                    • Mumbles0
                      Junior Member
                      • Jan 2009
                      • 240

                      #385
                      Event time sorting

                      granted,

                      There are many ways of achieving this. WStecher has outlined a way of loading the market data into a DataGridView control, then sorting the EventDate column. A more direct way would be to use a comparer class similar to what was done in Step 17.

                      Code:
                      Class CompareMarketTimes
                        Implements IComparer(Of MarketDataType)
                        Public Function Compare(ByVal x As Unpack.MarketDataType, ByVal y As Unpack.MarketDataType) As Integer Implements System.Collections.Generic.IComparer(Of Unpack.MarketDataType).Compare
                          Return Date.Compare(x.eventDate, y.eventDate)
                        End Function
                      End Class
                      Then add a line to perform the sort using an instance of this class:

                      Code:
                      [COLOR="Gray"]  .......
                        Dim AllMarkets As New UnpackAllMarkets(.marketData) 'Unpack .marketData
                        With AllMarkets
                          [COLOR="Black"]Array.Sort(.marketData, New CompareMarketTimes)[/COLOR]
                          For i = 0 To .marketData.Length - 1
                            With .marketData(i)
                            ..........[/COLOR]
                      This sorts the .marketData array according to the .eventDate property.

                      Comment

                      • Mumbles0
                        Junior Member
                        • Jan 2009
                        • 240

                        #386
                        Results

                        WStecher,

                        I don't think you can get results of matches via the API. You may have to resort to "scraping" web pages from sports websites.

                        Has anyone got any suggestions?

                        You could perhaps ask the question in the "General Sports Programming" section of the Forum.

                        Comment

                        • Mumbles0
                          Junior Member
                          • Jan 2009
                          • 240

                          #387
                          Price ladder &amp; multi markets

                          gviglinani,

                          If you need more than 3 best prices (for a price ladder), call getCompleteMarketPricesCompressed. This returns all prices for each runner. See step 10.

                          Working with multiple markets is more complicated, particularly if you are using async calls. When you receive a response you have to determine which market it is for. This is OK if you call getMarket or getMarketPricesCompressed because the marketId is returned in the response. This lets you update the correct group of controls for each market.

                          Comment

                          • granted
                            Junior Member
                            • Jun 2009
                            • 14

                            #388
                            load market runner error in my code

                            i have a text box called marketidt and a button called Load, so i was hoping to code a load runners PRINT like we did before with events, login etc

                            and here is the code i tried:

                            Private Sub bLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bLoad.Click
                            Print("*** MarketRunners ***")
                            Dim oGetMarketReq As New BFAU.GetMarketReq 'Create the request object
                            Dim oGetMarketResp As BFAU.GetMarketResp 'Create a variable for the response object
                            With oGetMarketReq
                            .header = oHeaderAU() 'Load request parameters
                            .marketId = marketidt
                            End With
                            With BetFairAU.getMarket(oGetMarketReq) 'Call the API
                            CheckHeader(.header)
                            Print("ErrorCode = " & .errorCode.ToString)
                            If .errorCode = BFAU.GetMarketErrorEnum.OK Then
                            Dim Markets As New UnpackAllMarkets(.market) 'Unpack .market
                            With Markets
                            For i = 0 To .market.Length - 1

                            Next
                            Print(.runners)
                            End With

                            End If
                            End With

                            End Sub

                            the error i get is that the marketidt text box cannot be converted to an integer and the other error i get is with Print("*** MarketRunners ***")

                            it also told me that oGetMarketResp variable was not being used

                            any help would be appreciated

                            thanks
                            Last edited by granted; 11-08-2010, 03:17 PM.

                            Comment

                            • gvigliani
                              Junior Member
                              • Jul 2010
                              • 17

                              #389
                              Originally posted by Mumbles0 View Post
                              gvigliani,

                              If you need more than 3 best prices (for a price ladder), call getCompleteMarketPricesCompressed. This returns all prices for each runner. See step 10.

                              Working with multiple markets is more complicated, particularly if you are using async calls. When you receive a response you have to determine which market it is for. This is OK if you call getMarket or getMarketPricesCompressed because the marketId is returned in the response. This lets you update the correct group of controls for each market.
                              Dear Mumbles0,
                              I have tried in every way but I can not find a way to access different shares to be included in a Ladder:
                              My code is:
                              Code:
                              Private Sub BetFairUK_getCompleteMarketPricesCompressedCompleted(ByVal sender As Object, ByVal e As BFUK.getCompleteMarketPricesCompressedCompletedEventArgs) Handles BetFairUK.getCompleteMarketPricesCompressedCompleted
                                      Dim RaceStart As Date = Nothing        'The time when the race turns in-play
                                      Dim RaceStarted As Boolean = Nothing    'A flag, set True when the race has started
                                      Try
                                          If Not e.Cancelled Then
                              
                                              With e.Result
                              
                                                  CheckHeader(.header)
                                                  PrintRes("ErrorCode = " & .errorCode.ToString)
                                                  If .errorCode = BFUK.GetCompleteMarketPricesErrorEnum.OK Then
                                                      Dim oMarketPrices As New UnpackCompleteMarketPricesCompressed(.completeMarketPrices)
                                                      With oMarketPrices
                                                          t_run.Text = .runnerInfo.Length
                                                          If .delay > 0 Then     'Race is in-play
                                                              Mark_status.BackColor = Color.Green
                                                              Mark_status.Text = "Market In Play"
                                                              If RaceStarted Then
                                                                  manca.Text = Now.Subtract(RaceStart).ToString.Substring(0, 8) 'Update the time label
                                                              Else           'Initialize the variables
                                                                  RaceStart = Now       'Save the race start time
                                                                  RaceStarted = True    'Set the flag
                                                              End If
                                                          Else
                                                              Mark_status.BackColor = System.Drawing.Color.FromArgb(CType(CType(47, Byte), Integer), CType(CType(70, Byte), Integer), CType(CType(232, Byte), Integer))
                                                          End If
                              
                                                      End With
                                                  End If
                                              End With
                                          End If
                               
                                      Catch ex As ApplicationException
                                          PrintRes(e.Error.Message)
                                      End Try
                                  End Sub
                              Can you give me a help to better understand?

                              Thank you so much!

                              gvigliani

                              Comment

                              • Mumbles0
                                Junior Member
                                • Jan 2009
                                • 240

                                #390
                                Reply to granted (getMarket code)

                                granted,

                                You learn by your mistakes.

                                Code:
                                Private Sub bLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bLoad.Click
                                
                                  Print("*** MarketRunners ***")      '[COLOR="Red"]<<< Note 1[/COLOR]
                                  Dim oGetMarketReq As New BFAU.GetMarketReq 'Create the request object
                                  Dim oGetMarketResp As BFAU.GetMarketResp '[COLOR="Red"]<<< Note 2[/COLOR]
                                  With oGetMarketReq
                                    .header = oHeaderAU() 'Load request parameters
                                    .marketId = marketidt      '[COLOR="Red"]<<< Note 3[/COLOR]
                                  End With
                                  With BetFairAU.getMarket(oGetMarketReq) 'Call the API
                                    CheckHeader(.header)
                                    Print("ErrorCode = " & .errorCode.ToString)
                                    If .errorCode = BFAU.GetMarketErrorEnum.OK Then
                                      Dim Markets As New UnpackAllMarkets(.market)  '[COLOR="Red"]<<< Note 4[/COLOR]
                                      With Markets
                                        For i = 0 To .market.Length - 1      
                                               '[COLOR="Red"]<<< Note 5[/COLOR]
                                        Next
                                        Print(.runners)
                                      End With
                                    End If
                                  End With
                                
                                End Sub
                                1. If you are having a problem with this my guess that the Print sub of Step 1 is missing. Ensure that this sub (and its associated textbox tLog) is included somewhere in your project.

                                2. The compiler is telling you that oGetMarketResp is unused because you’re not using it.

                                3. The .marketId property of the request object can only hold an integer. marketidt is the textbox object, not what’s in it. You cannot assign this object to an integer, hence the error. What’s in the textbox is given by the .Text property. So try:

                                  .marketId = marketidt.Text
                                4. You will be getting an error here. UnpackAllMarkets is a class to convert the data string returned by the API call getAllMarkets. It won’t work here because you are calling getMarket which returns an object which does not require unpacking. This statement is not right, so removed it. But when you do you will get more errors ...

                                5. This For..Next loop contains no statements - it is useless. This suggests that you need to understand more about programming. There are many VB tutorials on the Internet, for example here.


                                We covered what you are trying to do here in Step 12. Try to understand how we printed the list of runners in this step.

                                Comment

                                Working...
                                X