public void UpdateBuyOrders(Ticker ticker) { List <CryptoOrder> removedOrders = new List <CryptoOrder>(); foreach (var buyOrder in _pendingBuyOrders) { if (ticker.Ask <= buyOrder.PricePerUnit) { //Console.WriteLine($"Closed buy order {buyOrder.Uuid}"); buyOrder.Closed = ticker.Timestamp; buyOrder.IsClosed = true; buyOrder.IsClosed = true; OrderUpdated.OnNext(buyOrder); removedOrders.Add(buyOrder); } if (buyOrder.Canceled) { removedOrders.Add(buyOrder); } } foreach (var removedOrder in removedOrders) { _pendingBuyOrders.Remove(removedOrder); } }
public async Task <Unit> UpdateOrder(CryptoOrder cryptoOrder) { try { Log($"Closed order {cryptoOrder.Uuid} as {cryptoOrder.OrderType} at {cryptoOrder.Limit}"); switch (cryptoOrder.OrderType) { case CryptoOrderType.LimitSell: TraderState.Budget.Available += cryptoOrder.Price; var tradeForSellOrder = TraderState.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); TraderState.Budget.Profit += tradeProfit; TraderState.Budget.Earned += tradeForSellOrder.SellOrder.Price - tradeForSellOrder.BuyOrder.Price; Log($"{cryptoOrder.Uuid}: SELL - {tradeProfit}"); tradeForSellOrder.Profit = tradeProfit; tradeForSellOrder.Status = TradeStatus.Completed; tradeForSellOrder.SellOrder = cryptoOrder; } break; case CryptoOrderType.LimitBuy: var tradeForBuyOrder = TraderState.Trades.FirstOrDefault(t => t.BuyOrder.Uuid == cryptoOrder.Uuid); if (tradeForBuyOrder != null) { if (cryptoOrder.Canceled) { TraderState.Trades.Remove(tradeForBuyOrder); return(await Task.FromResult(Unit.Default)); } tradeForBuyOrder.Status = TradeStatus.Bought; tradeForBuyOrder.BuyOrder = cryptoOrder; } break; } } finally { OrderUpdated.OnNext(cryptoOrder); } return(await Task.FromResult(Unit.Default)); }
public void UpdateSellOrders(Ticker ticker) { List <CryptoOrder> removedOrders = new List <CryptoOrder>(); foreach (var sellOrder in _pendingSellOrders.Where(s => s.IsClosed == false)) { if (ticker.Bid >= sellOrder.PricePerUnit || sellOrder.OrderType == CryptoOrderType.ImmediateSell) { sellOrder.IsClosed = true; sellOrder.IsClosed = true; sellOrder.Closed = ticker.Timestamp; sellOrder.OrderType = CryptoOrderType.LimitSell; removedOrders.Add(sellOrder); OrderUpdated.OnNext(sellOrder); } } foreach (var removedOrder in removedOrders) { _pendingSellOrders.Remove(removedOrder); } }
public async Task <Unit> UpdateOrder(CryptoOrder cryptoOrder) { try { if (cryptoOrder.IsClosed == false) { return(await Task.FromResult(Unit.Default)); } switch (cryptoOrder.OrderType) { case CryptoOrderType.LimitSell: TraderState.Budget.Available += cryptoOrder.Price; var tradeForSellOrder = TraderState.Trades.FirstOrDefault(t => t.SellOrder.Uuid == cryptoOrder.Uuid); if (tradeForSellOrder != null) { if (cryptoOrder.Canceled) { tradeForSellOrder.Status = TradeStatus.Bought; tradeForSellOrder.SellOrder.IsClosed = true; return(await Task.FromResult(Unit.Default)); } tradeForSellOrder.SellOrder = cryptoOrder; var tradeProfit = tradeForSellOrder.BuyOrder.Price.GetReadablePercentageChange(tradeForSellOrder.SellOrder.Price); Console.WriteLine($"Sold {tradeForSellOrder.BuyOrder.Uuid} with profit {tradeProfit} \t\t" + $" {tradeForSellOrder.BuyReason}: {tradeForSellOrder.BuyOrder.PricePerUnit} " + $"\t\t {tradeForSellOrder.SellReason}: {tradeForSellOrder.SellOrder.PricePerUnit}"); TraderState.Budget.Profit += tradeProfit; TraderState.Budget.Earned += tradeForSellOrder.SellOrder.Price - tradeForSellOrder.BuyOrder.Price; tradeForSellOrder.Profit = tradeProfit; tradeForSellOrder.Status = TradeStatus.Completed; var tradeWithTriggeredBuy = TraderState.Trades.FirstOrDefault(t => t.TriggeredBuy && t.Status != TradeStatus.Completed); if (tradeWithTriggeredBuy != null && TraderState.Trades.Count(t => t.TriggeredBuy && t.Status != TradeStatus.Completed) == 0) { //Console.WriteLine($"Set triggered buy to false for {tradeWithTriggeredBuy.BuyOrder.Uuid}"); tradeWithTriggeredBuy.TriggeredBuy = false; } } break; case CryptoOrderType.LimitBuy: var tradeForBuyOrder = TraderState.Trades.FirstOrDefault(t => t.BuyOrder.Uuid == cryptoOrder.Uuid); if (tradeForBuyOrder != null) { if (cryptoOrder.Canceled) { TraderState.Trades.Remove(tradeForBuyOrder); return(await Task.FromResult(Unit.Default)); } tradeForBuyOrder.Status = TradeStatus.Bought; tradeForBuyOrder.BuyOrder = cryptoOrder; } break; } } finally { OrderUpdated.OnNext(cryptoOrder); } return(await Task.FromResult(Unit.Default)); }
protected void OnOrderUpdate(BittrexStreamOrderData bittrexOrder) { OrderUpdated.OnNext(bittrexOrder.ToCryptoOrder()); }