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/

    comparing indicator lines with strategy parameters

    General Code/Help
    2
    4
    306
    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.
    • Wayne Filkins 0
      Wayne Filkins 0 last edited by

      I created 2 buy conditions in the strategy init function.

              self.buyCondition1 = bt.ind.CrossOver(self.kst, self.p.zeroLine)
              self.buyCondition2 = bt.ind.CrossOver(self.kst, self.p.rsignal)
      

      self.kst is just a line in an indicator, so it's like an sma line, so this code is working for me. I use only one of these buy conditions, but have tested both and they both work. From what I read in the docs, it's better to keep this sort of thing in init() rather than next() for better performance.

      So I'm trying to do a 3rd condition to test out, but it involves comparing self.kst to a strategy parameter, self.p.overSold, or self.p.overBought. This doesn't seem to work. I imagine I can get it working in next(), but my question is, is there a proper way to do this in Init() ? Should I use a parameter from kst instead, or should I just do all of this in the kst indicator file? I'm sure if I continue to mess around with it I'll get it working, but I am trying to learn to make everything run as fast as possible. This is the code I tried for the 3rd buy condition:

      if self.kst <= self.p.overSold:
                  self.buyCondition3 = 1
              elif self.kst >= self.p.overBought:
                  self.buyCondition3 = -1
              else:
                  self.buyCondition3 = 0
      

      self.p.overBought and self.p.overSold are just 50 and -50

      Wayne Filkins 0 1 Reply Last reply Reply Quote 0
      • Wayne Filkins 0
        Wayne Filkins 0 @Wayne Filkins 0 last edited by

        on the 2nd code block the indentation looks odd but it's correct in the actual project

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

          Create line object using bt.LineNum and than you can compare it with the self.kst using bt.If.
          Links -
          Blogs - Crossing over numbers
          Docs - Platform Concepts - Some non-overriden operators/functions

          • 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
          Wayne Filkins 0 1 Reply Last reply Reply Quote 0
          • Wayne Filkins 0
            Wayne Filkins 0 @ab_trader last edited by

            @ab_trader Looks like the "if" was the problem. Didn't realize I had to use bt.If in init(). Used lineNum too but not sure if I had to, I'll just leave it though. Thanks!

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