Using VB2008 to acccess the Betfair API: A tutorial

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • NFLMAN
    Junior Member
    • Jun 2010
    • 17

    #721
    GetAllMarkets weird issue

    Something weird is happening to my GetAllMarkets response.

    This is my code:

    Code:
    With oMarketsReq
                .header = oHeaderUK()
                ReDim .eventTypeIds(0) : .eventTypeIds(0) = 1  'This selects soccer
                .fromDate = Now.AddMinutes(-10)
                .toDate = Now.AddMinutes(10)
    End With
                oMarketsResp = BetFairUK.getAllMarkets(oMarketsReq)
    This code should select all soccer markets that have started in the last 10 minutes or will start in the next 10 minutes.

    It works fine mostly but occasionaly it will ignore my date parameters and just give me ALL soccer markets.

    Any idea what could be causing this?

    Comment

    • Camper
      Junior Member
      • Jul 2011
      • 30

      #722
      Originally posted by Geierkind View Post
      Haha, so it's really first level Thank you very much, before your solution I was confident that I already knew a little bit about it
      One more thing, lets say Lay odd 1 is 1.01 - there's no back odd in that selection but it will remain as 1.01.

      If you do this
      Code:
      With .marketPrices
      With .runnerPrices(0)
      If .bestPricesToBack.Length > 0 Then
      Back1 = .bestPricesToBack(0).price
      Else
      Back1 = ""
      End If
      End With
      It will be fine I think.

      Comment

      • Camper
        Junior Member
        • Jul 2011
        • 30

        #723
        I was wondering if we could use profit and loss values and the current bestprices to lay and back to determine the bets we need to place in order to green.

        Is that possible?

        Comment

        • cfwnotlob
          Junior Member
          • Aug 2011
          • 2

          #724
          Hi All

          Thanks to mumbles for an excelent tutorial

          i have read through all 74 pages and tried to get some of the code to work

          wonder of wonders it works -- i did copy and paste so no typos

          just a quick query as to how do i place a multiple bet -- did i miss this in the tutorial?

          horse1 win -- horse 2 win -- horse 3 win
          or
          football match 1 draw -- football match 2 draw -- football match 3 draw
          these = treble bet i think but how do i code this type of bet

          any pointers would be great

          Comment

          • NFLMAN
            Junior Member
            • Jun 2010
            • 17

            #725
            To place multiple bets on the same market you need to load up the .bets array with the details of each bet. You have already created this array in the PlaceBets section but it only holds the details of one bet at present - .bets(0).

            So all you need is to loop through obets with the details of each bet and then add this to you .bets() array. Rinse and repeat until all bet details are added then send the bet in the usual manner.

            Hope this makes sense - I can't provide actual code as I'm not at home so don't have access to it but if you're still struggling let me know and I'll post some code when I get home.

            Comment

            • Camper
              Junior Member
              • Jul 2011
              • 30

              #726
              Could someone check what's wrong with this code?
              I'm getting wrong price values when market is inplay.

              Code:
              Private Sub BetFairUK_getMarketPricesCompressedCompleted(ByVal sender As Object, ByVal e As BFUK.getMarketPricesCompressedCompletedEventArgs) Handles BetFairUK.getMarketPricesCompressedCompleted
                      Dim tot1, tot2 As Double
                      id0 = CInt(Val(bSelecid.Text))
                      id1 = CInt(Val(bSelectid1.Text))
                      Try
                          If Not e.Cancelled Then
                              With e.Result
                                  CheckHeader(.header)
                                  If .errorCode = BFUK.GetMarketPricesErrorEnum.OK Then
                                      Dim oMarketPrices As New UnpackMarketPricesCompressed(.marketPrices)
                                      With oMarketPrices
                                          If .delay > 0 Then
                                              inplay()
                                          Else
                                              inplay0()
                                          End If
                                          For i = 0 To .runnerPrices.Length - 1
                                              With .runnerPrices(i)
                                                  If .selectionId = id0 Then
                                                      tot1 = CDbl(FormatNumber(.totalAmountMatched, 0))
                                                      If .bestPricesToBack.Length > 0 Then
                                                          Dim graphvalue As Double
                                                          graphvalue = .bestPricesToBack(0).price
                                                          aB0.Text = Replace(CStr(.bestPricesToBack(0).price), ",", ".")
                                                          aBA0.Text = CStr(Int(.bestPricesToBack(0).amountAvailable))
                                                          pgraph0.Add(graphvalue)
                                                      Else
                                                          aB0.Text = "Offer"
                                                          aBA0.Text = ""
                                                      End If
                                                      If .bestPricesToBack.Length > 1 Then
                                                          aB1.Text = Replace(CStr(.bestPricesToBack(1).price), ",", ".")
                                                          aBA1.Text = CStr(Int(.bestPricesToBack(1).amountAvailable))
                                                      Else
                                                          aB1.Text = ""
                                                          aBA1.Text = ""
                                                      End If
                                                      If .bestPricesToBack.Length > 2 Then
                                                          aB2.Text = Replace(CStr(.bestPricesToBack(2).price), ",", ".")
                                                          aBA2.Text = CStr(Int(.bestPricesToBack(2).amountAvailable))
                                                      Else
                                                          aB2.Text = ""
                                                          aBA2.Text = ""
                                                      End If
                                                      If .bestPricesToLay.Length > 0 Then
                                                          Dim graphvalue As Double
                                                          graphvalue = .bestPricesToLay(0).price
                                                          aL0.Text = Replace(CStr(.bestPricesToLay(0).price), ",", ".")
                                                          aLA0.Text = CStr(Int(.bestPricesToLay(0).amountAvailable))
                                                          prgraph2.Add(graphvalue)
                                                      Else
                                                          aL0.Text = ""
                                                          aLA0.Text = ""
                                                      End If
                                                      If .bestPricesToLay.Length > 1 Then
                                                          aL1.Text = Replace(CStr(.bestPricesToLay(1).price), ",", ".")
                                                          aLA1.Text = CStr(Int(.bestPricesToLay(1).amountAvailable))
                                                      Else
                                                          aL1.Text = ""
                                                          aLA1.Text = ""
                                                      End If
                                                      If .bestPricesToLay.Length > 2 Then
                                                          aL2.Text = Replace(CStr(.bestPricesToLay(2).price), ",", ".")
                                                          aLA2.Text = CStr(Int(.bestPricesToLay(2).amountAvailable))
                                                      Else
                                                          aL2.Text = ""
                                                          aLA2.Text = ""
                                                      End If
                                                  End If
                                                  If .selectionId = id1 Then
                                                      tot2 = CDbl(FormatNumber(.totalAmountMatched, 0))
                                                      If .bestPricesToBack.Length > 0 Then
                                                          Dim graphvalue As Double
                                                          graphvalue = .bestPricesToBack(0).price
                                                          bB0.Text = Replace(CStr(.bestPricesToBack(0).price), ",", ".")
                                                          bBA0.Text = CStr(Int(.bestPricesToBack(0).amountAvailable))
                                                          pgraph1.Add(graphvalue)
                                                      Else
                                                          bB0.Text = "Offer"
                                                          bBA0.Text = ""
                                                      End If
                                                      If .bestPricesToBack.Length > 1 Then
                                                          bB1.Text = Replace(CStr(.bestPricesToBack(1).price), ",", ".")
                                                          bBA1.Text = CStr(Int(.bestPricesToBack(1).amountAvailable))
                                                      Else
                                                          bB1.Text = ""
                                                          bBA1.Text = ""
                                                      End If
                                                      If .bestPricesToBack.Length > 2 Then
                                                          bB2.Text = Replace(CStr(.bestPricesToBack(2).price), ",", ".")
                                                          bBA2.Text = CStr(Int(.bestPricesToBack(2).amountAvailable))
                                                      Else
                                                          bB2.Text = ""
                                                          bBA2.Text = ""
                                                      End If
                                                      If .bestPricesToLay.Length > 0 Then
                                                          Dim graphvalue As Double
                                                          graphvalue = .bestPricesToLay(0).price
                                                          bL0.Text = Replace(CStr(.bestPricesToLay(0).price), ",", ".")
                                                          bLA0.Text = CStr(Int(.bestPricesToLay(0).amountAvailable))
                                                          prgraph3.Add(graphvalue)
                                                      Else
                                                          bL0.Text = ""
                                                          bLA0.Text = ""
                                                      End If
                                                      If .bestPricesToLay.Length > 1 Then
                                                          bL1.Text = Replace(CStr(.bestPricesToLay(1).price), ",", ".")
                                                          bLA1.Text = CStr(Int(.bestPricesToLay(1).amountAvailable))
                                                      Else
                                                          bL1.Text = ""
                                                          bLA1.Text = ""
                                                      End If
                                                      If .bestPricesToLay.Length > 2 Then
                                                          bL2.Text = Replace(CStr(.bestPricesToLay(2).price), ",", ".")
                                                          bLA2.Text = CStr(Int(.bestPricesToLay(2).amountAvailable))
                                                      Else
                                                          bL2.Text = ""
                                                          bLA2.Text = ""
                                                      End If
                                                  End If
                                                  Dim x As String
                                                  x = FLogin.bCurrency.Text
                                                  If x = "Euro" Then
                                                      HeaderTM.Text = ("Total Matched:" & " " & tot1 + tot2 & " €")
                                                  Else
                                                      HeaderTM.Text = ("Total Matched:" & " " & tot1 + tot2 & " £")
                                                  End If
                                              End With
                                          Next
                                      End With
                                  End If
                              End With
                          End If
                      Catch ex As ApplicationException
                      End Try
                  End Sub

              Comment

              • BigSprout
                Junior Member
                • Feb 2011
                • 60

                #727
                Camper,

                hmmmm - Inplay hasn't been covered in this tutorial so Mumbles may wish to cover it as a new step.

                I don't bet "Inplay" so my knowledge is gained from generally reading other posts, so my advice may not be absolutely correct.

                My understanding is that the delay reflects the state of the market and when delay > 0 then a delay is placed on placing new bets and increasing the size of a bet placed - it does not affect the market prices.

                I would be looking to the sub inplay() as causing the problem if market prices go haywire after delay > 0 - what does it do, why is it required? (same with inplay0())?

                You need to show what this code does:
                Code:
                                            If .delay > 0 Then
                                                inplay()
                                            Else
                                                inplay0()
                                            End If

                Comment

                • davecon
                  Junior Member
                  • Dec 2010
                  • 86

                  #728
                  Hi Camper - Bigsprout
                  Bigsprout is correct regarding the Delay - .delay simply returns the amount of Seconds Delay when placing a bet in running (eg 1 sec for UK Racing) Cancelling a Bet is still instant
                  This does not affect the prices in Any Way
                  So the Delay is 0 until the "Off" then this Changes to 1
                  But it Remains at 1 until the Market is Closed (It does not revert to 0 when Suspended at the end of the race!!)
                  I use the .delay along with various combinations of the .marketStatus (which returns whether the market is Active - Suspended or Closed), For Example I use these as Identifiers in a Heading label and to use a Gas() Sub to up my Prices Timer Speed to 1100 for In Running and back to 2000 once Suspended (Free api restriction) It reverts back to speed if reopens again for Photo etc then Back again etc
                  You can also use it to cancel any "Kept" bets at the Off for instance
                  Does your Code work ok if you Comment out the delay code highlighted by Bigsprout?
                  If you want it to work differently when "In Running" you will have to incorporate your code directly into your InPlay Subs
                  Hope this helps a bit
                  Dave

                  Comment

                  • Camper
                    Junior Member
                    • Jul 2011
                    • 30

                    #729
                    Originally posted by BigSprout View Post
                    Camper,

                    hmmmm - Inplay hasn't been covered in this tutorial so Mumbles may wish to cover it as a new step.

                    I don't bet "Inplay" so my knowledge is gained from generally reading other posts, so my advice may not be absolutely correct.

                    My understanding is that the delay reflects the state of the market and when delay > 0 then a delay is placed on placing new bets and increasing the size of a bet placed - it does not affect the market prices.

                    I would be looking to the sub inplay() as causing the problem if market prices go haywire after delay > 0 - what does it do, why is it required? (same with inplay0())?

                    You need to show what this code does:
                    Code:
                                                If .delay > 0 Then
                                                    inplay()
                                                Else
                                                    inplay0()
                                                End If
                    That code only changes a TextBox Text to inplay if the market is in play, nothing else.

                    The problem is not there I'm sure.

                    Comment

                    • Camper
                      Junior Member
                      • Jul 2011
                      • 30

                      #730
                      I don't know why this doesn't work, it's my code for greening a two selections market:
                      Code:
                          Dim backgreen, laygreen, difference, sizeG, greened As Decimal
                          Dim bettypeG, selectionG As Integer '0 Bak, 1 Lay
                          Private Sub GreenStatus_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GreenStatus.Tick
                              If Val(aB0.Text) < Val(bB0.Text) Then 'Favorito A
                                  selectionG = id0
                                  backgreen = CDec(Val(aB0.Text))
                                  laygreen = CDec(Val(aL0.Text))
                                  difference = CDec(profitA - profitB)
                                  If laygreen > 0 Then
                                      If difference > 0 Then
                                          bettypeG = 1
                                          sizeG = CDec(FormatNumber(difference / laygreen, 2))
                                          greened = CDec(profitA - sizeG * (laygreen - 1))
                                      End If
                                      If difference < 0 Then
                                          bettypeG = 0
                                          sizeG = CDec(FormatNumber(difference / backgreen, 2))
                                          greened = CDec(profitA + sizeG * (backgreen - 1))
                                      End If
                                  End If
                              Else 'Favorito B
                                  selectionG = id1
                                  backgreen = CDec(Val(bB0.Text))
                                  laygreen = CDec(Val(bL0.Text))
                                  difference = CDec(profitB - profitA)
                                  If laygreen > 0 Then
                                      If difference > 0 Then
                                          bettypeG = 1
                                          sizeG = CDec(FormatNumber(difference / laygreen, 2))
                                          greened = CDec(profitB - sizeG * (laygreen - 1))
                                      End If
                                      If difference < 0 Then
                                          bettypeG = 0
                                          sizeG = CDec(FormatNumber(difference / backgreen, 2))
                                          greened = CDec(profitB + sizeG * (backgreen - 1))
                                      End If
                                  End If
                              End If
                              If greened < 0 Then
                                  Panel3.BackColor = Color.Red
                              Else
                                  Panel3.BackColor = Color.LightGreen
                              End If
                                  Label18.Text = CStr(FormatNumber(greened, 2))
                          End Sub
                          Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                              If bettypeG = 0 Then
                                  prc = laygreen
                                  sid = selectionG
                                  sz = sizeG
                                  PlaceLet(markid, sid, BFUK.BetTypeEnum.L, prc, sz)
                              End If
                              If bettypeG = 1 Then
                                  prc = backgreen
                                  sid = selectionG
                                  sz = sizeG
                                  PlaceBet(markid, sid, BFUK.BetTypeEnum.B, prc, sz)
                              End If
                          End Sub
                      It correctly determines wich bet I need to place (back or lay) the selection and the amount. It's working fine but after i click the button to green nothing happens.

                      I get an OK error from the place bet don't know what I'm missing here.

                      EDIT: I figured it out. Thanks anyway.
                      Last edited by Camper; 07-09-2011, 02:11 AM.

                      Comment

                      • nanook
                        Junior Member
                        • Feb 2011
                        • 1

                        #731
                        Exporting data to excel

                        I'm a rookie VB programmer and have implemented Mumbles' code in VB 2010 (many thanks!). However I have been unable to figure out how to export data (eg. prices) to an excel worksheet which I would like to do so I can interact with some VBA programs.

                        Any help would be greatly appreciated.

                        Comment

                        • neverfails
                          Junior Member
                          • Aug 2011
                          • 2

                          #732
                          Hi - I'm having the same issues - would be great if someone could advise.

                          Also does anyone has the code already setup in an Excel spreadsheet - that can pull in all the *UK win markets* only from the "Horse Racing - Todays Card"?

                          I would really appreciate if someone could share that.... Thanks!!

                          Comment

                          • neverfails
                            Junior Member
                            • Aug 2011
                            • 2

                            #733
                            Also.... is anyone in this forum based in London? If so I'd really like to meet up to discuss something I'm trying to do -- but I don't have the necessary VBA/VB.net programming skills to do.

                            Will pay for the project - so if anyone here is London-based and can help pls send me a private message! Tks!

                            Comment

                            • NFLMAN
                              Junior Member
                              • Jun 2010
                              • 17

                              #734
                              You can write data to a file using:

                              My.Computer.FileSystem.WriteAllText(filename,data, append)

                              filename = the name of the file to write the data too as a string
                              data = the data you want to write to the file, price, betID etc...
                              append = True/False value depending on whether you want to append the file or not

                              This will create/append a .txt file which Excel will open.

                              There are probably other ways to do this but this method works for my needs

                              Comment

                              • NFLMAN
                                Junior Member
                                • Jun 2010
                                • 17

                                #735
                                underlying connection was closed

                                I'm occasionaly getting the "ERROR: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server." message but don't know how or where to catch it because my application doesn't stop. I get a JIT message and the program carries on working in the background. I just have to click 'continue' to get rid of the message. Annoying rather than a show stopper but I would still like to stop it happening.

                                My error handling knowledge is not great so where do I put the code to catch this error popping up and what would the code look like?

                                Thanks in advance

                                Comment

                                Working...
                                X