Handling Partial Matches Safely
Hi Mumbles
I am in the process of testing my auto trading bot which aims to automatically place trade bets on selections that have been pre-determined by a backend DB. My database is complete and selecting runners perfectly and I am in the process of testing with live data and money. On a side note it is a real pain not having a test mode to simulate real betting as I am having to waste real money just to debug my system under live conditions.
Everything is working fine apart from partial matches as obviously I need the lay amount to be equal to (or more if overlaying) to the back stake.
If I place the WIN part of the trade and it is only partially matched say £2 out of £10 then I don't want to place the LAY part at £2 if the remaining £8 is then matched later on for the WIN either on the market or at SP as I could lose out big time. This has actually happened today and because the win bet was matched in two stages the lay part was set at a lower figure than it should have been which meant when the horse lost I lost instead of winning money!
Therefore I have come up with the following business logic. I would appreciate it if you could check this logic for me and tell me if it looks correct to you.
1. My back end system calculates the runners to place trades on at intervals throughout the day and sets up records in a PENDING_BETS tables that specifies all the parameters such as Selection, Market, Bet Amount, Maximum Liability, Max / Min odds, best price, current status and so on.
2. For TRADE bets there are 2 corresponding records linked by a Trade ID key and at first only the WIN part of the bet is set to PENDING whilst the LAY part is not active. This way a LAY bet is only ever placed once the WIN part has already been matched.
3. My BOT polls this table at intervals checking for new bets to be placed and to check for settled bets or changes in statuses.
4. A WIN bet is placed at the best possible price and if the bet is matched then the LAY part of the TRADE is set to PENDING and it has it MAX Price set to the WIN bets Matched Price and a BEST Price set to something lower.
5. The bot tries to match the BEST price for the LAY bet up until the race is near and if it has not matched the BEST price by then it will resort to matching up to the MAX Price. The code will also calculate overlays when the best price can be obtained to obtain profits from losing runners.
Now the tricky bit with partial matches
I am currently checking for PARTIAL MATCHES in my PlaceBet method by checking the Size Matched against my initial desired amount to see if it is under. If they are equal then the full bet amount was taken.
I have found from my tests that a PARTIAL MATCH bet will use the same BetID for all parts. Therefore once I know that at least some of the bet has been matched I take this shared BetID from the successfully matched part and call a CancelBet method straight away. In theory this will only ever cancel any unmatched parts of the bet as you cannot cancel a matched bet.
Also if the bet was just unmatched rather than partially matched then this shouldn't cause any problems as the whole bet will be cancelled and then re-attempted on the next poll loop.
If the CancelBet call is unsuccessful due to the unmatched part having been taken OR only partially cancelled due to a slice of it being taken then I call my GetBetStatus method to get the current matched size of the bet safely knowing that this will now not change as I have cancelled everything I possibly can.
I can then call my SaveBetStatus method which will update my PENDING_BETS table in the DB so that the LAY bet amount is set to the correct amount.
For the remaining amount that was cancelled I can then create a new PAIR of WIN & LAY bets in my PENDING_BETS table.
Can you see any problems with this logic?
Thanks for your help on this matter.
Hi Mumbles
I am in the process of testing my auto trading bot which aims to automatically place trade bets on selections that have been pre-determined by a backend DB. My database is complete and selecting runners perfectly and I am in the process of testing with live data and money. On a side note it is a real pain not having a test mode to simulate real betting as I am having to waste real money just to debug my system under live conditions.
Everything is working fine apart from partial matches as obviously I need the lay amount to be equal to (or more if overlaying) to the back stake.
If I place the WIN part of the trade and it is only partially matched say £2 out of £10 then I don't want to place the LAY part at £2 if the remaining £8 is then matched later on for the WIN either on the market or at SP as I could lose out big time. This has actually happened today and because the win bet was matched in two stages the lay part was set at a lower figure than it should have been which meant when the horse lost I lost instead of winning money!
Therefore I have come up with the following business logic. I would appreciate it if you could check this logic for me and tell me if it looks correct to you.
1. My back end system calculates the runners to place trades on at intervals throughout the day and sets up records in a PENDING_BETS tables that specifies all the parameters such as Selection, Market, Bet Amount, Maximum Liability, Max / Min odds, best price, current status and so on.
2. For TRADE bets there are 2 corresponding records linked by a Trade ID key and at first only the WIN part of the bet is set to PENDING whilst the LAY part is not active. This way a LAY bet is only ever placed once the WIN part has already been matched.
3. My BOT polls this table at intervals checking for new bets to be placed and to check for settled bets or changes in statuses.
4. A WIN bet is placed at the best possible price and if the bet is matched then the LAY part of the TRADE is set to PENDING and it has it MAX Price set to the WIN bets Matched Price and a BEST Price set to something lower.
5. The bot tries to match the BEST price for the LAY bet up until the race is near and if it has not matched the BEST price by then it will resort to matching up to the MAX Price. The code will also calculate overlays when the best price can be obtained to obtain profits from losing runners.
Now the tricky bit with partial matches
I am currently checking for PARTIAL MATCHES in my PlaceBet method by checking the Size Matched against my initial desired amount to see if it is under. If they are equal then the full bet amount was taken.
I have found from my tests that a PARTIAL MATCH bet will use the same BetID for all parts. Therefore once I know that at least some of the bet has been matched I take this shared BetID from the successfully matched part and call a CancelBet method straight away. In theory this will only ever cancel any unmatched parts of the bet as you cannot cancel a matched bet.
Also if the bet was just unmatched rather than partially matched then this shouldn't cause any problems as the whole bet will be cancelled and then re-attempted on the next poll loop.
If the CancelBet call is unsuccessful due to the unmatched part having been taken OR only partially cancelled due to a slice of it being taken then I call my GetBetStatus method to get the current matched size of the bet safely knowing that this will now not change as I have cancelled everything I possibly can.
I can then call my SaveBetStatus method which will update my PENDING_BETS table in the DB so that the LAY bet amount is set to the correct amount.
For the remaining amount that was cancelled I can then create a new PAIR of WIN & LAY bets in my PENDING_BETS table.
Can you see any problems with this logic?
Thanks for your help on this matter.


Comment