Anyone have idea on how we can create Market close position order on Futures market? I tried this one as a close order for a Short trade by pass in all required params and one closePosition param then called this function
def placeBuyOrder():
orderBuy = Binance.client.futures_create_order(
symbol= self.symbol,
side= 'BUY',
type= 'MARKET',
closePosition= 'true'
)
but in return are these exceptions
File "C:\Users\chaum\AppData\Local\Programs\Python\Python38\lib\site-packages\binance\client.py", line 3584, in futures_create_order
return self._request_futures_api('post', 'order', True, data=params)
File "C:\Users\chaum\AppData\Local\Programs\Python\Python38\lib\site-packages\binance\client.py", line 222, in _request_futures_api
return self._request(method, uri, signed, True, **kwargs)
File "C:\Users\chaum\AppData\Local\Programs\Python\Python38\lib\site-packages\binance\client.py", line 197, in _request
return self._handle_response()
File "C:\Users\chaum\AppData\Local\Programs\Python\Python38\lib\site-packages\binance\client.py", line 230, in _handle_response
raise BinanceAPIException(self.response)
File "C:\Users\chaum\AppData\Local\Programs\Python\Python38\lib\site-packages\binance\exceptions.py", line 13, in __init__
self.code = json_res['code']
KeyError: 'code'
Is it means my function have problems or this is wrapper side problem?
Hi @chaudoe
If for example you have a short order worth 1 BTC. To close this you must place a long order of 1 BTC.
The issue in your code is that you have specified closePosition parameter on a MARKET order, closePosition is only used with STOP_MARKET or TAKE_PROFIT_MARKET. Also, since you are using a MARKET order you must also specify the quantity.
To close a short order worth 1 BTC you would need the following code:
Binance.futures_create_order(symbol=self.symbol, side='BUY', type='MARKET', quantity=1, reduceOnly='true')
I added reduceOnly parameter to prevent a BUY order being placed if there is no short position already in Binance.
If you do not know the exact quantity of the original position, you can also close the order by using an inflated value for quantity, for BTCUSDT you could use a value of 100 to close any shorts with a value below that.
Alternatively you may also try setting a STOP_MARKET order which would close the position once a particular price has been crossed. This does not require a quantity to be specified if you use the 'closePosition' parameter.
For example
Binance.futures_create_order(symbol=self.symbol, side=BUY, type='STOP_MARKET', stopPrice=stop_price, closePosition='true')
That last example is untested, but I believe it should work
Thank you sloth456, it works, I dont think it could be this simple. I think I tried that before but my quantity was converted to float so it might be rounded up somewhere so it didn't worked
Hello,
That works fine for me for Market orders. But I'm having issues to create stop_loss and take-profit LIMIT orders.
I can create LIMIT orders without any issue as follows:
client.futures_create_order(symbol=symbol, side='BUY', type='LIMIT', timeInForce='GTC', quantity=quantity, price=price)
But I don't know how to create take_profit and stop_loss LIMIT orders. I tried different things, but I always get errors. Could you please help me on that?
Thanks.
Ah, it's now solved. I was missing parematers.
Thanks,
I can create LIMIT orders without any issue as follows:
client.futures_create_order(symbol=symbol, side='BUY', type='LIMIT', timeInForce='GTC', quantity=quantity, price=price)
I think you should use 'STOP_LOSS_LIMIT' and 'TAKE_PROFIT_LIMIT' in type parameter like this
client.futures_create_order(symbol=symbol, side='BUY', type='TAKE_PROFIT_LIMIT', timeInForce='GTC', quantity=quantity, price=price)
client.futures_create_order(symbol=symbol, side='BUY', type='STOP_LOSS_LIMIT', timeInForce='GTC', quantity=quantity, price=price)
or included reduceOnly='true' like sloth456 mentioned above because your orders likely to be misunderstood as new long/short order, in that case I recommend you to look for cancel futures orders function in the wrapper to clear out all stop loss order after the price hit take profit
symbol=self.symbol, side=BUY, type='STOP_MARKET', stopPrice=stop_price, closePosition='true'
I keep having *** binance.exceptions.BinanceAPIException: APIError(code=-4061): Order's position side does not match user's setting. when I try bot sides.
BUY = client.SIDE_BUY
SELL = client.SIDE_SELL
TYPE = client.ORDER_TYPE_STOP_LOSS_LIMIT
client.futures_create_order(symbol=_symbol, side=BUY, type=TYPE, stopPrice=80.0, closePosition='true')
@avatar-lavventura without looking too deeply - just a hunch but is your account set in Hedge mode? You may need to set it to one way mode to get it working.
Check this link, different project but someone else having same error on binance api.
@sloth456 Yes my account was in hedge-mode. I will give it a try with the one way mode
Ah, it's now solved. I was missing parematers.
Thanks,
Hi @kretxo
How did you solve the problem of create stop_limit order in futures account?
I did create entry order by this:
client.futures_create_order(
symbol=order.symbol
side= Client.SIDE_BUY ,
type = Client.ORDER_TYPE_LIMIT,
timeInForce=Client.TIME_IN_FORCE_GTC,
quantity=order.quantity,
price=order.price)
and it's worked, but I can't create stop loss order. My request is :
client.futures_create_order(
symbol=order.symbol,
side= Client.SIDE_SELL,
type = Client.ORDER_TYPE_STOP_LOSS_LIMIT,
timeInForce=Client.TIME_IN_FORCE_GTC,
quantity=order.quantity,
stopPrice=order.stop,
price=order.stop)
and I got 'invalid orderType' error.
Any suggestion?
@majidnow
In regards to stop limit orders, when I read the official binance futures API docs:
https://binance-docs.github.io/apidocs/futures/en/#new-order-trade
I see that 'STOP_LOSS_LIMIT' isn't a thing. Looks like it's just named 'STOP' for a limit stop and 'STOP_MARKET' for a stop market. I haven't checked but it looks like you might need to do:
client.futures_create_order(
symbol=order.symbol,
side= Client.SIDE_SELL,
type = 'STOP',
timeInForce=Client.TIME_IN_FORCE_GTC,
quantity=order.quantity,
stopPrice=order.stop,
price=order.stop)
Looks like the constant defined in this module are incorrect or maybe just got outdated. See line 56 in Client.py
ORDER_TYPE_STOP_LOSS = 'STOP_LOSS'
ORDER_TYPE_STOP_LOSS_LIMIT = 'STOP_LOSS_LIMIT'
Hi everyone,
The api is good now, but i face an issue that i cannot close the position immediately, some time the stop price never matched. Sometimes it raised "order is immediately execution" exception. How can we close position like press the "market" button on the binance app?
Thank you,
Most helpful comment
Hi @chaudoe
If for example you have a short order worth 1 BTC. To close this you must place a long order of 1 BTC.
The issue in your code is that you have specified closePosition parameter on a MARKET order, closePosition is only used with STOP_MARKET or TAKE_PROFIT_MARKET. Also, since you are using a MARKET order you must also specify the quantity.
To close a short order worth 1 BTC you would need the following code:
Binance.futures_create_order(symbol=self.symbol, side='BUY', type='MARKET', quantity=1, reduceOnly='true')I added reduceOnly parameter to prevent a BUY order being placed if there is no short position already in Binance.
If you do not know the exact quantity of the original position, you can also close the order by using an inflated value for quantity, for BTCUSDT you could use a value of 100 to close any shorts with a value below that.
Alternatively you may also try setting a STOP_MARKET order which would close the position once a particular price has been crossed. This does not require a quantity to be specified if you use the 'closePosition' parameter.
For example
Binance.futures_create_order(symbol=self.symbol, side=BUY, type='STOP_MARKET', stopPrice=stop_price, closePosition='true')That last example is untested, but I believe it should work