public async Task <string> CancelOrderAsync(Core.Model.User user, string symbol, string orderId, string newClientOrderId = null, long recWindow = 0, CancellationToken cancellationToken = default)
        {
            if (orderId == null)
            {
                throw new ArgumentNullException(nameof(orderId));
            }

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var binanceApi = new BinanceApi();
            var id         = Convert.ToInt64(orderId, CultureInfo.InvariantCulture);

            using var apiUser = new BinanceApiUser(user.ApiKey, user.ApiSecret);
            var result = await binanceApi.CancelOrderAsync(apiUser, symbol, id, newClientOrderId, recWindow, cancellationToken).ConfigureAwait(false);

            return(result);
        }
Beispiel #2
0
        public async Task CancelOrder(Prices prices, string coin = null)
        {
            var openOrders = await _binanceApi.GetOpenOrdersAsync();

            if (coin != null)
            {
                openOrders = openOrders.Where(x => x.Symbol.Replace("BTC", string.Empty) == coin.ToUpper()).ToList();
            }

            if (openOrders.Count > 0)
            {
                Console.WriteLine(openOrders
                                  .ToStringTable <BinanceOrder>(
                                      new[]
                {
                    "Order ID", "Market", "Bid/Ask", "Quantity", "Remaining", "Estimated Total",
                    "Estimated EUR Value",
                    "Estimated USD Value"
                },
                                      x => openOrders.IndexOf(x) + 1,
                                      x => x.Symbol, x => x.Price.ToString("0.00000000"), x => x.OriginalQuantity,
                                      x => x.OriginalQuantity - x.ExecutedQuantity,
                                      x => ((x.Price * x.OriginalQuantity)).ToString("0.00000000"),
                                      x => (((x.Price * x.OriginalQuantity)) * prices.Eur.Last).ToString("0.00 EUR"),
                                      x => (((x.Price * x.OriginalQuantity)) * prices.Usd.Last).ToString("0.00 USD")));

                Console.Write("\tWhich order should be cancelled (ID) (or enter -1 for all): ");
                int orderId = 0;

                if (int.TryParse(Console.ReadLine(), out orderId))
                {
                    if (orderId > 0 && orderId <= openOrders.Count)
                    {
                        ConsoleHelpers.WriteColored("\tWARNING: GOING TO CANCEL AN ORDER, DO YOU WANT TO CONTINUE? (YES/NO) ", ConsoleColor.Yellow);

                        if (Console.ReadLine()?.ToLower() == "yes")
                        {
                            await _binanceApi.CancelOrderAsync(openOrders[orderId - 1].Symbol, openOrders[orderId - 1].OrderId);

                            ConsoleHelpers.WriteColoredLine($"\tOrder {openOrders[orderId - 1].OrderId} cancelled.", ConsoleColor.Green);
                        }
                        else
                        {
                            ConsoleHelpers.WriteColoredLine("\tCancel cancelled (cancelception?).", ConsoleColor.Red);
                        }
                    }
                    else if (orderId == -1)
                    {
                        ConsoleHelpers.WriteColored("\tWARNING: GOING TO CANCEL ALL OF THE ABOVE ORDERS, DO YOU WANT TO CONTINUE? (YES/NO) ", ConsoleColor.Yellow);

                        if (Console.ReadLine()?.ToLower() == "yes")
                        {
                            foreach (var order in openOrders)
                            {
                                await _binanceApi.CancelOrderAsync(order.Symbol, order.OrderId);

                                ConsoleHelpers.WriteColoredLine($"\tOrder {order.OrderId} cancelled.", ConsoleColor.Green);
                            }
                        }
                        else
                        {
                            ConsoleHelpers.WriteColoredLine("\tCancel cancelled (cancelception?).", ConsoleColor.Red);
                        }
                    }
                    else
                    {
                        ConsoleHelpers.WriteColoredLine("\tInvalid order ID.", ConsoleColor.Red);
                    }
                }
                else
                {
                    ConsoleHelpers.WriteColoredLine("\tInvalid order ID.", ConsoleColor.Red);
                }
            }
            else
            {
                Console.WriteLine("\tNo open orders found...");
            }
        }