Using VB2008 to acccess the Betfair API: A tutorial

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Drifter
    Junior Member
    • Mar 2009
    • 30

    #106
    on DB connections

    If you look in the Headfirst C# (sorry... ) book (google for it if you don't know it, can't find it in hard copy etc.), the very first example (of porting a rolodex) shows how to connect to the SQL server lite which will almost certainly have been installed when you installed VS2005/8. The example is very clear, very quick, and you can bang out the C# version in 20 minutes. Doing a VB version ought not to take much longer (though I haven't done it [in VB]), and I suspect it may be more stable and easier than getting it into to Access.

    If you go that route, perhaps consider posting it as an example would be of interest to the wider community on the parallel 'questions' thread.

    Comment

    • willyray1
      Junior Member
      • Apr 2009
      • 9

      #107
      Hi all, I am trying to get the bestprices to back/lay and as they're not readily available in BetFairUK_getCompleteMarketPricesCompressedComplet ed I am trying to use BetFairUK_getMarketPricesCompressedCompleted. There is no .runnerInfo, so using runnerPrices. However code getting a System.Reflection.TargetInvocationException error. Anyone know how to sort the MarketPrices?

      Comment

      • Vadim
        Junior Member
        • Mar 2009
        • 12

        #108
        marketData question

        Question. In GetAllMarkets if eventTypeIds = 15 (Greyhound - Todays Card), in response marketData = "". In Horse Racing - Todays Card is similar. What is the problem?

        Comment

        • bfhopsen
          Junior Member
          • Feb 2009
          • 3

          #109
          I have noticed same behavior as you can see in http://forum.bdp.betfair.com/showthread.php?t=214.
          GetAllMarkets does not work on Todays Card
          I am using GetEvents to extract markets from Todays Card.

          Comment

          • TIP102
            Junior Member
            • Apr 2009
            • 3

            #110
            Hi all,

            Thank you for answering question about save data to database I am still trying to work this out. But before saving data I tried to call GetInPlayMarkets with this code:

            Private Sub bInPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bInPlay.Click
            Print("*** InPlay Markets ****")
            Dim oInPlayReq As New BFUK.GetInPlayMarketsReq
            Dim oInPlayResp As New BFUK.GetInPlayMarketsResp
            oInPlayReq.header = oHeaderUK()
            oInPlayResp = BetFairUK.getInPlayMarkets(oInPlayReq)
            With oInPlayResp
            CheckHeader(.header)
            Print("ErrorCode = " & .errorCode.ToString)
            End With
            End Sub

            When I start debuging I recive error:

            *** InPlay Markets ****
            HeaderCode = SERVICE_NOT_AVAILABLE_IN_PRODUCT
            ErrorCode = API_ERROR

            Please can you help me, am I missing something obvious I tried to follow tutorial step by step.

            Best regrads!

            Comment

            • Mumbles0
              Junior Member
              • Jan 2009
              • 240

              #111
              Reply to TIP102 (re: getInPlayMarkets)

              getInPlayMarkets in not available on the Free API. Check the product comparison table here. That's what your error message is telling you.

              The data that is returned by getInPlayMarkets can be obtained from getAllMarkets. In the request specify the next 24-hour period. From the response simply filter out the markets which have .turningInPlay = True

              Code:
              [COLOR="Gray"]Private Sub bInPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bInPlay.Click
                Print("*** InPlay Markets ***")
                Dim oMarketsReq As New BFUK.GetAllMarketsReq
                With oMarketsReq
                  .header = oHeaderUK()
               [COLOR="Black"]   .fromDate = Now
                  .toDate = Now.AddDays(1)  'Specifies next 24 hour period[/COLOR]
                End With
                With BetfairUK.getAllMarkets(oMarketsReq)    'Call the UK API
                  CheckHeader(.header)
                  Print("ErrorCode = " & .errorCode.ToString)
                  If .errorCode = BFUK.GetAllMarketsErrorEnum.OK Then
                    Dim AllMarkets As New UnpackAllMarkets(.marketData)  'Unpack .marketData
                    With AllMarkets
                      For i = 0 To .marketData.Length - 1
                        With .marketData(i)
              [COLOR="Black"]            If .turningInPlay Then  'This market will become InPlay
                            Print(.menuPath & "\" & .marketName)
                          End If[/COLOR]
                        End With
                      Next
                    End With
                  End If
                End With
              End Sub[/COLOR]

              Comment

              • Mumbles0
                Junior Member
                • Jan 2009
                • 240

                #112
                Reply to Vadim (re: Todays Card)

                Just repeating what has been said by others:

                getAllMarkets does not return data for eventTypeIds:
                • 13 (Horse Racing - Todays Card)
                • 14 (Soccer - Fixtures)
                • 15 (Greyhound - Todays Card)

                Comment

                • Mumbles0
                  Junior Member
                  • Jan 2009
                  • 240

                  #113
                  Reply to willyray1 (Re: Best Back/Lay prices)

                  Although you can get best back/lay prices from getCompleteMarketPricesCompressed, it is easier to obtain them from getMarketPricesCompressed (covered in Step 14). Within each .runnerPrices array element there are two arrays .bestPricesToBack and .bestPricesToLay which contain the top 3 prices (if they exist). These prices are exactly as shown on the website. To demonstrate, add 2 extra lines to this sub:

                  Code:
                  [COLOR="Gray"]Private Sub BetFairUK_getMarketPricesCompressedCompleted(ByVal sender As Object, ByVal e As BFUK.getMarketPricesCompressedCompletedEventArgs) Handles BetFairUK.getMarketPricesCompressedCompleted
                    Try
                      ..........
                  
                        With oMarketPrices
                          Print(.marketId & " " & .marketStatus.ToString & " " & .runnerPrices.Length & " runners")
                          'Process returned market prices here.
                          For i = 0 To .runnerPrices.Length - 1
                            With .runnerPrices(i)
                              Print(.sortOrder & " " & .selectionId & " " & .totalAmountMatched & " " & .lastPriceMatched)
                  [COLOR="Black"]            If .bestPricesToBack.Length > 0 Then Print("Best back = " & .bestPricesToBack(0).price)
                              If .bestPricesToLay.Length > 0 Then Print("Best lay = " & .bestPricesToLay(0).price)[/COLOR]
                            End With
                          Next
                        End With
                    
                      ..........
                  
                    End Try
                  End Sub[/COLOR]
                  Here we assume the best price is in each array element 0. We print it only if there is at least one price for the runner.

                  If we don’t include the tests (.Length > 0) and we encounter a runner having no back or lay prices then a run-time exception will occur, displaying the Exception Assistant. This tells us that we have “TargetInvocationException”. If you click the “View Detail...” link you will see that the InnerException is: "Index was outside the bounds of the array." This tells us exactly what is wrong.

                  I’m guessing, but I think you might have been having this problem.

                  Comment

                  • willyray1
                    Junior Member
                    • Apr 2009
                    • 9

                    #114
                    Thanks for the response mumbles, I am actually trying to sort the sortOrder in your above example:
                    Print(.sortOrder & " " & .selectionId & " " & .totalAmountMatched & " " & .lastPriceMatched)

                    What I am doing in this order:
                    1. Load form and treeview
                    2. Select market from tree
                    3. Check to see if a tab exists with the market id, if not create one
                    4. Use the number of runners from the marketdata to create a panel for each runner with 6 buttons for the odds in each panel and an overall timer to refresh the market using the GetMarketPricesCompressedReq to refresh the prices every second.
                    5. Load the runner names from oMarketResp

                    I think its something to do with the timer when I switch tabs so will have to do a bit more debugging however whenever it falls over I get the TargetInvocationException error. I might set the timer when the tab is selected only.

                    Comment

                    • Vadim
                      Junior Member
                      • Mar 2009
                      • 12

                      #115
                      ???

                      Code:
                       Private Sub bPlaceBets_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPlaceBets.Click
                              Print("*** PlaceBets ***")
                              Dim oPlaceBetsReq As New BFUK.PlaceBetsReq     
                              Dim oPlaceBetsResp As New BFUK.PlaceBetsResp    
                              Dim obets As New BFUK.PlaceBets
                              With oPlaceBetsReq
                                  .header = oHeaderUK()
                                  With obets
                                      .asianLineId = 0
                                      .betCategoryType = BFUK.BetCategoryTypeEnum.NONE
                                      .betPersistenceType = BFUK.BetPersistenceTypeEnum.NONE
                                      .betType = BFUK.BetTypeEnum.B
                                      .marketId = 100535013
                                      .price = 1000
                                      .selectionId = 204703
                                      .size = 4.0
                                  End With
                              End With
                              oPlaceBetsResp = BetFairUK.placeBets(oPlaceBetsReq)    
                              With oPlaceBetsResp
                                  CheckHeader(.header)
                                  Print("ErrorCode = " & .errorCode.ToString)
                                  With .betResults
                                     ...
                      HeaderCode = OK
                      ErrorCode = BETWEEN_1_AND_60_BETS_REQUIRED

                      What's mistake?

                      Comment

                      • Mumbles0
                        Junior Member
                        • Jan 2009
                        • 240

                        #116
                        Reply to Vadim (re: placeBets)

                        There are 2 problems:

                        (1) You are calling placeBets with oPlaceBetsReq.bets = Nothing because you are not assigning the obets object to this parameter. This is causing the error. Add the 2 lines as shown. Note that .bets is an array. For a single bet we put the bet details into element 0.

                        (2) Set bspLiability = 0 because API requires a value here. API has bug (#34314).

                        Also (3), Not a problem, but perhaps better to use BetCategoryEnum.E rather than BetCategoryEnum.NONE. (Although it works OK either way)

                        Code:
                        Private Sub bPlaceBets_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPlaceBets.Click
                          Print("*** PlaceBets ***")
                          Dim oPlaceBetsReq As New BFUK.PlaceBetsReq
                          Dim oPlaceBetsResp As New BFUK.PlaceBetsResp
                          Dim obets As New BFUK.PlaceBets
                          With oPlaceBetsReq
                            .header = oHeaderUK()
                            With obets
                              .asianLineId = 0
                              .betCategoryType = BFUK.BetCategoryTypeEnum.E             '<<< Perhaps use 'E' (note 3)
                              .betPersistenceType = BFUK.BetPersistenceTypeEnum.NONE
                              .betType = BFUK.BetTypeEnum.B
                              .marketId = [I]Your market[/I]
                              .price = 1000
                              .selectionId = [I]Your selection[/I]
                              .size = 4.0
                              .bspLiability = 0       '<<< Add this (note 2)
                            End With
                            ReDim .bets(0)            '<<< Add this  
                            .bets(0) = obets          '<<<  and this (note 1)
                          End With
                          oPlaceBetsResp = BetfairUK.placeBets(oPlaceBetsReq)
                          With oPlaceBetsResp
                            CheckHeader(.header)
                            Print("ErrorCode = " & .errorCode.ToString)
                            If .errorCode = BFUK.PlaceBetsErrorEnum.OK Then
                              For i = 0 To .betResults.Length - 1
                                With .betResults(i)
                                  Print("BetId = " & .betId & " " & .resultCode.ToString & " " & "Success = " & .success)
                                End With
                              Next
                            End If
                          End With
                        End Sub

                        Comment

                        • John Moore
                          Junior Member
                          • Feb 2009
                          • 3

                          #117
                          Difficulty retrieving eventDate and time

                          Hi Mumbles, thanks for this thread. I`m attempting to write a bet bot. I am storing my marketIds, selectionIds into a dim array. but I am having trouble retriving the event date/time. Can you suggest which API call to make, and when to call it?

                          Thanks again John Moore

                          Comment

                          • daylosh
                            Junior Member
                            • May 2009
                            • 1

                            #118
                            How to revise step 6?

                            Mumbles great post!

                            How do i revise step 6 to make the code execute faster? Probably seems stupid but im not sure which bit to replace with 'string.split'.

                            Any help appreciated, thanks.

                            Comment

                            • Vadim
                              Junior Member
                              • Mar 2009
                              • 12

                              #119
                              Internal_error

                              Mumbles0, thanks. PlaceBets and CancelBets me be clear. But UpdateBets error INTERNAL_ERROR.

                              Code:
                               Private Sub bUpdateBets_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bUpd:cool:ateBets.Click
                                      Print("*** UpdateBets ***")
                                      Dim oUpdateBetsReq As New BFUK.UpdateBetsReq
                                      Dim oUpdateBetsResp As New BFUK.UpdateBetsResp
                                      Dim oUpdateBets As New BFUK.UpdateBets
                                      With oUpdateBetsReq
                                          .header = oHeaderUK()
                                          With oUpdateBets
                                              .betId = 7919011854
                                              '.newBetPersistenceType = BFUK.BetPersistenceTypeEnum.NONE
                                              '.newPrice = 1000
                                              .newSize = 4.5
                                              .oldBetPersistenceType = BFUK.BetPersistenceTypeEnum.NONE
                                              .oldPrice = 1000
                                              '.oldSize
                                          End With
                                          ReDim .bets(0)
                                          .bets(0) = oUpdateBets
                                      End With
                                      oUpdateBetsResp = BetFairUK.updateBets(oUpdateBetsReq) 'INTERNAL_ERROR SoapHeaderException was unhandled
                                      With oUpdateBetsResp
                                          CheckHeader(.header)
                                          Print("ErrorCode = " & .errorCode.ToString)
                                          If .errorCode = BFUK.PlaceBetsErrorEnum.OK Then
                                              For i = 0 To .betResults.Length - 1
                                                  With .betResults(i)
                                                      Print("BetId = " & .betId & " " & .resultCode.ToString & " " & "Success = " & .success)
                                                      Print("NewBetId = " & .newBetId & " NewPrice = " & .newPrice & " NewSize = " & .newSize & " SizeCancelled = " & .sizeCancelled)
                                                  End With
                                              Next
                                          End If
                                      End With
                                  End Sub

                              Comment

                              • TIP102
                                Junior Member
                                • Apr 2009
                                • 3

                                #120
                                Data from getMarketPrices to database

                                Thanks Mumbels a lot for answering about inPlay!

                                I am trying to work it out how to save prices in database. Enclosed is the code I used to save marketID when calling getMarketPrices. I declared mymarketID as globaled variable and declered value for it by inserting in With MpriceResp...and this worked...

                                Please if someone can help me with hint how to save prices in database.

                                Thank you!


                                Code:
                                Private Sub bPrices_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPrices.Click
                                
                                        Print("*** Prices ***")
                                        ShowMprices(BetFairUK.getMarketPrices(MpricesReq))    'Get market prices
                                
                                        Dim Test As New SqlClient.SqlConnection("Data Source=MyPC\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True")
                                        Test.Open()
                                        Dim sqlStatement As New SqlClient.SqlCommand("INSERT INTO Table1 (MarketID) VALUES (" & mymarketID & ", Test)
                                
                                        sqlStatement.ExecuteNonQuery()
                                        Test.Close()
                                    End Sub
                                
                                xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                                
                                    Sub ShowMprices(ByVal MpriceResp As BFUK.GetMarketPricesResp)
                                        Dim Lay, Back As String
                                        With MpriceResp
                                            CheckHeader(.header)
                                            Print("ErrorCode = " & .errorCode.ToString)
                                            If .errorCode = BFUK.GetMarketPricesErrorEnum.OK Then
                                                With .marketPrices
                                                    [COLOR="Red"]mymarketID = .marketId    [/COLOR]             
                                                    Print("MarketID = " & .marketId)
                                Last edited by TIP102; 08-05-2009, 05:33 PM.

                                Comment

                                Working...
                                X