public int CalcWorstSpread(string asset) { var buyOrders = LimitOrdersBuy.Where(itm => itm.Order.Asset == asset).ToArray(); var sellOrders = LimitOrdersSell.Where(itm => itm.Order.Asset == asset).ToArray(); if (buyOrders.Length == 0 || sellOrders.Length == 0) { return(0); } return((int)(buyOrders.Max(itm => itm.Order.Price) - sellOrders.Min(itm => itm.Order.Price)) * _assetPairsDictionary.Get(asset).Multiplier()); }
private async Task ExecuteOrders(TraderOrderBase order1, TraderOrderBase order2) { var exchangeVolume = GetExchangeVolume(order1, order2); var price = GetOrderPrice(order1, order2); var assetPair = _assetPairsCachedReader.Get(order1.Asset); var marketOrderExchangeVolume = order1.BaseCurrency == assetPair.BaseAssetId ? exchangeVolume * price : exchangeVolume / price; var limitOrderExchangeVolume = order2.BaseCurrency == assetPair.BaseAssetId ? exchangeVolume * price : exchangeVolume / price; if (!await CheckIfEnoughBalance(assetPair, order1, marketOrderExchangeVolume)) { order1.Status = OrderStatus.Canceled; order1.FailReason = OrderFailReason.NotEnoughFunds; await _ordersRepository.UpdateOrderAsync(order1); return; } if (!await CheckIfEnoughBalance(assetPair, order2, limitOrderExchangeVolume)) { order2.Status = OrderStatus.Canceled; order2.FailReason = OrderFailReason.NotEnoughFunds; await _ordersRepository.UpdateOrderAsync(order2); return; } order1.Exchanged += exchangeVolume; order2.Exchanged += exchangeVolume; order1.AddExchangingOrder(order2.Id); order2.AddExchangingOrder(order1.Id); if (order1.IsOrderFullfiled()) { order1.Status = OrderStatus.Done; } await _ordersRepository.UpdateOrderAsync(order1); if (order2.IsOrderFullfiled()) { order2.Status = OrderStatus.Done; } await _ordersRepository.UpdateOrderAsync(order2); await ChangeBalance(assetPair, order1, marketOrderExchangeVolume); await ChangeBalance(assetPair, order2, limitOrderExchangeVolume); await NotifyOrderBookChanged(assetPair.Id); }