erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.84k stars 769 forks source link

Issue with attaching stoploss and take profit order #614

Closed logivity closed 1 year ago

logivity commented 1 year ago

Hello, have issue with attaching stoploss and profit taker. Also the order doesn't get's transmitted.

`from ib_insync import *

import random

ib = IB() ib.connect('127.0.0.1', 7497, clientId=random.randint(1,99))

c = Forex('EURCAD')

c = ib.qualifyContracts(c)[0]

parent = Order(orderId=7520, action='BUY', orderType='LMT', totalQuantity=1,lmtPrice=1.46025, transmit= False)

takeProfit = Order() takeProfit.orderId = parent.orderId + 1 takeProfit.action = "SELL" if parent.action == "BUY" else "BUY" takeProfit.orderType = "LMT" takeProfit.totalQuantity = parent.totalQuantity takeProfit.lmtPrice = 1.5 takeProfit.parentId = parent.orderId takeProfit.transmit = False

stopLoss = Order() stopLoss.orderId = parent.orderId + 2 stopLoss.action = "SELL" if parent.action == "BUY" else "BUY" stopLoss.orderType = "STP" stopLoss.totalQuantity = parent.totalQuantity stopLoss.auxPrice = 1.4 stopLoss.parentId = parent.orderId stopLoss.transmit = True

ords = [parent, takeProfit, stopLoss]

for o in ords: trade=ib.placeOrder(c, o) print(trade)`

logivity commented 1 year ago

Ok, solved partially by adding ib.sleep(1) so orders are attached. But now price doesn't attach and it doesn't transmit.

erdewit commented 1 year ago

Why not use this instead:

https://ib-insync.readthedocs.io/api.html#ib_insync.ib.IB.bracketOrder