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/

    Indicator development - missing a key piece

    General Code/Help
    2
    4
    2294
    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.
    • C
      cemdev last edited by cemdev

      I'm still not sure when to use init and when to use next.

      The indicator I'm trying to build is simple.

      it's effectively: (i'm making up the actual formula, as this one is proprietary, but it's all regular variables off the data object)

      if the close > last close then x, if close < last close then y, if close == lastclose then 0

      effectively, what i want is:

      x = self.data.close - self.data.close(-1) * self.data.high
      y = self.data.close(-1) - self.data.close * self.data.high

      self.lines.myindicator = x if close > close(-1) else y if close < close(-1) else 0

      but that throws an error, and i'm really not sure where to start, there's so much about the indicators I don't really get at this point.

      bool should return bool, returned LineOwnOperation

      effectively - can I run boolean operations in init? if so, how? if not - then I guess I have to do this in next - but I run into trouble there too.

      1 Reply Last reply Reply Quote 0
      • C
        cemdev last edited by

        never mind, solved by using next

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

          @cemdev said in Indicator development - missing a key piece:

          x =  self.data.close - self.data.close(-1) * self.data.high
          y =  self.data.close(-1) - self.data.close * self.data.high
          self.lines.myindicator =  x if close > close(-1) else y if close < close(-1) else 0
          

          (Use ``` to quote code blocks)

          The above is almost ok for __init__. The problem being that not everything in python can be overridden and if ... elif ... else is one of those things. One needs to use backtrader.If (capital I to distinguish it even with from backtrader import *)

          See here: Docs - Platform Concepts, specifically the section Some non-overriden operators/functions

          With that in mind

          self.lines.myindicator =  bt.If(close > close(-1), x, bt.If(close < close(-1), y, 0))
          

          Which effectively says: if close > close(-1) then x elif close < close(-1) then y else 0

          1 Reply Last reply Reply Quote 0
          • C
            cemdev last edited by cemdev

            oh, that is EXCELLENT. Many thanks for the responses, especially on a sunday. i have it working with next, but i'll refactor with this, seems much cleaner.

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