Problem with the simple sample



  • Using the sample code:

     from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    
     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].datetime.date(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]:
                # 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])
                    self.buy()
    
    
    if __name__ == '__main__':
        # Create a cerebro entity
        cerebro = bt.Cerebro()
    
        # Add a strategy
        cerebro.addstrategy(TestStrategy)
    
        # 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.YahooFinanceData(
            dataname="AAPL",
            # 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
            reverse=False)
    
        # Add the Data Feed to Cerebro
        cerebro.adddata(data)
    
        # Set our desired cash start
        cerebro.broker.setcash(100000.0)
    
        # Print out the starting conditions
        print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
    
        # Run over everything
        cerebro.run()
    
        # Print out the final result
        print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
    

    I get the following output:

    Starting Portfolio Value: 100000.00
    2000-12-29, Close, 0.97
    2000-12-28, Close, 0.96
    2000-12-28, BUY CREATE, 0.96
    2000-12-27, Close, 0.96
    2000-12-26, Close, 0.96
    2000-12-22, Close, 0.98
    2000-12-21, Close, 0.91
    2000-12-20, Close, 0.94
    2000-12-19, Close, 0.91
    2000-12-18, Close, 0.93
    2000-12-15, Close, 0.91
    2000-12-14, Close, 0.94
    2000-12-13, Close, 0.98
    2000-12-12, Close, 1.00
    2000-12-11, Close, 0.99
    2000-12-08, Close, 0.98
    2000-12-08, BUY CREATE, 0.98
    2000-12-07, Close, 0.93
    2000-12-07, BUY CREATE, 0.93
    2000-12-06, Close, 0.93
    2000-12-05, Close, 1.11
    2000-12-04, Close, 1.09
    2000-12-01, Close, 1.11
    2000-11-30, Close, 1.07
    2000-11-29, Close, 1.14
    2000-11-28, Close, 1.17
    2000-11-27, Close, 1.22
    2000-11-24, Close, 1.26
    2000-11-22, Close, 1.20
    2000-11-21, Close, 1.22
    2000-11-20, Close, 1.23
    2000-11-17, Close, 1.20
    2000-11-16, Close, 1.24
    2000-11-15, Close, 1.29
    2000-11-14, Close, 1.32
    2000-11-13, Close, 1.26
    2000-11-10, Close, 1.24
    2000-11-10, BUY CREATE, 1.24
    2000-11-09, Close, 1.31
    2000-11-08, Close, 1.31
    2000-11-07, Close, 1.39
    2000-11-06, Close, 1.39
    2000-11-03, Close, 1.45
    2000-11-02, Close, 1.45
    2000-11-01, Close, 1.33
    2000-10-31, Close, 1.27
    2000-10-31, BUY CREATE, 1.27
    2000-10-30, Close, 1.26
    2000-10-30, BUY CREATE, 1.26
    2000-10-27, Close, 1.21
    2000-10-27, BUY CREATE, 1.21
    2000-10-26, Close, 1.20
    2000-10-26, BUY CREATE, 1.20
    2000-10-25, Close, 1.20
    2000-10-24, Close, 1.23
    2000-10-23, Close, 1.33
    2000-10-20, Close, 1.27
    2000-10-19, Close, 1.23
    2000-10-19, BUY CREATE, 1.23
    2000-10-18, Close, 1.31
    2000-10-17, Close, 1.31
    2000-10-16, Close, 1.40
    2000-10-13, Close, 1.44
    2000-10-12, Close, 1.30
    2000-10-11, Close, 1.28
    2000-10-11, BUY CREATE, 1.28
    2000-10-10, Close, 1.36
    2000-10-09, Close, 1.42
    2000-10-06, Close, 1.44
    2000-10-05, Close, 1.44
    2000-10-04, Close, 1.54
    2000-10-03, Close, 1.45
    2000-10-02, Close, 1.58
    2000-09-29, Close, 1.68
    2000-09-28, Close, 3.48
    2000-09-27, Close, 3.18
    2000-09-26, Close, 3.35
    2000-09-25, Close, 3.48
    2000-09-22, Close, 3.40
    2000-09-21, Close, 3.69
    2000-09-20, Close, 3.97
    2000-09-19, Close, 3.90
    2000-09-18, Close, 3.95
    2000-09-15, Close, 3.59
    2000-09-14, Close, 3.70
    2000-09-13, Close, 3.77
    2000-09-12, Close, 3.76
    2000-09-11, Close, 3.80
    2000-09-08, Close, 3.83
    2000-09-07, Close, 4.03
    2000-09-06, Close, 3.80
    2000-09-05, Close, 4.06
    2000-09-01, Close, 4.13
    2000-08-31, Close, 3.96
    2000-08-30, Close, 3.87
    2000-08-30, BUY CREATE, 3.87
    2000-08-29, Close, 3.85
    2000-08-29, BUY CREATE, 3.85
    2000-08-28, Close, 3.78
    2000-08-28, BUY CREATE, 3.78
    2000-08-25, Close, 3.70
    2000-08-25, BUY CREATE, 3.70
    2000-08-24, Close, 3.65
    2000-08-24, BUY CREATE, 3.65
    2000-08-23, Close, 3.53
    2000-08-23, BUY CREATE, 3.53
    2000-08-22, Close, 3.36
    2000-08-22, BUY CREATE, 3.36
    2000-08-21, Close, 3.29
    2000-08-21, BUY CREATE, 3.29
    2000-08-18, Close, 3.25
    2000-08-18, BUY CREATE, 3.25
    2000-08-17, Close, 3.35
    2000-08-16, Close, 3.16
    2000-08-15, Close, 3.04
    2000-08-15, BUY CREATE, 3.04
    2000-08-14, Close, 3.06
    2000-08-11, Close, 3.10
    2000-08-10, Close, 3.09
    2000-08-09, Close, 3.09
    2000-08-08, Close, 3.04
    2000-08-07, Close, 3.12
    2000-08-04, Close, 3.08
    2000-08-03, Close, 3.12
    2000-08-02, Close, 3.07
    2000-08-01, Close, 3.21
    2000-07-31, Close, 3.31
    2000-07-28, Close, 3.14
    2000-07-27, Close, 3.38
    2000-07-26, Close, 3.26
    2000-07-25, Close, 3.26
    2000-07-24, Close, 3.17
    2000-07-21, Close, 3.48
    2000-07-20, Close, 3.59
    2000-07-19, Close, 3.43
    2000-07-18, Close, 3.72
    2000-07-17, Close, 3.79
    2000-07-14, Close, 3.75
    2000-07-13, Close, 3.68
    2000-07-13, BUY CREATE, 3.68
    2000-07-12, Close, 3.83
    2000-07-11, Close, 3.70
    2000-07-10, Close, 3.72
    2000-07-07, Close, 3.54
    2000-07-06, Close, 3.37
    2000-07-06, BUY CREATE, 3.37
    2000-07-05, Close, 3.36
    2000-07-05, BUY CREATE, 3.36
    2000-07-03, Close, 3.47
    2000-06-30, Close, 3.41
    2000-06-29, Close, 3.33
    2000-06-29, BUY CREATE, 3.33
    2000-06-28, Close, 3.54
    2000-06-27, Close, 3.37
    2000-06-26, Close, 3.52
    2000-06-23, Close, 3.36
    2000-06-22, Close, 3.50
    2000-06-21, Close, 3.62
    2000-06-20, Close, 3.29
    2000-06-19, Close, 3.14
    2000-06-19, BUY CREATE, 3.14
    2000-06-16, Close, 2.97
    2000-06-16, BUY CREATE, 2.97
    2000-06-15, Close, 3.00
    2000-06-14, Close, 2.94
    2000-06-13, Close, 3.07
    2000-06-12, Close, 2.97
    2000-06-09, Close, 3.11
    2000-06-08, Close, 3.08
    2000-06-07, Close, 3.14
    2000-06-06, Close, 3.02
    2000-06-05, Close, 2.97
    2000-06-05, BUY CREATE, 2.97
    2000-06-02, Close, 3.01
    2000-06-01, Close, 2.90
    2000-05-31, Close, 2.73
    2000-05-31, BUY CREATE, 2.73
    2000-05-30, Close, 2.85
    2000-05-26, Close, 2.81
    2000-05-25, Close, 2.84
    2000-05-24, Close, 2.85
    2000-05-23, Close, 2.79
    2000-05-22, Close, 2.93
    2000-05-19, Close, 3.06
    2000-05-18, Close, 3.28
    2000-05-17, Close, 3.30
    2000-05-16, Close, 3.44
    2000-05-15, Close, 3.29
    2000-05-12, Close, 3.50
    2000-05-11, Close, 3.34
    2000-05-10, Close, 3.23
    2000-05-10, BUY CREATE, 3.23
    2000-05-09, Close, 3.43
    2000-05-08, Close, 3.58
    2000-05-05, Close, 3.68
    2000-05-04, Close, 3.60
    2000-05-03, Close, 3.74
    2000-05-02, Close, 3.83
    2000-05-01, Close, 4.04
    2000-04-28, Close, 4.04
    2000-04-27, Close, 4.12
    2000-04-26, Close, 3.95
    2000-04-25, Close, 4.17
    2000-04-24, Close, 3.92
    2000-04-20, Close, 3.87
    2000-04-20, BUY CREATE, 3.87
    2000-04-19, Close, 3.94
    2000-04-18, Close, 4.13
    2000-04-17, Close, 4.03
    2000-04-14, Close, 3.64
    2000-04-14, BUY CREATE, 3.64
    2000-04-13, Close, 3.70
    2000-04-12, Close, 3.55
    2000-04-11, Close, 3.89
    2000-04-10, Close, 4.07
    2000-04-07, Close, 4.29
    2000-04-06, Close, 4.07
    2000-04-05, Close, 4.24
    2000-04-04, Close, 4.14
    2000-04-03, Close, 4.34
    2000-03-31, Close, 4.42
    2000-03-30, Close, 4.09
    2000-03-29, Close, 4.42
    2000-03-28, Close, 4.53
    2000-03-27, Close, 4.54
    2000-03-24, Close, 4.51
    2000-03-23, Close, 4.60
    2000-03-22, Close, 4.69
    2000-03-21, Close, 4.39
    2000-03-20, Close, 4.00
    2000-03-20, BUY CREATE, 4.00
    2000-03-17, Close, 4.07
    2000-03-16, Close, 3.95
    2000-03-15, Close, 3.78
    2000-03-15, BUY CREATE, 3.78
    2000-03-14, Close, 3.72
    2000-03-14, BUY CREATE, 3.72
    2000-03-13, Close, 3.95
    2000-03-10, Close, 4.09
    2000-03-09, Close, 3.98
    2000-03-08, Close, 3.97
    2000-03-08, BUY CREATE, 3.97
    2000-03-07, Close, 4.00
    2000-03-06, Close, 4.09
    2000-03-03, Close, 4.16
    2000-03-02, Close, 3.97
    2000-03-01, Close, 4.24
    2000-02-29, Close, 3.73
    2000-02-28, Close, 3.68
    2000-02-28, BUY CREATE, 3.68
    2000-02-25, Close, 3.59
    2000-02-25, BUY CREATE, 3.59
    2000-02-24, Close, 3.75
    2000-02-23, Close, 3.78
    2000-02-22, Close, 3.70
    2000-02-18, Close, 3.62
    2000-02-18, BUY CREATE, 3.62
    2000-02-17, Close, 3.74
    2000-02-16, Close, 3.71
    2000-02-15, Close, 3.87
    2000-02-14, Close, 3.77
    2000-02-11, Close, 3.54
    2000-02-11, BUY CREATE, 3.54
    2000-02-10, Close, 3.69
    2000-02-09, Close, 3.66
    2000-02-08, Close, 3.74
    2000-02-07, Close, 3.71
    2000-02-04, Close, 3.51
    2000-02-04, BUY CREATE, 3.51
    2000-02-03, Close, 3.36
    2000-02-03, BUY CREATE, 3.36
    2000-02-02, Close, 3.21
    2000-02-02, BUY CREATE, 3.21
    2000-02-01, Close, 3.26
    2000-01-31, Close, 3.38
    2000-01-28, Close, 3.31
    2000-01-27, Close, 3.58
    2000-01-26, Close, 3.58
    2000-01-25, Close, 3.65
    2000-01-24, Close, 3.46
    2000-01-21, Close, 3.62
    2000-01-20, Close, 3.69
    2000-01-19, Close, 3.47
    2000-01-18, Close, 3.38
    2000-01-18, BUY CREATE, 3.38
    2000-01-14, Close, 3.27
    2000-01-14, BUY CREATE, 3.27
    2000-01-13, Close, 3.15
    2000-01-13, BUY CREATE, 3.15
    2000-01-12, Close, 2.84
    2000-01-12, BUY CREATE, 2.84
    2000-01-11, Close, 3.02
    2000-01-10, Close, 3.18
    2000-01-07, Close, 3.24
    2000-01-06, Close, 3.09
    2000-01-05, Close, 3.38
    2000-01-04, Close, 3.33
    2000-01-03, Close, 3.64
    Final Portfolio Value: 100000.00
    

    Why the cash is not reducing?


  • administrators

    Because you have told the platform to go from the future to the past? (See the date ordering of your own log)



  • Thanks! I missed that. should have been reverse=True


Log in to reply
 

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