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 = self.data.close[-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 = 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 = self.__close.lines.close
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
You want to use the recorded last close value as an indicator (for visual or calculation purposes) in a (for example)
Is that right? Be it so ... isn't this something like the
Yes, I think you have the broad strokes. It does sound like
PivotIndicatorbut 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
PivotPointindicator 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
PivotPointto simply generate the
closeline as output.
@Paska-Houso I will try that out, thanks!
ab_trader last edited by
@Cheez try this code:
class LastClose(bt.Indicator): lines = ('close',) params = (('period', 0),) def __init__(self): self.lines.close = self.data.close(-self.p.period)
class MasterStrategy(bt.Strategy): def __init__(self): self.lclose = lclose = LastClose(self.datas) 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
PivotPointwas the right direction, the simplicity of your solution (empowered by the underlying mechanics) has really amazed me.
ab_trader last edited by ab_trader
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?
ab_trader last edited by
Sorry I missed your question. Probably both options will work, but better test it.