Using VB2008 to acccess the Betfair API: A tutorial

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Logun
    Junior Member
    • Feb 2009
    • 5

    #241
    When nothing is returned

    Hi Mumbles,

    Got a problem i'm trying to fix. I'm using these values in my code:

    (.bestPricesToBack(0).price)
    (.bestPricesToLay(0).price)
    (.bestPricesToBack(0).amountAvailable) all 3 values
    (.bestPricesToLay(0).amountAvailable) all 3 values

    Here's a simple version of the code:

    With .runnerPrices(i)
    If .sortOrder = 1 And .bestPricesToBack.Length > 0 Then
    Runner1ID(.selectionId)
    If .bestPricesToBack(0).price > 0 Then
    Back1 = Math.Round((.bestPricesToBack(0).price), 2)
    lblBack1Line1.Text = Back1
    ElseIf .bestPricesToBack.Length < 1 Then
    Back1 = Decimal.Round(0.0)
    lblBack1Line1.Text = Back1
    End If
    If .bestPricesToLay(0).price > 0 Then
    Lay1 = Math.Round((.bestPricesToLay(0).price), 2)
    lblLay1Line1.Text = Lay1
    ElseIf .bestPricesToLay(0).price < 1 Then
    Lay1 = Decimal.Round(0.0)
    lblLay1Line1.Text = Lay1
    End if
    If .bestPricesToBack(0).amountAvailable > 0 Then
    Print(.bestPricesToBack(0).amountAvailable)
    ElseIf .bestPricesToBack(0).amountAvailable < 1 Then
    WomB1L1 = Decimal.Round(0.0)
    End If
    If .bestPricesToLay(0).amountAvailable > 0 Then
    Print(.bestPricesToLay(0).amountAvailable
    ElseIf .bestPricesToLay(0).amountAvailable < 1 Then
    WL1L1 = Decimal.Round(0.0)
    End If
    End With

    I use the .SortOrder as my filter for all the values for that particular horse or whatever. So i reiterate the above for .sortOrder 2, 3, 4, 5, 6 and this works really well.
    The problem come when there is no information in say .bestPricesToLay(0).amountAvailable. If this string returns nothing then it crashes. I tried to counter this by using the elseif statement to insert a value (0.0) but this is not working. Can you offer any advice?

    Thanks
    Tony
    Last edited by Tony Logun; 15-10-2009, 11:32 AM.

    Comment

    • Mumbles0
      Junior Member
      • Jan 2009
      • 240

      #242
      Reply to Tony Logun (re your code)

      Tony,

      A tip: When pasting code snippets into a forum post, enclose it between [code] tags like this:
      [code]
      Paste your code here
      [/code]

      It make it a lot easier to see what’s going on.

      I think you have realized that, when a market is new, some runners may not yet have any bets placed on them, so no information is returned by the bestPrices arrays. In this case, your code shown here will crash as you’ve discovered (probably with the “Index was outside the bounds of the array” exception).

      Code:
      If .bestPricesToLay(0).price > 0 Then
        Lay1 = Math.Round((.bestPricesToLay(0).price), 2)
        lblLay1Line1.Text = Lay1
      ElseIf .bestPricesToLay(0).price < 1 Then
        Lay1 = Decimal.Round(0.0)
        lblLay1Line1.Text = Lay1
      End If
      Here you are looking at the array element .bestPricesToLay(0). But if there are no bets on this runner, the array will be empty and this element will not exist, so you must not attempt to access it. When an array is empty, its length is zero, so use this test instead:

      Code:
      If .bestPricesToLay.Length > 0 Then         'Array contains at least one element
        lblLay1Line1.Text = .bestPricesToLay(0).price       'Show the best price
      Else      'Array is empty
        lblLay1Line1.Text = 0       'Show 0
      End If
      That seems to be you main problem.

      By the way....

      Your Round statements are not doing anything. Prices returned by the API will never have more than 2 decimal places. Decimal.Round(0.0) simply evaluates to 0.

      Also, I’m not wishing to nit-pick, but you’ve referred to .bestPricesToLay(0).amountAvailable as a string. A string is something else. It is important to know what a string is.
      Last edited by Mumbles0; 15-10-2009, 02:55 PM.

      Comment

      • Tony Logun
        Junior Member
        • Feb 2009
        • 5

        #243
        Thanks Mumbles,

        I'm still a novice programmer but i should know that .bestPricesToLay(0).amountAvailable is a double not a string. Was typing from work in a hurry and reading back i realise i need to make the question clearer. I think the rounding is something i was thinking of implementing just in case it could solve the problem, i wont bother now but will try your solution when i get back home.

        Cheers.
        Tony

        Comment

        • supunsilva.
          Junior Member
          • Aug 2009
          • 32

          #244
          Getting profit &amp; loss for each and every runner

          Hi Mumbles, Thanks for your reply. your update bet code is working fine. I have found how to place low amount(Stake<2) bets. Thanks for all.

          Could you kindly tell me .. is there any method to get profit and loss for each and every runners. I have attached JPEG for your review. I don't like to bother you too much.. Reply me when you free.

          Thanks,
          Supun Silva
          Attached Files

          Comment

          • puntingking
            Junior Member
            • Jul 2009
            • 3

            #245
            Big pat on the back for Mumbles0

            Awesome tutorial Mumbles0, you've allowed those of us that need the guiding hand to build our own bots, so thanks heaps.

            supunsilva: When you post a question on a forum and subsequently figure out the solution yourself, you should post the solution rather than "Don't worry, I've figured it out". That way you may help others that have the same problem.

            Comment

            • supunsilva.
              Junior Member
              • Aug 2009
              • 32

              #246
              Hi... puntingking... Thankyou for your instructions. I'm really apologizing about my knowledge. My english is not very clever. I will try to correct my presentations...

              Thank you very much!!
              Supun Silva

              Comment

              • supunsilva.
                Junior Member
                • Aug 2009
                • 32

                #247
                Getting profit &amp; loss for each and every runner

                Hi Mumbles.. I have tried your sub called 'PrintOutcome'. But it is not working. It is unable to find the market id in for loop. That's why I asked you any other method. I forgot to write above things in my previous post. I apologize for it.

                Thank You,
                Supun Silva

                Comment

                • Mumbles0
                  Junior Member
                  • Jan 2009
                  • 240

                  #248
                  Reply to supunsilva (re: Profit &amp; Loss)

                  The sub PrintOutcome (see this post) is an example of how to call getAccountStatement to see if your bets on a market have won or lost. It works OK for me. Call it with the marketId of a market you have had matched bets on

                  The API also has a service called getMarketProfitAndLoss which returns P&L data you may be interested in. This code gives a calling example:

                  Code:
                  Sub ProfitAndLoss(ByVal marketId As Integer)
                  
                    Print(vbCrLf & "*** Market P&L ***")
                    Dim MPLreq As New BFUK.GetMarketProfitAndLossReq
                    Dim MPLresp As BFUK.GetMarketProfitAndLossResp
                  
                    With MPLreq     'Set up the request
                      .header = oHeaderUK
                      .marketID = MarketOfInterest
                    End With
                  
                    MPLresp = BetfairUK.getMarketProfitAndLoss(MPLreq)    'Call the API
                  
                    With MPLresp    The response
                      CheckHeader(.header)
                      Print(.errorCode.ToString)
                      If .errorCode = BFUK.GetMarketProfitAndLossErrorEnum.OK Then    'Process the response
                        For i = 0 To UBound(.annotations)
                          With .annotations(i)         'Print the list of runner P&Ls
                            Print(.selectionId & " " & .selectionName & " " & .ifWin)
                          End With
                        Next
                      End If
                    End With
                  
                  End Sub
                  The .annotations array returns a list of runners and the expected P&L if the runner wins. The values are based on matched bets.

                  Comment

                  • supunsilva.
                    Junior Member
                    • Aug 2009
                    • 32

                    #249
                    Hi...Mumbles,
                    Thank you for your Great job! It is working fine. Thank you Thank you so much..

                    Regards,
                    Supun Silva

                    Comment

                    • supunsilva.
                      Junior Member
                      • Aug 2009
                      • 32

                      #250
                      Hi.. Again..... Is there any method to get Profit with deduct betfair commission using direct API Call?

                      Thanks..
                      Supun Silva

                      Comment

                      • Mumbles0
                        Junior Member
                        • Jan 2009
                        • 240

                        #251
                        Reply to supunsilva (re: P&amp;L commission)

                        Supunsilva,

                        Can you read the Sports API Guide? Your English can’t be that bad if you can read the forum. The API Guide has detailed explanations of all the parameters for all the calls - both request and response.

                        If you read the API Guide for getMarketProfitAndLoss you will see quite clearly that it has a request parameter called .netOfCommission which is normally False, but if you set it True the P&L values returned will have commission deducted. There are other request parameters as well.

                        It’s time for you to do some research yourself.

                        Comment

                        • supunsilva.
                          Junior Member
                          • Aug 2009
                          • 32

                          #252
                          Hi.. Mumbles..... Thank you for your support. I will look into API Guide. It is displaying as a XML. I will read it and If I have any other question I will ask you... Sorry for unnecessary questions...

                          Thank you,
                          Supun Silva

                          Comment

                          • Chasing Mug
                            Junior Member
                            • Oct 2009
                            • 3

                            #253
                            Event Hierarchy for a given MarketID

                            Is there a way to get the Event Hierarchy for a given MarketID (for a closed Market)? I am doing the following:

                            1. Getting a list of bets I placed using getBetHistory method.
                            2. For each bet in getBetHistory response, I fetch the MarketID for the bet and call the getMarket method.
                            3. The getMarket method returns the Event Hierarchy in its response as an array of integers. However, for me, the event hierarch array always has only one element in it and that is the marketid that I passed.

                            Could anyone help me out and say what I am doing wrong and if there is any other way I could get the event hierarchy of the market for the bet I placed?

                            - Chasing Mug....

                            Comment

                            • Mumbles0
                              Junior Member
                              • Jan 2009
                              • 240

                              #254
                              Reply to Chasing Mug (re: eventHierarchy)

                              I don't think you're doing anything wrong. I’ve looked at getMarket and it appears that you don’t get much info returned after the market has closed. An example of a horse race at Ling (marketId 100888038):

                              While the market was ACTIVE .eventHierarchy returned 3 eventIds:
                              7 (Horse racing)
                              298251 (GB)
                              26520377 (Ling 20th Oct)

                              After the market had CLOSED .eventHierarchy returned only the last eventId:
                              26520377 (Ling 20th Oct)

                              (You say that this returns marketId, but I don’t think this is the case.)

                              An API call that may be useful is getAccountStatement. This returns details of your settled bets, including:
                              .eventTypeId (7)
                              .fullMarketName ("GB/Ling 20th Oct/6f Mdn Stks")
                              .marketName ("6f Mdn Stks")
                              .eventId (100888038 - actually the marketId)
                              .etc

                              If this isn’t much use, you might have to keep track of market info when you place a bet.

                              Comment

                              • supunsilva.
                                Junior Member
                                • Aug 2009
                                • 32

                                #255
                                Retrieving Market Total Matched Amount

                                Hi... I have tried write code for retrieve Total Amount Matched for Market.
                                I have read API Guide as you told me. After that I have written following code.
                                Code:
                                  Dim ToMreq As New BFUK.GetMarketTradedVolumeCompressedReq
                                        Dim ToMresp As BFUK.GetMarketTradedVolumeCompressedResp
                                
                                        With ToMreq
                                            .header = oHeaderUK()
                                            .marketId = marketid
                                        End With
                                        ToMresp = BetFairUK2.getMarketTradedVolumeCompressed(ToMreq)
                                        With ToMresp
                                            CheckHeader(.header)
                                            If .errorCode = BFUK.GetMarketTradedVolumeCompressedErrorEnum.OK Then
                                                txtstake.Text = .tradedVolume
                                            End If
                                        End With
                                Following output is retrieving...

                                :4235600~0~0.0~0.0~0.0|2.1~269.2|2.12~1372.69|2.14 ~1101.8|2.16~411.14|2.18~497.08|2.2~656.48|2.22~85 1.56|2.24~665.44|2.26~529.94|2.28~855.72|2.3~1157. 64|2.32~713.06|2.34~384.5|2.36~490.76|2.38~84.88|2 .4~501.78|2.42~565.9|2.44~243.2|2.46~164.4|2.48~11 8.84|2.5~200.04|2.52~82.0|2.56~9.0|2.6~49.2|3.95~0 .02|4.1~3.98:4186669~0~0.0~0.0~0.0|14.5~2.18|15.0~ 3.98|16.0~0.02|16.5~3.98|26.0~2.16|27.0~3.84|28.0~ 13.7|29.0~9.96|30.0~3.32|32.0~6.0|34.0~1.12|36.0~1 4.56|38.0~12.58|40.0~19.18|42.0~19.78|44.0~2.58|48 .0~2.52|50.0~11.0|55.0~8.08|95.0~0.2:4187090~0~0.0 ~0.0~0.0|4.5~4.0|5.0~6.84|5.1~41.46|5.2~70.12|5.3~ 279.88|5.4~63.66|5.5~191.28|5.6~119.34|5.7~218.02| 5.8~3.98|6.0~10.0|6.2~30.76|6.4~76.94|6.6~103.04|6 .8~176.14|7.0~3.86|7.4~4.0|7.6~67.6|7.8~4.0:418875 2~0~0.0~0.0~0.0|2.62~0.2|2.64~3.8|2.74~4.0|2.82~74 .0|2.84~11.56|2.86~13.98|2.94~3.86|2.98~9.46|3.0~8 0.0|3.05~33.6|3.15~60.28|3.2~21.78|3.25~62.94|3.3~ 35.32|3.35~261.94|3.4~101.84|3.45~335.62|3.5~475.6 8|3.55~864.64|3.6~759.56|3.65~324.06|3.7~21.94|3.7 5~3.84|3.8~31.6|3.85~6.42|4.4~11.3:4243209~0~0.0~0 .0~0.0|15.0~8.76|15.5~3.98|16.5~13.6|17.0~3.8|17.5 ~11.34|18.0~3.86|18.5~4.08|19.5~6.76|20.0~7.7|21.0 ~13.62|22.0~1.84|23.0~6.02|24.0~6.72|26.0~21.52|28 .0~25.2|34.0~13.3|38.0~6.0|48.0~4.0|50.0~2.1|60.0~ 23.88|70.0~15.1|80.0~13.92|85.0~4.5:4188756~0~0.0~ 0.0~0.0|18.5~4.0|19.0~7.84|20.0~0.52|21.0~6.18|22. 0~6.02|23.0~5.82|24.0~8.0|25.0~7.98|26.0~20.42|27. 0~5.62|28.0~12.2|29.0~7.1|30.0~28.3|32.0~34.44|34. 0~20.64|36.0~3.14|38.0~3.16|40.0~6.74|44.0~4.06|46 .0~11.16|48.0~10.0:4233252~0~0.0~0.0~0.0|12.0~10.2 6|12.5~3.86|13.0~3.86|19.0~27.82|19.5~5.4|20.0~16. 18|21.0~71.72|22.0~10.38|23.0~10.56|24.0~3.08|25.0 ~12.02|26.0~0.6|27.0~15.8|28.0~5.9|29.0~4.34|30.0~ 17.08|32.0~16.2|36.0~3.02|44.0~2.74|46.0~4.48|50.0 ~11.2

                                I want Total Amount Matched for the Market and Total Matched for each and every runner. Please let me know what is the mistake I have made.. This is really appreciated..............



                                Thank You,
                                Supun Silva

                                Comment

                                Working...
                                X