public static void CancelAndSell(this Deal d) { void SelloffLeftovers() { // Selloff leftovers var trade = TradeInterface.PlaceImmediateOrder(d.Symbol, d.Leftovers, Binance.Net.Objects.OrderSide.Sell); d.Leftovers -= TradeInterface.GetActualOrders(d.Symbol, trade.ClientOrderId).Sum(x => x.Quantity); d.OtherSellOrder = new ClientServerOrder() { ClientOrderId = trade.ClientOrderId, FilledOrders = trade.FilledOrders }; } // Cancel open orders switch (d.CurrentState) { case Deal.State.WaitForBuy: // Cancel Buy order TradeInterface.Client.CancelOrder(d.Symbol, origClientOrderId: d.BuyOrder.ClientOrderId).GetOrThrow(); break; case Deal.State.WaitForGoal1: TradeInterface.Client.CancelOrder(d.Symbol, origClientOrderId: d.Goal1SellOrder.ClientOrderId).GetOrThrow(); TradeInterface.Client.CancelOrder(d.Symbol, origClientOrderId: d.Goal2SellOrder.ClientOrderId).GetOrThrow(); SelloffLeftovers(); break; case Deal.State.WaitForGoal2: TradeInterface.Client.CancelOrder(d.Symbol, origClientOrderId: d.Goal2SellOrder.ClientOrderId).GetOrThrow(); SelloffLeftovers(); break; case Deal.State.Done: throw new Exception("You cannot cancel a done deal"); } d.CurrentState = Deal.State.Done; d.CurrentResult = Deal.Result.Cancelled; }
private static void WaitForBuy(Deal deal) { var buyOrder = TradeInterface.GetOrderById(deal.Symbol, deal.BuyOrder.ClientOrderId); if (OrderWorked(buyOrder)) { // Download actual order var actualOrders = TradeInterface.GetActualTrades(buyOrder.Symbol, buyOrder.OrderId); deal.BuyOrder.FilledOrders = actualOrders.Select(x => x.OrderId).ToList(); decimal quantity = (actualOrders.Sum(x => x.Quantity) * FEEMODIFIER).Normalize(); // Set leftovers deal.Leftovers = quantity; // Calculate Sell points decimal price = actualOrders.EffectivePrice(); // We put 50 % sell on 1 % profit var order1 = TradeInterface.PlaceTakeProfitOrder(deal.Symbol, quantity: quantity * 0.5m, limit: price * (1 + deal.Sell1Perc), orderSide: OrderSide.Sell); // And we put another 50 % sell on 2 % profit var order2 = TradeInterface.PlaceTakeProfitOrder(deal.Symbol, quantity: quantity * 0.5m, limit: price * (1 + deal.Sell2Perc), orderSide: OrderSide.Sell); deal.Goal1SellOrder = new ClientServerOrder() { ClientOrderId = order1.ClientOrderId }; deal.Goal2SellOrder = new ClientServerOrder() { ClientOrderId = order2.ClientOrderId }; // Set next state deal.CurrentState = Deal.State.WaitForGoal1; } else if (OrderCancelled(buyOrder)) { StateTransistion_Cancelled(deal); } }
private static void WaitForGoal2(Deal deal) { var sellOrder = TradeInterface.GetOrderById(deal.Symbol, deal.Goal2SellOrder.ClientOrderId); if (OrderWorked(sellOrder)) { // Download actual order var actualOrders = TradeInterface.GetActualTrades(sellOrder.Symbol, sellOrder.OrderId); deal.Goal2SellOrder.FilledOrders = actualOrders.Select(x => x.OrderId).ToList(); deal.Leftovers -= actualOrders.Select(x => x.Quantity).Sum(); // Set next state deal.CurrentState = Deal.State.Done; deal.CurrentResult = Deal.Result.GoalsArchived; } else if (OrderCancelled(sellOrder)) { StateTransistion_Cancelled(deal); } }