ccxt / ccxt

A JavaScript / TypeScript / Python / C# / PHP cryptocurrency trading API with support for more than 100 bitcoin/altcoin exchanges
https://docs.ccxt.com
MIT License
31.94k stars 7.38k forks source link

BingX create order futures problem #20548

Closed xerno0om closed 6 months ago

xerno0om commented 6 months ago

Operating System

Windows 11

Programming Languages

Python

CCXT Version

4.0.29

Description

I can't open an order in Perpetual Futures, I use the same (create_market_order) for bybit and kucoin and it worked fine, but when I run the code it tells me ccxt.base.errors.ExchangeError: bingx { "code ": 100004,"msg": "Authorization denied because the API key was created without authorization", "success": false, "timestamp": 1703485688822} in my API on BingX I only checked "perpetual futures trading", ...so the code wants to open in "Spot trading", how can I make it open in "perpetual futures trading"

Code

  import ccxt

exchange = ccxt.bingx({
    'apiKey': 'apiKey',
    'secret': 'secret',
    'adjustForTimeDifference': True,
})

symbol = 'BTC-USDT'
cost = 10.00
Side = 'SHORT'

order = exchange.create_market_order(symbol, Side, cost, params={'leverage': 74})
print(order)
xerno0om commented 6 months ago

I tried to do :

import ccxt
symbol = 'BTC/USDT:USDT'

exchange = ccxt.bingx({
    'apiKey': 'key',
    'secret': 'secret',
    'options': {
        'adjustForTimeDifference': True,
        'defaultType': 'swap',
    },
})
exchange.verbose = True
order = exchange.create_order(symbol, "market", 'sell', 0.0001, {
    'PositionSide': 'SHORT'
})
print(order)

and

order = exchange.create_market_order(symbol, "market", 'sell', 0.0001, {
    'PositionSide': 'SHORT'
})
print(order)

and he tells me : ccxt.base.errors.BadRequest: bingx {"code":80014,"msg":"Invalid parameters, err:Key: 'PlaceOrderRequest.ReqOrderData.PositionSide' Error:Field validation for 'PositionSide' failed on the 'required' tag","data":{}}

I simply want to open an order in market and perpetual futures, help me

tbehzad76 commented 6 months ago

I think your problem is because of leverage you can set leverage like this

bingx.set_leverage(leverage=10, symbol, params={"marginMode": "cross", 'side': 'LONG'})
bingx.set_leverage(leverage=10, symbol, params={"marginMode": "cross", 'side': 'SHORT'})
xerno0om commented 6 months ago

I think your problem is because of leverage you can set leverage like this

bingx.set_leverage(leverage=10, symbol, params={"marginMode": "cross", 'side': 'LONG'})
bingx.set_leverage(leverage=10, symbol, params={"marginMode": "cross", 'side': 'SHORT'})

No when I run the script even without params={'leverage': 74}, it wants to do spot trading, so it tells me

{"code":100004,"msg":"Permission denied as the API key was created without the permission","success":false,"timestamp":1703493028133}

I must add or modify what for it makes me perpetual futures?

sc0Vu commented 6 months ago

Hi @xerno0om

You can use unified symbol like BTC/USDT:USDT to create order. For the position side, the param might be positionSide, they probably use different name tag in server data model.

xerno0om commented 6 months ago

it's good I found how to open a position in perpetual futures in market :

import ccxt

symbol = 'BTC-USDT'
cost = 1.00
Side = 'SHORT'

exchange = ccxt.bingx({
    'apiKey': 'x',
    'secret': 'x',
    'options': {
        'adjustForTimeDifference': True,
        'defaultType': 'swap',
    },
})
exchange.verbose = True
exchange.create_order(symbol, "market", 'sell', 0.0001, 30000, {
    'positionSide':'SHORT',
})

I was inspired by this code that I found on another issue, he opens a position in perpetual futures in limit :

https://github.com/ccxt/ccxt/issues/19422
import ccxt

symbol = 'BTC-USDT'
cost = 1.00
Side = 'SHORT'

exchange = ccxt.bingx({
    'apiKey': 'x',
    'secret': 'x',
    'options': {
        'adjustForTimeDifference': True,
        'defaultType': 'swap',
    },
})
exchange.verbose = True
exchange.create_order(symbol, "limit", 'sell', 0.0001, 30000, {
    'triggerPrice': '30000',
    'positionSide':'SHORT',
})

exchange.create_order(symbol, "market", 'sell', 0.0001, 30000, {
    'positionSide':'SHORT',
})

works the same with :

exchange.create_market_order(symbol, 'sell', 0.0001, 30000, {
    'positionSide':'SHORT',
})
Asho198967 commented 3 months ago

how to make TP and SL?