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

    #31
    Reply to Monairda (Re: Form)

    Betfair offer a good form service in the horse racing section of their main website. Also, the GetSilks API call returns limited form information. (Note that GetSilks is not available through the Free Access API).

    There are any number of horse racing form guide websites on the internet. Try a Google search, specifying your country of interest. I am not the best person to advise you on this subject.

    Comment

    • Mumbles0
      Junior Member
      • Jan 2009
      • 240

      #32
      Reply to HBBF ...

      If you add another statement it should work:

      Code:
      Dim zBets(0 To 0) As BFUK.PlaceBets
      For i = 0 To 0
        zBets(i) = New BFUK.PlaceBets       'Add this
        With zBets(i)
        ........
      The Dim statement creates an empty array. You must also create a new object in each array element using the “New” keyword.

      Comment

      • HBBF
        Junior Member
        • Feb 2009
        • 4

        #33
        Re:Mumbles0

        Appreciation
        Thank you very much
        ReDim me to switch to

        Originally posted by Mumbles0 View Post
        If you add another statement it should work:

        Code:
        Dim zBets(0 To 0) As BFUK.PlaceBets
        For i = 0 To 0
          zBets(i) = New BFUK.PlaceBets       'Add this
          With zBets(i)
          ........
        The Dim statement creates an empty array. You must also create a new object in each array element using the “New” keyword.

        Comment

        • gohawk
          Junior Member
          • Feb 2009
          • 4

          #34
          Hello and thank you for this useful thread

          I Call GetMUBets and i take the message NO_RESULTS but i have already place a bet on this market what i m doing wronk?


          Dim Req As New betfair.UK.exchange.GetMUBetsReq()
          Dim Resp As New betfair.UK.exchange.GetMUBetsResp

          With Req
          .header = oHeaderUK()
          .marketId = 100391221
          .betStatus = BetStatusEnum.MU
          End With

          Resp = BetFairUK.getMUBets(Req)

          With Resp
          CheckHeader(.header)
          MsgBox(.errorCode.ToString)
          If .errorCode = betfair.UK.exchange.GetMUBetsErrorEnum.OK Then
          For i = 0 To .bets().Length - 1
          lst.Items.Add(.bets(i).betId)
          Next
          End If
          End With

          Comment

          • Monairda
            Junior Member
            • Jan 2009
            • 32

            #35
            Mumbles0 Thank you very much.
            Some websites will seek such information for inclusion and to use the Free Access API and I will can not use the GetSilks.

            Comment

            • Mumbles0
              Junior Member
              • Jan 2009
              • 240

              #36
              Reply to gohawk ...

              The line where you assign .betStatus doesn’t look right. It should have a reference to the API’s namespace (in your case: betfair.UK.exchange). Also, the .recordCount property should be set to a number greater than the maximum number of bets you are likely to place on the market.

              Code:
              With Req
                .header = oHeaderUK()
                .marketId = [i]marketId of interest[/i]
                .betStatus = betfair.UK.exchange.BetStatusEnum.MU      '<<< Check this
                .recordCount = 100                                     '<<< Add this
              End With
              If this doesn’t work make sure you have the correct marketId.

              Comment

              • gohawk
                Junior Member
                • Feb 2009
                • 4

                #37
                Originally posted by Mumbles0 View Post
                Code:
                With Req
                  .header = oHeaderUK()
                  .marketId = [i]marketId of interest[/i]
                  .betStatus = betfair.UK.exchange.BetStatusEnum.MU      '<<< Check this
                  .recordCount = 100                                     '<<< Add this
                End With
                Appreciation
                Thank you very much
                Now it works

                Comment

                • HBBF
                  Junior Member
                  • Feb 2009
                  • 4

                  #38
                  Again ask Mumbles0 about GetMarketProfitAndLossResp

                  Again ask Mumbles0 about GetMarketProfitAndLossResp

                  For i = 0 To UBound(.annotations)
                  With .annotations(i)
                  zPrice1(i).Value = .selectionName
                  zPrice2(i).Value = .ifWin
                  'zPrice3(i).Value = .ifLoss 'Invalid
                  End With
                  Next

                  At Multi-Winner Odds Market circumstances, How to Return ifLoss?

                  Comment

                  • Vadim
                    Junior Member
                    • Mar 2009
                    • 12

                    #39
                    Step 6

                    Mumbles0, thanks for the tutorial!!!

                    I'm having difficulty in step 6. Print out:
                    *** Markets ***
                    HeaderCode = OK
                    ErrorCode = OK

                    More nothing prints. I understand there must print MarketID, MarketStatus, MarketName and MenuPath.
                    What can I do?

                    Comment

                    • Mumbles0
                      Junior Member
                      • Jan 2009
                      • 240

                      #40
                      Reply to HBBF (Re: getMarketProfitAndLoss)

                      You are not the first person to have a problem with the getMarketProfitAndLoss response. Betfair sure know how to test our understanding of objects!

                      This problem arises because, for the .annotations array, we are expecting an array of type BFUK.ProfitAndLoss, but what we actually receive is an array of BFUK.MultiWinnerOddsLine. This is a class derived from BFUK.ProfitAndLoss and has an additional “ifLoss” property. This applies to both single-winner and multi-winner markets, even though the API Guide suggests otherwise. Just to make things even more complicated, Asian handicap markets return something different again.

                      An easy way to fix this is to create a response object from the generic Object class (instead of BFUK.GetMarketProfitAndLossResp). This makes all returned properties accessible, regardless of the returned .annotations array type. For example:

                      Code:
                      Dim zReq As New BFUK.GetMarketProfitAndLossReq
                      Dim zResp As New Object           '<<< This is different
                      With zReq
                        .header = oHeaderUK
                        .marketID =  [i]Your  marketId of interest[/i]
                      End With
                      zResp = BetfairUK.getMarketProfitAndLoss(zReq)       'Call the API
                      With zResp
                        CheckHeader(.header)
                        Print(.errorCode.ToString)
                        If .errorCode = BFUK.GetMarketProfitAndLossErrorEnum.OK Then
                          For i = 0 To UBound(.annotations)
                            With .annotations(i)
                              zPrice1(i).Value = .selectionName
                              zPrice2(i).Value = .ifWin
                              zPrice3(i).Value = .ifLoss
                            End With
                          Next
                        End If
                      End With
                      Note that “Intellisense” does not operate for the zResp object - you must enter the property names correctly.

                      Try this out. I hope it works.
                      Last edited by Mumbles0; 10-03-2009, 11:03 PM.

                      Comment

                      • Mumbles0
                        Junior Member
                        • Jan 2009
                        • 240

                        #41
                        Reply to Vadim (Re: Step 6)

                        Did you get Step 5 working?

                        Are you loading the request header with the horse racing values as per Step 5, or are you using something else?

                        If you reinstate the Print(.marketData) statement (as per Step 5) the printed string should look something like this (for the horse racing example):

                        Code:
                        :20879657~World Hrd~O~ACTIVE~1236679200000~\Horse Racing\ANTEPOST\GB\Chelt 12th Mar
                        ~/7/10394282/10394283/26402796/20879657~0~1~GBR~1236662239667~16~1~420836.54~N~N
                        :21214073~Ryanair Chs~O~ACTIVE~1236679200000~\Horse Racing\ANTEPOST\GB\Chelt 12th Mar
                        ~/7/10394282/10394283/26402796/21214073~0~1~GBR~1236662239667~12~1~134169.1~N~N
                        :21226953~RSA Chs~O~ACTIVE~1236679200000~\Horse Racing\ANTEPOST\GB\Chelt 11th Mar
                        ~/7/10394282/10394283/26402795/21226953~0~1~GBR~1236662239667~19~1~192731.62~N~N
                        :21226954~National Hunt Nov Chs~O~ACTIVE~1236679200000~\Horse Racing\ANTEPOST\GB\Chelt
                         11th Mar~/7/10394282/10394283/26402795/21226954~0~1~GBR~1236662239667~40~1~57364.06
                        ~N~N:21226958~Ballymore Pro Nov Hrd~O~ACTIVE~1236679200000~\Horse Racing\ANTEPOST\GB
                        \Chelt 11th Mar~/7/10394282/10394283/26402795/21226958~0~1~GBR~1236662239667~29~1
                        ~206386.66~N~N:100221558~Champion Bumper~O~ACTIVE~1236679200000~\Horse Racing\ANTEPOST
                        \GB\Chelt 11th Mar~/7/10394282/10394283/26402795/100221558~0~1~GBR~1236662239667~31
                        ~1~178495.94~N~N
                        If nothing prints, then you may not be using a meaningful set of request parameters (eventTypeIds, countries, date limits). Check this.

                        If this string prints OK then it looks like something is wrong with the unpacking method - the MarketData array is not being built. Make sure your code for Class UnpackAllMarkets is exactly as it appears in Step 6. (Note that I have added an extra line.)
                        Last edited by Mumbles0; 11-03-2009, 12:46 AM.

                        Comment

                        • Vadim
                          Junior Member
                          • Mar 2009
                          • 12

                          #42
                          Re: Reply to Vadim

                          I understood why I have encountered an error InvalidCastException. In the control panel - I have a standard language is Russian (because I live in Belarus). I changed the language to English (UK) and all the work, everything is OK.

                          The question arises as to how to do that, and with Russian and English when I have everything working?

                          Comment

                          • Mumbles0
                            Junior Member
                            • Jan 2009
                            • 240

                            #43
                            Reply to Vadim (continued...)

                            Try this:

                            In Class UnpackAllMarkets, locate the line:

                            .TotalAmountMatched = NextField()

                            and change it to:

                            .TotalAmountMatched = Val(NextField())

                            I changed my computer setting to Russian and got “InvalidCastException” like you. I think the problem is because Russian uses “,” whereas English uses “.” for decimal (example: 81606,52 instead of 81606.52). The cast from string to double had a problem with this. The Val() function should fix it.

                            Comment

                            • elmariachi
                              Junior Member
                              • Feb 2009
                              • 16

                              #44
                              Decimal seperators

                              Hi,

                              the problem with the locale is in fact based on the decimal seperator issue.
                              But you can force .NET to assume the english locale.

                              I would recommend ensuring that when you login you request set the request locale to the english locale, this ensure that the double values are always formated the "english" way and not in the format that you might use in your betfair account.

                              Then everytime you read a double from the api you should use a method like this (its c# but it should still make clear how to do it):
                              Code:
                                      /// <summary>
                                      /// Converts to double.
                                      /// </summary>
                                      /// <param name="data">The data.</param>
                                      /// <returns></returns>
                                      private static double ConvertToDouble(string data)
                                      {
                                          // if there is data
                                          if (!string.IsNullOrEmpty(data))
                                          {
                                              if (data == "Infinity")
                                                  return Double.PositiveInfinity;
                              
                                              if (data == "-Infinity")
                                                  return Double.NegativeInfinity;
                              
                                              if (data != "NaN" && data != "")
                                              {
                                                  return Convert.ToDouble(data, System.Globalization.NumberFormatInfo.InvariantInfo);
                                              }
                                          }
                              
                                          return 0;
                                      }
                              Actually unsing the invariant info is not the 100% proper way of doing it. Best would be to load the english NumberFormatInfo. But since for our purpose this is sufficientt and the InvariantInfo is always available I would recommend using this one.

                              Hope that helps
                              E
                              Last edited by elmariachi; 12-03-2009, 10:45 AM.

                              Comment

                              • Dodgee
                                Junior Member
                                • Jan 2009
                                • 17

                                #45
                                GetAllMarkets

                                I'm also having trouble with GetAllMarkets:

                                It only seems to return a response at certain times of day (from mid-evening onwards). Any other time I get a deserialization error.

                                When it does come back with some markets, I don't think it's coming back with everything it should. There are a lot more available when I look on the Betfair website, for example. Also, the number of available markets decreases through the day until there are none at the end of the day. This makes it sound like it's only supplying the markets for the current day, but the .menupath item claims that the market date is after today.

                                Could it be some issue with the time conversion from the UTC timestamp? I tried experimenting with .toDate, changing it to Today.AddDays(2) but this also produced an error. I also tried requesting .EventDate in the call to GetAllMarkets and this just came back with 10am on whichever day I was calling it for every market, e.g. 12/03/2009 10:00:00.

                                I checked back through the code and it is exactly as you posted in each step Mumbles.

                                Anyone have any ideas?

                                Comment

                                Working...
                                X