Backtrader Community

    • 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 for yesterday's close

    Indicators/Strategies/Analyzers
    indicator
    3
    13
    3667
    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.
    • ?
      A Former User last edited by

      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] = 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.

      Thanks!

      1 Reply Last reply Reply Quote 0
      • ?
        A Former User last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • P
          Paska Houso last edited by

          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?

          ? 1 Reply Last reply Reply Quote 0
          • ?
            A Former User @Paska Houso last edited by

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

            1 Reply Last reply Reply Quote 0
            • P
              Paska Houso last edited by

              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

              ? 1 Reply Last reply Reply Quote 0
              • ?
                A Former User @Paska Houso last edited by

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

                1 Reply Last reply Reply Quote 0
                • P
                  Paska Houso last edited by

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

                  • https://www.backtrader.com/docu/mixing-timeframes/indicators-mixing-timeframes.html

                  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.

                  • https://github.com/mementum/backtrader/blob/master/backtrader/indicators/pivotpoint.py
                  ? 1 Reply Last reply Reply Quote 0
                  • ?
                    A Former User @Paska Houso last edited by

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

                    A 1 Reply Last reply Reply Quote 0
                    • A
                      ab_trader @Guest last edited by

                      @Cheez try this code:
                      indicator

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

                      strategy

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

                      results
                      0_1514523286087_2017-12-28_20-54-06.png

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

                      • If my answer helped, hit reputation up arrow at lower right corner of the post.
                      • Python Debugging With Pdb
                      • New to python and bt - check this out
                      ? 1 Reply Last reply Reply Quote 2
                      • P
                        Paska Houso last edited by

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

                        A 1 Reply Last reply Reply Quote 1
                        • A
                          ab_trader @Paska Houso last edited by ab_trader

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

                          • If my answer helped, hit reputation up arrow at lower right corner of the post.
                          • Python Debugging With Pdb
                          • New to python and bt - check this out
                          1 Reply Last reply Reply Quote 1
                          • ?
                            A Former User @ab_trader last edited by

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

                            A 1 Reply Last reply Reply Quote 0
                            • A
                              ab_trader @Guest last edited by

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

                              • If my answer helped, hit reputation up arrow at lower right corner of the post.
                              • Python Debugging With Pdb
                              • New to python and bt - check this out
                              1 Reply Last reply Reply Quote 1
                              • 1 / 1
                              • First post
                                Last post
                              Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors