public async Task CloseOrderAsync(string accountId, string orderId) { var account = await _accountInfoRepository.GetByIdAsync(accountId); var activeOrder = await _marketOrderRepository.GetAsync(accountId, orderId); var assetPair = await _dictionaryProxy.GetAssetPairByIdAsync(activeOrder.AssetPairId); var quote = await _assetPairQuoteRepository.GetByIdAsync(activeOrder.AssetPairId); var transactionHistory = new TransactionHistory { AccountId = activeOrder.ClientId, AssetPairId = activeOrder.AssetPairId, CompletedAt = DateTime.UtcNow, TransactionId = activeOrder.Id, Price = activeOrder.OrderAction == OrderAction.Buy ? quote.Ask : quote.Bid }; var profitLoss = await _orderCalculator.CalculateProfitLossAsync(activeOrder.Price, transactionHistory.Price, activeOrder.Volume, assetPair, account.BaseAssetId); transactionHistory.ProfitLoss = profitLoss; await _transactionHistoryRepository.AddAsync(transactionHistory); await _marketOrderRepository.DeleteAsync(accountId, orderId); account.Balance += profitLoss; await _accountInfoRepository.UpdateAsync(account); await _matchingEngineEventSubscriber.AccountUpdatedAsync(accountId); }