For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Indicator development - missing a key piece

  • 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 = - *
    y = - *

    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.

  • never mind, solved by using next

  • administrators

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

    x = - *
    y = - *
    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

  • 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.

Log in to reply