OS: Linux PYTHONWORK 5.3.0-18-generic #19-Ubuntu SMP Tue Oct 8 20:14:06 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
Programming Language version: Python 2.7.17
CCXT version: latest
Exchange: binance
Method: fapiPrivatePostMarginType, createOrder
Hi,
For the FUTURE market, I am trying to set the ‘positionSide’ value of an Order to LONG or SHORT. Right now, I am getting the value of BOTH.
=> this is how I initiliazed the connection to the server:
[start PART 1: ... snip ...]
LD_API_KEY = os.getenv("BINANCE_APIKEY")
LD_API_SECRET = os.getenv("BINANCE_API_SECRET")
conn_exchange = ccxt.binance({
'apiKey': LD_API_KEY,
'secret': LD_API_SECRET,
'timeout': 30000,
'enableRateLimit': True,
'rateLimit': 250,
'options': { 'defaultType': 'future', },
})
[end PART 1 ... snip ...]
[start PART 2: ... snip ...]
exchange = initialize_connection( in_market ) # NOTE <- initialization for FUTURES market
exchange.load_markets()
# the futures acount can be ISOLATED or CROSSED -!!! MAKE SURE IT STAYS ISOLATED !!!
if ( in_market == t_bin_param.BIN_TRADE_FUTURES ):
new_symbol = in_symbol.replace("/", "")
param = {
'symbol' : new_symbol,
'marginType' : 'ISOLATED', #or 'CROSSED' or 'ISOLATED'
}
try:
# (Pdb) p e - the function below always kickes out some kind of error, trying to avoid it
# ExchangeError('binance {"code":-4046,"msg":"No need to change margin type."}')
exchange.fapiPrivatePostMarginType ( param )
except ccxt.ExchangeError as f: # code below to avoid process stopping due to message
if ( f.args[0].find("-4046") == -1 ): # -4046, MarginType adjustment not necessary
raise # (otherwise CONTINUE processing)
[end PART 2 ... snip ...]
NOTE: The reason for the code at -4046 is to make sure that ISOLATED has been set. Things can get bad if ISOLATED is ~not set~
Below is where I am trying to set the “positionSide”
[start PART 3 ... snip ...]
# see: https://www.qdxingtu.com/key/Binance%20Future%20position%20side.html
# see: https://github.com/ccxt/ccxt/issues/6859
if ( in_side.upper() == 'BUY') :
server_params['positionSide'] = 'LONG'
elif ( in_side.upper() == 'SELL') :
server_params['positionSide'] = 'SHORT'
else:
raise CustomProgramError ('-11091', 'BIG ERROR! - the SIDE passed in is NOT ->BUY<- or ->SELL<- !')
[end PART 3 ... snip ...]
Below is what is being executed for a MARKET order
[start PART 4 ... snip ...]
orders = exchange.createOrder( in_symbol, k_type, side, in_amount, None, server_params )
[end PART 4 ... snip ...]
The values are as follows for the statement above:
debugger>> print in_symbol, k_type, side, in_amount, None, server_params
(‘BTC/USDT’, ‘MARKET’, ‘BUY’, 0.001, None, {‘newClientOrderId’: ‘3f437e0c86b74df584f2ec9b8c8a8660’, ‘positionSide’: ‘LONG’})
PROBLEM: This is the error I get:
ExchangeError(‘binance {“code”:-4061,“msg”:“Order’s position side does not match user’s setting.”}’)
When I take out the part associated with [ start PART 3/end PART 3 ] above, everything works - BUT - when doing a query for OPEN POSITIONS in the FUTURES MARKET ( as querying for OPEN POSITIONS in a SPOT MARKET is not possible/valid )
I get something like the following:
This is what I got with the position:
{‘return_code’: 0, ‘data’: [{‘symbol’: ‘BTCUSDT’, ‘positionAmt’: ‘0.001’, ‘entryPrice’: ‘11498.50000’, ‘markPrice’: ‘11468.43959888’, ‘unRealizedProfit’: ‘-0.03006040’, ‘liquidationPrice’: ‘7695.68814257’, ‘leverage’: ‘3’, ‘maxNotionalValue’: ‘200000000’, ‘marginType’: ‘isolated’, ‘isolatedMargin’: ‘3.80353421’, ‘isAutoAddMargin’: ‘false’, ‘positionSide’: ‘BOTH’}]}
which shows the “‘positionSide’: ‘BOTH’”
WHAT I TRIED
I added “hedgeMode” = True (according to this : https://github.com/ccxt/ccxt/issues/6859 )
[start PART 5 ... snip ...]
LD_API_KEY = os.getenv("BINANCE_APIKEY_MARGIN")
LD_API_SECRET = os.getenv("BINANCE_API_SECRET_MARGIN")
conn_exchange = ccxt.binance({
'apiKey': LD_API_KEY,
'secret': LD_API_SECRET,
'timeout': 30000,
'enableRateLimit': True,
'rateLimit': 250,
'options': { 'defaultType': 'future', 'hedgeMode': True },
})
print("\n\n ---- THE FUTURES MARKET HAS BEEN SET --- \n\n")
[end PART 5 ... snip ...]
BUT - I still got the SAME ERROR (i.e. : ExchangeError(‘binance {“code”:-4061,“msg”:“Order’s position side does not match user’s setting.”}’) )
I also tried it this way too:
LD_API_KEY = os.getenv("BINANCE_APIKEY_MARGIN")
LD_API_SECRET = os.getenv("BINANCE_API_SECRET_MARGIN")
conn_exchange = ccxt.binance({
'apiKey': LD_API_KEY,
'secret': LD_API_SECRET,
'timeout': 30000,
'enableRateLimit': True,
'rateLimit': 250,
'hedgeMode': True,
'options': { 'defaultType': 'future' },
})
print("\n\n ---- THE FUTURES MARKET HAS BEEN SET --- \n\n")
but it still did not work (got the SAME ERROR (i.e. : ExchangeError(‘binance {“code”:-4061,“msg”:“Order’s position side does not match user’s setting.”}’)
QUESTION:
NOTE: The verbose version of the run is listed below.
- I was trying to get ‘positionSide’ to be set in order to be able to CLOSE a position properly should it become necessary. If the ‘positionSide’ is LONG, then I would close the position by creating a SELL Order against the trading pair (ex: BTC/USDT). If the ‘positionSide’ is SHORT, the I would close it out by creating a BUY Order
Is this correct?
- If this is correct, then how can I set the positionSide properly - or - is there some kind of Work-Around?
Any help, hints or advice would be greatly appreciated.
Regards.