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/

    Optstrategy Problem

    General Code/Help
    2
    5
    1062
    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.
    • Developing Coder
      Developing Coder last edited by Developing Coder

      I'm having problems with using cerebro.optstrategy().

      1. Can't use multi-CPU computations. cerebro.run() only works with maxcpus=1.

      2. It doesn't give me the nice printout which can be seen in the end of Quickstart Guide.

      The rest I can live with but next one is really important.

      1. I used optstrategy() like this.
      cerebro.optstrategy(
          TrendStrategy,
          trend_period=range(5, 7)
      )
      

      Then attempted to peek at the results like this.

      strats = cerebro.run(maxcpus=1)
      
      for s in strats:
          print(s[0].p.trend_period)
          print(s[0].broker.get_value())
      

      But the output isn't correct.

      5
      29964.1648
      6
      29964.1648
      

      get_value() seems to return the value from last run for each run. If I change params = (('trend_period', 5), ) manually then cerebro.broker.getvalue() does indeed return different values for 5 and 6.

      Thanks!

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

        The broker is unique to the system. You need to keep the value in the strategy itself. For example in stop

        @developing-coder said in Optstrategy Problem:

        Can't use multi-CPU computations. cerebro.run() only works with maxcpus=1.

        This is probably your own fault, because you run under Windows in one of those super nice Python hijacking IDEs which destroy the multiprocessing module.

        @developing-coder said in Optstrategy Problem:

        It doesn't give me the nice printout which can be seen in the end of Quickstart Guide.

        And which printout do you get?

        1 Reply Last reply Reply Quote 0
        • Developing Coder
          Developing Coder last edited by

          The broker is unique to the system. You need to keep the value in the strategy itself. For example in stop

          OK, clear!

          This is probably your own fault, because you run under Windows in one of those super nice Python hijacking IDEs which destroy the multiprocess module.

          You are right, I am using PyCharm on Windows. Wouldn't call it exactly a fault tho. PyCharm is a valuable tool and widely used. I've used multiprocessing module in the past without issues.

          Tried running the script via cmd and still same problem. Maybe it's happening because I use a virtual env?

          And which printout do you get?

          Nothing, just the stuff I print from inside strategy.

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

            @developing-coder said in Optstrategy Problem:

            You are right, I am using PyCharm on Windows. Wouldn't call it exactly a fault tho. PyCharm is a valuable tool and widely used. I've used multiprocessing module in the past without issues.

            Some things work, some doesn't. But it is a well known issue. See this for example: https://stackoverflow.com/questions/48078722/no-multiprocessing-print-outputs-spyder?noredirect=1&lq=1

            1 Reply Last reply Reply Quote 0
            • Developing Coder
              Developing Coder last edited by

              Yeah, in my case it doesn't even work when running directly on command line tho.

              Stumbled across this, might be related.

              https://github.com/kernc/backtesting.py/issues/5

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