-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
-
I would like to backtest a breakout strategy, but is it that backtesting.py is unable to implement intra-bar backtesting?
For example, when using day-level data, I expect to execute a buy immediately when the price rises above the previous day's closing price plus 1. However, I've found that the current code can only execute trades at the earliest in the next trading day.
breakout = self.data.Close[-1] + 1
If I want to implement a similar strategy, I can only use lower time frame data—for instance, switching to hour-level data. But there are still similar issues: even if the price breaks through the preset level at 9:45 after the market opens at 9:30, the earliest trade can only be executed at 10:30. To optimize this, I would need to use even lower time frames, but it becomes relatively more difficult to obtain a large amount of relevant data (e.g., half-hourly or minute-level data).
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments
-
You can also use Strategy.buy(..., stop=..., limit=...)
parameters to enter the trade conditionally if the stop price is hit ...
Beta Was this translation helpful? Give feedback.
All reactions
-
@kernc
Although using 'stop' can implement a breakout strategy, the code cannot execute stop losses within the same bar—it can only do so at the earliest in the next bar. Is there a way to execute a trade at the breakout point and stop loss at the stop loss point if the price within the same bar has ever reached both the breakout level and the stop loss level? This would only make the backtest results lower than actual execution, which is better for users because underestimating results is preferable to overestimating them.
Beta Was this translation helpful? Give feedback.