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/

    Can we create Strategies with Dynamic Indicators with 100% declarative in the __init__ method?

    General Code/Help
    2
    3
    35
    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.
    • J
      Jensen Yan last edited by

      I am a bt newbie and I follow the examples in https://www.backtrader.com/blog/posts/2018-02-06-dynamic-indicator/dynamic-indicator/ .

      I really appreciate the beauty and elegance of being 100% declarative in the init method mentioned here https://www.backtrader.com/blog/2019-07-08-canonical-or-not/canonical-or-not/ . But something go wrong when I include the dynamic variable.

      I try to rewrite
      case1:604109e1-9991-4fa4-a483-f70672908812-image.png into
      case2:815d7028-55b8-48c6-8cba-4b9075e31387-image.png
      Case1 was successfully run but the running result was wrong in case2 even I have already set runonce = False . I don't know what's happening. And I am looking for a way that we can minimize the logic checking in next( ) method .

      any help is appreciated. Thank you.

      1 Reply Last reply Reply Quote 0
      • J
        Jensen Yan last edited by

        sorry, I made some mistakes .The code should be something like this.

        #case1
        class MyStrategy(bt.Strategy):
            def __init__(self):
                self.dyn = MyDynamicIndicator()
        
        def notify_trade(self, trade):
            xxxxxx
        
        def next(self):
            if self.dyn:
                print('ABOUT TO DO SOMETHING')
        
        #case2
        class MyStrategy(bt.Strategy):
            def __init__(self):
                self.dyn = MyDynamicIndicator() > 100
        
            def notify_trade(self, trade):
               xxxxx
        
            def next(self):
                if self.dyn:
                    print('ABOUT TO DO SOMETHING')
        1 Reply Last reply Reply Quote 0
        • A
          ab_trader last edited by

          You are not creating dynamic indicator, since it doesn't depend on the strategy execution. You just compare two lines: self.dyn line and line which has 100 as a value. This can be done like this:

              def __init__(self):
                  self.dyn = MyDynamicIndicator()
                  self.line100 = bt.LineNum(100.0)
                  self.compare = self.dyn.indicator_line_which_is_unknown_but_missed_here > self.line100
          

          If you want to develop an indicator which depends on the strategy execution results like order execution, trade open/closed, than you will need next().

          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(); }); }