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

Why do I get the END of the Line When Accessing Negative Indices at the Beginning of Backtesting?

  • Hi,

    I am looking at the Quickstart manual and specifically at the section

    Adding some Logic to the Strategy
    Let’s try some crazy idea we had by looking at some charts

    I observe a strange behaviour: I look in def next(self): during the beginning of the processing for negative indices of the line, I get values from the end of the line. I would expect that somehow these values are delivered as NaN. In later loops, if that row exists, I do get the proper value from that row back.

    Connected to pydev debugger (build 173.4301.16)
    Starting Portfolio Value: 100000.00
    2000-01-03, Close, 29.53
    self.dataclose[0,-1,-2]: 29.53 29.06 31.06
                Date  Adj Close
    1514  2000-12-29    29.0625
                Date  Adj Close
    1513  2000-12-28    31.0625
    Process finished with exit code 0

    This is my extended code:

    from __future__ import (absolute_import, division, print_function,
    import datetime  # For datetime objects
    import os.path  # To manage paths
    import sys  # To find out the script name (in argv[0])
    # Import the backtrader platform
    import backtrader as bt
    # Create a Stratey
    class TestStrategy(bt.Strategy):
        def log(self, txt, dt=None):
            ''' Logging function fot this strategy'''
            dt = dt or self.datas[0]
            print('%s, %s' % (dt.isoformat(), txt))
        def __init__(self):
            # Keep a reference to the "close" line in the data[0] dataseries
            self.dataclose = self.datas[0].close
        def next(self):
            # Simply log the closing price of the series from the reference
            self.log('Close, %.2f' % self.dataclose[0])
            print("self.dataclose[0,-1,-2]:", self.dataclose[0],self.dataclose[-1],self.dataclose[-2])
            import pandas as pd
            df = pd.read_csv(r"/home/user/PycharmProjects/0330_backtrader_in_detail/datas/orcl-1995-2014.txt")
            df[['Date']] = df[['Date']].astype('str')
            my_close = df[['Date', 'Adj Close']]
            print(my_close[(my_close["Adj Close"] >= 29.06) & (my_close["Adj Close"] <= 29.07)])
            print(my_close[(my_close["Adj Close"] >= 31.06) & (my_close["Adj Close"] <= 31.07)])
            import sys
            if self.dataclose[0] < self.dataclose[-1]:
                # current close less than previous close
                if self.dataclose[-1] < self.dataclose[-2]:
                    # previous close less than the previous close
                    # BUY, BUY, BUY!!! (with all possible default parameters)
                    self.log('BUY CREATE, %.2f' % self.dataclose[0])
    if __name__ == '__main__':
        # Create a cerebro entity
        cerebro = bt.Cerebro()
        # Add a strategy
        # Datas are in a subfolder of the samples. Need to find where the script is
        # because it could have been called from anywhere
        modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
        datapath = os.path.join(modpath, '../../datas/orcl-1995-2014.txt')
        # Create a Data Feed
        data = bt.feeds.YahooFinanceCSVData(
            # Do not pass values before this date
            fromdate=datetime.datetime(2000, 1, 1),
            # Do not pass values before this date
            todate=datetime.datetime(2000, 12, 31),
            # Do not pass values after this date
        # Add the Data Feed to Cerebro
        # Set our desired cash start
        # Print out the starting conditions
        print('Starting Portfolio Value: %.2f' %
        # Run over everything
        # Print out the final result
        print('Final Portfolio Value: %.2f' %

  • administrators

    @newesttrader said in Why do I get the END of the Line When Accessing Negative Indices at the Beginning of Backtesting?:

    print("self.dataclose[0,-1,-2]:", self.dataclose[0],self.dataclose[-1],self.dataclose[-2])

    Because you don't have any indicator and have not added any warm up period and Python arithmetic for arrays (basics 101) wraps over.

    Basically accessing -1 and -2 in that scenario is a bug in your code.

  • Thanks for your reply.

    With your response in mind, I tried to set the warmup period this way:

        def start(self):
            self._minperiod = 15

    But with this the logics still starts at 1. At least it seems so, if I print the cycle counter with:

    def next(self):
        self.log(str(len(self)) + ' Close, %.2f' % self.dataclose[0])

    Apparently I have not yet understood how to set the warmup period properly...

  • administrators

    You can do it by using something which sets the warm up period for you automatically.

    Since you are checking old values

    def __init__(self):
        self.data_close1 =  # get the data close delayed 1 bar
    def next(self):
        if < self.data_close1:
            print('bla bla bla ...')