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/

    Detecting if we are running an Optimization from within a Strategy

    Indicators/Strategies/Analyzers
    2
    5
    798
    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.
    • vbs
      vbs last edited by

      I am looking for a way inside a strategy to detect if we a running a regular backtest or if we are running an optimization. Is this possible?

      The background is that I would like to do logging only in regular backtest runs. During (multiprocessor) optimization runs there should be no logging (it would not be properly readable anyway, right)?

      So my function log would start with something like this:

      if is_opt:
          return
      

      One workaround idea would be to use regular python logging module in the strategy and to disable the logger from outside before starting the optimization run.

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        Put a parameter is_opt in the strategy and set it to True when invoking optstrategy

        1 Reply Last reply Reply Quote 0
        • vbs
          vbs last edited by

          Ok, thanks, thats another solution. But I thought it would be nice to make that parameter not part of the regular strategy parameter set. Mainly because I would prefer if that parameter would not appear as normal parameter in my result evaluation scriptings.

          I went for using this:

                  if self.cerebro._dooptimize:
                      return
          

          I know _dooptimize is not part of the official API and might break anytime but I think it is ok for now ofr my use case. Thanks!

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            Subclass Cerebro, add an attribute which you set to True when optstrategy is called (obviously you override that method) and you won't have to depend on an internal value.

            1 Reply Last reply Reply Quote 0
            • vbs
              vbs last edited by

              Ok, thanks alot!

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors