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

Quickstart - get different result for the tutorial

  • Hi,

    For below code which i took from the quick start, tutorial shows result as 95823.62, but i am get 99725.08, which seems incorrect. Can you please advise? thanks.

        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 backtrader as bt
        # Create a strategy
        class TestStrategy(bt.Strategy):
            def log(self, txt, dt=None):
                '''Loggin function for 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])
                if self.dataclose[0] < self.dataclose[-1]:
                    # Currnet close less than previous close
                    if self.dataclose[-1] < self.dataclose[-2]:
                        # previous close less than the previous close
                        self.log('BUY CREATE, %.2f' % self.dataclose[0])
        if __name__ == '__main__':
            cerebro = bt.Cerebro()
            # Add a strategy
            # Load Data
            modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
            datapath = os.path.join(modpath, 'datas/orcl-1995-2014.txt')
            # Create a datafeed
            data = bt.feeds.YahooFinanceCSVData(
                # Do not pass values before this date
                fromdate=datetime.datetime(2000, 1, 1),
                # Do not pass values after this date
                todate=datetime.datetime(2000, 12, 31),
            # Add the Data Feed to Cerebro
            # Set our desired cash start
            print('Starting Portfolio Value: %.2f' %
            print('Final Portfolio Value: %.2f' %

    Results from the tutorial

    2000-12-20T00:00:00, BUY CREATE, 26.88
    2000-12-21T00:00:00, Close, 27.82
    2000-12-22T00:00:00, Close, 30.06
    2000-12-26T00:00:00, Close, 29.17
    2000-12-27T00:00:00, Close, 28.94
    2000-12-27T00:00:00, BUY CREATE, 28.94
    2000-12-28T00:00:00, Close, 29.29
    2000-12-29T00:00:00, Close, 27.41
    Final Portfolio Value: 95823.62

    Results on my laptop

    2000-12-14 BUY CREATE, 25.93
    2000-12-15 Close, 26.93
    2000-12-18 Close, 30.18
    2000-12-19 Close, 28.88
    2000-12-20 Close, 26.88
    2000-12-20 BUY CREATE, 26.88
    2000-12-21 Close, 27.82
    2000-12-22 Close, 30.06
    2000-12-26 Close, 29.17
    2000-12-27 Close, 28.94
    2000-12-27 BUY CREATE, 28.94
    2000-12-28 Close, 29.29
    2000-12-29 Close, 27.41
    Final Portfolio Value: 99725.08

  • administrators

    That's a leftover in the documentation. The Quickstart was one of the 1st parts to be ever written and the sample was then changed and in the process of updating, that line remained there (you can see that the operations match).

    Documentation has been updated (Use Shift + F5 to force a reload in the browser bypassing the cache)

  • Thanks for the prompt reply!

Log in to reply