(looked to submit this bug on github directly, but didn't see the option there. also didn't see any bug sections of forums, apologies if this isn't posted to the ideal spot...)
Bug: only 1 trade executes from code snippet on home page with a few reasonable modifications
Screenshot: https://imgur.com/a/bd2hUqi
Modifications:
- params = (('pfast', 10), ('pslow', 30),) change to ==> params = (('pfast', 50), ('pslow', 200),)
- data = bt.feeds.YahooFinanceData(dataname='MSFT', fromdate=datetime(2011, 1, 1), todate=datetime(2012, 12, 31)) change to ==> data = bt.feeds.YahooFinanceData(dataname='ESNT', fromdate=datetime(2014, 1, 1),
todate=datetime(2018, 12, 1)) - add setting: cerebro.addsizer(bt.sizers.AllInSizer)
Note:
- switching ticker symbol from 'ESNT' to 'SPY' over the same period makes zero trades despite several signal crossovers.
- switching ticker symbol to 'SPY' and using date range (2003,1,1) to (2018,12,1) makes a few trades, but also misses a few trades that should execute.
Full code to reproduce:
from datetime import datetime
import backtrader as bt
class SmaCross(bt.SignalStrategy):
params = (('pfast', 50), ('pslow', 200),)
def init(self):
sma1, sma2 = bt.ind.SMA(period=self.p.pfast), bt.ind.SMA(period=self.p.pslow)
self.signal_add(bt.SIGNAL_LONG, bt.ind.CrossOver(sma1, sma2))
cerebro = bt.Cerebro()
data = bt.feeds.YahooFinanceData(dataname='ESNT', fromdate=datetime(2014, 1, 1),
todate=datetime(2018, 12, 1))
cerebro.adddata(data)
cerebro.addsizer(bt.sizers.AllInSizer)
cerebro.addstrategy(SmaCross)
cerebro.run()
cerebro.plot()