Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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

    Indicators/Strategies/Analyzers
    1
    1
    39
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Xavier Escudero
      Xavier Escudero last edited by

      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}')      
      
      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
      $(document).ready(function () { app.coldLoad(); }); }