sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
5.89k stars 2.2k forks source link

futures_coin_cancel_orders() not working after June-16 update #1332

Open blksith0 opened 1 year ago

blksith0 commented 1 year ago

https://binance-docs.github.io/apidocs/futures/en/#change-log

Binance is more picky about how it wants its URL encoding now.

{"code":-1022,"msg":"Signature for this request is not valid."}

blksith0 commented 1 year ago

Here, so if you convert your orderId list to a string without spaces, you get something like this: [8476729388,8476729387,8476729398,8476729391,8476729386,8476729378,8476729392,8476729390,8476729381,8476729397] Then you can send that over. client.futures_coin_cancel_orders(symbol=symbol, orderIdList=batch)

Then edit client.py

    def futures_coin_cancel_orders(self, **params):
        """Cancel multiple futures orders

        https://binance-docs.github.io/apidocs/delivery/en/#cancel-multiple-orders-trade

        """

        params['orderIdList'] = urlencode({'orderIdList': params['orderIdList']}, doseq=True)[12:].replace('%27', '%22')

        return self._request_futures_coin_api(
            "delete", "batchOrders", True, data=params
        )
blazerr1313 commented 6 months ago

Below the code example that works without changing code in Client.py

  1. In my example orderidList is LIST with INT type orderids (i get them with another func) . Originally orderidList in my example formed in strukture where order ids is separated with comma and space after comma.

  2. The main thing that you need to do - proper orderidList encoding before you generate signature and forming request URL

  3. In this code example - urllib.parse.quote(str(order_ids_list).replace(' ', ''), safe="") - trims spaces after COMMA , and then encoding the orderidList for URL request.

  4. Enjoy


import urllib.parse

order_ids_list = [4941809706, 4941813337, 4941823531]  #Order IDS that i get with another func , orderids list  contain  INT type orderd IDS separated with comma and space after comma.  You need ENCODE this list before you generating signature and forming request URL 

def cancel_orders_by_idlist(coin, order_ids_list):

    encoded_order_id_list = urllib.parse.quote(str(order_ids_list).replace(' ', ''), safe="") # You need properly encode API request 
    try:
        # Проверяем, что длина списка ID ордеров не превышает 10
        if len(order_ids_list) > 10:
            print("Максимальная длина списка ID ордеров - 10")
            return

        result = client.futures_cancel_orders(symbol=coin, orderIdList=encoded_order_id_list)
        print(f"RESULT =  {result}\n\n")

    except Exception as e:
        print(f"Не удалось отменить ордеры. Ошибка: {e}")