public async Task <OpenOrderResult> PlaceOpeningOrder(TradeDetail trade, IEnumerable <TradeDetail> relatedTrades) { var market = trade.Match.BetfairData.Markets.Single(x => x.MarketName == trade.MarketName); var runnerId = market.Runners.Single(x => x.Name == trade.RunnerName).Id; var latestRunnerBook = await _runnerService.GetRunnerDetails(market.MarketId, runnerId); var orderTick = _orderPriceFinder.GetPrice(trade.Side, latestRunnerBook.ExchangePrices); var openingOrderWrapper = new OrderWrapper(market.MarketId, runnerId, trade.Side, orderTick, PersistenceType.LAPSE); var policy = Policy.Handle <MarketSuspendedException>().Or <OrderActionErrorException>().Or <OrderNotPlaceableException>().WaitAndRetry(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt * 4))); var betReport = await policy.Execute(() => _orderPlacer.PlaceOrder(openingOrderWrapper)); var betId = betReport.InstructionReports.Single().BetId; openingOrderWrapper.AddBetId(betId); _sleepService.Sleep(5000); var matchReport = await _runnerService.GetRunnerDetails(market.MarketId, runnerId); var orderReport = matchReport.Orders.Where(x => x.BetId == betId).Sum(x => x.SizeRemaining); if (orderReport != 0) { var cancelReport = await _orderPlacer.CancelOrder(betId, market.MarketId); if (cancelReport.Status == ExecutionReportStatus.SUCCESS) { throw new OrderCancelledException(betId); } } return(new OpenOrderResult(trade, openingOrderWrapper)); }
public void Initialize() { mockSleepService = new Mock <ISleepService>(); mockSleepService.Setup(x => x.Sleep(It.IsAny <int>())); mockRunnerService = new Mock <IRunnerService>(); mockRunnerService.Setup(x => x.GetRunnerDetails(It.IsAny <string>(), It.IsAny <long>())).Returns(Task.FromResult(new TradePlacement.Models.Api.Runner() { ExchangePrices = new ExchangePrices() { AvailableToBack = new List <PriceSize>() { new PriceSize() { Price = 5, Size = 10 } }, AvailableToLay = new List <PriceSize>() { new PriceSize() { Price = 8, Size = 15 } } }, Orders = new List <Order>() { new Order() { BetId = "ABC", }, new Order() { BetId = "XYZ", SizeRemaining = 0 } } })); mockOrderPlacer = new Mock <IOrderPlacer>(); mockOrderPlacer.Setup(x => x.PlaceOrder(It.IsAny <OrderWrapper>())).Returns(Task.FromResult(new PlaceExecutionReport() { InstructionReports = new List <PlaceInstructionReport>() { new PlaceInstructionReport() { BetId = "XYZ" } } })); mockClosingStakeCalculator = new Mock <IClosingStakeCalculator>(); mockClosingStakeCalculator.Setup(x => x.GetFullHedgeStake(It.IsAny <List <Order> >(), It.IsAny <double>())).Returns(new KeyValuePair <Side, double>(Side.BACK, 200)); tradeDetail = new TradeDetail() { Tracking = 100 }; orderWrapper = new OrderWrapper("A", 1, Side.BACK, new OrderTick(2, 3), PersistenceType.LAPSE); orderWrapper.AddBetId("ABC"); }