简体   繁体   English

如何在 binance 上创建期货限价订单以及止盈和止损?

[英]How to create a futures limit order together with take profit and stop loss on binance?

I am trying to create a limit order together with take profit and stop loss on binance, but this does not always work.我正在尝试在 binance 上创建一个限价订单以及止盈和止损,但这并不总是有效。 Occasionally this code does everything right, but most often I get the error "API Error(code=-2021): Order would immediately trigger."有时此代码会正确执行所有操作,但大多数情况下我会收到错误“API 错误(代码=-2021):订单会立即触发。” Sometimes applications can be created, but after a couple of seconds they just disappear.有时可以创建应用程序,但几秒钟后它们就会消失。 What is the mistake?错误是什么?

from binance.client import Client
from binance.helpers import round_step_size

api_key = ''
api_secret = ''
client = Client(api_key, api_secret)
symbol = 'KNCUSDT'


def get_account_balance():
    balance = client.futures_account_balance()[6]['balance']
    return float(balance)


def get_min_quant(symbol):
    info = client.futures_exchange_info()
    for item in info['symbols']:
        if item['symbol'] == symbol:
            for f in item['filters']:
                if f['filterType'] == 'PRICE_FILTER':
                    return f['tickSize']



bet = 13  # the percentage of the balance I am willing to buy with
balance = get_account_balance() * bet / 100

tick_size = float(get_min_quant(symbol))
print(tick_size)

symbol_info = client.get_ticker(symbol=symbol)
symbol_price = float(symbol_info['lastPrice'])
quantity = int(balance / symbol_price)

enter_long_coeff = 0.4
take_profit_percent = 0.2
stop_loss_percent = 0.2

price_long_enter = symbol_price * (1 - enter_long_coeff / 100)  # entry price for a limit order
price_long_enter = round_step_size(price_long_enter, tick_size)

take_profit_price = price_long_enter * (1 + take_profit_percent / 100)
take_profit_price = round_step_size(take_profit_price, tick_size)

stop_loss_price = price_long_enter * (1 + take_profit_percent / 100)
stop_loss_price = round_step_size(stop_loss_price, tick_size)

print(price_long_enter)
print(stop_loss_price)
print(take_profit_price)
print('=======')

limit_order_long = client.futures_create_order(
    symbol=symbol,
    side='BUY',
    positionSide='LONG',
    type='LIMIT',
    quantity=quantity,
    timeInForce='GTC',
    price=price_long_enter
)

sell_gain_market_long = client.futures_create_order(
    symbol=symbol,
    side='SELL',
    type='TAKE_PROFIT_MARKET',
    positionSide='LONG',
    quantity=quantity,
    stopPrice=take_profit_price
)

sell_stop_market_short = client.futures_create_order(
    symbol=symbol,
    side='SELL',
    type='STOP_MARKET',
    positionSide='LONG',
    quantity=quantity,
    stopPrice=stop_loss_price
)



Binance does not support the 3 actions at the same time (open_order, take_profit and stop_loss). Binance 不支持同时执行 3 个操作(open_order、take_profit 和 stop_loss)。 You have to send the 3 different orders manually and keep the status of them (new, filled or so...) and do whatever you want with them.您必须手动发送 3 个不同的订单并保持它们的状态(新的、已完成的等等...),然后随心所欲地处理它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python-binance 下达带有止盈和止损的期货市场订单 - How to place a Futures Market order with Take Profit and Stop Loss using python-binance 如何在 Binance API Python 上设置止盈止损的市场期货订单? - How to place a market futures order with take profit and stop loss on Binance API Python? Python 币安期货止盈止损订单 API 错误 2021 订单将立即触发 - Python Binance Futures Take Profit and Stop Loss order API Error 2021 Order would immediately trigger 使用 ccxt 创建带止盈和止损的合约订单 - Create Contract order with Take Profit and Stop Loss with ccxt Python Binance Futures - 创建止盈限价订单时出现问题 ->(APIError(代码=-2021):订单会立即触发。) - Python Binance Futures - problem creating Take Profit Limit order -> (APIError(code=-2021): Order would immediately trigger.) 如何取消期货币安订单? - How to cancel futures binance order? 股票头寸的获利和止损字典 - Dictionary for Take Profit and Stop Loss on Stock Positions 无法使用币安设置止损限价单 Python API - Unable to set the STOP-LOSS limit order using Binance Python API 如何在 python 中使用 ccxt 制作币安期货订单? - How to make a binance futures order with ccxt in python? python-binance 中的止损限价单 API - Stop Limit Order in python-binance API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM