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

analyzers erro: AttributeError: 'list' object has no attribute 'analyzers'



  • Hello! When studying this library, I ran into a problem.

    I do everything on the lesson of trade analysis. But I get an error.

    My cod:

    cerebro = bt.Cerebro(stdstats=False)
        data = PandasData(dataname=df, timeframe=bt.TimeFrame.Minutes)
        cerebro.addanalyzer(TradeAnalyzer, _name="ta")
    
        cerebro.adddata(data)
        cerebro.optstrategy(Strategy)
    
        thestrats = cerebro.run()
        strat = thestrats[0]
    
        print(strat.analyzers.ta.get_analysis())
    

    error:

    print(strat.analyzers.ta.get_analysis())
    AttributeError: 'list' object has no attribute 'analyzers'
    

    What is the problem?



  • IIRC if optstrategy is used, the 'run' method will return a list of lists. Quoting the docs ( 'Returning the results' section of the cerebro section https://www.backtrader.com/docu/cerebro/):

    Returning the results
    
    cerebro returns the instances of the strategies it created during backtesting. This allows to analyze what they did, because all elements in the strategies are accessible:
    
    result = cerebro.run(**kwargs)
    
    The format of result returned by run will vary depending on whether optimization is used (a strategy was added with optstrategy):
    
        **All strategies added with addstrategy**
    
        result will be a list of the instances run during the backtesting
    
        **1 or more strategies were added with optstrategy**
    
        result will be a list of list. Each internal list will contain the strategies after each optimization run
    ``


  • @vladisld Thanks a lot!


Log in to reply
 

});