Step 19. Placing a Bet
I have recently been asked about bet placement procedures, so I am posting some steps giving examples of calling the API’s bet placement and monitoring services, starting with placeBets.
A typical application might monitor a market by calling getMarketPricesCompressed at regular intervals and, if conditions are right, place a bet by calling placeBets. With placeBets you can submit several bets at once and it can handle different types of bet in one call. To keep things simple the following example places a single standard exchange bet.
Add this sub to the TestForm class:
This is a sync call which follows the normal calling procedure. We create an object oBet to hold our bet parameters. This object is then assigned to the first element of the .bets array in the request object. The API Guide give details of each request parameter. In most cases, the purpose of each parameter is fairly obvious.
This example has .betPersistenceType NONE. The other bet persistence types (SP, IP) can be used as described in the API guide. Note that not all markets have these other bet persistence types available. It’s a good idea to check the .bspMarket parameter returned by getMarket before placing a bet with SP persistence. Similarly check .turningInPlay from getAllMarkets before selecting IP.
To place a bet you simply call Sub PlaceBet with your bet requirement as parameters. The calling syntax is:
To test this add a “PlaceBet” button to the TestForm. Name it bPlaceBet and add this code.
This sets up a test bet. Note that the values shown are for my example. You must plug in your own values. YourMarketId can be obtained from a previous call to getAllMarkets (refer Steps 5, 6, also 15), YourSelectionId can be obtained from a previous getMarket call for your market (refer Step 12). Change the BetType, BetPrice and BetSize to any values you like. This example specifies a Back bet, odds = 100, bet amount = 10 currency units. To test this, I suggest you choose a suitable market and set the bet parameters for a high odds back bet on a short-priced runner (this bet is unlikely to be matched).
Now log onto the Betfair website and go to your market of interest. Observe the “My Bets” tab (which will probably be empty). Now run the project. When you click the “PlaceBet” button the bet should be placed. If successful, the bet appears on the “My Bets” tab. The Ref: number shown is the betId. You can change or cancel this bet on the website if you wish.
That’s how to place a bet via the API. In the next step we will call getMUBets to monitor the status of the bet.
I have recently been asked about bet placement procedures, so I am posting some steps giving examples of calling the API’s bet placement and monitoring services, starting with placeBets.
A typical application might monitor a market by calling getMarketPricesCompressed at regular intervals and, if conditions are right, place a bet by calling placeBets. With placeBets you can submit several bets at once and it can handle different types of bet in one call. To keep things simple the following example places a single standard exchange bet.
Add this sub to the TestForm class:
Code:
Private Sub PlaceBet(ByVal YourMarket As Integer, ByVal YourSelection As Integer, ByVal YourBetType As BFUK.BetTypeEnum, ByVal BetPrice As Decimal, ByVal BetSize As Decimal)
Dim oPlaceBetsReq As New BFUK.PlaceBetsReq 'Create the request object
Dim oPlaceBetsResp As BFUK.PlaceBetsResp 'A variable to hold the response object
Dim oBet As New BFUK.PlaceBets 'An object to specify the bet
With oPlaceBetsReq
.header = oHeaderUK() 'The standard header
With oBet 'Specify the bet details:
.asianLineId = 0 'Non-asian handicap market
.betCategoryType = BFUK.BetCategoryTypeEnum.E 'Normal exchange bet
.betPersistenceType = BFUK.BetPersistenceTypeEnum.NONE 'Standard bet (not SP or InPlay persistence)
.betType = YourBetType 'Back or Lay
.marketId = YourMarket 'The market of interest
.price = BetPrice 'The desired price (odds)
.selectionId = YourSelection 'Your selection of interest
.size = BetSize 'The bet amount (stake)
.bspLiability = 0 'Must be 0 for non-SP bet
End With
ReDim .bets(0) 'Array length = 1 to hold single bet
.bets(0) = oBet 'Put the bet details object in element 0
End With
oPlaceBetsResp = BetFairUK.placeBets(oPlaceBetsReq) 'Call API to place the bet
With oPlaceBetsResp 'The response
CheckHeader(.header)
Print("ErrorCode = " & .errorCode.ToString)
If .errorCode = BFUK.PlaceBetsErrorEnum.OK Then 'The call succeeded
For i = 0 To .betResults.Length - 1 'In this case there should only be 1 result.
With .betResults(i)
Print(.resultCode.ToString) 'Result of the bet placement
If .success Then
Print("BetId = " & .betId) 'Print the betId if successful
TheBetId = .betId 'Save this
End If
End With
Next
End If
End With
End Sub
This example has .betPersistenceType NONE. The other bet persistence types (SP, IP) can be used as described in the API guide. Note that not all markets have these other bet persistence types available. It’s a good idea to check the .bspMarket parameter returned by getMarket before placing a bet with SP persistence. Similarly check .turningInPlay from getAllMarkets before selecting IP.
To place a bet you simply call Sub PlaceBet with your bet requirement as parameters. The calling syntax is:
PlaceBet(marketId, selectionId, betType, price, size)
To test this add a “PlaceBet” button to the TestForm. Name it bPlaceBet and add this code.
Code:
Dim Market As Integer = [I]YourMarketId[/I] 'The Id of your market
Dim Selection As Integer = [I]YourSelectionId[/I] 'The Id of your selection
Dim BetType As BFUK.BetTypeEnum = BFUK.BetTypeEnum.B 'For back bet
Dim BetPrice As Decimal = 100 'Your desired odds
Dim BetSize As Decimal = 10 'Your bet amount in currency units
Dim TheBetId As Long 'The betId (returned from placeBets)
Private Sub bPlaceBet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPlaceBet.Click
Print("*** PlaceBet ***")
PlaceBet(Market, Selection, BetType, BetPrice, BetSize) 'The call to place the bet
End Sub
Now log onto the Betfair website and go to your market of interest. Observe the “My Bets” tab (which will probably be empty). Now run the project. When you click the “PlaceBet” button the bet should be placed. If successful, the bet appears on the “My Bets” tab. The Ref: number shown is the betId. You can change or cancel this bet on the website if you wish.
That’s how to place a bet via the API. In the next step we will call getMUBets to monitor the status of the bet.


Comment