Update stoploss and profit orders of a bracket
-
def moveBracketStopLoss(self, brackets): # cancel stop order automatically cancels the profit order of the bracket too self.cancel(brackets[1]) self.cancel(brackets[2]) before = brackets[1].created.price stopOrder = profitOrder = None # need to replace both stopOrder and profitOrder if brackets[0].ordtype == 0: # buy stopOrder = self.sell( price=brackets[0].executed.price, size=brackets[0].size, exectype=bt.Order.Stop, transmit=False) profitOrder = self.sell( price=brackets[2].created.price, size=brackets[0].size, exectype=bt.Order.Limit, transmit=True, oco=stopOrder) if brackets[0].ordtype == 1: # sell stopOrder = self.buy( price=brackets[0].executed.price, size=brackets[0].size, exectype=bt.Order.Stop, transmit=False) profitOrder = self.buy( price=brackets[2].created.price, size=brackets[0].size, exectype=bt.Order.Limit, transmit=True, oco=stopOrder) brackets[1] = stopOrder brackets[2] = profitOrder self.log('Move stoploss to breakeven for ', 'long' if brackets[0].ordtype == 0 else 'short', 'position from ', before, 'to', brackets[1].created.price)
Not sure what is wrong with the code, but I just noticed a stopOrder order is not getting created with Interactive brokers live trading. Would transmit=True of profit order sumbit the stop oder too?
update: https://www.backtrader.com/blog/posts/2017-03-19-oco/oco.html Here it says OCO order doesn't support live trading, so how can I replace a bracket order of which main order is already completed? Can I still attach a new bracket to the completed main order?
-
I noticed @backtrader updated the documentations, it says Interactive Brokers support for OCO order.
https://cookbook.backtrader.com/documentation/order-creation-execution/oco/oco/
Not sure if my code above is correct, but one day I noticed that my OCO order with stop and profit was not working on IB, stop order is missing.. only profit order is placed
-
@han-chen said in Update stoploss and profit orders of a bracket:
noticed @backtrader updated the documentations, it says Interactive Brokers support for OCO order.
No. Format, layout and topic ordering have changed with the goal to try to make the content more accessible (if possible, sometimes one aims for the goal and it ends up being a throw in)
The content hasn't changed. Version
1.9.6.36
released on2017-03-28
(Commit hash:e6a7f1c80ca85e4b46c112e48ef879d881c3e817
) added it.A
Stop
order has no guaranteed execution price. From experience withbracket
orders, this means it cannot be related to other orders. It may simply be the ordering, i.e.: theStop
can only be the stop to aLimit
and not the other way round.
-
@backtrader I swap the ordering of stop and profit. Now the stoploss got placed but profit was missing.
IB gateway: v974.4g
http://cdn.quantconnect.com/interactive/ibgateway-latest-standalone-linux-x64-v974.4g.shI moved the stop from 2834.25 to 2842.25, but profit order didn't get placed at all..
def moveBracketStopLoss(self, bracket, bt): # cancel stop order automatically cancels the profit order of the bracket too self.cancel(bracket[1]) self.cancel(bracket[2]) before = bracket[1].created.price stopOrder = profitOrder = None # need to replace both stopOrder and profitOrder if bracket[0].ordtype == 0: # buy profitOrder = self.sell( price=bracket[2].created.price, size=bracket[0].size, exectype=bt.Order.Limit, transmit=False) stopOrder = self.sell( price=bracket[0].executed.price, size=bracket[0].size, exectype=bt.Order.Stop, transmit=True, oco=profitOrder) if bracket[0].ordtype == 1: # sell profitOrder = self.buy( price=bracket[2].created.price, size=bracket[0].size, exectype=bt.Order.Limit, transmit=False) stopOrder = self.buy( price=bracket[0].executed.price, size=bracket[0].size, exectype=bt.Order.Stop, transmit=True, oco=profitOrder) bracket[1] = stopOrder bracket[2] = profitOrder Helper.log(self, 'Move stoploss to breakeven for ', 'long' if bracket[0].ordtype == 0 else 'short', 'position from ', before, 'to', bracket[1].created.price)