Keyboard Shortcuts
ctrl + shift + ? :
Show all keyboard shortcuts
ctrl + g :
Navigate to a group
ctrl + shift + f :
Find
ctrl + / :
Quick actions
esc to dismiss
Likes
Search
Updating a profit target order
Hello!
?
I have been trying to fix something for the last few days but I failed. When the code attempts to update a target order belonging to a bracket order, It throws error 135. After logging I confirmed it tries to update the entry order instead of the profit taking order. Everything in the code seems fine.? ?
?
On trade class:
?
? ? def update_profit_target_price(self, broker_instance, new_profit_target):
? ? ? ? self.profit_order.lmtPrice = new_profit_target
? ? ? ? self.profit_order.transmit = True
? ? ? ? broker_instance.placeOrder(self.profit_order.orderId, self.contract, self.profit_order)
?
?
And on the logic class after being called to update:
?
def update_profit_targets_to_new_fibo_level(self):
? ? ? ? # when levels change, the profit target for any OPEN positions that were entered in the current session
? ? ? ? # is updated; ?the stop loss for the position remains unchanged
? ? ? ? for t in self.trade_list:
? ? ? ? ? ? assert isinstance(t, Trade)
? ? ? ? ? ? status = t.status_at_broker(self.ib)
? ? ? ? ? ? if status == 'OPEN':
? ? ? ? ? ? ? ? # open position: adjust profit target for open position to new level; leave stop loss as is
? ? ? ? ? ? ? ? ? ? new_profit_target_price = (self.data[self.profit_taking_for_long_positions])
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? new_profit_target_price = (self.data[self.profit_taking_for_short_positions])
? ? ? ? ? ? LOG.info(f'{self.contract_ticker} ~ update ~ Trade {t.trade_id}: status = {status}, '
? ? ? ? ? ? ? ? ? ? ? ? ?f'Profit target for open position will be updated to new level '
? ? ? ? ? ? ? ? ? ? ? ? ?f'(${new_profit_target_price})')
? ? ? ? ? ? t.update_profit_target_price(self.ib, new_profit_target_price)
?
?
Order structure:
?
# Main order (limit order)
? ? ? ? main_order = Order()
? ? ? ? main_order.eTradeOnly = False
? ? ? ? main_order.firmQuoteOnly = False
? ? ? ? main_order.orderId = entry_order_id
? ? ? ? main_order.orderRef = f'{self.stamp}_ENTRY'
? ? ? ? main_order.action = action
? ? ? ? main_order.orderType = "LMT"
? ? ? ? main_order.totalQuantity = quantity
? ? ? ? main_order.lmtPrice = limit_price ?# This is now the limit price
? ? ? ? main_order.transmit = False ?# To ensure the main order isn't transmitted before its children
? ? ? ? main_order.tif = 'DAY' ?# Make the order Good-till-Cancelled
? ? ? ? main_order.outsideRth = False ?# Enable Outside Regular Trading Hours
? ? ? ? # Take profit order
? ? ? ? take_profit = Order()
? ? ? ? take_profit.eTradeOnly = False
? ? ? ? take_profit.firmQuoteOnly = False
? ? ? ? take_profit.orderId = entry_order_id + 1
? ? ? ? take_profit.orderRef = f'{self.stamp}_PROFIT_TARGET'
? ? ? ? take_profit.action = "SELL" if action == "BUY" else "BUY"
? ? ? ? take_profit.orderType = "LMT"
? ? ? ? take_profit.totalQuantity = quantity
? ? ? ? take_profit.lmtPrice = take_profit_price
? ? ? ? take_profit.parentId = entry_order_id ?# Important: this ties the order to the parent order
? ? ? ? take_profit.tif = 'GTC'
? ? ? ? take_profit.transmit = False
? ? ? ? take_profit.outsideRth = True ?# Here is where you set the order to allow execution outside RTH
? ? ? ? # Stop loss order
? ? ? ? stop_loss = Order()
? ? ? ? stop_loss.eTradeOnly = False
? ? ? ? stop_loss.firmQuoteOnly = False
? ? ? ? stop_loss.orderId = entry_order_id + 2
? ? ? ? stop_loss.orderRef = f'{self.stamp}_STOP_LOSS'
? ? ? ? stop_loss.action = "SELL" if action == "BUY" else "BUY"
? ? ? ? stop_loss.orderType = "STP"
? ? ? ? stop_loss.totalQuantity = quantity
? ? ? ? stop_loss.auxPrice = stop_loss_price ?# Stop price
? ? ? ? stop_loss.parentId = entry_order_id ?# Important: this ties the order to the parent order
? ? ? ? stop_loss.tif = 'GTC'
? ? ? ? stop_loss.transmit = True
? ? ? ? stop_loss.outsideRth = True ?# Here is where you set the order to allow execution outside RTH
? ? ? ? self.entry_order = main_order
? ? ? ? self.profit_order = take_profit
? ? ? ? self.stop_loss_order = stop_loss
?
?
I have been trying to fix it for a while now but running out of options before digging deeper into status at broker. Does anyone see anything I am missing? Learning here, so please go easy on me.
?
thanks in advance for any input,
?
m?
? |
When you make the order update call, does the original parent (entry) order still exist, or has that been filled by that time? Take a look at the topic "10326 - OCA group revision is not allowed". Apparently, the bracket order update behavior changed and TWS 10.30 and TWS 10.32 will reject the update if the parent order is not open any longer, but the order your are trying to update still has the parentId field pointing there. If you cannot use a TWS/IBGW prior to 10.30, one suggestion was to set the parentId to 0 when you update it. Just a guess. 闯ü谤驳别苍 ?
?
?
On Wed, Nov 13, 2024 at 06:16 PM, IBmark wrote:
|
to navigate to use esc to dismiss