For a buy only and exit strategy, when I run the following code I am able to see the chart and data and the indicator. However it doesnt appear to run any trades. Is this because I need trade analyzers. I did this with a strategy before and it worked as designed. But when I changed the data to intraday it gave me loads of problems.
I end up with the following.
import backtrader as bt
from datetime import datetime
class SmaCross(bt.Strategy):
params = dict(
pfast=10,
pslow=30
)
def __init__(self):
sma1 = bt.ind.SMA(period=self.p.pfast)
sma2 = bt.ind.SMA(period=self.p.pslow)
self.crossover = bt.ind.CrossOver(sma1, sma2)
def next(self):
if not self.position:
if self.crossover > 0:
self.buy()
elif self.crossover < 0:
self.close()
In[8]:
data = bt.feeds.GenericCSVData(
dataname="test.csv",
dtformat='%Y-%m-%d %H:%M:%S',
open=1,
high=2,
low=3,
close=4,
volume=5,
fromdate=datetime(2020, 1, 1),
todate=datetime(2021, 11, 4)
)
In[9]:
cerebro = bt.Cerebro()
cerebro.adddata(data)
cerebro.addstrategy(SmaCross)
cerebro.run()
cerebro.plot(style='Candlestick')