23 lines
676 B
Python
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()
|