Bracket Order Calculation Question that No One Asked.
-
I have been playing with bracket orders for my simple crossover strategy and all the trade entry time and execution price is matching up with Tradingview's result which is great news. However as I examine each trade, the P&L and closing time is quite off.
def next(self): close = self.data.close[0] long_tp = close + 0.025 ##### REWARD long_stop = close - 0.1 ##### RISK short_tp = close - 0.025 ##### REWARD short_stop = close + 0.1 ##### RISK if not self.position: if self.crossover: self.buy_bracket(stopprice=long_stop, limitprice=long_tp, exectype=bt.Order.Market, size=1) if self.crossunder: self.sell_bracket(stopprice=short_stop, limitprice=short_tp, exectype=bt.Order.Market, size=1) RESULTS: Open Time Close Time Entry Mins PnL(Ticks) 2020-11-16 04:28:00 2020-11-16 05:04:00 25.11 36 0.02 2020-11-16 05:44:00 2020-11-16 06:09:00 25.025 25 -0.095 2020-11-16 06:17:00 2020-11-16 06:31:00 24.95 14 0.025 2020-11-16 06:42:00 2020-11-16 06:45:00 24.99 3 0.02 2020-11-16 07:40:00 2020-11-16 08:31:00 24.97 51 -0.1 2020-11-16 09:31:00 2020-11-16 09:42:00 24.945 11 0.03
What I noticed is that with backtrader's bracket order, the [main, stop, limit] are all calculated based on the previous candle's Close price. So now the parent order [main] is executed based on market price, hence the next candle's open price. Because othewise, all the P&L should either be 0.025 or -0.1 Whereas for Tradingview, the children orders [stop, limit] are calculated based on the parent order's [main] executed price. Looking below, you can see that all the P&L are consistent.
Anyone got a clue on how to replicate this behavior in backtrader's bracket order?
-
@backtrader, @ab_trader , @run-out you guys seems to be the most active gurus on this community, if you can please shine some light on my question, it'd be greatly appreciated.
-
@Jeffrey-C-F-Wong said in Bracket Order Calculation Question that No One Asked.:
Anyone got a clue on how to replicate this behavior in backtrader's bracket order?
If you want to use bracket order with the main market order executed on the
open
, than you can usecheat-on-open
and issue the bracket order duringnext_open()
call. If I remember correctly, than duringnext_open()
the only price delivered is currentopen
, so yourstop
andlimit
prices should be calculated correctly.