python-binance icon indicating copy to clipboard operation
python-binance copied to clipboard

Cancel all orders

Open TroyHGP opened this issue 4 years ago • 23 comments

Just wondering if there is a plan to implement this feature from the binance-official-api: Cancel All Open Orders on a Symbol (TRADE) - Cancels all active orders on a symbol. This includes OCO orders.

This would be a really useful feature and save working with Order ID's. Thanks for your hard work.

TroyHGP avatar Jun 08 '20 12:06 TroyHGP

I want to clarify - I know we can use OCO to cancel our trades but the amount of trades possible appears to be heavily limited when placing OCO orders. I can only place 6 at a time which really isn't enough for me. If we could cancel all orders on a pair it would allow us to control our stops manually and keep up a high volume of orders.

TroyHGP avatar Jun 15 '20 03:06 TroyHGP

The API call for this on Binance is documented here: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#cancel-all-open-orders-on-a-symbol-trade

in client.py:

def cancel_orders(self, **params):
        return self._delete('openOrders', True, data=params)

Then client.cancel_orders(symbol='BNBBTC')

ghost avatar Jun 15 '20 21:06 ghost

I noticed that this function is not available anymore, or am I wrong?

When I use: client.cancel_orders(symbol='BTCUSDT')

I get: Unresolved attribute reference 'cancel_orders' for class 'Client'

BottoBoy avatar Dec 19 '20 17:12 BottoBoy

I noticed that this function is not available anymore, or am I wrong?

When I use: client.cancel_orders(symbol='BTCUSDT')

I get: Unresolved attribute reference 'cancel_orders' for class 'Client'

As @iterums mentioned, 'cancel_orders' new function should be in 'client' and you re-open your project and re-try. It works for me.

Ben21k avatar Dec 20 '20 08:12 Ben21k

PLEASE HELP NOT WORC close my all orders ((

from binance.client import Client from binance.websockets import BinanceSocketManager import time import math api_key = 'IIIIIIIIIIIIIIIIIIII' api_secret = 'IIIIIIIIIIIIIIIIIIII' client = Client(api_key, api_secret) ASSET = 'ZECUSDT' CURRENCY = 'ZEC' CRYPTOCURRENCY = 'USDT' def balance(symbol): balance = client.get_asset_balance(asset=symbol) balance = {'free': balance['free'], 'locked':balance['locked']} return balance def history(symbol): history = client.get_my_trades(symbol=symbol) return history def price(symbol): price = client.get_avg_price(symbol=symbol)['price'] return float(price) def order_market_buy(quantity): order = client.order_market_buy(symbol=ASSET, quantity=quantity) def order_market_sell(quantity): order = client.order_market_sell(symbol=ASSET, quantity=quantity) #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! NOT WORC! def cancel_orders(self, **params): return self._delete('openOrders', True, data=params) ################# PRICE = price(ASSET) def main(FIRST_PRICE): print('СЕЙЧАС ЦЕНА',int(PRICE)) nteu = PRICE numbermu = 12 rezultae = nteu - numbermu okruglenie = round(rezultae, 2) print('ЗА СКОЛЬКО КУПИТЬ!', + okruglenie ) nteuz = PRICE numbermuz = 14 rezultaez = nteuz + numbermuz okrugleniez = round(rezultaez, 2) print('ЗА СКОЛЬКО ПРОДАТЬ!', + okrugleniez ) rtytrr = 170 print(okrugleniez) ######################################

#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! NOT WORC! client.cancel_orders(symbol='ZECUSDT')

###################################### START_PRICE = price(ASSET) main(START_PRICE)

G34567 avatar Feb 26 '21 09:02 G34567

[PLEASE HELP NOT WORK!] def cancel_orders(self, **params): return self._delete('openOrders', True, data=params)

You must add that code to the Client class in the client.py file itself, which is located wherever your python-binance is installed.

(In VSCode with the right setup, you can right-click 'Client' on the line 'from binance.client import Client', select "Go to Definition", and that opens client.py.)

jp-diegidio avatar Feb 26 '21 09:02 jp-diegidio

[PLEASE HELP NOT WORK!] def cancel_orders(self, **params): return self._delete('openOrders', True, data=params)

You must add that code to the Client class in the client.py file itself, which is located wherever your python-binance is installed.

(In VSCode with the right setup, you can right-click 'Client' on the line 'from binance.client import Client', select "Go to Definition", and that opens client.py.)

how to cancel all sell or buy individually? it's possible?

G34567 avatar Feb 27 '21 15:02 G34567

It's not possible?

G34567 avatar Feb 28 '21 12:02 G34567

@G34567 > how to cancel all sell or buy individually? it's possible?

Your questions are inappropriate here: python-binance is only a Python wrapper around the Binance API, so in this repository you should only post about issues with the wrapper itself. As for the Binance API and what that can or cannot do, you should refer to the official Binance documentation: https://binance-docs.github.io/apidocs/

Hope that helps.

jp-diegidio avatar Feb 28 '21 14:02 jp-diegidio

[PLEASE HELP NOT WORK!] def cancel_orders(self, **params): return self._delete('openOrders', True, data=params)

You must add that code to the Client class in the client.py file itself, which is located wherever your python-binance is installed.

(In VSCode with the right setup, you can right-click 'Client' on the line 'from binance.client import Client', select "Go to Definition", and that opens client.py.)

Guys, listen to this. It works! shiftclick "Client" from import Client. Then input the code with the other defs and then save!

ItsMinjae avatar Apr 08 '21 04:04 ItsMinjae

The API call for this on Binance is documented here: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#cancel-all-open-orders-on-a-symbol-trade

in client.py:

def cancel_orders(self, **params):
        return self._delete('openOrders', True, data=params)

Then client.cancel_orders(symbol='BNBBTC')

I'm getting "TypeError: cancel_orders() got an unexpected keyword argument 'symbol'"

Fairly new to python, but I've got a little experience programming as a student. Wondering if the key is something to do with the double asterisk params and its relationship to the data=params passed in to the self._delete function

pufferwockey avatar Apr 23 '21 05:04 pufferwockey

The API call for this on Binance is documented here: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#cancel-all-open-orders-on-a-symbol-trade in client.py:

def cancel_orders(self, **params):
        return self._delete('openOrders', True, data=params)

Then client.cancel_orders(symbol='BNBBTC')

I'm getting "TypeError: cancel_orders() got an unexpected keyword argument 'symbol'"

Fairly new to python, but I've got a little experience programming as a student. Wondering if the key is something to do with the double asterisk params and its relationship to the data=params passed in to the self._delete function

hi, can you post your code here so we can figure out?

fengster123 avatar May 12 '21 06:05 fengster123

@pufferwockey you have to use the following syntax: cancel_orders(symbol='ETHUSDT')

ahmedyounis avatar May 19 '21 21:05 ahmedyounis

When I try client.cancel_orders(symbol="BTCUSDT"), I get the following error:

APIError(code=-2011): Unknown order sent.

jhogg11 avatar Jul 10 '21 04:07 jhogg11

@jhogg11 have you found a solution yet? I'm encountering the same problem.

mareck8 avatar Aug 09 '21 17:08 mareck8

All,

Looks like method cancel_orders is not implemented yet by the current version of library. The direct call worked for me

binance_client._delete('openOrders', True, data={'symbol': 'DOGEBTC'})

dmitry-ee avatar Aug 11 '21 12:08 dmitry-ee

I'm trying what dmitry-ee says ... I have another q: is it possible tu modify a tp? or to cancel only the last tp? how?

jsgaston avatar Jan 05 '22 15:01 jsgaston

you can extend the Client as workaround:

class ExtendedClient(Client):
    def cancel_open_orders(self, **params):
        return self._delete('openOrders', True, data=params)

and use

client = ExtendedClient(APIKEY, APISECRET)
client.cancel_open_orders('BTCUSDT')

sahalsaad avatar Jan 25 '22 19:01 sahalsaad

All,

Looks like method cancel_orders is not implemented yet by the current version of library. The direct call worked for me

binance_client._delete('openOrders', True, data={'symbol': 'DOGEBTC'})

I'm getting this error when calling this method APIError(code=-2011): Unknown order sent.

Karlheinzniebuhr avatar Jul 17 '22 20:07 Karlheinzniebuhr

def clearOrders(self):
    order = self.client.futures_cancel_all_open_orders(
        symbol="BNBUSDT")

martines328 avatar Aug 31 '22 09:08 martines328

binance_client._delete('openOrders', True, data={'symbol': 'DOGEBTC'})

All,

Looks like method cancel_orders is not implemented yet by the current version of library. The direct call worked for me

binance_client._delete('openOrders', True, data={'symbol': 'DOGEBTC'})

This worked for me too, Thank you so much dmitry-ee!

ramananr77 avatar Nov 08 '22 08:11 ramananr77

This solution:

binance_client._delete('openOrders', True, data={'symbol': 'DOGEBTC'})

works well as long as there is at least one open order to cancel. However, if you try this when there are no open orders, then you will still get an exception with the message:

Unknown order sent.

which some others have mentioned above. One possible workaround (not necessarily a good one) is to edit the client.py file directly. At the moment, the relevant exception is raised at line 367. You can change it to something like this:

            if response.status_code == 400:
                print(f"WARNING: code 400 (probably Unknown order sent.). I'm just going to ignore it")
                return response.text
            else:
                raise BinanceAPIException(response, response.status_code, response.text)

This isn't a great solution, but it worked for me.

colintbowers avatar May 07 '23 12:05 colintbowers

I suggest this code:

orders = client.get_open_orders(symbol='BTCUSDT')
df_orders = pd.DataFrame(orders)
open_orders_count = len(df_orders.index)
for i in range(open_orders_count):
    client.cancel_order(symbol='BTCUSDT, orderId=orders[i].get('orderId'))

It counts open orders with symbol BTCUSDT in data frame (number of rows). For me works fine

burza625 avatar Jul 25 '23 10:07 burza625