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/

    Same entry, different exits

    Indicators/Strategies/Analyzers
    3
    6
    279
    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.
    • oakenstream
      oakenstream last edited by

      Hey,

      this might be a bit hard to explain, but I would like to test a strategy with the same buying condition but with varying sellling conditions. Like sell when < 200ma or sell at first profit or after 7 days.

      Do anyone have a suggestion how to do that. Would I write different strategies for each one and then run them with a shell script to compare or could I modularize the exit-criteria somehow to not have so much duplicate code?

      I hope I could explain it in a good way.

      Regards

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

        If you don't want to put several strategies into the system, then you can write single strategy with exit type parameter. Based on this parameter you can run different pieces of code in the next() or __init__() based on if self.p.param == something statements.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 1
        • oakenstream
          oakenstream last edited by

          I understand that sounds very reasonable of course.

          I'm thinking along these lines: I want to keep a bunch of "exits" in a folder somewhere and then I can combine the current entry with those exits. If this could be modularized I dont have to paste in 15 exits into each strategy class.

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

            @oakenstream as an idea - setup separate class. Methods of the class will be your exit functions. Then you can pass these methods as parameters into strategy.

            • If my answer helped, hit reputation up arrow at lower right corner of the post.
            • Python Debugging With Pdb
            • New to python and bt - check this out
            1 Reply Last reply Reply Quote 2
            • B
              backtrader administrators last edited by

              With OOP the obvious approach is having a base class which defines the entry setup (invoked for example with method name check_and_enter.

              That same class defines the logic to call another method for the strategy, for example: check_and_exit.

              Subclasses of the base class have all the same entry method, but override and have different logics for the check_and_exit method.

              1 Reply Last reply Reply Quote 2
              • oakenstream
                oakenstream last edited by

                Ok, thanks both! I will experiment with this approach.

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