strategy.py
lint 通过
# 动量突破策略 — 继承 StrategyBase,沙箱执行
from strategy_sdk import StrategyBase, register
@register("动量突破")
class Momentum(StrategyBase):
def __init__(self, lookback=20, size=5, band=1.5):
self.lookback, self.size, self.band = lookback, size, band
def on_bar(self, ctx):
px = ctx.history("close", self.lookback)
if len(px) < self.lookback: return
hi, lo = max(px), min(px)
last = ctx.price()
if last >= hi:
ctx.target_position(self.size) # 突破上轨 → 做多
elif last <= lo:
ctx.target_position(-self.size) # 跌破下轨 → 做空