#量化分析入门10:backtrader的策略入门2(策略示例)
#作者:冯德平(山野雪人)
#这里是一个简单的移动平均线交叉的片段(以股票万科为例)。
from datetime import datetime
import backtrader as bt
class SmaCross(bt.SignalStrategy):
def __init__(self):
sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30)
crossover = bt.ind.CrossOver(sma1, sma2)
self.signal_add(bt.SIGNAL_LONG, crossover)
cerebro = bt.Cerebro()
cerebro.addstrategy(SmaCross)
data0 = bt.feeds.YahooFinanceData(dataname='000002.csv', fromdate=datetime(2020, 11, 1),
todate=datetime(2025,12,31))
#结束时间2025年12月31日或更长时间,它只取结束时间为现在的时间
cerebro.adddata(data0)
cerebro.run()
cerebro.plot()
#https://cloud.tencent.com/developer/article/1820100
#https://github.com/mementum/backtrader
#https://blog.csdn.net/ndhtou222/article/details/105236618