Using VB2008 to acccess the Betfair API: A tutorial

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BigSprout
    Junior Member
    • Feb 2011
    • 60

    #556
    Dave, not sure if this is correct - but I am not going to learn if I just sit back.

    I believe that when a goal is scored (non-runner in a race), the market is suspended and unmatched bets are cancelled.

    So check 1 would be if marketstatus is "SUSPENDED" and then not try to load prices

    or check 2
    Code:
    if  .bestpricestoback.length=0 then
        Print("an error Message")
        Exit Sub
    endif
    eventually all "Back" prices would be filled and betting can commence
    Last edited by BigSprout; 14-02-2011, 02:20 AM.

    Comment

    • calypsored
      Junior Member
      • Dec 2009
      • 5

      #557
      Thanks for the reply BigSprout, I appreciate your effort to help... but...

      All unmatched bets are cancelled when a goal is scored, but that scoreline does not become a non-runner! Let's take 0-0 as an example - obviously if a team scores that result can't now happen (unless the game is abandoned and the result voided I suppose!). However lots of people might have laid it at highish odds and have a lot of money tied up in a dead rubber.

      Some layers, with more cash in their accounts than Yours Truly, offer a lay at odds of 1000 - the reason being so that those people who laid 0-0 previously can back 0-0 at odds of 1000 to free their liability....and free money to those later layers (if that all makes sense!)

      Also, some of the late night matches (Latin America etc) have scorelines where no money is being offered on one or both sides of the market for some scores!

      I have in fact tried exactly the trap you suggest (amongst many others) to no avail. I'm unsure how to test if a given element in an array is empty, null, non-numeric - that's the nub of my problem!

      Comment

      • Grantay.
        Junior Member
        • Jan 2010
        • 53

        #558
        Empty back or lay prices

        Calupsored,

        Some where in this thread Mumbles(0) has descibed this issue, and the solution; I cant find it exactly, but you need to have a test to ensure the back or lay side is not empty.

        Here is the code I use in market prices (from the tutorial):

        Code:
                        With .marketPrices
                            Print("MarketID = " & .marketId)
                            For i = 0 To .runnerPrices.Length - 1
                                With .runnerPrices(i)
                                    Print("Runner " & i + 1 & "  LPM = " & .lastPriceMatched)
                                    Back = ""
                                    For j = 0 To .bestPricesToBack.Length - 1 '[COLOR="Red"]TEST here[/COLOR]
                                        With .bestPricesToBack(j)
                                            Back = Back & "  " & .price & "/" & Int(.amountAvailable)
                                        End With
                                    Next
                                    Lay = ""
                                    For j = 0 To .bestPricesToLay.Length - 1 [COLOR="red"]'TEST here[/COLOR]
                                        With .bestPricesToLay(j)
                                            Lay = Lay & "  " & .price & "/" & Int(.amountAvailable)
                                        End With
                                    Next
                                    Print("Back = " & Back & "   Lay = " & Lay)
                                End With
                            Next
                        End With
        If the lay of back side is empty the code skips over it.

        grantay

        Comment

        • Mumbles0
          Junior Member
          • Jan 2009
          • 240

          #559
          Thanks Grantay. I will just add a bit more...

          I presume calypsored is calling getMarketPricesCompressed (or getMarketPrices) to get the odds. For each runner these calls return the “3 best” prices and amounts available in the arrays .bestPricesToBack and .bestPricesToLay, providing 3 best prices exist.

          Sometimes this may not be the case. This can occur if, for example, a market is new, or a runner is not fancied, or for some other reason such as the football case when the score line becomes impossible.

          Looking at the .bestPricesToBack array:

          If there are less than 3 back prices on offer, the array length will contract to accommodate only what’s available. To illustrate this:

          Usual case: 3 or more back prices available, .bestPricesToBack.Length is 3
          .bestPricesToBack(0) contains best price and amount
          .bestPricesToBack(1) contains 2nd best
          .bestPricesToBack(2) contains 3rd best

          Only 2 back prices available, .bestPricesToBack.Length is 2
          .bestPricesToBack(0) contains best price and amount
          .bestPricesToBack(1) contains the other price and amount

          Only 1 back price available, .bestPricesToBack.Length is 1
          .bestPricesToBack(0) contains the only price and amount

          No back prices available, .bestPricesToBack.Length is 0
          In this case the array exists, but its length is zero. This may sound a bit strange, but it is an empty array.

          If you attempt to access an non-existent array element you will get the “Index was outside to bounds of the array” exception, which is what will happen if you try something like:

          Print(.bestPricesToBack(0).price)
          when there are no back prices.

          So the rule is to always test .bestPricesToBack.Length to ensure that an element exists before you access it. So you should always do something like this:

          Code:
           If .bestPricesToBack.Length > 0 Then  'Array contains at least one price
             Print(.bestPricesToBack(0).price)
           Else
             Print("No price available")  'Take alternative action
           End If
          This, of course, applies to .bestPricesToLay as well.

          Comment

          • BigSprout
            Junior Member
            • Feb 2011
            • 60

            #560
            Mumbles, thank you for a wonderful and informative thread. I read through it a number of times so that I understood it and made a pledge to myself to refrain from asking questions that were already covered or could easily be found by a small effort on my part

            I succeeded

            I now have my own program working through the API to my satisfaction - it did have its moments tho

            Incident 1
            Reading through the literature and trying to work out the sequence of events in the case of a non-runner (got that sorted now)

            Incident 2
            Testing the Place multiple bets - Field Lay of $5 @ 1.01
            I was checking to see all bets were showing on my datagridview - all good
            Just before market close, one of the lays turned to "M"...somebody had placed a back bet (probably incorrectly) on the fav at odds of 100/1 ON


            ....it WON

            5 cents was deducted from my account (fav was showing around 2.90 on BF before the jump)

            Anyway it has been an enjoyable journey and I appreciate the time and effort you have put in to help all of us - well done

            cheers Les
            Last edited by BigSprout; 15-02-2011, 03:31 AM.

            Comment

            • calypsored
              Junior Member
              • Dec 2009
              • 5

              #561
              Thanks guys

              Mumbles, Grantay and Bigsprout - you can probably deduce that arrays frazzle my brain. I tried lots of different traps but not .length (associating that as I did with string length...).

              Thanks for your help.

              And I'd like to echo BigSprout's comments about the thread. I too now have an application doing exactly what I wanted it to do, and couldn't have done it without this thread. I look forward to fine tuning it now - and am sure I'll be back with other daft questions sooner or later!

              Comment

              • Grantay.
                Junior Member
                • Jan 2010
                • 53

                #562
                Humble thanks to Mumbles

                While we are doing backslaps and handshakes, I too must offer my most sincere thanks to Mumbles for this thread.

                Like bigsprout I have "hastened slowly" and built a solid app that does what I want and need.

                Not only is the information valuable, accurate and informative, Mumbles has shown infinite patience with a range of requests from users wanting a "quick answer" without going though the thread diligently, to the quite complex intricate detail of the often very confusing object structure that is the BF API. One huge advantage this tutorial provides is the confidence to launch in to the other functions and build on the overall template provided here.

                One series of Mumbles functions alone - PricetoInc and the reverse saved me hours of thinking and programming - it was a simple plug in!

                I come from an Access VBA environment so vb.net was a major leap foward; I hope one day to have the depth of understanding you have Mumbles that you have so generously provided here for all to devour and learn from.

                grantay (Canberra Australia)

                Comment

                • Timmyag
                  Junior Member
                  • Feb 2011
                  • 2

                  #563
                  Logging in

                  Ok i got stuck at step 1, i've fixed it now, well maybe but either i'm doing something daft or the guide needs updating

                  i' tried to follow the guide and got
                  Code:
                  Public Class testform
                      Dim oHeaderGL As New BFGlobal.APIRequestHeader
                      Dim BetfairGL As BFGlobal.BFGlobalService
                  
                      Private Sub BLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Blogin.Click
                          Print("*** Login ***")
                  
                          Dim oLoginReq As New BFGlobal.LoginReq
                          Dim oLoginResp As New BFGlobal.LoginResp
                          With oLoginReq
                              .username = "YourUsername"
                              .password = "YourPassword"
                              .productId = 82           'For free API
                          End With
                          'Dim oLoginIn As New BFGlobal.loginIn(oLoginReq)
                          oLoginResp = BetfairGL.login(oLoginReq)     'Call the API
                          With oLoginResp
                              CheckHeader(.header)
                              Print("ErrorCode = " & .errorCode.ToString)
                          End With
                      End Sub
                  i'm using VS2010
                  firstly the complier wont have
                  Code:
                  Dim BetfairGL As new BFGlobal.BFGlobalService
                  because new cant by used on an interface

                  so i skipped the new, and hey presto, suddenly the related call

                  Code:
                  oLoginResp = BetfairGL.login(oLoginReq)
                  is a problem, its not expecting to be passed a BFGlobal.LoginReq it wants a BFGlobal.LoginIn.

                  so i was looking in the object browser and BFGlobal.BFGlobalService doesn't have a new method / is a service/ sort of made sense why i couldn't do what i wanted. But BFGlobal.BFGlobalServiceClient did and looked like what i needed.

                  so i swapped

                  Dim BetfairGL As new BFGlobal.BFGlobalService
                  for
                  Dim BetfairGL As new BFGlobal.BFGlobalServiceClient

                  and it seemed to work. Is this me doing something wrong or have they changed it since the first page was written?

                  Thanks
                  Tim

                  Comment

                  • Mumbles0
                    Junior Member
                    • Jan 2009
                    • 240

                    #564
                    Timmyag,

                    Looks like you've added a Service Reference instead of a Web Reference. This is a common trap. See this post.

                    Comment

                    • Timmyag
                      Junior Member
                      • Feb 2011
                      • 2

                      #565
                      thank you
                      missed that, your guide was a big help

                      Comment

                      • Sports API + VB .Net
                        Junior Member
                        • Sep 2010
                        • 11

                        #566
                        Hi Mumble thanks for this great tuitorial can you point out me the page in which you have disscussed about how to display odds in grid (anygrid).......
                        what i am trying to do is on form load i populate treeview then get market id then how to display runners and odds in grid to bet please reply me as sooon as possbile with full detail i will be very thank full of yours bye tc have a nice day

                        Comment

                        • Sports API + VB .Net
                          Junior Member
                          • Sep 2010
                          • 11

                          #567
                          hi bigsproute....
                          brother i am reading this thread for last six months but i didnt get the idea that how can i make a bot which is look like betfair website like on form_load i populate treeview (provide by mumble) then display runners and three back and lay in grid and bet on appropreiate selection i am very confused if i didnt complete this task may be i fail in my life please if you can train me and send me some sort of help i will be very thankfull of yours

                          Comment

                          • BigSprout
                            Junior Member
                            • Feb 2011
                            • 60

                            #568
                            Hi Sports,
                            I am only interested in Aus horse racing and I have designed my API program with this in mind, I also enjoy betting and don't see the point in designing a bot to do all the enjoyable stuff for me.

                            That said, to have the treeview populated on Form_Load, there is a major step that is needed to be carried out beforehand:

                            "Login successfully"

                            This requires your "Username" and "Password" stored either in a file or in "My.Settings" - storing this type of data is NOT RECOMMENDED, so populating a treeview in Form_Load is something I am not even going to attempt to explain.

                            You could however, have your Login sub call the "Sub Call_PopulateTreview()" when the Login is successful

                            When reading the tutorials, where you see:
                            Print(.bestPricesToBack(0).price)

                            replace with:
                            dgvField("FdBk", .sortOrder).Value =.bestPricesToBack(0).price

                            where dgvField is the name of a "DataGridView",
                            and "FdBk" is the name of the column header

                            a "for..next" loop could be used to read all 3 back/lay prices into different dgvField columns

                            This post by Mumbles explains loading runners, just use the above code to display the odds:
                            https://forum.bdp.betfair.com/showpo...&postcount=475

                            hope this is of some use
                            cheers Les

                            Comment

                            • BigSprout
                              Junior Member
                              • Feb 2011
                              • 60

                              #569
                              Graphics drawing

                              In keeping with this tutorial I have tried to extend it to include graphics and have come across a problem that I am unable to solve.

                              I have dual monitors, on one monitor I have Form1 which displays the markets, fields, Betting Panel etc,
                              on the 2nd monitor I have Form2 with 8 WebBrowsers showing the price/volume charts of selected runners.

                              a. it is slow in filling all web browsers
                              b. the charts are already delayed, so are of minimal use

                              My next stage in the project is to collect the data and display it graphically on Form2

                              I found this code and it works on Form1, but I can't seem to get it to work on form2 (only see a blank form2)
                              Code:
                                 Sub AddRectangle(ByVal Lt As Integer, ByVal Tp As Integer, ByVal Wd As Integer, ByVal Ht As Integer)
                                      'Add rectangles to the Web Page Form for the Price/Volume charts
                              
                                      
                                      Dim myBrush As New System.Drawing.SolidBrush(System.Drawing.Color.Red)
                                      Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Black)
                                      Dim formGraphics As System.Drawing.Graphics
                              
                                      formGraphics = Me.CreateGraphics()
                                      formgraphics.DrawRectangle(myPen, New Rectangle(Lt, Tp, Wd, Ht))
                                      formgraphics.FillRectangle(myBrush, New Rectangle(Lt, Tp, Wd, Ht))
                                      myPen.Dispose()
                                      myBrush.Dispose()
                                      formGraphics.Dispose()
                              
                                  End Sub
                              I have tried sending to form2 from Form1 and also activating the code on form2 to no success.

                              I can write the code to display several rectangles and show the nice squiggly lines representing change in last matched prices once I can work out how to display graphics on a separate form.

                              I would appreciate some help with this minor problem.

                              cheers Les

                              Comment

                              • BigSprout
                                Junior Member
                                • Feb 2011
                                • 60

                                #570
                                Persevered and came up with a solution:
                                Code:
                                   Private Sub Form2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
                                        AddRectangle(20, 10, 200, 300)
                                    End Sub
                                ...now to get it all working


                                cheers Les

                                Comment

                                Working...
                                X