Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Step 28. Placing Multiple Bets with placeBets.

In Step 19 we called placeBets to place a single bet. In the example we assigned our desired bet parameters to the properties of an oBet object (of type BFUK.PlaceBets), then assigned this object to the first element of the .bets array in the request object oPlaceBetsReq. Because the .bets property is an array, we can use this to place several bets at once simply by increasing the length of this array (with a Redim statement), and assigning the parameters for each bet to each element of this array. The length of the .bets array must equal the number of bets to be placed.

Note this restriction: all bets must be on the same market.

A scheme to place 3 bets could be like this:

Code:

Dim oPlaceBetsReq As New BFUK.PlaceBetsReq 'Create the request object

Dim oPlaceBetsResp As BFUK.PlaceBetsResp 'A variable to hold the response object

With oPlaceBetsReq

.header = oHeaderUK() 'The standard header

ReDim .bets(2) 'Set array upper bound = 2 (to hold 3 bets)

 

.bets(0) = New BFUK.PlaceBets 'Create an object in element(0)

With .bets(0)

'Assign properties to specify 1st bet

End With

 

.bets(1) = New BFUK.PlaceBets 'Create an object in element(1)

With .bets(1)

'Assign properties to specify 2nd bet

End With

 

.bets(2) = New BFUK.PlaceBets 'Create an object in element(2)

With .bets(2)

'Assign properties to specify 3rd bet

End With

 

End With

 

oPlaceBetsResp = BetfairUK.placeBets(oPlaceBetsReq) 'Call API to place the bet

 

...........................

 

process response as per step 19 example

............................

This extends the example in Step 19 to the multiple bet scenario. In the code there is a minor difference. We don’t create an intermediate oBet object. Here we put a BFUK.PlaceBets object into each element of .bets and assign the properties directly.

The results of the bet placement are returned in the .betResults array of the response object in the same order as they appear in the .bets array of the request object (at least I assume this is the case - the documentation does not specifically say so but this seems to be what happens).

Here is another example which uses a List(Of T) object to hold a list of bet details. This is a convenient array-based object because it grows automatically as you add more items. No Redim is required.

In an application you may be analysing a market looking for betting opportunities. Rather than call placeBets whenever a bet is found, you can save the bet details in this list and submit a batch of bets later on. We create a suitable List object with this statement:

Code:

Private BetList As New List(Of BFUK.PlaceBets)

You call Sub ListBet whenever you wish to add a bet to the List:

Code:

Sub ListBet(ByVal MarketId As Integer, ByVal YourSelection As Integer, ByVal YourBetType As BFUK.BetTypeEnum, ByVal BetPrice As Decimal, ByVal BetSize As Decimal)

 

Dim oBet As New BFUK.PlaceBets 'An object to specify the bet



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 = MarketId '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

BetList.Add(oBet) 'Add this bet to the list

 

End Sub

This sub sets up an oBet object in a similar way to Sub PlaceBet of Step 19, but this object is saved in BetList, rather than sent immediately to the API in the request object. You can keep calling Sub ListBet to add bets to the list. You can then send the listed bets to the API in one hit using Sub SubmitBets:

Code:

Sub SubmitBets(ByVal MarketId As Integer)

Dim oPlaceBetsReq As New BFUK.PlaceBetsReq 'Create the request object

With oPlaceBetsReq

.header = oHeaderUK() 'The standard header

.bets = BetList.ToArray 'The array containing the list of bets

End With

BetfairUK.placeBetsAsync(oPlaceBetsReq, New UserState(MarketId)) 'Call API to place the bets

BetList.Clear() 'Clear the list

End Sub

Here we assign the request object in the usual way. Note the use of the .ToArray property of BetList. This returns a reference to BetList’s internal array.

I have made this an async call to provide best efficiency, particularly when working with multiple markets. For multiple markets you can to repeat the “list and submit” process for your bets on each market. This can be done in rapid succession.

Each async call responds via a common event handler (placeBetsCompleted). If you look at the API Guide for placeBets you will see that the response contains the betResults array which returns the betIds (if successful), but there is no marketId or other identifying parameter. So if you receive several responses at once for bets placed on different markets, you don’t know which market each response is for.

To overcome this problem we can make good use of the optional userState parameter of the async call. We require a simple class to encapsulate a user parameter (or set of parameters in an object or structure):

Code:

Class UserState

Property Param As Object

Sub New(ByVal Value As Object)

Param = Value

End Sub

End Class

(Note here I’m using the new single-line Property statement of VB2010. If you are using VB2008 either complete the Property Get/Set snippet or use the Public keyword.)

Using this we send the marketId along with the request object in the async call. Because this UserState object is returned in the response, we can identify the market associated with each response. The UserState objects thus sent are considered to be unique (even if the marketId is the same).

An event handler for testing this:

Code:

Private Sub BetfairUK_placeBetsCompleted(ByVal sender As Object, ByVal e As BFUK.placeBetsCompletedEventArgs) Handles BetfairUK.placeBetsCompleted

Try

If Not e.Cancelled Then

With e.Result

CheckHeader(.header)

Print("MarketId = " & e.UserState.Param) 'The marketId comes back in e.UserState

If .errorCode = BFUK.PlaceBetsErrorEnum.OK Then 'placeBets call OK

 

For i = 0 To .betResults.Length - 1

With .betResults(i)

Print(.resultCode.ToString) 'Result of the bet placement

If .success Then Print("BetId = " & .betId) 'Print the betId if successful

End With

Next

 

Else

Print("errorCode = " & .errorCode.ToString) 'If placeBets error

End If

End With

End If

 

Catch ex As ApplicationException 'If problem

Print(ex.Message)

End Try

 

End Sub

Note that the response object is returned in e.Result, and e.UserState contains the UserState object exactly as sent in Sub SubmitBets.

If you can get a scheme working based on these ideas your project should be capable of placing a large number of bets in a short space of time. Good luck.

 

 


Date: 2014-12-21; view: 1189


<== previous page | next page ==>
Step 27. Understanding Object References | UserState object.
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.008 sec.)