币安API开仓期货交易的正确方式?

5

我目前正在使用币安的API为自己编写一个小型Python机器人,而我发现文档相当缺乏,想知道这里是否有人能够帮忙。

假设我想要以BTCUSDT的配对方式在市场价格下开设5倍杠杆和100美元的保证金进行期货交易,并设置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()的函数可以改变你的杠杆,那么我必须初始化一个仓位,然后改变杠杆?那不也只是缩小我的保证金规模吗?
对于后面的属性,我也很迷茫,不知道如何设置止盈和止损订单。
感谢任何帮助。
3个回答

6

从运行一些测试来看,似乎Binance在Web、桌面或移动应用程序上使用保证金类型(全仓或分仓)和您最后在该交易对上使用的杠杆。如果您没有更改它,则默认为20倍全仓。

因此,在开立期货交易之前,您应该更改杠杆和保证金类型,然后再开立头寸。

例如:

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')

我仍在摸索如何设置止损和获利点,甚至可能包括移动止损。如果我找到了相关的方法,我会及时通知您。


你能否解释一下为什么你从答案中选择了设置杠杆的代码行? - Jaqueline Passos
1
嗨。设置保证金类型方面它运行良好,但是设置杠杆方面仍然无法正常工作。 - Borut Flis
1
@JaquelinePassos 因为保证金类型是针对每个符号单独设置的,而不包括交易本身的开放。 - Christian Kammerer
设置杠杆的方式似乎对我不起作用,至少在币安期货测试网中是这样的。订单总是以平台上指定的杠杆执行。 - technoleap84

2

如果有人在2023年中期搜索它。

为了完全回答一个问题,使用最新的Binance库'binance.um_futures':

首先,您需要为该符号设置杠杆,比如BTCUSDT:

client.change_leverage(symbol="BTCUSDT", leverage=5, recvWindow = 60000)

接下来,打开初始仓位: 首先,使用以下公式计算100美元等值的数量:

数量 = 100美元 * 5倍杠杆 / BTCUSDT_当前价格

initial_position = client.new_order(
        symbol="BTCUSDT", 
        side="BUY",
        type=order_type,
        quantity=quantity,
        timestamp = timestamp,
        recvWindow = 60000
    )

接下来,要实际设置止盈和止损,您需要发送两个相反方向的单独订单:

止盈示例: 您需要使用以下公式计算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。

希望这详细地回答了你的问题。


-1

这是我正在执行的平仓操作。

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'
      )

1
请确保用英语编写答案。 - EJoshuaS - Stand with Ukraine

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接