資料來源 :
https://github.com/backtrader/backtrader/blob/master/backtrader/strategies/sma_crossover.py
import backtrader as bt
import pandas as pd
import datetime
import backtrader.indicators as btind
class TestStrategy(bt.Strategy):
def log(self, txt, dt=None):
''' Logging function fot this strategy'''
dt = self.datas[0].datetime.date(0)
print('%s, %s' % (dt.isoformat(), txt))
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
# Buy/Sell order submitted/accepted to/by broker - Nothing to do
return
# Check if an order has been completed
# Attention: broker could reject order if not enough cash
if order.status in [order.Completed]:
if order.isbuy():
self.log('BUY EXECUTED, %.2f' % order.executed.price)
elif order.issell():
self.log('SELL EXECUTED, %.2f' % order.executed.price)
self.bar_executed = len(self)
elif order.status in [order.Canceled, order.Margin, order.Rejected]:
self.log('Order Canceled/Margin/Rejected')
# Write down: no pending order
self.order = None
alias = ('SMA_CrossOver',)
params = (
# period for the fast Moving Average
('fast', 10),
# period for the slow moving average
('slow', 30),
# moving average to use
('_movav', btind.MovAv.SMA)
)
def __init__(self):
self.dataclose = self.datas[0].close
self.order = None
sma_fast = self.p._movav(period=self.p.fast)
sma_slow = self.p._movav(period=self.p.slow)
self.buysig = btind.CrossOver(sma_fast, sma_slow)
def next(self):
self.log('Close, %.2f' % self.dataclose[0])
if self.position.size:
if self.buysig < 0:
self.log('SELL CREATE, %.2f' % self.dataclose[0])
self.sell()
elif self.buysig > 0:
self.log('BUY CREATE, %.2f' % self.dataclose[0])
self.buy()
if __name__ == '__main__':
cerebro = bt.Cerebro()
cerebro.addstrategy(TestStrategy)
cerebro.broker.setcash(100000.0)
dataframe = pd.read_csv('d://0050.tw.csv', index_col=0, parse_dates=True)
dataframe['openinterest'] = 0
data0 = bt.feeds.PandasData(dataname=dataframe,
fromdate = datetime.datetime(2008, 1, 1),
todate = datetime.datetime(2010, 1, 1)
)
cerebro.adddata(data0)
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.run()
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.plot()
執行結果長這樣
留言列表