Monitoring Bets by BetID
Posted in Betfair bot for this week, algorithm and strategy by Mir.
Ahoj Štefane,
díky příkladům vím, jak zjistit uzavřené či neuzavřené sázky.
Jak se dají, prosim tě, nejlépe zkontrolovat sázky podle BetID; jestli jsou uzavřené či neuzavřené?
(umístím konkrétní sázku a pak chci vědět, jestli se ta konkrétní sázka uzavřela; bez ohledu na další vývoj na trhu)
--------------------------
hi Stefan and all "casual coders" ;)
For my long trading strategy I would like to know how monitoring bets by BetID. Idea is simple: I want to know if single bet placed on selection was matched or not (without other event in the market).
Saturday, April 17, 2010
Betfair bot: Bfexplorer.Scripting.TestMonitoringBetsByBetIdBot
This simple bot script code shows how the bot places 3 lay bets. The first lay at the best offered odds and two bets offering on best back odds - 1 and 2 ticks. That means that at least two bets should be unmatched. The bot stores then the bet id of the first unmatched bet and reports all bets status. Run the bot in the practice mode to see how bots operates.
You can manually update or cancel bet/s again to see how the bot will interact, so you will be able to understand the bot code. The bot stops its execution after all bets are placed, or when there is no unmatched bet, after cancelling them.
class TestMonitoringBetsByBetIdBot : Bot { // Const private const double Stake = 2; // Data private long myBetId = 0; public TestMonitoringBetsByBetIdBot(IBetfairService betfairService, MonitoredMarket monitoredMarket, Runner runner) : base(betfairService, monitoredMarket, runner) { } public override void DoYourJob() { switch (tradingState) { case TradingState.WaitingForTrade: PlaceMyTestBets(); break; case TradingState.BackingInProgress: case TradingState.LayingInProgress: BackingOrLayingInProgress(); break; case TradingState.BackingBetDone: case TradingState.LayingBetDone: AllDoneStopTheBot(); break; default: break; } } private BetBase GetBetInstance(BetType betType, double odds, double stake) { return new BetBase(MarketId, runner.SelectionId, runner.AsianLineId, betType, odds, stake); } private void PlaceMyTestBets() { BetPrice layBetPrice = runner.BestPriceToLay; BetPrice backBetPrice = runner.BestPriceToBack; if (layBetPrice != null && backBetPrice != null) { List < BetBase > betsToPlace = new List < BetBase >(); betsToPlace.Add(GetBetInstance(BetType.Lay, layBetPrice.Price, Stake)); betsToPlace.Add( GetBetInstance(BetType.Lay, (double)OddsRange.GetOdds((decimal)backBetPrice.Price, -1), Stake)); betsToPlace.Add( GetBetInstance(BetType.Lay, (double)OddsRange.GetOdds((decimal)backBetPrice.Price, -2), Stake)); PlaceBets(BetType.Lay, betsToPlace); /* Other methods to place, update or cancell bets: void PlaceBet(BetType type, double odds, double stake) void PlaceBets(BetType type, IList bets) void UpdateBet(UpdateBet betToUpdate) void UpdateBets(List betsToUpdate) void CancelBetsAndPlaceBet(BetType type, double odds, double stake) void CancelAll() */ } } private void BackingOrLayingInProgress() { ShowMyBets(); AddMessage("BackingOrLayingInProgress"); if (myBetId == 0) { MyBets myBets = monitoredMarket.MyBets; List < Bet > unmatchedBets = myBets.GetBetsBySelectionAndStatus( runner.SelectionId, runner.AsianLineId, BetStatus.Unmatched); /* Or you can use one of the following functions to get your bets: List < Bet > GetBetsBySelectionAndStatus( int selectionId, int asianLineId, BetStatus betStatus) List < Bet > GetBetsBySelectionTypeAndStatus( int selectionId, int asianLineId, BetType betType, BetStatus betStatus) List < Bet > GetBetsBySelectionBetTypeStatusAndLastBetId( int selectionId, int asianLineId, BetType betType, BetStatus betStatus, long lastBetId) List < Bet > GetUnmatchedBetsBySelectionAndOdds( int selectionId, int asianLineId, double odds) List < Bet > GetBetsByBetMatchCriteria(Predicate betMatchCriteria) */ if (unmatchedBets != null) { // The bet Id of the first unmatched bet Bet myBet = unmatchedBets[0]; // Do not use the reference to the bet! myBetId = myBet.Id; ShowMyBet(myBet); } } } private void AllDoneStopTheBot() { ShowMyBets(); if (myBetId != 0) { Bet myBet = monitoredMarket.MyBets.GetBet(myBetId); if (myBet != null) { ShowMyBet(myBet); } else { AddMessage(string.Format("My bet (bet id: {0}) was cancelled.", myBetId)); } AddMessage("My bet status:"); } AddMessage("AllDoneStopTheBot"); Stop(); } private void ShowMyBet(Bet bet) { AddMessage( string.Format("Id: {0}, BetStatus: {1}, Type: {2}, BetPrice: {3}", bet.Id, bet.Status, bet.Type, bet.BetPrice)); } private void ShowMyBets() { MyBets myBets = monitoredMarket.MyBets; if (myBets.HaveBets) { foreach (Bet bet in myBets.Bets) { ShowMyBet(bet); } } else { AddMessage("No bets!"); } } }You can download the source code here, unzip the file and copy the betfair bot script to MyBots folder.
Sunday, April 18, 2010
thanks a lot! for your work.