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

Strategy or Analyzer init - Avoid re-execution if used again



  • Hi. I want to store in memory data of several stocks data with computed indicators, so they are reused in several executions.

    I've tried to use the method init in one analyzer/strategy, but everytime I run the cerebro the 'init' method is started again, and it spends a lot of time (15 seconds with two indicators, and 1440 stocks. I've read in posts that cerebro can't be reused.

    There's anyway to load the indicators once, and reuse them for every execution?

    class ScreenerAnalyzer(bt.Analyzer):
        params = dict(period=10)
        
        def __init__(self):
            print('INIT----------')
            
            self.rets['over'] = list()
            self.rets['under'] = list()
    
            self.smas = {data: bt.indicators.SMA(data, period=self.p.period)
                         for data in self.datas}
            self.atrs = {data: bt.indicators.ATR(data, period=14)
                         for data in self.datas}
    
     def execute_analysis(self):
            cerebro = bt.Cerebro()
            for i in range(len(self.data_list)):            
                data = PandasData(
                    dataname=self.data_list[i][0], # Pandas DataFrame
                    name=self.data_list[i][1] # The symbol
                    )            
                cerebro.adddata(data)
    
            cerebro.addanalyzer(ScreenerAnalyzer, period=10)
            
            start_time = timeit.default_timer()
            cerebro.run(runonce=False, stdstats=False, writer=False)
            elapsed = timeit.default_timer() - start_time
            print(f'analysis: {elapsed}')      
    

Log in to reply
 

});