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

I am struggling to save my backtrader optimizer results(params and analyzers) into a CSV file



  • Hello, I am trying to run a back trader strategy and right now working on optimizing it. Even after referring to https://community.backtrader.com/topic/174/create-a-csv-of-the-result-of-optstrategy/8 I find it difficult to print(and save to csv) the params and analyzers The problem I face is that after using the following code,

    def stop(self):
        self.cash_start = self.broker.get_value()
        self.cash_final = self.broker.get_cash()
        self.draw_down = self.analyzers.drawdown.get_analysis()
    
        self.log(f"Period: {self.p.period}, Abs Margin is {self.p.abs_margin}, trailstop is {self.p.trail_stop}, Vol_Max is {self.p.Vol_Max} and the final value is {self.broker.getvalue()}", doprint=True)
    
    def main():
       ..
       ..
       ..
        strats = cerebro.optstrategy(
            strategy_name, period = [10, 100, 100], ........ 
            )
    
        workers = cerebro.run(maxcpus=48)  
    
        all_strategies = [r[0] for r in workers]
    
        all_results = [str(s.cash_start) for s in all_strategies]
        print(all_results)
    
    

    But the issue is that I get an Attribute error that OptRetur object has no attribute 'cash_start'. The error is as follows and can someone please help me figure out where am I going wrong??

    AttributeError: 'OptReturn' object has no attribute 'cash_start'
    


  • please go over the following docs:

    https://www.backtrader.com/docu/cerebro/#returning-the-results
    https://www.backtrader.com/docu/optimization-improvements/#results-management

    By default the optreturn Cerebro parameter is set to True - meaning that run method will not return the list of Strategy objects but a stripped down OptReturn objects containing only the original strategy parameters and analyzers.

    Quoting the description of the optreturn parameter:

    ``optreturn`` (default: ``True``)
    
            If ``True`` the optimization results will not be full ``Strategy``
            objects (and all *datas*, *indicators*, *observers* ...) but and object
            with the following attributes (same as in ``Strategy``):
    
              - ``params`` (or ``p``) the strategy had for the execution
              - ``analyzers`` the strategy has executed
    
            In most occassions, only the *analyzers* and with which *params* are
            the things needed to evaluate a the performance of a strategy. If
            detailed analysis of the generated values for (for example)
            *indicators* is needed, turn this off
    

    The best way of getting the optimization statistics is to use the analyzer objects that will collect and report such statistics. There are two benefits using the analyzers :

    • Modular and easy to understand design
    • Some performance benefits (up to 15% according to the docs ) in execution time.


  • @vladisld said in I am struggling to save my backtrader optimizer results(params and analyzers) into a CSV file:

    By default the optreturn Cerebro parameter is set to True - meaning that run method will not return the list of Strategy objects but a stripped down OptReturn objects containing only the original strategy parameters and analyzers.

    Hey, Thank you so much for your response. This totally solved the errors I was facing



  • @vladisld said in I am struggling to save my backtrader optimizer results(params and analyzers) into a CSV file:

    The best way of getting the optimization statistics is to use the analyzer objects that will collect and report such statistics. There are two benefits using the analyzers :

    Modular and easy to understand design
    Some performance benefits (up to 15% according to the docs ) in execution time.

    What about this? Can you refer me to an example regarding this? I mean the analyzer objects which were used in some code or something? I am sorry but looking at an example helps me understand things better. Thank you for your time :)


Log in to reply
 

});