How Do I Access Past Raw Data in Strategy?



  • 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?


  • administrators

    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.



  • 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


  • administrators

    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.



  • <deleted>



  • <deleted>


Log in to reply
 

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