Using VB2008 to acccess the Betfair API: A tutorial

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John_The_Greek
    Junior Member
    • Nov 2010
    • 6

    #526
    Thanks mubles0

    Comment

    • monkeymagix
      Junior Member
      • Jul 2010
      • 105

      #527
      Unmatched bets

      Hi Mumbles

      Hope you had a nice Xmas and a break from coding / betting for a while due to all the abandoned race meetings.

      Your correct in that my term "partial cancellation" just means that I went to cancel an unmatched bet only for part of the bet to be matched in that split second. Therefore only a portion of the total amount I wanted to cancel was actually cancelled. The issue lies in the fact that Betfair uses the same BetID to refer to all parts of a bet rather than having say a Parent ID and then Bet Segment IDs for each matched, unmatched, settled or cancelled portion. This makes identifying the exact status of a bet quite tricky from the BetID alone.

      I have a couple of questions

      I have followed your suggestion about leaving bets on the market rather than having a stab and then cancelling straight after however I have noticed that on a number of occasions my bet doesn't get accepted even with a status of unmatched (U). I haven't found any kind of pattern to this and sometimes my bets will get matched and I get a status of M and a BetID, sometimes they get taken as unmatched e.g I get a BetID and a status code of U and sometimes I won't get anything e.g no status and no BetID. The code I am using is exactly the same so I am a little unsure as why this is occurring as I am not experiencing any errors during the placement of the bet.

      Also I noticed today that a load of my bets were not placed due to Ffos Las not being in the Timeform racecourse abbreviation CSV file. I have tried looking on the web for the code but cannot find anything and I am guessing at the moment that it is either Ffos or Ffl however I cannot test this out as there are no races at that course tomorrow and the compressed market data only contains races in the future. I don't suppose you know the code?

      Thanks

      Comment

      • monkeymagix
        Junior Member
        • Jul 2010
        • 105

        #528
        Change in match price

        I had something odd happen today with one of my bets that I am trying to find an explaination for.

        I placed a To WIN bet on a horse at 9.37 am and it was matched at a price of 12.80.

        My bot then checks the status of all open bets every half hour in case their status changes, they are pulled out or the meeting is cancelled etc.

        However at 11.00am when I checked the status of this bet the matched price had changed from 12.80 to 11.80.

        I could understand if the bet had been given a betPersistenceType of SP and the price changed at the race time but it wasn't and the matched price changed well over 3 hours before the race. Also the full desired amount was matched so it wasn't as if the bet was matched in chunks e.g multiple partial matches.

        I haven't seen this happen before and it messed up my LAY as the the Max LAY Price was set to high to offset the WIN bet.

        Can anyone think of a possible reason why the matched price would change like this and how I could find out what had happened now that the bet has been settled.

        The details from my log file are blow

        Update WIN BET Status for BetID: 13154626462 - Skippers Brig - Leopardstown - Dec 29 2010 2:30PM; Bet Status: M; System Status: BET PLACED; Matched Amount: 2.80; Matched Price: 12.50; Liability: 0.00; 2010-12-29 09:37:03.370


        Update WIN BET Status for BetID: 13154626462 - Skippers Brig - Leopardstown - Dec 29 2010 2:30PM; Bet Status: M; System Status: BET PLACED; Matched Amount: 2.80; Matched Price: 11.80; Liability: 0.00; 2010-12-29 11:00:03.463

        Am I missing something?

        Comment

        • davecon
          Junior Member
          • Dec 2010
          • 86

          #529
          Bet Change

          Update WIN BET Status for BetID: 13154626462 - Skippers Brig - Leopardstown - Dec 29 2010 2:30PM; Bet Status: M; System Status: BET PLACED; Matched Amount: 2.80; Matched Price: 12.50; Liability: 0.00; 2010-12-29 09:37:03.370


          Update WIN BET Status for BetID: 13154626462 - Skippers Brig - Leopardstown - Dec 29 2010 2:30PM; Bet Status: M; System Status: BET PLACED; Matched Amount: 2.80; Matched Price: 11.80; Liability: 0.00; 2010-12-29 11:00:03.463

          Am I missing something?[/QUOTE]

          Hi Monkey
          As your bet is showing Odds of 11.80 (Not allowable odds) then this must have been subject to the Dreaded Reduction Factor after a Non-Runner - Were any other unmatched bets you had in this Market also Cancelled? - You will have to look into this regarding your Program as well because all these prices must also be subject to the Reduction Factor if not
          It makes matters worse when Betfair do not even meter this reduction factor - Timeform do it apparently
          I cant help you with the Programming I'm afraid as I am just working through this great course from Mumbles
          (I will certainly have to later)
          Hope this Helps
          Dave

          Comment

          • Mumbles0
            Junior Member
            • Jan 2009
            • 240

            #530
            Your Trading App

            Monkeymagix,

            Problem: Missing bet status.

            I have followed your suggestion about leaving bets on the market rather than having a stab and then cancelling straight after however I have noticed that on a number of occasions my bet doesn't get accepted even with a status of unmatched (U). I haven't found any kind of pattern to this and sometimes my bets will get matched and I get a status of M and a BetID, sometimes they get taken as unmatched e.g I get a BetID and a status code of U and sometimes I won't get anything e.g no status and no BetID. The code I am using is exactly the same so I am a little unsure as why this is occurring as I am not experiencing any errors during the placement of the bet.
            If I can have a guess: Perhaps you are sending the status request too soon after placing a bet. A finite amount of time is required for the exchange to process the bet and for the resulting status to be sent to the server which handles your bet status request.

            If you request the status too early, the info for the bet you have just placed might not appear in the bet status response.

            Comment

            • monkeymagix
              Junior Member
              • Jul 2010
              • 105

              #531
              Thanks Davecon I didn't even realise there was a limit on the kind of prices you could set bets to and because my bot is automatically calculating the TRADE LAY price by deducting a specified percentage from the matched WIN price on occasion this is giving me prices that are unacceptable e.g 11.80 as from between 10-20 you must increment in steps of 0.5.

              I have never seen a list of these acceptable price ranges anywhere so I have created one myself by using the betfair website and I will need to update my code to handle this.

              Strange thing is I wasn't getting any error message back from the API which would have pointed this out to me a long time ago. I get errors back if the bet amount is below £2 or I put a long floating number in as a price so I would have expected the BFUK.PlaceBetsResultEnum.INVALID_SIZE error to be raised but its not.

              Thanks for pointing this out to me

              Comment

              • monkeymagix
                Junior Member
                • Jul 2010
                • 105

                #532
                Code to ensure a price is valid

                If anyone is interested the following code may be useful for ensuring that a price is valid. Betfair only allows prices that increment in certain steps (which I only found out today) therefore if you have a price of 11.80 it won't be accepted as prices between 10 and 20 must increment in steps of 0.5 so 11, 11.50 or 12 are valid.

                I am doing automatic trading where the LAY bet price is set once the WIN bet has been placed and I create a range (best price, max price) by deducting specified values from the matched WIN price. Therefore quite often these prices are actually invalid and my bets were not being placed.

                I created this User Defined Function (SQL 2005) which will round any invalid price down to the next valid value and I just wrap any potential prices in it before use. You can use it as is or take the code out and use it a pseudo code.

                Code:
                -- =============================================
                -- Author:		Rob Reid
                -- Create date: 04-JAN-2011
                -- Description: Ensure prices are valid in terms of betfairs price ranges
                /*
                
                -- examples 
                SELECT dbo.udf_BETFAIR_CHECK_PRICE(11.82) 
                11.50
                
                SELECT dbo.udf_BETFAIR_CHECK_PRICE(115.81)
                110.00
                
                SELECT dbo.udf_BETFAIR_CHECK_PRICE(2.47)
                2.46
                
                SELECT dbo.udf_BETFAIR_CHECK_PRICE(1.49)
                1.49
                
                */
                -- =============================================
                ALTER FUNCTION [dbo].[udf_BETFAIR_CHECK_PRICE]
                (
                	@Price decimal(10,2)
                )
                RETURNS decimal(10,2)
                AS
                BEGIN
                		
                	DECLARE @Remainder decimal(10,2)
                	
                	-- check whether our price is within each betfair range and check for any remainder
                	-- usinga MOD function. If there is no remainder the price is already valid and if
                	-- there is a remainder we can deduct it from our price to create a valid value
                	SELECT	@Remainder = CASE 	WHEN @Price BETWEEN  100 AND 1000 THEN @Price % 10 
                				WHEN @Price BETWEEN  50 AND 99 THEN @Price % 5  
                				WHEN @Price BETWEEN  30 AND 49 THEN @Price % 2  
                				WHEN @Price BETWEEN  20 AND 29 THEN @Price % 1  
                			        WHEN @Price BETWEEN  10 AND 19.99 THEN @Price % 0.5  
                				WHEN @Price BETWEEN  6 AND 9.99 THEN @Price % 0.2  
                				WHEN @Price BETWEEN  4 AND 5.99 THEN @Price % 0.1  
                				WHEN @Price BETWEEN  3 AND 3.99 THEN @Price % 0.05  
                				WHEN @Price BETWEEN  2 AND 2.99 THEN @Price % 0.02  
                				ELSE 0 END
                								
                	-- if there is any remainder we deduct it from our price to create a valid price
                	IF @Remainder > 0
                	  SELECT @Price = @Price - @Remainder	  
                	
                	RETURN @Price	
                	
                END

                Comment

                • Mumbles0
                  Junior Member
                  • Jan 2009
                  • 240

                  #533
                  Price Increments

                  Monkeymagix,

                  The table of price increments is given here.

                  If you attempt to place a bet at price 11.80 the resultCode will return INVALID_INCREMENT.

                  Comment

                  • monkeymagix
                    Junior Member
                    • Jul 2010
                    • 105

                    #534
                    Thansk

                    Thanks for that Mumbles.

                    I really hadn't seen that table anywhere before and the limit on prices definitely explains a lot of issues I have had with my auto trading bot.

                    I have updated my bot to handle the INVALID_INCREMENT error message.

                    Can you point me to something that lists out the specific details of the errors and what would cause them to be raised. Most of them are obvious but I am interested in knowing what instances would cause CANNOT_ACCEPT_BET
                    to be raised.

                    Thanks for your help

                    Comment

                    • monkeymagix
                      Junior Member
                      • Jul 2010
                      • 105

                      #535
                      LAPSED Bet status hours before Race Time

                      Could someone help me explain something I have noticed today.

                      My bot placed a LAY bet at 3.00 am this morning which wasn't matched and it had a status of U for a good few hours. The race time is for 14:15 this afternoon and it was part of a trade bet and the WIN part was matched immediately.

                      However at 09:01:02 this morning when my bot checked the current status of the bet to see if it had been matched the betfair API returned a status code of L = LAPSED.

                      I have gone into the betfair website to double check this and the bet is under the LAPSED section in the current bets part of the site.

                      The WIN part of the TRADE is still active and appearing in the MATCHED section so the runner hasn't been removed from the race or the race cancelled and I would have expected a V = VOID status code if that had happened anyway.

                      Does anyone know what would cause a status code of L = LAPSED to occur before the market has been settled? I was under the impression the only way to obtain a status of LAPSED was when an unmatched bet couldn't be matched before the market was settled.

                      From my log file

                      Checking Bet Status for a LAY bet with BetID = 13274041XXX which was for XX LAY bet for: Inside Dealer at Taunton - 10/01/2011 14:15:00; Last Bet Status: U
                      This bets status has changed from U to L

                      Thanks

                      Comment

                      • John_The_Greek
                        Junior Member
                        • Nov 2010
                        • 6

                        #536
                        Problem with datagrid

                        Hello friends,

                        i have a little problem

                        i populate a datagrid view through a DB with an sql sommand. One collumn is a race time which in .net appears with full date and time (e.g. 1/15/2011 11:50 am).

                        Please advice how i can achieve to show only the time (e.g 11:50)


                        Thanks alot


                        John

                        Comment

                        • Mumbles0
                          Junior Member
                          • Jan 2009
                          • 240

                          #537
                          Formatting Time column

                          John_The_Greek,

                          You can format the data in a column of a DataGridView by adding code to handle its CellFormatting event. This event fires whenever your program writes data to a cell of the DataGridView. Here is an example:

                          Code:
                          Private Sub DGView_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DGView.CellFormatting
                              Dim d As Date
                          
                              If e.ColumnIndex = RaceTimeColNo Then  'Race time column is being updated
                                d = e.Value
                                e.Value = d.ToString("H:mm")  'Convert DateTime value to 24-hour time value
                              End If
                          
                            End Sub
                          The system calls this with e being an object containing some properties of the cell currently being written to.
                          RaceTimeColNo is the column number of your race time column.
                          The code modifies the Value property of the cell using the ToString method with a custom format string ("H:mm").

                          Comment

                          • John_The_Greek
                            Junior Member
                            • Nov 2010
                            • 6

                            #538
                            mubles0,

                            thank you so much...

                            so simple and yet so effective...

                            thanks again friend

                            have a nice day

                            Comment

                            • John_The_Greek
                              Junior Member
                              • Nov 2010
                              • 6

                              #539
                              Problem with bet result

                              Hello friends,

                              I have read the thread and the API guide but i can not understand how can i get the result of a settlet bet. (e.g. i have the bet id and i want the API to return the bet's profit or loss).

                              Please if you have write about this in this forum tell me the page no or the topic title.


                              Thanks


                              John

                              Comment

                              • Mumbles0
                                Junior Member
                                • Jan 2009
                                • 240

                                #540
                                Bet results

                                Call getBet. This returns a lot of info about your bet.

                                Comment

                                Working...
                                X