Files
AITrading/python-app/app/strategies/sma_cross.py
2026-03-26 14:13:44 +08:00

23 lines
676 B
Python

from __future__ import annotations
import backtrader as bt
class SmaCrossStrategy(bt.Strategy):
params = (
("fast", 3),
("slow", 8),
("stake", 1),
)
def __init__(self) -> None:
self.fast_sma = bt.indicators.SMA(self.data.close, period=self.params.fast)
self.slow_sma = bt.indicators.SMA(self.data.close, period=self.params.slow)
self.cross_signal = bt.indicators.CrossOver(self.fast_sma, self.slow_sma)
def next(self) -> None:
if not self.position and self.cross_signal > 0:
self.buy(size=self.params.stake)
elif self.position and self.cross_signal < 0:
self.close()