Hello,
I found something that I don't understand on how bracket orders work, I'll explain:
my strategy tries to catch runaways using the bollinger bands, so what I do is to place:
two stoplimit bracket orders: buy above current price, and a sell below current price,
These should open respectively a long and short position.
They are also OCO, so that if a long is open then the short will not, and vice versa.
This is my code to place such orders:
self.long_trade = self.buy_bracket(
price=entry_long,
exectype=bt.Order.StopLimit,
plimit=entry_long,
stopprice=stop_long,
limitprice=limit_long
)
self.short_trade = self.sell_bracket(
price=entry_short,
exectype=bt.Order.StopLimit,
plimit=entry_short,
stopprice=stop_short,
limitprice=limit_short,
oco=self.long_trade[0]
)
With this I expect that both stoplimit orders get accepted
, if price allows one of them to be opened (say the long/buy
one), the other order (the short/sell
and its respective bracked orders ) gets canceled
, at the same time the two bracket orders (at stopprice
and limitprice
) become active.
With these orders my strategy can open as many long positions as it wants and everything works as expected. As soon as the price hits a short/sell
order, something weird happens, here's my logs to show it:
ORDER 151, main status: Accepted - Buy, price: 1.04395
ORDER 152, (parent 151) stop status: Accepted - Sell, price: 1.03978
ORDER 153, (parent 151) limit status: Accepted - Sell, price: 1.04645
ORDER 154, main status: Accepted - Sell, price: 1.03895
ORDER 155, (parent 154) stop status: Accepted - Buy, price: 1.04312
ORDER 156, (parent 154) limit status: Accepted - Buy, price: 1.03645
ORDER 154, main status: Completed - Sell, price: 1.03895 actual price: 1.03895
ORDER 151, main status: Canceled - Buy, price: 1.04395
ORDER 156, (parent 154) limit status: Canceled - Buy, price: 1.03645
ORDER 155, (parent 154) stop status: Canceled - Buy, price: 1.04312
- orders
[151, 152, 153]
are respectively the main, stop, limit orders for the buy_bracket
,
- orders
[154, 155, 156]
are respectively the main, stop, limit orders for the sell_bracket
,
- order
154
is completed, so the main buy order is canceled as expected,
- orders
[152, 153]
should be canceled too but they are not.
- orders
[155, 156]
should NOT be canceled, but they are
this leaves my strategy with an open position (which prevents from creating further orders) that has no orders to close it, effectively hanging the whole strategy.
I tried reverting the order creation in the code, first creating the short_trade, and lastly creating the long_trade:
self.short_trade = self.sell_bracket(
price=self.sma - distance,
exectype=bt.Order.StopLimit,
plimit=self.sma - distance,
stopprice=self.sma + stopdistance,
limitprice=self.sma - limitdistance)
self.long_trade = self.buy_bracket(
price=self.sma + distance,
exectype=bt.Order.StopLimit,
plimit=self.sma + distance,
stopprice=self.sma - stopdistance,
limitprice=self.sma + limitdistance,
oco=self.short_trade[0])
and exactly the reverse happens, I can open and close short trades no problems, but as the first long/buy
trade is opened its bracket orders are canceled
and the whole strategy stops working.
I'm not sure what I'm doing wrong here, any ideas?