private IEnumerable <Order> GetPendingOrdersToBeExecuted(InstrumentBidAskPair quote) { var pendingOrders = _ordersCache.Active.GetOrdersByInstrument(quote.Instrument); foreach (var order in pendingOrders) { var price = quote.GetPriceForOrderDirection(order.Direction); if (order.IsSuitablePriceForPendingOrder(price) && _validateOrderService.CheckIfPendingOrderExecutionPossible(order.AssetPairId, order.OrderType, ShouldOpenNewPosition(order))) { if (quote.GetVolumeForOrderDirection(order.Direction) >= Math.Abs(order.Volume)) { _ordersCache.Active.Remove(order); yield return(order); } else //let's validate one more time, considering orderbook depth { var me = _meRouter.GetMatchingEngineForExecution(order); var executionPriceInfo = me.GetBestPriceForOpen(order.AssetPairId, order.Volume); if (executionPriceInfo.price.HasValue && order.IsSuitablePriceForPendingOrder(executionPriceInfo.price.Value)) { _ordersCache.Active.Remove(order); yield return(order); } } } } }
private List <MarginTradingAccount> UpdateClosePriceAndDetectStopout(InstrumentBidAskPair quote) { var positionsByAccounts = _ordersCache.Positions.GetPositionsByInstrument(quote.Instrument) .GroupBy(x => x.AccountId).ToDictionary(x => x.Key, x => x.ToArray()); var accountsWithStopout = new List <MarginTradingAccount>(); foreach (var accountPositions in positionsByAccounts) { var account = _accountsCacheService.Get(accountPositions.Key); var oldAccountLevel = account.GetAccountLevel(); foreach (var position in accountPositions.Value) { var closeOrderDirection = position.Volume.GetClosePositionOrderDirection(); var closePrice = quote.GetPriceForOrderDirection(closeOrderDirection); if (quote.GetVolumeForOrderDirection(closeOrderDirection) < Math.Abs(position.Volume)) { var defaultMatchingEngine = _meRouter.GetMatchingEngineForClose(position.OpenMatchingEngineId); var orderbookPrice = defaultMatchingEngine.GetPriceForClose(position.AssetPairId, position.Volume, position.ExternalProviderId); if (orderbookPrice.HasValue) { closePrice = orderbookPrice.Value; } } if (closePrice != 0) { position.UpdateClosePriceWithoutAccountUpdate(closePrice); UpdateTrailingStops(position); } } account.CacheNeedsToBeUpdated(); var newAccountLevel = account.GetAccountLevel(); if (newAccountLevel == AccountLevel.StopOut) { accountsWithStopout.Add(account); } if (oldAccountLevel != newAccountLevel) { _marginCallEventChannel.SendEvent(this, new MarginCallEventArgs(account, newAccountLevel)); } } return(accountsWithStopout); }