Using VB2008 to acccess the Betfair API: A tutorial

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bamboozled
    Junior Member
    • Mar 2011
    • 2

    #586
    selectionid problem

    Fantastic tutorial for which many thanks. I modified showmprices slightly
    Code:
    Print("Runner " & i + 1 & .selectionId & "  LPM = " & .lastPriceMatched)
    The selectionids which shows here are different from the selectionids returned in getrunners for the same market. Why is this and how can I be be sure I've linked correct price with runner?

    Comment

    • bamboozled
      Junior Member
      • Mar 2011
      • 2

      #587
      Spotted the problem

      Duh - I am concatenating the counter to the selectionid which is obviously giving me mismatched selectionids. Stupid!

      Comment

      • Monairda
        Junior Member
        • Jan 2009
        • 32

        #588
        How to create a user control to show the datas

        Good night

        I would like to advise me how to start to make a panel for the data request to the API with a custom user control. An example might start with something like the bot written in C # (http://forum.bdp.betfair.com/attachm...1&d=1256657197)

        Can you help me create a custom user control that contains the information Mumbles has taught us to ask the API and stop using the Gridview control?

        Thank you very much!!

        Comment

        • BigSprout
          Junior Member
          • Feb 2011
          • 60

          #589
          Moniarda,

          I think it is a datagridview that is being used (or you can use one the same way), except buttons have been used in some columns

          If you put a "DGV" on a form:
          click on it and select "Colums - collections"
          an "Edit Colums" window pops up

          click "Add" - Add Column window displayed with:
          Name: Column1
          Type: DataGridViewTextBoxColumn
          HeaderText: Column1

          Look at - Type: DataGridViewTextBoxColumn

          to the right is a down arrow for a drop down box display
          Click on the arrow

          there is now a list of selections including "DataGridViewButtonColumn"
          You can also use this box to add "checkbox, combobox....etc" to your datagridview


          so for the runner names you would use the default DataGridViewTextBoxColumn

          then select dropdown box and select "DataGridViewButtonColumn" for your Back/SP/Lay information
          this will now display buttons in the selected columns instead of text boxes

          cheers

          Comment

          • Monairda
            Junior Member
            • Jan 2009
            • 32

            #590
            Thank you very much BigSprout

            I'll try what you mention.

            Cheers

            Comment

            • Monairda
              Junior Member
              • Jan 2009
              • 32

              #591
              Insert a datagridviewButtonColumn

              Good morning

              I'm trying to insert a button in a DataGridView cell.

              Use the code provided by Mumbles0 and following the advice of BigSprout:

              Code:
              Public Class RunnerGrid   'A customised DataGridView
              
                  Inherits DataGridView
              
                  Public Const colRunner = "Runner"  'Runner names column
                  Public Const colSelId = "SelId"    'SelectionId column
                  Public Const colBack = "Back"      'Back price column
                  Public Const colLay = "Lay"        'Lay price column
                  Public Const butback = "BBack"    'Back price button column
              
              
                  Sub New(ByVal Runners As BFUK.Runner())
              
                      RowHeadersVisible = False  'Turn off row headers
                      Dock = DockStyle.Fill       'The grid fills the Tab
                      If Runners.Length > 0 Then   'Runner data exists
              
                          Dim NameColumn As New DataGridViewTextBoxColumn  'Add column   for runner names 
                          With NameColumn
                              .Name = colRunner
                              .ReadOnly = True
                              .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells  'Column width is automatic
                          End With
                          Columns.Add(NameColumn)  'Add to columns collection
              
                          Dim SelIdColumn As New DataGridViewTextBoxColumn  'Add column for selectionId
                          With SelIdColumn
                              .Name = colSelId
                              .Width = 70       'Column width is fixed
                              .ReadOnly = True
                          End With
              
                          Columns.Add(SelIdColumn)  'Add to columns collection
              
                          RowCount = Runners.Length  'A row for each runner 
                          For i = 0 To RowCount - 1
                              With Runners(i)
                                  Item(colRunner, i).Value = .name  'Add the runner name
                                  Item(colSelId, i).Value = .selectionId  'Add the selectionId
                              End With
                          Next
              
                          Dim BackColumn As New DataGridViewTextBoxColumn  'Add column for Back prices
                          With BackColumn
                              .Name = colBack
                              .Width = 50
                              .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                              .DefaultCellStyle.Format = "f2"
                              .ReadOnly = True
                          End With
                          Columns.Add(BackColumn)
              
                          Dim LayColumn As New DataGridViewTextBoxColumn  'Add column for Lay prices
                          With LayColumn
                              .Name = colLay
                              .Width = 50
                              .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                              .DefaultCellStyle.Format = "f2"
                              .ReadOnly = True
                          End With
                          Columns.Add(LayColumn)
              
                          Dim buttonBack As New DataGridViewButtonColumn()
                          With buttonBack
                              .Name = butback
                              .UseColumnTextForButtonValue = True
                              .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
                              .FlatStyle = FlatStyle.Flat
                              .CellTemplate.Style.BackColor = Color.Blue
                              .DisplayIndex = 0
                          End With
                          Columns.Add(buttonBack)
              
                      End If
                  End Sub
              
                  Sub UpdatePrices(ByVal RunnerPrices As BFUK.RunnerPrices())  'Method to update the Prices
                      Dim i, j, SelectionId As Integer
              
                      For i = 0 To RowCount - 1  'For each row in grid
                          SelectionId = Item(colSelId, i).Value
                          j = Array.FindIndex(RunnerPrices, Function(x As BFUK.RunnerPrices) x.selectionId = SelectionId) 'Array index for this runner
                          If j >= 0 Then 'Prices exist for this runner
                              With RunnerPrices(j)
                                  If .bestPricesToBack.Length > 0 Then
                                      Item(colBack, i).Value = .bestPricesToBack(0).price
                                      Item(butback, i).Value = .bestPricesToBack(0).price
                                  Else
                                      Item(colBack, i).Value = ""
                                      Item(butback, i).Value =""
                                  End If
                                  If .bestPricesToLay.Length > 0 Then
                                      Item(colLay, i).Value = .bestPricesToLay(0).price
                                  Else
                                      Item(colLay, i).Value = ""
                                  End If
                                  'Item(colBack, i).Value = If(.bestPricesToBack.Length > 0, .bestPricesToBack(0).price, "")   'Update best Back price
                                  'Item(colLay, i).Value = If(.bestPricesToLay.Length > 0, .bestPricesToLay(0).price, "")      'Update best Lay price
                              End With
                          End If
                      Next
              
                  End Sub
                  
              End Class
              But when I run it does not fill the price value on the button

              Can you help me? Thanks

              Comment

              • 1nner
                Junior Member
                • Mar 2011
                • 11

                #592
                getMarketTradedVolumeAsync

                Hi,

                I have a question regarding that getMarketTradedVolumeAsync call. I am supplying a marketId and selectionId, and getting the response as expected. The problem I'm having is that the response does not contain the marketId paramater, and because I'm making many of these async calls I cannot tell which reponse is which.

                The only way I can think of to solve this is to use the userState parameter to pass the marketId, which I can then examine later but I'm not sure if this is the best way of doing things (ie does the e.UserState property always contain the right value)?

                Any help would be greatly appreciated.

                Regards,

                Andrew
                Last edited by 1nner; 03-04-2011, 07:00 AM.

                Comment

                • Monairda
                  Junior Member
                  • Jan 2009
                  • 32

                  #593
                  Good morning
                  I know how to solve the problem. As simple as removing the following:
                  Code:
                  Dim buttonBack As New DataGridViewButtonColumn()
                              With buttonBack
                                  .Name = butback
                                  [B][COLOR="Red"].UseColumnTextForButtonValue = True[/COLOR][/B]
                                  .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
                                  .FlatStyle = FlatStyle.Flat
                                  .CellTemplate.Style.BackColor = Color.Blue
                                  .DisplayIndex = 0
                              End With
                              Columns.Add(buttonBack)
                  If we take to next line
                  .UseColumnTextForButtonValue = True
                  everything works fine

                  Now the question I have is like the button to display the two values. The value of the odd and the amount available

                  Greetings

                  Comment

                  • tagorac
                    Junior Member
                    • Jan 2009
                    • 3

                    #594
                    Originally posted by 1nner View Post
                    [...]

                    ie does the e.UserState property always contain the right value?

                    [...]
                    It does, but i don't think the marketId alone is unique enough for (potentially) multiple/concurrent asynchronous calls. I'm sending a custom object containing the marketId + a random number.

                    Stephan

                    Comment

                    • Mumbles0
                      Junior Member
                      • Jan 2009
                      • 240

                      #595
                      UserState parameter

                      1nner,

                      Using the userState parameter in the async call is the way to do it. You can plug the marketId directly into the call like this:

                      BetFairUK.getMarketTradedVolumeAsync(Req, Req.marketId)
                      then get the marketId from the response like this:

                      marketId = e.UserState
                      But the problem here is that you cannot have any outstanding async requests (i.e. requests that are waiting for a response). So it’s better to use a userState object containing the marketId:

                      BetFairUK.getMarketTradedVolumeAsync(Req, New UserState(Req.marketId))
                      then extract the marketId from the response:

                      marketId = e.UserState.Param
                      where UserState is this simple class:

                      Code:
                      Class UserState  'A class for userState objects
                          Property Param As Object
                          Sub New(ByVal Value As Object)
                            Param = Value
                          End Sub
                        End Class
                      You could add another property to this class to hold the selectionId if you wanted to.

                      Tagorac has suggested including a random number in the object as well as the marketId. This certainly would work OK, but I don’t think it’s necessary. The objects created from class UserState should be unique enough.

                      This topic has been discussed previously. Refer here & here.
                      Last edited by Mumbles0; 05-04-2011, 12:31 PM.

                      Comment

                      • 1nner
                        Junior Member
                        • Mar 2011
                        • 11

                        #596
                        Thanks for the replies, very helpful. Regarding the posts you linked to mumbles - does this mean I should pass the marketId with every Async call and never rely on the marketId property that is returned in the response object?

                        Comment

                        • Mumbles0
                          Junior Member
                          • Jan 2009
                          • 240

                          #597
                          It's a good idea.

                          If the marketId property is given in the response object then it's OK to use this, but there may be times when it doesn't return, for example if an error occurs.

                          Comment

                          • waldjunge
                            Junior Member
                            • Apr 2009
                            • 12

                            #598
                            There are Place, Lay, Back bets on betfair.

                            I can place Lay and Back bets with the API with no problem. How to place "Place" bets where the horse has place 1,2 or 3 with the free API?

                            Comment

                            • Mumbles0
                              Junior Member
                              • Jan 2009
                              • 240

                              #599
                              Place markets

                              Waldjunge,

                              For a horse race the "Place" market is a separate market.

                              If you call getAllMarkets and look at the marketId, menuPath and marketName parameters in the marketData array you might have, for example:
                              102596266 \Horse Racing\GB\Wolv 11th Apr 1m Hcap
                              102596267 \Horse Racing\GB\Wolv 11th Apr To Be Placed
                              102596268 \Horse Racing\GB\Wolv 11th Apr 5f Claim Stks
                              102596269 \Horse Racing\GB\Wolv 11th Apr To Be Placed
                              102596270 \Horse Racing\GB\Wolv 11th Apr 6f Hcap
                              102596271 \Horse Racing\GB\Wolv 11th Apr To Be Placed
                              102596272 \Horse Racing\GB\Wolv 11th Apr 7f Hcap
                              102596273 \Horse Racing\GB\Wolv 11th Apr To Be Placed
                              102596274 \Horse Racing\GB\Wolv 11th Apr 1m Mdn Stks
                              102596275 \Horse Racing\GB\Wolv 11th Apr To Be Placed
                              102596276 \Horse Racing\GB\Wolv 11th Apr 1m1f Claim Stks
                              102596277 \Horse Racing\GB\Wolv 11th Apr To Be Placed
                              102596278 \Horse Racing\GB\Wolv 11th Apr 6f Hcap
                              102596279 \Horse Racing\GB\Wolv 11th Apr To Be Placed
                              Every second market is, in fact, a "Place" market. So 10259266 is the "Win" market for Wolv 11th Apr 1m Hcp, while 10259267 is the "Place" market for the same race, and so on. The "Place" market allows you to bet a horse to run 1st, 2nd or 3rd.

                              Comment

                              • 1nner
                                Junior Member
                                • Mar 2011
                                • 11

                                #600
                                Connection closed exception

                                Hey,

                                I'm having a problem with an exception being thrown that I do not understand. I'm calling getMarketPricesCompressedAsync once per second (for different markets) and it's been working fine up until today.

                                This morning however, my program made 15 calls in a row with no response and then an error was thrown with the message "The underlying connection was closed: An unexpected error occurred on a receive."

                                Has anyone encountered anything similar to this before?

                                Any help would be appreciated.

                                Regards,

                                Andrew

                                Comment

                                Working...
                                X