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/

    self.env.runstop() when optstrategy results in strange behaviour

    General Code/Help
    2
    4
    85
    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.
    • monstrar
      monstrar last edited by

      Hi all,

      I run cerebro using cerebro.optstrategy(strategy, rsiparam=[20, 40, 60]). If I use a standard strategy without using self.env.runstop(), Cerebro runs 3 times as expected, one for each param. However, if I use self.env.runstop() in the strategy next() method once a certain condition is triggered, the following happens:

      • The current run with param (rsiparam=20) is stopped (expected behaviour)
      • The rsiparam=40 run is started, however is immediately stopped, start() and stop() methods are called, next() is not. (unexpected behaviour).
      • Rsiparam=60 same as rsiparam=40 run.
        Therefore, it seems that if I run self.env.runstop(), all the other optstrategy runs after are also stopped. How to fix this?

      Thanks!

      Kind regards,

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

        runstop() method just sets a special flag (_event_stop) in Cerebro engine (not a strategy - that's important) - nothing more. This flag is checked at multiple check points inside the Cerebro machinery during the live cycle of the strategy and if set - the engine stops the execution of its current strategy and if there is nothing else to do, just returns from its run method.

        Now for the scenario in question. When optstrategy is used - Cerebro runs multiple instances of the same strategy for each parameters permutation. However it may run all of those strategy instances using the same Cerebro engine instance or multiple Cerebro engine instances (in separate processes).

        The first case occurs only if you have a single CPU (or set the maxcpus parameter to 1). So , as mentioned above, the same Cerebro instance is used to run all those strategy instances. If some strategy calls the runstop() method, the Cerebro engine will eventually stop the execution of the strategy at the closest check point and will try to execute the next strategy. However the flag is still set - so the next strategy will be stopped at the first suitable check point as well - the same with the following strategies.

        The second case is much more complicated ( from the Cerebro engine perspective - not from user perspective) since now multiple Cerebro engines are instantiated (actually cloned or pickle-ized) in separate processes in order to execute the strategies in parallel. Here if runstop method is called within a single strategy - it will impact only the Cerebro engine in the same process - others should continue to execute.

        It seems that the behavior you have described matches the first case.

        monstrar 1 Reply Last reply Reply Quote 1
        • vladisld
          vladisld @monstrar last edited by

          @monstrar said in self.env.runstop() when optstrategy results in strange behaviour:

          How to fix this?

          I'm not sure I have a good advice here - probably others will help.

          However if your were able to detect the 'certain condition' during the strategy's __init__ method - the better option is to raise the StrategySkipError instead of calling the runstop method.

          1 Reply Last reply Reply Quote 0
          • monstrar
            monstrar @vladisld last edited by

            @vladisld

            Thank you for the detailed comment. You are correct, I am running cerebro.run(maxcpu=1) so the first case you described happened for me. I will try rasing an exception instead as you recommended, thanks!

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