fromdatetimeimportdatetimeimportbacktraderasbt# Create a subclass of SignaStrategy to define the indicators and signalsclassSmaCross(bt.SignalStrategy):# list of parameters which are configurable for the strategyparams=dict(pfast=10,# period for the fast moving averagepslow=30# period for the slow moving average)def__init__(self):sma1=bt.ind.SMA(period=self.p.pfast)# fast moving averagesma2=bt.ind.SMA(period=self.p.pslow)# slow moving averagecrossover=bt.ind.CrossOver(sma1,sma2)# crossover signalself.signal_add(bt.SIGNAL_LONG,crossover)# use it as LONG signalcerebro=bt.Cerebro()# create a "Cerebro" engine instance# Create a data feeddata=bt.feeds.YahooFinanceData(dataname='MSFT',fromdate=datetime(2011,1,1),todate=datetime(2012,12,31))cerebro.adddata(data)# Add the data feedcerebro.addstrategy(SmaCross)# Add the trading strategycerebro.run()# run it allcerebro.plot()# and plot it with a single command