public async Task <(IOrder order, bool amountRemainingLow, bool OrderError)> TryLiquidateOperation(Operation op, string reason) { (IOrder order, bool amountRemainingLow, bool OrderError)result = default; await Executor.CancelAllOrders(op); if (op.AmountRemaining > 0) { Logger.Info($"Liquidating operation {op.ToString("c")} because: " + reason); var symData = this.GetSymbolData(op.Symbol); var adj = symData.Feed.GetOrderAmountAndPriceRoundedDown(op.AmountRemaining, (decimal)symData.Feed.Bid); adj = ClampOrderAmount(symData, op.ExitTradeDirection, adj); if (adj.amount > 0) { //immediatly liquidate everything with a market order Logger.Info($"Try market exit amount:{adj.amount} - price: {adj.price}."); var orderInfo = new OrderInfo() { Symbol = op.Symbol.Key, Type = OrderType.Market, Effect = this.DoMarginTrading ? MarginOrderEffect.ClosePosition : MarginOrderEffect.None, Amount = adj.amount, ClientOrderId = op.GetNewOrderId(), Direction = op.ExitTradeDirection }; var request = await Market.PostNewOrder(orderInfo); if (request.IsSuccessful) { result.order = request.Result; } else { result.OrderError = true; Logger.Error($"Error liquidating operation {op} - symbol {op.Symbol.Key} - amount:{adj.amount} - price: {adj.price}"); } } else { result.amountRemainingLow = true; Logger.Info($"Unable to liquidate operation {op} because amount remaining is too low"); } } return(result); }