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/

    How to convert strategy results to a dictionary without knowing parameter names

    General Code/Help
    3
    5
    205
    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.
    • S
      scottz1 last edited by

      After passing a dictionary of param values into the strategy (using **params in the call to optstrategy), and calling cerebro.run, the result includes a list of strategy objects that contain an object called backtrader.metabase.AutoInfoClass_LineRoot_LineMultiple_LineSeries_LineIterator_DataAccessor_StrategyBase_Strategy_MyStrategy. The per-instance parameter values are there, but that object doesn't support dict(), items(), or enumerate(). Is there some way the caller of cerebro.run can convert that to a dictionary without hard-coding parameter names so it can track which parameter set had the best performance?

      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        You can iterate through your strategies and extract analyzers for information. There are a number of built in analyzers as well you can custom make your own for your personal information needs.

        In the following example I have used variable parameters on 'fast' and 'slow'.

        I have used the transactions analyzer as follows:

        cerebro.addanalyzer(bt.analyzers.Transactions, _name="transactions")
        

        To go through the results and create a dictionary of the 'Transactions' analyzyer just loop through the strategies, extract your personal parameters for each strategy, make that a tuple which will be the key for the dictionary, then extract the analyzer data and make that the value.

        # Optimizing on 'fast' and 'slow'
        dct_transactions = {}
        for strat in results:
            param_id = (strat[0].params.fast, strat[0].params.slow)
            dct_transactions[param_id] = strat[0].analyzers.getbyname("transactions")
        

        The result will be a dictionary for each optimization as key and with an analyzer object as value. To view the contents of any analyzer object use .get_analysis. You could loop though the dictionary or just call a specific optimization level as below, fast: 11, slow: 20:

        dct_transactions[(11,20)].get_analysis()
        

        I used this method in Jupyter extensively to do further analysis. In particular I like to obtain: All transactions, Position each day, Total cash and market values.

        1 Reply Last reply Reply Quote 0
        • S
          scottz1 last edited by

          Thanks for posting, but that misses my question. I have a generic walkforward class that needs to get a dictionary of parameter names and values from the result of cerebro run. Because it's generic for any strategy, it can't hard-code parameter names like strat[0].params.slow.

          1 Reply Last reply Reply Quote 0
          • A
            ab_trader last edited by

            I was retrieving parameters from the strategy output after optimization run with the following code:

            optreturn = cerebro.run()
                
            for optsingle in optreturn:
                optresults = dict()
                for strat in optsingle:
                    optresults.update(dict(strat.params._getkwargs()))
            

            optresults variable is a dictionary of parameters for single optimization run.

            1 Reply Last reply Reply Quote 2
            • S
              scottz1 last edited by

              Thank you! _getkwargs looks like exactly what I need.

              1 Reply Last reply Reply Quote 1
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
              $(document).ready(function () { app.coldLoad(); }); }