我目前正在使用 Binance 的 API 为自己编写一个小型 python 机器人,我发现文档相当乏味,想知道这里是否有人可以提供帮助。
假设我想以市价开仓 BTCUSDT 配对的期货交易,杠杆为 5 倍,保证金为 100 USDT,止盈为 50%,止损为 10%。
from binance.client import Client
import cfg
client = Client(cfg.api_key, cfg.api_secret)
client.futures_create_order(symbol='BNBUSDT', side='LONG', type='MARKET', quantity = 100 USDT * leverage / asset_price)
据我所知,这大约是。我没有看到任何杠杆属性,但是还有另一个函数称为
futures_change_leverage()
它能够改变你的杠杆,所以我必须初始化一个头寸,然后改变杠杆?这不是也会缩小我的利润吗?
我也对后面的属性如何工作以及如何下止盈和止损订单感到相当迷失。
感谢您的帮助。
从运行一些测试来看,币安似乎使用了保证金类型(交叉或逐仓)以及您上次在网络、桌面或移动应用程序上的配对中使用的杠杆。如果您没有更改,则默认为 20x Cross。
因此,在开仓期货交易之前,您应该更改杠杆和保证金类型,然后才开仓。
例如:
client.futures_change_margin_type(symbol='BNBUSDT', marginType='ISOLATED')
marginType 必须是“ISOLATED”或“CROSSED”。
由于出现了有关杠杆和保证金类型以及如何设置这些的问题:
def adjust_leverage(symbol, client):
client.futures_change_leverage(symbol=symbol, leverage=10)
def adjust_margintype(symbol, client):
client.futures_change_margin_type(symbol=symbol, marginType='ISOLATED')
我仍在研究如何进行止损和止盈,甚至可能是追踪止损,如果我确实找到了它们,我会随时通知您。
如果有人在 2023 年中期搜索它。
要完整回答问题,请使用最新的币安库“binance.um_futures”:
首先,您需要为交易品种设置杠杆,比如 BTCUSDT:
client.change_leverage(symbol="BTCUSDT", leverage=5, recvWindow = 60000)
接下来,开仓: 首先,使用以下公式计算 100 USDT 的数量:
数量 = 100 USDT * 5 杠杆 / BTCUSDT_当前价格
initial_position = client.new_order(
symbol="BTCUSDT",
side="BUY",
type=order_type,
quantity=quantity,
timestamp = timestamp,
recvWindow = 60000
)
接下来,要实际设置止盈和止损,您需要发送 2 个相反方向的单独订单:
止盈示例: 您需要使用以下公式计算 50% ROE%:
def calculate_tpsl(symbol, side, leverage, tp_percentage=take_profit_percentage, sl_percentage=stop_loss_percentage):
def calculate_target_price(roe):
if side == "SELL":
sltp_target_price = entry_price / (1 - roe / leverage)
else:
sltp_target_price = entry_price * (1 + roe / leverage)
return sltp_target_price
if side == "BUY":
profit_perc = tp_percentage / 100
profit_mark_price = calculate_target_price(profit_perc)
loss_perc = -sl_percentage / 100
loss_mark_price = calculate_target_price(loss_perc)
if side == "SELL":
profit_perc = -tp_percentage / 100
profit_mark_price = calculate_target_price(profit_perc)
loss_perc = sl_percentage / 100
loss_mark_price = calculate_target_price(loss_perc)
return profit_mark_price, loss_mark_price
发送止盈头寸:
take_profit_position = client.new_order(
symbol="BTCUSDT",
side="SELL",
type="TAKE_PROFIT_MARKET",
quantity=quantity,
stopPrice=take_profit_price,
timeInForce="GTE_GTC",
reduceOnly="True",
timestamp = timestamp,
recvWindow = 60000
)
你也可以做同样的事情来止损,输入:STOP_MARKET
希望能详细解答您的问题。
这就是我平仓的方式。
Par = clientR.futures_symbol_ticker(Symbol=ticker)
def CLOSE_POSITIONLONG(ticker):
par = client.futures_symbol_ticker(Symbol=ticker)
preciodeventa = float(par.get('price'))
print("Precio de cierre: " + str(preciodeventa))
triggerB = float(preciodeventa)+(float(preciodeventa)*0.0005)
client.futures_create_order(
symbol = ticker
,side='SELL'
,closePosition=True
,stopPrice= round(triggerB,3)
,type='TAKE_PROFIT_MARKET'
)
triggerS = float(preciodeventa)-(float(preciodeventa)*0.0005)
client.futures_create_order(
symbol = ticker
,side='SELL'
,closePosition=True
,stopPrice= round(triggerS,3)
,type='STOP_MARKET'
)