Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Hello Algotrading!

A classicSimple Moving Average Crossover strategy, can be easilyimplemented and in different ways. The results and thechart are the same for the three snippets presented below.

image

fromdatetimeimportdatetimeimportbacktraderasbt# Create a subclass of Strategy to define the indicators and logicclassSmaCross(bt.Strategy):# 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 averageself.crossover=bt.ind.CrossOver(sma1,sma2)# crossover signaldefnext(self):ifnotself.position:# not in the marketifself.crossover>0:# if fast crosses slow to the upsideself.buy()# enter longelifself.crossover<0:# in the market & cross to the downsideself.close()# close long positioncerebro=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
fromdatetimeimportdatetimeimportbacktraderasbt# Create a subclass of Strategy to define the indicators and logicclassSmaCross(bt.Strategy):# 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 averageself.crossover=bt.ind.CrossOver(sma1,sma2)# crossover signaldefnext(self):ifnotself.position:# not in the marketifself.crossover>0:# if fast crosses slow to the upsideself.order_target_size(target=1)# enter longelifself.crossover<0:# in the market & cross to the downsideself.order_target_size(target=0)# close long positioncerebro=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
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

[8]ページ先頭

©2009-2025 Movatter.jp