fd86190e9a
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
24 lines
774 B
Python
24 lines
774 B
Python
"""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()
|