feat: add 5-day reversal strategy

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Yuxuan Yan
2026-06-07 09:26:35 +08:00
parent fd86190e9a
commit 085e51abf1
3 changed files with 52 additions and 4 deletions
+27
View File
@@ -0,0 +1,27 @@
import backtrader as bt
class FiveDayReversal(bt.Strategy):
"""Buy on 5-day oversold signal, sell on bounce or time stop."""
params = (
("lookback", 5),
("entry_threshold", -0.05), # buy when 5-day return < -5%
("exit_threshold", 0.0), # sell when 1-day return > 0%
("max_hold", 3), # max hold days
)
def __init__(self):
self.roc5 = bt.indicators.RateOfChange(self.data.close, period=self.params.lookback)
self.hold_counter = 0
def next(self):
if not self.position:
if self.roc5[0] < self.params.entry_threshold: # RateOfChange returns a fraction, not %
self.buy()
self.hold_counter = 0
else:
self.hold_counter += 1
roc1 = (self.data.close[0] / self.data.close[-1] - 1) * 100
if roc1 > self.params.exit_threshold * 100 or self.hold_counter >= self.params.max_hold:
self.close()