Navigation

    Backtrader Community

    • Register
    • 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/

    How Do I Access Past Raw Data in Strategy?

    Indicators/Strategies/Analyzers
    2
    6
    2803
    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.
    • T
      Taewoo Kim last edited by Taewoo Kim

      This is a 3 part question

      How do I access the past X closes as well as volume ? (raw values from the CSV) Ideally as a list.

      If I wanted to invoke a (computationally expensive, hence slow) method in "batch" mode, what is the most efficient way to do this? Run @ every Xth next() step? ( i.e. len(line) % step )

      Pyfolio: I noticed in the quickstart demo that the author keeps tabs of all the open positions, p & l, etc. Based on the pyfolio doc.. does including the PyFolio in backtrader automatically take care of this?

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        All lines and lines objects support a get(ago, size) method which will return a list with the values starting at ago bars (0 is current, -1 is previous) and going then backwards size. The returned values are in the regular expected orders.

        You seem to be looking at developing an indicator which is done in a tight loop. This is done by developing it supporting the once method (and if needed be, preonce and/or oncestart)

        From the source code a sample to make an Average

        class Average(PeriodN):
            '''
            Averages a given data arithmetically over a period
        
            Formula:
              - av = data(period) / period
        
            See also:
              - https://en.wikipedia.org/wiki/Arithmetic_mean
            '''
            alias = ('ArithmeticMean', 'Mean',)
            lines = ('av',)
        
            def next(self):
                self.line[0] = \
                    math.fsum(self.data.get(size=self.p.period)) / self.p.period
        
            def once(self, start, end):
                src = self.data.array
                dst = self.line.array
                period = self.p.period
        
                for i in range(start, end):
                    dst[i] = math.fsum(src[i - period + 1:i + 1]) / period
        

        Pyfolio: I noticed in the quickstart demo that the author keeps tabs of all the open positions, p & l, etc. Based on the pyfolio doc.. does including the PyFolio in backtrader automatically take care of this?

        That needs some elaboration to be understood. But the analyzers involved in general PyFolio analyzer try to do the same done by the original.

        1 Reply Last reply Reply Quote 1
        • T
          Taewoo Kim last edited by Taewoo Kim

          Thanks that worked perfectly.

          Is there a more efficient way to retrieve the past X bars of data in a numpy data format instead of fetching the data as a list and creating a numpy dataframe? Seems wasteful..

          PS: im trying to calculate rolling_mean and pct_change

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            backtrader doesn't use numpy (the reasons and original goals of the project have been discussed in other threads) and there is therefore always going to be an associated cost converting from array.array to something else.

            lines objects hold the array.array buffer in an attribute called array (bummer there) as you may see in the quoted code from above in the once.method.

            You can access that array to make the conversion directly to numpy, but have then to calculate the indexing manually.

            1 Reply Last reply Reply Quote 0
            • T
              Taewoo Kim last edited by Taewoo Kim

              <deleted>

              1 Reply Last reply Reply Quote 0
              • T
                Taewoo Kim last edited by Taewoo Kim

                <deleted>

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                $(document).ready(function () { app.coldLoad(); }); }