For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Why is backtesting speed very slow?



  • This is my code :

    class SmaCross(bt.SignalStrategy):
        def __init__(self):
            sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30)
    
    cerebro = bt.Cerebro()
    cerebro.addstrategy(SmaCross)
    
    dataframe = pd.read_csv(r'D:\aaa.csv', index_col=0, parse_dates=True)
    print(dataframe)
    
    dataframe['openinterest'] = 0
    data = bt.feeds.PandasData(dataname=dataframe,
                            fromdate = datetime(2015, 1, 1),
                            todate = datetime(2016, 12, 31)
                            )
    cerebro.adddata(data)
    start = datetime.now()
    cerebro.run()
    stop = datetime.now()
    print(stop-start)
    cerebro.plot()
    

    The total processing time is 15 mins 50 sec ,
    What settings should I make to speedup ?

    Thanks.


  • administrators

    @zeroc said in Why is backtesting speed very slow?:

    What settings should I make to speedup ?

    • Probably you want to reduce your dataset to the actual data you are processing

    If not, then Python is the wrong choice for you.



  • @backtrader
    Thanks reply ,
    I find this thread how-to-speed-up-backtest

    the dataset he used is 6 months , and he takes 96 seconds to run , but my dataset is one years and taks 15 mins . that's crazy...

    I don't know what caused this difference in time.


  • administrators

    @zeroc said in Why is backtesting speed very slow?:

    the dataset he used is 6 months , and he takes 96 seconds to run , but my dataset is one years

    To start with you have a start and end dates which are 2 years apart and not 1 year.

    @zeroc said in Why is backtesting speed very slow?:

    data = bt.feeds.PandasData(dataname=dataframe,
                            fromdate = datetime(2015, 1, 1),
                            todate = datetime(2016, 12, 31)
                            )
    

    But the fact that those dates are 2 years apart doesn't mean that the actual source dataset may not contain 100 years.

    Furthermore, even if your dataset is 2 years long, it may contain 1-second bars, which doesn't yield the same numbers of bars as if you have 1-day

    Running through 2 years of daily bars is a decent computer with 2 simple moving averages takes well under 1 minute.