是否有币安 API 端点可以平仓所有仓位?

Bor*_*lis 2 python binance ccxt binance-api-client

是否有特定的币安期货 API 端点可以自动平仓所有仓位?GUI中有这样一个选项。现在我只能想象获得所有头寸的金额然后卖出该金额,但是有更简单的方法吗?

最好我希望能够调用 ccxt 库或 python-binance 库。

Igo*_*tor 6

这取决于持仓方,就币安而言是“单向”(默认)还是“对冲” :

老实说,没有任何端点可以在一次调用中平掉您的所有仓位。不过,您可以一一平仓。

为了平掉单个单向头寸(具有 的头寸side: "BOTH"),您只需下相反一侧的订单,金额等于您带有标志的头寸reduceOnly

因此,如果您有规模为 1 的未平仓多头头寸(您买入 1 份合约),则要平仓,您需要下相反的订单来卖出 1 份合约。反之亦然,如果您有规模为 1 的未平仓空头头寸,则您买入 1 份合约来平仓。

import ccxt
from pprint import pprint

# make sure it's 1.51+
print('CCXT Version:', ccxt.__version__)


exchange = ccxt.binanceusdm({
    'apiKey': 'YOUR_API_KEY',
    'secret': 'YOUR_SECRET',
})

markets = exchange.load_markets()

# exchange.verbose = True  # uncomment for debugging purposes

symbol = 'BTC/USDT'
type = 'market'  # market order
side = 'sell'  # if your position is long, otherwise 'buy'
amount = THE_SIZE_OF_YOUR_POSITION  # in contracts
price = None  # required for limit orders
params = {'reduceOnly': 'true'}

try:
    closing_order = exchange.create_order(symbol, type, side, amount, price, params)
    pprint(closing_order)
except Exception as e:
    print(type(e).__name__, str(e))
Run Code Online (Sandbox Code Playgroud)