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

Indicator for yesterday's close

  • I'm looking to develop an indicator for yesterday's close primarily so that I can see it on my charts. I've written it like this:

    class LastClose(bt.Indicator):
        lines = ('close',)
        plotinfo = dict(subplot = False)
        def next(self):
            self.lines.close[0] =[-1]

    And in the strategy:

    def __init__(self):
        self.__close = LastClose(self.getdatabyname('daily'))

    It appears to work correctly, but I'm wondering if there is a better way to show it on my charts. Primarily, I want to show it on a lower timeframe chart as opposed to daily but I'm not sure what the best way to do it would be.


  • This is what I ended up doing:

    class LevelIndicator(bt.Indicator):
        lines = ('level',)
        plotinfo = dict(subplot = False)
        def __init__(self):
            self.level = None
        def next(self):
            if self.level is not None:
                self.lines.level[0] = self.level
    class MyStrategy(bt.Strategy):
       def __init__(self):
          self.__close = LastClose(self.data1)
          self.__closeForPlot = LevelIndicator(self.data0) # lower timeframe
       def next(self):
         self.__closeForPlot.lines.level[0] = self.__close.lines.close[0]

    Seems kind of obtuse, but if there is a more intelligent approach, please let me know.

  • Let me ask ... just to see if I have been able to understand your worries:

    • You want to record the last close on a 1-day data feed

    • You want to use the recorded last close value as an indicator (for visual or calculation purposes) in a (for example) 5-minutes data feed.

    Is that right? Be it so ... isn't this something like the PivotPointIndicator?

  • @Paska-Houso

    Yes, I think you have the broad strokes. It does sound like PivotIndicator but I am unable to figure out how to use it to JUST plot the close, because it uses high/low and has all the other support lines as well.

  • I didn't mean it was actually the PivotPointIndicator, but something along the same lines (I need to refresh what it does by reading) in which data from one timeframe is used (not only plotted) in a different timeframe

  • @Paska-Houso Ah, ok. I will give it a closer look then, thanks!

  • After quickly re-reading the doc, I believe this is what you need

    The PivotPoint indicator takes the value from the monthly close and the indicator is adapted (coupled in the doc's jargon) to the daily timeframe.

    You probably want to try your luck by modifying the PivotPoint to simply generate the close line as output.

  • @Paska-Houso I will try that out, thanks!

  • @Cheez try this code:

    class LastClose(bt.Indicator):
        lines = ('close',)
        params = (('period', 0),)
        def __init__(self):
            self.lines.close =


    class MasterStrategy(bt.Strategy):
        def __init__(self):
            self.lclose = lclose = LastClose(self.datas[1])
            lclose.plotinfo.subplot = False
            lclose1 = lclose()
            lclose1.plotinfo.subplot = False


    data0 - smaller timeframe
    data1 - higher timeframe (resampled from data0)

  • @ab_trader : Impressive.

    Even if I have worked with the platform and I thought that PivotPoint was the right direction, the simplicity of your solution (empowered by the underlying mechanics) has really amazed me.

  • @Paska-Houso
    Thank you! But these are DRo's ideas from time frame mixing section. My python skills are not that cool.

  • @ab_trader ooh very nice! Does data1 need to be resampled from data0 for this to work or are different timeframes enough?

  • @Cheez
    Sorry I missed your question. Probably both options will work, but better test it.

Log in to reply

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.