【问题标题】:Python Binance Futures - problem creating Take Profit Limit order -> (APIError(code=-2021): Order would immediately trigger.)Python Binance Futures - 创建止盈限价订单时出现问题 -> (APIError(code=-2021): Order will immediately trigger.)
【发布时间】:2022-11-27 17:48:26
【问题描述】:

尝试用 python 编写一个基本的 Binance 交易机器人。 继续收到“APIError(code=-2021): Order would immediately trigger”,即使在下限价订单时没有任何意义。

在撰写本文时,ETH/BUSD 汇率约为 1210。

我打印出我的当前价格 (1210.00) 和目标价格 (1215.44) 应该触发止盈。我可以通过 Binance GUI 毫无问题地执行此操作,订单被接受并触发。

但是通过 API,即使我将我的价格设置为高于(或低于)当前市场价格,并将目标价格设置为 2000(远高于市场价格),订单也不会被接受,我会得到同样的错误。 我认为我的 futures_create_order 参数有问题,但我无法从文档中弄清楚。任何帮助将不胜感激。

这是我的代码

from binance.client import Client

test_key = "xxx"
test_secret_key = "xxx"
client = Client(test_key, test_secret_key, testnet = True)

symbol = 'ETHBUSD'
tar_profit = 0.09 #take profit when ROE hits 9%
lev = 20 #leverage

ticker_data = client.futures_symbol_ticker(symbol = symbol)
current_price = float(ticker_data["price"])
cp_adder = 1 + float(tar_profit / lev)
tp_price = round(current_price * cp_adder, 2)
qty = 0.2

client.futures_create_order(
    symbol=symbol,
    side='BUY', #'SELL' or 'BUY'
    type ='TAKE_PROFIT',
    timeInForce='GTC', #good until cancelled
    price = current_price,
    quantity = qty,
    #isolated=True,
    stopPrice = tp_price, #take_profit price
    workingType='CONTRACT_PRICE' #or MARK PRICE
)

【问题讨论】:

    标签: python trading binance cryptocurrency


    【解决方案1】:

    回答我自己的问题,因为我想通了。

    不好意思承认,但我意识到止盈/止损订单是额外的单独订单,您可以在您的第一个限价/市价订单之后发送。这意味着您必须向币安发送 2 个单独的订单。如果我们看一下我的例子:

    首先,我发送一个限价订单以开立多头/空头头寸。 在那之后,我可以发送一个等效但相反的获利订单(reduceOnly 参数设置为 True;重要!),一旦满足条件,这将关闭我的头寸。

    因此,只有在第一个订单执行并开仓后,第二个订单才能“激活”并平仓。

        client.futures_create_order(
            symbol=symbol,
            side='BUY', #'SELL' or 'BUY'
            type ='LIMIT',
            timeInForce='GTC',
            price = price,
            quantity = qty,
            #isolated=True,
            #stopPrice=stop_price,
            workingType='CONTRACT_PRICE' #or MARK PRICE
            )
    
        client.futures_create_order(
            symbol=symbol,
            side='SELL', #'SELL' or 'BUY'
            type ='TAKE_PROFIT',
            timeInForce='GTC',
            price = price,
            reduceOnly= True,
            quantity = qty,
            #isolated=True,
            stopPrice=stop_price,
            workingType='CONTRACT_PRICE' #or MARK PRICE
            )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-24
      • 2022-12-17
      • 1970-01-01
      • 1970-01-01
      • 2022-07-30
      • 2019-01-31
      • 2022-07-19
      • 1970-01-01
      相关资源
      最近更新 更多