2019년 9월 17일 화요일

backtrader 백테스트

python 백테스트

https://www.backtrader.com/

https://github.com/backtrader/backtrader

https://backtest-rookies.com/

를 통하여 backtest 해보기...


1. 설치

pip install backtrader


2. tutorial



import backtrader as bt
from datetime import datetime

class SmaCrossSignal(bt.SignalStrategy):
    # list of parameters which are configurable for the strategy
    params = dict(
        pfast=10,  # period for the fast moving average
        pslow=30   # period for the slow moving average
    )

    def __init__(self):
        sma1 = bt.ind.SMA(period=self.p.pfast)  # fast moving average
        sma2 = bt.ind.SMA(period=self.p.pslow)  # slow moving average
        crossover = bt.ind.CrossOver(sma1, sma2)  # crossover signal
        self.signal_add(bt.SIGNAL_LONG, crossover)  # use it as LONG signal



if __name__ == "__main__":

    cerebro = bt.Cerebro()

    # Add a strategy
    cerebro.addstrategy(SmaCrossSignal)

    # Create a data feed
    data = bt.feeds.YahooFinanceData(dataname='MSFT',
                                 fromdate=datetime(201111),
                                 todate=datetime(20121231))

    cerebro.adddata(data)

    # Run over everything
    cerebro.run()

    # Plot the result
    cerebro.plot(style='candlestick')





댓글 없음:

댓글 쓰기