Using VB2008 to acccess the Betfair API: A tutorial

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts

  • Oldb
    replied
    Compresses Problem...

    I am trying to get the latest BACK and LAY prices ( eg as i see it on the betfair web page)

    Using uncompressed i am getting the figures all ok

    BUT

    When using the compressed option... as below

    I am getting the figures i need but they are not consistantly in the same place in the array....

    The lines start with the id and the m= ie 50/5

    Code...


    m = UBound(Field) \ 5 - 1 'm = number of prices - 1
    ReDim .prices(m) : k = 0

    POdds(i) = POdds(i) & m & " / "

    For j = 0 To m ' eg m=5

    .prices(j) = New PricesType

    With .prices(j)
    'Load the price array
    .price = Val(Field(k + 0))
    POdds(i) = POdds(i) & .price & " / "
    .backAmount = Val(Field(k + 1))
    .layAmount = Val(Field(k + 2))
    .totalBspBackAmount = Val(Field(k + 3))
    .totalBspLayAmount = Val(Field(k + 4))
    k += 5
    End With
    Next

    I am loading into Podds()

    BUT the current Back and Lay are not allways at the end of the array Podds()


    This is the output of Podds

    4970766 @ 11 / 1.01 / 1.02 / 1.06 / 1.11 / 1.4 / 2.38 / 3.05 / 6 / 6.2 / 8 / 160 / 200 /
    4970767 @ 12 / 1.01 / 1.02 / 1.06 / 1.11 / 1.4 / 2.68 / 2.76 / 6 / 7.2 / 8.2 / 10 / 75 / 990 /
    4333840 @ 11 / 1.01 / 1.02 / 1.06 / 1.11 / 1.4 / 2.88 / 5.1 / 6 / 30 / 280 / 320 / 1000 /
    4970764 @ 18 / 1.01 / 1.02 / 1.06 / 1.11 / 1.4 / 1.46 / 1.72 / 1.73 / 2 / 2.02 / 2.1 / 2.22 / 2.88 / 2.9 / 3 / 11 / 100 / 990 / 1000 /
    4970765 @ 12 / 1.01 / 1.02 / 1.06 / 1.11 / 1.4 / 3 / 3.05 / 6 / 6.2 / 14.5 / 20 / 150 / 190 /
    4935477 @ 11 / 1.01 / 1.02 / 1.06 / 1.11 / 1.4 / 2.18 / 2.98 / 6 / 27 / 40 / 50 / 990 /
    4919959 @ 11 / 1.01 / 1.02 / 1.06 / 1.11 / 1.4 / 2.26 / 3.9 / 6 / 6.2 / 10 / 160 / 180 /
    4970772 @ 14 / 1.01 / 1.02 / 1.06 / 1.11 / 1.33 / 1.4 / 2.14 / 2.5 / 6 / 6.2 / 7 / 10 / 65 / 90 / 990 /
    4970771 @ 11 / 1.01 / 1.02 / 1.06 / 1.11 / 1.4 / 1.71 / 1.72 / 6 / 6.2 / 7 / 42 / 990 /
    4970770 @ 12 / 1.01 / 1.02 / 1.06 / 1.11 / 1.4 / 2 / 2.02 / 6 / 6.2 / 9 / 55 / 170 / 1000 /
    4970769 @ 10 / 1.01 / 1.02 / 1.06 / 1.11 / 1.4 / 2.64 / 4.3 / 6 / 16 / 150 / 230 /
    4970768 @ 12 / 1.01 / 1.02 / 1.06 / 1.11 / 1.33 / 1.4 / 2.5 / 4.5 / 6 / 12 / 90 / 370 / 390 /

    Regards
    Oldb

    Leave a comment:


  • Peter Simple
    replied
    System.ServiceModel.CommunicationException

    When i use the "getAllMarkets" method i am running in a problem when the API sends a large data file. Then it will cause a

    System.ServiceModel.CommunicationException

    because the MaxReceivedMessageSize (65536) is exceeded.

    Leave a comment:


  • Peter Simple
    replied
    Mumbles0

    Thank you very much for this tutorial. It is extremely helpfull to make the first steps.

    In Step 2 you define new Global Service object so

    Dim BetfairGL As New BFGlobal.BFGlobalService
    I used these declaration

    Dim service As New Session_Manager.BFGlobalServiceClient
    Session_Manager = BFGlobal, the difference is the "Client".

    (even when your code was my guide my test app had an other structure). Was there an update of the API. The same happened when i wanted access to the exchange markets.

    I have now tested various calls and it worked like it should.

    Leave a comment:


  • granted
    replied
    how do i access the oMarketResp.market.runners(i).selectionId from another sub

    in lesson 29 we had the code below...how can i access the oMarketResp.market.runners(i).selectionId from another sub?

    here is the code where its from:

    Public Class MarketForm

    Const PosLeft = 25 'Position of buttons from left edge of form
    Const PosTop = 25 'Position of buttons from top of form
    Const PosBut = 30 'Button spacing
    Const Bwidth = 150 'Button width

    Private Buttons As Button() 'The array of buttons

    Sub AddRunnerButtons(ByVal oMarketResp As BFUK.GetMarketResp)
    Dim Tpos As Integer = PosTop 'Top position of first button
    Dim m As Integer = oMarketResp.market.runners.Length - 1 'Number of runners - 1

    ReDim Buttons(m)
    For i = 0 To m 'For each runner
    Buttons(i) = New Button 'Create a new button object in the array element
    With Buttons(i) 'Set button properties as required
    .Left = PosLeft 'Set left position
    .Text = oMarketResp.market.runners(i).name 'Set the text
    .Tag = oMarketResp.market.runners(i).selectionId 'Save the selectionId in the button's Tag property
    .TextAlign = ContentAlignment.MiddleLeft 'Set the text alignment
    .Top = Tpos 'Set top position
    .Width = Bwidth 'Set button width
    End With
    Controls.Add(Buttons(i)) 'Display this button on the form
    AddHandler Buttons(i).Click, AddressOf RunnerButton_Click 'Assign the event handler for this button's click event
    Tpos += PosBut 'Top position of the next button
    Next

    End Sub

    'The 'Click' event handler

    Private Sub RunnerButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim RunnerButton As Button = sender 'A reference to the button that was clicked
    With RunnerButton
    MsgBox(.Tag & " " & .Text) 'Show the data for the clicked button
    End With
    End Sub

    End Class
    Last edited by granted; 09-09-2010, 02:35 PM.

    Leave a comment:


  • Mumbles0
    replied
    Step 30. Saving your project’s settings.

    A while back Welcometomylair1 asked a question about saving project settings so they would persist from one run to the next. In my reply I outlined a way to extend the existing SessToken.txt file to save project settings as well as the session token. G-Factor suggested a simpler way of doing this using VB’s inbuilt application settings facility. In this step we look at this.

    1. Saving the value of a control’s property.

    For example the Text property of a TextBox.

    TextBoxes are handy controls which are commonly used to hold settings for your project, e.g. marketId, sample frequency, etc. For this exercise drag a TextBox from the ToolBox onto the TestForm and rename it to tParam (or anything you like).

    With this TextBox selected so that its properties appear in the "Properties" widow, expand the (ApplicationSettings) node. Click on (PropertyBindings) then click (...) to show the "Applications Setting for tParam" form. Select the Text property then click (New...) in the property’s associated drop down list window to show the "New Application Setting" form. Enter these settings:

    DefaultValue: 123
    Name: Param1
    Scope: User
    Click OK, OK. With this procedure we have bound the tParam.Text property to an application setting we have created named Param1. Think of an application setting as being a special variable whose value is retained from one run to the next.

    Now run the project. Notice that the default value we entered appears in the TextBox. Change this value to something else (e.g. 456), then close the form to stop the project running. Re-run the project and notice that the new value is now in the TextBox. Thus the TextBox always retains the most recently entered value. We have achieved this without writing any code.

    2. Saving any value.

    The application settings can be used to retain any value you like. You can create a new setting by clicking "BetfairX Properties" on the Project menu to show the Properties form. Select the "Settings" tab. Here you will see all the settings used by the project, including Param1 which we added above. You add a new setting by adding a new entry to the table. For example, add the name Param2, having value "This is Param2". This setting becomes a property of the My.Settings object and can be accessed from anywhere in you code with:

    My.Settings.Param2
    To demonstrate, put this code in Sub TestForm_Load:

    Code:
      .....
      Print("Last run: " & My.Settings.Param2)  'Previous run time
      My.Settings.Param2 = Now              'Re-assign the setting 
      Print("This run: " & My.Settings.Param2)   'Current run time
      .....
    and run the project a few times. You will see that Param2 initially holds the previous run time.

    Note:

    We could have used an application setting to hold the session token rather than storing it in the auxiliary file SessToken.txt.

    Two "scopes" are available. With "User" scope the values of the settings apply only to the current (Windows) user. With "Application" scope the settings values apply to all users.

    Leave a comment:


  • Oldb
    replied
    Throttle Exceeded

    Mumbles0

    I probably did not do enough research to identify the throttle problem, sorry.

    Will now alter my code accordingly.

    I should have used the compressed function in the 1st place..!!!!!

    Thanks again for all the free advice, much appreciated.

    Regards Oldb

    Leave a comment:


  • Mumbles0
    replied
    I'm guessing that you should be placing your trimmed runners names into an array (to be sorted into alphabetical order) but perhaps don't know how to go about this.

    Post some of your code so we can see what's happening.

    Leave a comment:


  • granted
    replied
    i trimmed the numbers from oMarketResp.market.runners(i).name and then wanted them sorted by name.....

    they list out in order of their numbers even though i removed them but now i wanted to sort them alphabetically

    Leave a comment:


  • Mumbles0
    replied
    So NewString is a string, presumably containing a horse's name (e.g. "Sterling Prince").

    It is quite meaningless to call Array.Sort to sort this single string. What are you trying to achieve?

    It would work if the parameter was an array of strings, but you haven't got this.

    Leave a comment:


  • granted
    replied
    i trimmed those annoying numbers at the front of the aust. names using trim....
    and then i did the following:

    Dim NewString As String = MyString.TrimStart(MyChar)

    newstring is the names without the numbers....

    then i tried array.sort(newstring)

    thats when everything goes pear-shaped

    Leave a comment:


  • Mumbles0
    replied
    Reply to Oldb (re: getMarketPrices)

    You are calling getMarketPrices. This has a limit of only 10 calls/minute on the free API. It is better to call getMarketPricesCompressed because this has a higher limit of 60 calls/minute.

    If you are using code similar to Step 7 it is easy to change this so that getMarketPricesCompressed is called instead of getMarketPrices:

    Code:
    [COLOR="Gray"]ShowMprices(BetFairAU.[COLOR="Black"]getMarketPricesCompressed[/COLOR](MpricesReq)) 'Get market prices
    ......[/COLOR]
    Code:
    [COLOR="Gray"]Function MpricesReq() As BFAU.[COLOR="Black"]GetMarketPricesCompressedReq[/COLOR]
      Dim oMPReq As New BFAU.[COLOR="Black"]GetMarketPricesCompressedReq[/COLOR]
      With oMPReq
        .header = oHeaderAU()
        .marketId = [i]an active market ID[/i]
      End With
      Return oMPReq
    End Function
    
    Sub ShowMprices(ByVal MpriceResp As BFAU.[COLOR="Black"]GetMarketPricesCompressedResp[/COLOR])
      Dim Lay, Back As String
      With MpriceResp
        CheckHeader(.header)
        Print("ErrorCode = " & .errorCode.ToString)
        If .errorCode = BFAU.GetMarketPricesErrorEnum.OK Then
          With [COLOR="Black"]New UnpackMarketPricesCompressed(.marketPrices)[/COLOR]
            Print("MarketID = " & .marketId)
            For i = 0 To .runnerPrices.Length - 1
              With .runnerPrices(i)
    
              .................
              .................
    
              End With
            Next
          End With
        End If
      End With
    End Sub[/COLOR]
    The class UnpackMarketPricesCompressed is given in Step 14

    I don’t quite understand what your problem is with your GetRunners sub. If this calls getMarket, bear in mind that the free API limit is only 5 calls/minute.

    Leave a comment:


  • MrChameleon
    replied
    I'm a C# programmer, not VB.NET, but try this ...

    myArray.Sort()

    given ...

    List<MyClass> myArray = new List<MyClass.()
    myArray.Add(myObject);
    etc.

    Note also that you may define the the sort criterion by implementing IComparable<MyClass> on the class MyClass.

    Not sure this will help you, but it can't harm

    Leave a comment:


  • Mumbles0
    replied
    What's in myarray ?

    Leave a comment:


  • granted
    replied
    array sorting problem

    i keep getting an array sort error and i don't know why...anyone have any ideas..

    i tried array.sort(myarray(i))

    i tried array.sort(myarray)

    and i always get the error

    overload resolution failed...

    what am i not doing right?

    Leave a comment:


  • Oldb
    replied
    Exceed Throttle

    I have been using this program to retrieve odds for a year now, and really appreciate the code... its a life saver..

    I have only one lingering item....


    I am calling for odds every 10 seconds , but get an exceed throttle message., on occassions.

    I am not 100% sure of the limitation of the free api. I thought i could do 60 calls per minute.?????

    I have a timer running calling
    ShowMprices(BetFairAU.getMarketPrices(MpricesReq)) 'Get market prices
    every 10 seconds.....

    This routine calls
    GetRunners() 'Get the actual runner numbers and put into ActualRunners()

    With .marketPrices
    With .marketPrices

    Really appreciate if someone could explain how this throttle limit would apply when using this vb2008 routines..

    Regards Oldb................

    Leave a comment:

Working...
X