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

help with indicators mark signals based on future data

  • hey,
    I am trying to build a strategy with indicators which do calculation and signal their own line in the "past". For example, I build a swing indicator (found it here in some question) which look like this:

    class SwingInd(bt.Indicator):
        lines = ('swingline',)
        params = (('period', 5),)
        def __init__(self):
            self.swing_range = (self.params.period * 2) + 1
            self.mean_range = self.params.period
            # this is needed to tell when there is enough data to start signaling
        def next(self):
            # crude swing up
            highs =
            lows =
            if highs.pop(self.mean_range) >= max(highs):
                self.lines.swingline[-self.mean_range] = 1
            elif lows.pop(self.mean_range) <= min(lows):
                self.lines.swingline[-self.mean_range] = -1
                self.lines.swingline[-self.mean_range] = 0

    and I see that its mark the self.lines.swingline[-self.mean_range] in the "past" with a val.
    in my strategy I am looking at this indicator in the 'next' function with data[0] (current) and check if should I buy/sell based on

    if self.swing1[0] == -1/1:
        buy ....

    but in order to self.swing1[0] to have values of 1/-1 it needs to have future data already in order to mark current as 'swing'. Am I wrong ? How should I use this indicators that need 'future' data to signal in the current 'next' ?

  • administrators

    That's not an indicator. The concept is called "Study" because it analyzes the present to see what happened in the past and depict things accordingly.

    Furthermore, the title you have chosen for the post should say it all:

    @יהודה-שפירא said in help with indicators mark signals based on future data:

    help with indicators mark signals based on future data

    No, you cannot access at [0] what has yet to be calculated in the future (because the future will place things in the past)

  • so, it I change

    self.lines.swingline[-self.mean_range] = -1


    self.lines.swingline[0] = -1

    it is "valid" indicator (will signal in the current date about what happened in the past). no?

  • administrators

    Everything is a valid indicator. I simply said the "concept" is different because it is studying what has happened and not what is "actually" happening.

    You may of course signal at instant [0] and react accordingly, but the event will still have happened in the past.

Log in to reply