private async Task ExecuteOrders(TraderOrderBase order1, TraderOrderBase order2) { var exchangeVolume = GetExchangeVolume(order1, order2); var price = GetOrderPrice(order1, order2); var instrument = GlobalSettings.FinanceInstruments[order1.Asset]; var marketOrderExchangeVolume = order1.BaseCurrency == instrument.Base ? exchangeVolume * price : exchangeVolume / price; var limitOrderExchangeVolume = order2.BaseCurrency == instrument.Base ? exchangeVolume * price : exchangeVolume / price; if (!await CheckIfEnoughBalance(order1, marketOrderExchangeVolume)) { order1.Status = OrderStatus.Canceled; order1.FailReason = OrderFailReason.NotEnoughFunds; await _ordersRepository.UpdateOrderAsync(order1); return; } if (!await CheckIfEnoughBalance(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(order1, marketOrderExchangeVolume); await ChangeBalance(order2, limitOrderExchangeVolume); }
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); }