public async Task <Unit> UpdateOrder(CryptoOrder cryptoOrder) { try { Console.WriteLine($"Closed order {cryptoOrder.Uuid} as {cryptoOrder.OrderType} at {cryptoOrder.Limit}"); switch (cryptoOrder.OrderType) { case CryptoOrderType.LimitSell: Budget.Available += cryptoOrder.Price; var tradeForSellOrder = Trades.FirstOrDefault(t => t.SellOrder.Uuid == cryptoOrder.Uuid); if (tradeForSellOrder != null) { if (cryptoOrder.Canceled) { tradeForSellOrder.Status = TradeStatus.Bought; tradeForSellOrder.SellOrder.IsOpened = false; return(await Task.FromResult(Unit.Default)); } var tradeProfit = tradeForSellOrder.BuyOrder.Price.GetReadablePercentageChange(tradeForSellOrder.SellOrder.Price); Budget.Profit += tradeProfit; Budget.Earned += tradeForSellOrder.SellOrder.Price - tradeForSellOrder.BuyOrder.Price; Console.WriteLine($"{cryptoOrder.Uuid}: SELL - {tradeProfit}"); await _pushManager.TriggerPush(PushMessage.FromMessage($"Sold {Market} for profit {tradeProfit}%")); tradeForSellOrder.Profit = tradeProfit; tradeForSellOrder.Status = TradeStatus.Completed; tradeForSellOrder.SellOrder = cryptoOrder; } break; case CryptoOrderType.LimitBuy: var tradeForBuyOrder = Trades.FirstOrDefault(t => t.BuyOrder.Uuid == cryptoOrder.Uuid); if (tradeForBuyOrder != null) { await _pushManager.TriggerPush(PushMessage.FromMessage($"Bought {Market} at {cryptoOrder.Limit} BTC")); if (cryptoOrder.Canceled) { tradeForBuyOrder.Status = TradeStatus.Empty; tradeForBuyOrder.BuyOrder = new CryptoOrder(); return(await Task.FromResult(Unit.Default)); } tradeForBuyOrder.Status = TradeStatus.Bought; tradeForBuyOrder.BuyOrder = cryptoOrder; } break; } } finally { if (!IsInTestMode) { var traderData = await _traderGrain.GetTraderData(); traderData.CurrentTicker = Ticker; await _hubNotifier.UpdateTrader(traderData); } } return(await Task.FromResult(Unit.Default)); }
public async Task <Unit> UpdateOrder(CryptoOrder cryptoOrder) { if (!IsInTestMode) { var traderData = await _traderGrain.GetTraderData(); traderData.CurrentTicker = Ticker; await _hubNotifier.UpdateTrader(traderData); } return(await Task.FromResult(Unit.Default)); }