'How to cancel futures binance order?

I'm working with python-binance. In my code, I place a futures market order, using this code:

order = self.client.futures_create_order(
                symbol=coin_pare,
                type='MARKET',
                side=route,
                quantity=value * self.main_leverage,

            )

Then, when I want to close this order, I decide to use cancel_order in this library, using this code:

self.client.cancel_order(symbol=pare, orderId=order_id, origClientOrderId=client_order_id)

And I get an error: APIError(code=-2011): Unknown order sent. Is there another way how to cancel certain order?



Solution 1:[1]

timestamp is mandatory in parameters for cancel orders mentioned in https://binance-docs.github.io/apidocs/futures/en/#query-order-user_data you should edit like this:

(self.client.cancel_order(symbol=pare, orderId=order_id, origClientOrderId=client_order_id, timestamp=true)

Solution 2:[2]

You just need to remove spaces and encode the string. Do this:

from urllib.parse import quote
from json import dumps
    
    order_id_str = dumps(order_id).replace(" ", "")
    order_id_str = quote(order_id_str)
    self.client.cancel_order(symbol=pare, orderIdList=order_id_str)

Solution 3:[3]

I think it's better this way.

def days_b():
    keyboard = [
    [
        InlineKeyboardButton("Mon", callback_data='0'),
        InlineKeyboardButton("Tue", callback_data='1'),
        InlineKeyboardButton("Wen", callback_data='2'),
        InlineKeyboardButton("Thu", callback_data='3'),
        InlineKeyboardButton("Fri", callback_data='4'),
          ],
    ]    
    reply_markup = InlineKeyboardMarkup(keyboard)
    return reply_markup
      
def week_command(update: Update, context: CallbackContext) -> None:
     update.message.reply_text(daysList[0], reply_markup=days_b())

def button(update: Update, context: CallbackContext) -> None:
     query = update.callback_query
     query.edit_message_text(text=daysList[query.data], reply_markup=days_b())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeremy Caney
Solution 2 FreezeTime
Solution 3 Arthur Hakobyan