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/

    Early stopping of backtesting

    General Code/Help
    early stopping cancel stop
    3
    5
    2050
    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.
    • H
      holicst last edited by

      Hi Everybody,

      I'd like to use the multiprocessing feature of cerebro with optstrategy(). However, I'd like to stop backtesting, if a strategy performs poorly. So for example, if the initial cash is 100.000, then I want to stop if the total portfolio value drops below 50.000. Or maybe check if the performance after every N bars is above 90% etc.

      Is there a feature like this already? I did some research but have not found anything. (Actually this is independent of using addstrategy or optstrategy)

      Thanks and best regards,
      Tamás

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

        Docs - Cerebro - See method: runstop.

        Docs - Strategy - See attribute: env

        H 1 Reply Last reply Reply Quote 0
        • H
          holicst @backtrader last edited by

          @backtrader
          Many thanks!

          1 Reply Last reply Reply Quote 0
          • Андрей Музыкин
            Андрей Музыкин last edited by Андрей Музыкин

            I just want to add some (maybe trivial) clarifications:

            Since MyStrategy(bt.Strategy) is not instantiated until actual run, neither env attribute is.
            Meanwhile calling for self.env of actual strategy instance (while running backtesting) is ok for defining early stop method:

            class MyStrategy(bt.Strategy):
            ...
               def early_stop(self):
                  self.env.runstop()
                  print('Early stop envoked!')
               
               def next(self):
                  ...
                  if <early stop condition>: 
                     self.early_stop()
                  ...
                  <some other next() computations>
            

            Note: even if early_stop() is called somewhere inside next() body, it will not terminate imidiately, i.e. <some other next() computations> above still be executed once.

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

              You could always do:

                    if <early stop condition>: 
                       self.early_stop()
                       return
              

              No further calculations will run after return

              The original poster asked how to stop during the calculations and not during instantiation (__init__). To do so see:

              • Docs - Exceptions
              • Community - How to check the legality of parameters before running the test?
              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors