如何在ib_insync中运行命令bracketOrder

问题描述 投票:0回答:2

在下面的代码片段中,我尝试创建一个包含止损和获利的操作

from ib_insync import *


util.startLoop()
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1)

contract = Future(symbol='ES', lastTradeDateOrContractMonth='202309', exchange='CME')

order = ib.placeOrder(contract, MarketOrder('BUY', 1))
ib.sleep(1)

ticker = ib.reqMktData(contract, genericTickList='', snapshot=True) # Request real-time market data
ib.sleep(1) # Wait for the ticker to receive data

current_price = ticker.marketPrice()

takeProfitPrice = current_price + 10 # Replace with your desired take profit price
stopLossPrice = current_price - 10 # Replace with your desired stop loss price

takeProfitOrder = LimitOrder('SELL', 1, takeProfitPrice)
stopLossOrder = StopOrder('SELL', 1, stopLossPrice)

ib.sleep(1)

bracket = ib.bracketOrder('SELL', 1, takeProfitPrice+100, stopLossPrice+100, stopLossPrice-200)
bracket[0].parentId = order.order.orderId # Set the parent ID for the main order

bracket[2].parentId = order.order.orderId # Set the parent ID for the stop loss order
bracket[2]. transmit = True # Set transmit to True for the stop loss order

bracket[1].parentId = order.order.orderId # Set the parent ID for the take profit order
bracket[1]. transmit = True # Set transmit to True for the take profit order

ib.placeOrder(contract, bracket) # Place the bracket order

ib.run()
ib.disconnect()

我收到错误:

错误 201,reqId 1172:订单被拒绝 - 原因:父订单正在被取消。 取消订单: Trade(contract=Future(symbol='ES', lastTradeDateOrContractMonth='202309', Exchange='CME'),

非常感谢您帮助创建命令,以便我同时激活止损和止盈,并且第一个执行的命令将取消相应的操作。

提前感谢所有帮助者

python-3.x interactive algorithmic-trading interactive-brokers ib-insync
2个回答
0
投票

我尝试了多种方式进行交易,但每次都被取消 我也尝试阅读并应用文档,但不够清晰


0
投票

代码中有些东西不起作用:

  1. ib.placeOrder 返回交易,而不是订单
  2. 更重要的是,文档中解释说,您必须将父订单和止盈订单的传输标志设置为 False,然后将止损订单的传输标志设置为 True 将发送整个括号订单。 请参阅https://interactivebrokers.github.io/tws-api/bracket_order.html
© www.soinside.com 2019 - 2024. All rights reserved.