Using VB2008 to acccess the Betfair API: A tutorial

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

    #301
    Sorry Mumbles,

    Maybe I am not properly explained. I do not want the data that shows column To Back or To Lay. I want the data that appears in the column Amount Matched, attached an image.

    I understood that this data showed the call to getMarketTradedVolumeCompressedCompleted, print the table of odds and amounts matched.
    That's why I wondered whether there was any call that returns a datatable

    I would like to do this table:

    Marketid | SelectionId | totalAmountMatched | lastPriceMatched | bestPricesToBack(0).price | bestPricesToBack(0).amountAvailable | tradedVolume(j).totalMatchedAmount (Sum amount matched From Odds 1.01 to 2.40 in this image) | tradedVolume(j).totalMatchedAmount ( Sum amount matched From Odds 2.42 to 1000 in this image)

    I had thought to cross the information of the two calls (getmarketTradedVolumCompressedCompeted +getMarketPricesCompressedCompleted) and set up a datatable, but I do not know how to pass information from one call to another

    Thanks

    Last edited by Monairda; 04-03-2010, 09:20 PM.

    Comment

    • Mumbles0
      Junior Member
      • Jan 2009
      • 240

      #302
      Processing data from 2 async calls.

      Monairda,

      Perhaps your problem is when to process the data from the 2 responses. This is what I do:
      Define a call counter to count the responses, clear it, the send the 2 async requests:

      Code:
      [COLOR="Gray"]Private oMarketPrices As UnpackMarketPricesCompressed
      Private oTradedVolume As UnpackMarketTradedVolumeCompressed
      [COLOR="Black"]Private CallCount As Integer[/COLOR]
      
      Private Sub GetMarketData()    'Sub to send the requests
        [COLOR="Black"]CallCount = 0  'Clear the call count
        [I]Call getMarketPricesCompressedAsync (as per step 14)[/I]
        [I]Call getMarketTradedVolumeCompressedAsync (as per step 24)[/I][/COLOR] 
      End Sub[/COLOR]
      The API now sends back the 2 responses. Problem is we don’t know which one comes back first, so we count them in the event handlers. When both responses have been received CallCount = 2, so now we call a sub to process the 2 unpacked data objects.

      Code:
      [COLOR="Gray"]Private Sub BetfairUK_getMarketPricesCompressedCompleted(ByVal sender As Object, ByVal e As BFUK.getMarketPricesCompressedCompletedEventArgs) Handles BetfairUK.getMarketPricesCompressedCompleted
        .........
        [I](Code similar to Step 14)[/I]
        .........
        If .errorCode = BFUK.GetMarketPricesErrorEnum.OK Then
          oMarketPrices = New UnpackMarketPricesCompressed(.marketPrices)   'Unpack the market prices data
          [COLOR="Black"]CallCount += 1    'Count this response
          If CallCount = 2 Then BuildDataTable()   'Process responses when both received[/COLOR]
        End If
        .........
        .........
      End Sub
      
      Private Sub BetfairUK_getMarketTradedVolumeCompressedCompleted(ByVal sender As Object, ByVal e As BFUK.getMarketTradedVolumeCompressedCompletedEventArgs) Handles BetfairUK.getMarketTradedVolumeCompressedCompleted
        ........
        [I](Code similar to Step 24)[/I]
        ........
        If .errorCode = BFUK.GetMarketTradedVolumeCompressedErrorEnum.OK Then
          oTradedVolume = New UnpackMarketTradedVolumeCompressed(.tradedVolume)  'Unpack the traded volume data
          [COLOR="Black"]CallCount += 1  'Count this response
          If CallCount = 2 Then BuildDataTable()  'Process responses when both received[/COLOR]
        End If
        .........
        .........
      End Sub
      
      [COLOR="Black"]Private Sub BuildDataTable()
        [I]Builds a data table from data contained in oMarketPrices and oTradedVolume objects[/I]
      End Sub[/COLOR][/COLOR]
      Sub BuildDataTable is only called once, from the event handler which handles the second response (whichever that is). This ensures that both responses have arrived before output processing commences.
      Last edited by Mumbles0; 15-05-2010, 11:09 AM.

      Comment

      • Monairda
        Junior Member
        • Jan 2009
        • 32

        #303
        Mumbles,

        Thank you very much, I will try to implement it

        Best Regards

        Comment

        • Aitor
          Junior Member
          • Nov 2009
          • 8

          #304
          Why doesnt work with Champions League

          Hello!I have a small problem using this function. y put the name of a football team there like "equipo" and the code is the football code, this is correct when i put a name of for example a spanish football team, but the problem is that one team is playing champions league, this doesn´t appear. what is hapening???what is wrong?


          Private Function TeamEvents(ByVal fecha As Date, ByVal fechaini As Date, ByVal equipo As String, ByVal cod As String) As List(Of MarketDataType)
          Dim Names As String(), Equipos As New List(Of MarketDataType)

          Dim oMarketsReq As New BFUK.GetAllMarketsReq
          Dim oMarketsResp As BFUK.GetAllMarketsResp
          With oMarketsReq
          .header = oHeaderUK()
          ReDim .eventTypeIds(0) : .eventTypeIds(0) = cod
          .fromDate = fechaini
          .toDate = fecha 'Today.AddDays(2) esto seria hoy y mañana
          End With
          oMarketsResp = BetFairUK.getAllMarkets(oMarketsReq) 'Call the UK API

          With oMarketsResp
          CheckHeader(.header)
          Print("ErrorCode = " & .errorCode.ToString)
          If .errorCode = BFUK.GetAllMarketsErrorEnum.OK Then
          Dim AllMarkets As New UnpackAllMarkets(.marketData)
          With AllMarkets
          For i = 0 To .marketData.Length - 1
          If .marketData(i).menuPath.Contains(equipo) Then
          Names = .marketData(i).menuPath.Split("\") 'Lo filtramos segun su /
          Equipos.Add(.marketData(i)) 'Añadir un mercado a la lista
          End If
          Next
          End With
          Equipos.Sort(New CompareMarketTimes) 'Ordenamos los mercados por fecha
          End If
          End With
          Return Equipos
          End Function

          Comment

          • Mumbles0
            Junior Member
            • Jan 2009
            • 240

            #305
            Reply to Aitor (re: getAllMarkets filter)

            Your function works OK for me. When I make the test call:

            Code:
            Private Sub bTestAitor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bTestAitor.Click
            
              With TeamEvents(Today.AddDays(7), Today, "Real Madrid", 1)
                For i = 0 To .Count - 1
                  With .Item(i)
                    Print(.menuPath & "*" & .marketName)
                  End With
                Next
              End With
            
            End Sub
            the function returns all 28 markets with .menuPath =
            \Soccer\UEFA Champions League\Fixtures 10 March\Real Madrid v Lyon

            and all 37 markets with .menuPath =
            \Soccer\Spanish Soccer\Primera Division\Fixtures 14 March\Valladolid v Real Madrid

            Make sure that the date limits you use include days when your team (equipo) is playing in a Champions League match.

            Comment

            • Aitor
              Junior Member
              • Nov 2009
              • 8

              #306
              Thanks a lot Numbles0 i was doing bad writting questions, your help was very importatn for me, i don´t mean thtat the paths were diferents.
              Thansk a lot

              Comment

              • Aitor
                Junior Member
                • Nov 2009
                • 8

                #307
                problem with session token

                i have a trouble with session token
                Betfair3._0.BFUK.GetMarketPricesErrorEnum.API_ERRO R
                MpriceResp.errorCodeField{4}
                and header_field [error_code{8}

                i am calling the betfair.getmarketprices from another class, what is hapenning with the session token?what its wrong?

                Comment

                • Osprey
                  Junior Member
                  • Jan 2010
                  • 1

                  #308
                  A vote of thanks ...

                  Really great thread Mumbles,

                  I have been trying to make the painful transition from VB6 to VB.Net+++ for quite some time now, your tutorial has added that extra whatsit, making the journey much easier and more interesting to boot!

                  Thus far, touching wood, I have successfully followed all of your tutorials/suggestions and therefore have code that can potentially be extended to money making uses. :-)

                  One related idea occurred to me however, under the heading of "what next", it seemed to me that LINQ could simplify a lot of the 'housekeeping' that you have been describing. If you agree, perhaps a tutorial on LINQ implementations would be useful?

                  Osprey

                  Comment

                  • Mumbles0
                    Junior Member
                    • Jan 2009
                    • 240

                    #309
                    Linq

                    Osprey,

                    Thanks for the kind words.

                    LINQ allows you to query a data source using convenient VB-language statements. Data sources can be in-memory objects, XML files or SQL databases.

                    At this stage I don’t know much LINQ, not enough to write an authoritative tutorial. There are, of course, many articles about LINQ on the web.

                    Comment

                    • jaybeeb
                      Junior Member
                      • Sep 2009
                      • 2

                      #310
                      Loving this tutorial and making a good stab at extra things myself now but having an issue with the times returned, not sure if its after the clocks went forward but with the code in this thread for the todayscard() all the race times are an hour behind??

                      Comment

                      • Mumbles0
                        Junior Member
                        • Jan 2009
                        • 240

                        #311
                        Local time

                        Jaybeeb,

                        I guess you are in the UK. All times returned by the API are UTC (or GMT if you like).

                        Use the ToLocalTime method to convert the event times to your local time.
                        Change the code in the Step 18 example to do this:

                        Code:
                          [COLOR="Gray"]For Each Race In TodaysCard    'Print the card
                            With Race
                              Names = .menuPath.Split("\")
                              Print(.eventDate[COLOR="Black"].ToLocalTime[/COLOR].TimeOfDay.ToString & " " & .marketId & " " & Names(3) & " - " & .marketName)
                            End With
                          Next[/COLOR]

                        Comment

                        • jaybeeb
                          Junior Member
                          • Sep 2009
                          • 2

                          #312
                          Thats the one Mumbles. Thanks

                          Comment

                          • Mumbles0
                            Junior Member
                            • Jan 2009
                            • 240

                            #313
                            Visual Basic 2010

                            Microsoft have recently released a new version of Visual Basic - VB2010. Like VB2008, an Express Edition is available free of charge. There are some new language features and the IDE has had a facelift. Any code you have developed using VB2008 should be fully compatible with the new compiler.

                            Existing projects created with VB2008 will require some conversion. For example, to run the Betfair tester project with VB2010 you can proceed along these lines:

                            Copy the solution folder BetfairX to a new location. Rename it to something like BetfairX2010. The original folder is retained as a backup.

                            Start VB2010 and from the File menu select "Open Project". Browse to BetfairX.sln in the new BetfairX2010 folder. Click Open. This launches the conversion wizard. Click Next. Select No to the backup question (because we already have a backup). Click Finish. The conversion is now done. Check “Show the conversion log” and close the wizard. Now run the project. The familiar Betfair TestForm should appear and operate as expected.

                            In general, the source files will not require conversion, but the solution file (*.sln) will. The means that, once converted, the solution cannot be loaded with VB2008.

                            I suggest that you install this new version and use it for future development. It has a very similar feel to VB2008. If you have problems post a comment.

                            M0.

                            Comment

                            • ribogio7
                              Junior Member
                              • Apr 2010
                              • 4

                              #314
                              Hello Mumbles0

                              First of all my english is not very good.

                              I try to write a method which it returns all the results of bet history.

                              Code:
                                      Dim oGBHReq As New BFExchange.GetBetHistoryReq
                                      Dim oGBHResp As New BFExchange.GetBetHistoryResp
                              
                                      With oGBHReq
                                          .header = oHeaderUK
                                          .placedDateFrom = Now.AddDays(-7)
                                          .placedDateTo = Now
                                          .recordCount = 30
                                          ReDim .marketTypesIncluded(0) : .marketTypesIncluded(0) = BFExchange.MarketTypeEnum.R
                                          .sortBetsBy = BFExchange.BetsOrderByEnum.PLACED_DATE
                                      End With
                              
                                      oGBHResp = BetfairUK.getBetHistory(oGBHReq)
                              
                                      With oGBHResp
                                          CheckHeader(.header)
                                          MsgBox("ErrorCode = " & .errorCode.ToString)
                                          If .errorCode = BFExchange.GetBetHistoryErrorEnum.OK Then
                                              For i = 0 To .betHistoryItems().Length - 1
                                                  RichTextBox1.Text = .betHistoryItems(i).betId & .betHistoryItems(i).betType & .betHistoryItems(i).placedDate & .betHistoryItems(i).price & vbNewLine
                                              Next
                                          End If
                                      End With
                              It appears an Errorcode=No_Results. Please look this part of code.

                              Comment

                              • Mumbles0
                                Junior Member
                                • Jan 2009
                                • 240

                                #315
                                getBetHistory

                                ribogio7,

                                You need to include the .betTypesIncluded parameter in the request header. Use M or U for bets on open markets, or use S for settled markets.

                                For .marketTypesIncluded you are using R (for range markets). Do you have any bets on range markets? Perhaps try O (for Odds markets).

                                If you have placed bets on normal Odds markets in the past 7 days you should return some bet history with this request object:

                                Code:
                                With oGBHReq
                                      .header = oHeaderUK()
                                      .placedDateFrom = Now.AddDays(-7)
                                      .placedDateTo = Now
                                      .recordCount = 30
                                      .betTypesIncluded = BFExchange.BetStatusEnum.S
                                      ReDim .marketTypesIncluded(0) : .marketTypesIncluded(0) = BFExchange.MarketTypeEnum.O
                                      .sortBetsBy = BFExchange.BetsOrderByEnum.PLACED_DATE
                                    End With

                                Comment

                                Working...
                                X