feat: add 5-day reversal strategy
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -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()
|
||||
Reference in New Issue
Block a user