feat: phase 1 — data downloader, backtrader runner, SMA strategy
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
"""Base strategy and example SMA crossover for Chinese equities."""
|
||||
import backtrader as bt
|
||||
|
||||
|
||||
class SmaCross(bt.Strategy):
|
||||
"""Simple SMA crossover strategy: buy when fast crosses above slow, sell when below."""
|
||||
|
||||
params = (
|
||||
("fast", 10),
|
||||
("slow", 30),
|
||||
)
|
||||
|
||||
def __init__(self):
|
||||
self.fast_ma = bt.indicators.SMA(self.data.close, period=self.params.fast)
|
||||
self.slow_ma = bt.indicators.SMA(self.data.close, period=self.params.slow)
|
||||
self.crossover = bt.indicators.CrossOver(self.fast_ma, self.slow_ma)
|
||||
|
||||
def next(self):
|
||||
if not self.position:
|
||||
if self.crossover > 0: # fast crosses above slow
|
||||
self.buy()
|
||||
elif self.crossover < 0: # fast crosses below slow
|
||||
self.close()
|
||||
Reference in New Issue
Block a user