sammchardy / python-binance

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

Cannot create margin (short) sell order #1209

Closed jabranzahid-dev closed 2 years ago

jabranzahid-dev commented 2 years ago

Hi I am trying to create a margin sell order (to short the coin) using the function client.create_margin_order(symbol='CTXCUSDT',type='ORDER_TYPE_MARKET',side='SIDE_SELL',quantity=final_amount_to_buy, sideEffectType='MARGIN_BUY',isIsolated=True)

but I get Traceback (most recent call last): File "C:\Python\Trading\Pairs trading\binance api practice.py", line 26, in open_position = client.create_margin_order(symbol='CTXCUSDT',type='ORDER_TYPE_MARKET',side='SIDE_SELL',quantity=final_amount_to_buy, sideEffectType='MARGIN_BUY',isIsolated=True) File "C:\Users\Sarim\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\binance\client.py", line 3170, in create_margin_order return self._request_margin_api('post', 'margin/order', signed=True, data=params) File "C:\Users\Sarim\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\binance\client.py", line 212, in _request_margin_api return self._request(method, uri, signed, **kwargs) File "C:\Users\Sarim\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\binance\client.py", line 197, in _request return self._handle_response() File "C:\Users\Sarim\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\binance\client.py", line 230, in _handle_response raise BinanceAPIException(self.response) binance.exceptions.BinanceAPIException: APIError(code=-1102): Mandatory parameter 'side|type' was not sent, was empty/null, or malformed.

Not sure which parameter I am missing here, any advice please

thanks

jabranzahid-dev commented 2 years ago

I managed to resolve this. I was using strings instead of enums for parameters. This is the correct code

from binance.enums import * open_position = client.create_margin_order(symbol='CTXCUSDT',type=ORDER_TYPE_MARKET,side=SIDE_SELL,quantity=final_amount_to_buy, sideEffectType='MARGIN_BUY',isIsolated=True)

Karlheinzniebuhr commented 2 years ago

I managed to resolve this. I was using strings instead of enums for parameters. This is the correct code

from binance.enums import * open_position = client.create_margin_order(symbol='CTXCUSDT',type=ORDER_TYPE_MARKET,side=SIDE_SELL,quantity=final_amount_to_buy, sideEffectType='MARGIN_BUY',isIsolated=True)

How did you manage to close your margin order? I opened a second order in the other direction but it didn't actually liquidate the margin position

jabranzahid-dev commented 2 years ago

I use this to check how much (of short sell) I am holding

currently_open = client.get_isolated_margin_account(symbols=self.coin1) amount_to_close = currently_open['assets'][0]['baseAsset']['borrowed'] remainder_close = Decimal(amount_to_close) % Decimal(self.min_trade_amount) final_amount_to_close = round(Decimal(amount_to_close) - Decimal(remainder_close),1)

At this stage, I now have a short position of my desired amount

and then close that position

order_close = client.create_margin_order(symbol=self.coin1,type=ORDER_TYPE_MARKET,side=SIDE_BUY,quantity=final_amount_to_close, sideEffectType='AUTO_REPAY',isIsolated=True)

I believe the key here is 'sideEffectType='AUTO_REPAY' to repay the loan.

However, I have also noticed, when this order to buy is executed, there is a very small amount of LONG position that appears in my account, as opposed to properly liquidating my position, I still need to work out why