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/

    Bactrader broker portfolio value increases instead of decreasing when only buying the stocks.

    General Code/Help
    3
    6
    244
    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.
    • M
      Mihir last edited by

      Hello,
      I am new to backtrader and python and was trying the quickstart guide. I simple copy and pasted the example shown in the guide.
      Starting value for my portfolio is 100000. After running the strategy when I print the portfolio values using cerebro.broker.get_value() again it shows increase in the portfolio. In the strategy I am only buying, thus the portfolio should only decrease, instead I see an increment and the portfolio value is 100589.88. To check what is causing the issue, I checked portfolio after each buy. The portfolio increases after each buy.
      In the quick start it says the value of portfolio should decrease. By in my code, it increases. Could someone explain, what could cause this issue.

      '''

      class TestStrategy(bt.Strategy):
          def log(self, txt, dt=None):
          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.log('Portfolio %.2f' % cerebro.broker.getvalue() )
                  self.buy()
      
      
      
      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, 'AAPL.csv')
      
      # Create a Data Feed
      data = bt.feeds.YahooFinanceCSVData(
          dataname=datapath,
          # Do not pass values before this date
          fromdate=datetime.datetime(2020, 4, 28),
          # Do not pass values before this date
          todate=datetime.datetime(2021, 4, 27),
          # 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())'''
      
      Starting Portfolio Value: 100000.00
      2020-04-28, Close, 69.11
      2020-04-29, Close, 71.38
      2020-04-30, Close, 72.89
      2020-05-01, Close, 71.71
      2020-05-04, Close, 72.73
      2020-05-05, Close, 73.82
      2020-05-06, Close, 74.58
      2020-05-07, Close, 75.35
      2020-05-08, Close, 77.14
      2020-05-11, Close, 78.36
      2020-05-12, Close, 77.46
      2020-05-13, Close, 76.53
      2020-05-13, BUY CREATE 76.53
      2020-05-13, Portfolio 100000.00
      2020-05-14, Close, 77.00
      2020-05-15, Close, 76.54
      2020-05-18, Close, 78.35
      2020-05-19, Close, 77.89
      2020-05-20, Close, 79.41
      2020-05-21, Close, 78.82
      2020-05-22, Close, 79.32
      2020-05-26, Close, 78.79
      2020-05-27, Close, 79.13
      2020-05-28, Close, 79.16
      2020-05-29, Close, 79.09
      2020-06-01, Close, 80.06
      2020-06-02, Close, 80.43
      2020-06-03, Close, 80.87
      2020-06-04, Close, 80.18
      2020-06-05, Close, 82.46
      2020-06-08, Close, 82.95
      2020-06-09, Close, 85.57
      2020-06-10, Close, 87.77
      2020-06-11, Close, 83.55
      2020-06-12, Close, 84.28
      2020-06-15, Close, 85.32
      2020-06-16, Close, 87.58
      2020-06-17, Close, 87.46
      2020-06-18, Close, 87.49
      2020-06-19, Close, 86.99
      2020-06-22, Close, 89.27
      2020-06-23, Close, 91.17
      2020-06-24, Close, 89.56
      2020-06-25, Close, 90.75
      2020-06-26, Close, 87.96
      2020-06-29, Close, 89.99
      2020-06-30, Close, 90.74
      2020-07-01, Close, 90.57
      2020-07-02, Close, 90.57
      2020-07-06, Close, 92.99
      2020-07-07, Close, 92.71
      2020-07-08, Close, 94.87
      2020-07-09, Close, 95.27
      2020-07-10, Close, 95.44
      2020-07-13, Close, 95.00
      2020-07-14, Close, 96.57
      2020-07-15, Close, 97.24
      2020-07-16, Close, 96.04
      2020-07-17, Close, 95.85
      2020-07-17, BUY CREATE 95.85
      2020-07-17, Portfolio 100020.10
      2020-07-20, Close, 97.87
      2020-07-21, Close, 96.51
      2020-07-22, Close, 96.79
      2020-07-23, Close, 92.38
      2020-07-24, Close, 92.15
      2020-07-24, BUY CREATE 92.15
      2020-07-24, Portfolio 100012.62
      2020-07-27, Close, 94.34
      2020-07-28, Close, 92.79
      2020-07-29, Close, 94.56
      2020-07-30, Close, 95.71
      2020-07-31, Close, 105.73
      2020-08-03, Close, 108.39
      2020-08-04, Close, 109.12
      2020-08-05, Close, 109.51
      2020-08-06, Close, 113.33
      2020-08-07, Close, 110.76
      2020-08-10, Close, 112.37
      2020-08-11, Close, 109.02
      2020-08-12, Close, 112.65
      2020-08-13, Close, 114.64
      2020-08-14, Close, 114.54
      2020-08-17, Close, 114.24
      2020-08-17, BUY CREATE 114.24
      2020-08-17, Portfolio 100077.80
      2020-08-18, Close, 115.19
      2020-08-19, Close, 115.34
      2020-08-20, Close, 117.90
      2020-08-21, Close, 123.97
      2020-08-24, Close, 125.45
      2020-08-25, Close, 124.42
      2020-08-26, Close, 126.12
      2020-08-27, Close, 124.61
      2020-08-28, Close, 124.41
      2020-08-28, BUY CREATE 124.41
      2020-08-28, Portfolio 100118.73
      2020-08-31, Close, 128.63
      2020-09-01, Close, 133.75
      2020-09-02, Close, 130.98
      2020-09-03, Close, 120.49
      2020-09-03, BUY CREATE 120.49
      2020-09-03, Portfolio 100096.37
      2020-09-04, Close, 120.57
      2020-09-08, Close, 112.46
      2020-09-09, Close, 116.94
      2020-09-10, Close, 113.13
      2020-09-11, Close, 111.64
      2020-09-11, BUY CREATE 111.64
      2020-09-11, Portfolio 100044.08
      2020-09-14, Close, 114.99
      2020-09-15, Close, 115.17
      2020-09-16, Close, 111.77
      2020-09-17, Close, 109.99
      2020-09-17, BUY CREATE 109.99
      2020-09-17, Portfolio 100029.82
      2020-09-18, Close, 106.50
      2020-09-18, BUY CREATE 106.50
      2020-09-18, Portfolio 100001.84
      2020-09-21, Close, 109.73
      2020-09-22, Close, 111.45
      2020-09-23, Close, 106.78
      2020-09-24, Close, 107.87
      2020-09-25, Close, 111.92
      2020-09-28, Close, 114.59
      2020-09-29, Close, 113.72
      2020-09-30, Close, 115.44
      2020-10-01, Close, 116.41
      2020-10-02, Close, 112.66
      2020-10-05, Close, 116.13
      2020-10-06, Close, 112.80
      2020-10-07, Close, 114.71
      2020-10-08, Close, 114.60
      2020-10-09, Close, 116.59
      2020-10-12, Close, 124.00
      2020-10-13, Close, 120.71
      2020-10-14, Close, 120.80
      2020-10-15, Close, 120.32
      2020-10-16, Close, 118.64
      2020-10-16, BUY CREATE 118.64
      2020-10-16, Portfolio 100113.40
      2020-10-19, Close, 115.61
      2020-10-19, BUY CREATE 115.61
      2020-10-19, Portfolio 100082.17
      2020-10-20, Close, 117.13
      2020-10-21, Close, 116.49
      2020-10-22, Close, 115.38
      2020-10-22, BUY CREATE 115.38
      2020-10-22, Portfolio 100079.42
      2020-10-23, Close, 114.67
      2020-10-23, BUY CREATE 114.67
      2020-10-23, Portfolio 100070.26
      2020-10-26, Close, 114.68
      2020-10-27, Close, 116.23
      2020-10-28, Close, 110.84
      2020-10-29, Close, 114.95
      2020-10-30, Close, 108.51
      2020-11-02, Close, 108.42
      2020-11-02, BUY CREATE 108.42
      2020-11-02, Portfolio 99990.04
      2020-11-03, Close, 110.09
      2020-11-04, Close, 114.58
      2020-11-05, Close, 118.65
      2020-11-06, Close, 118.51
      2020-11-09, Close, 116.15
      2020-11-09, BUY CREATE 116.15
      2020-11-09, Portfolio 100097.37
      2020-11-10, Close, 115.80
      2020-11-10, BUY CREATE 115.80
      2020-11-10, Portfolio 100092.89
      2020-11-11, Close, 119.31
      2020-11-12, Close, 119.03
      2020-11-13, Close, 119.08
      2020-11-16, Close, 120.12
      2020-11-17, Close, 119.21
      2020-11-18, Close, 117.85
      2020-11-18, BUY CREATE 117.85
      2020-11-18, Portfolio 100124.47
      2020-11-19, Close, 118.46
      2020-11-20, Close, 117.16
      2020-11-23, Close, 113.68
      2020-11-23, BUY CREATE 113.68
      2020-11-23, Portfolio 100054.02
      2020-11-24, Close, 115.00
      2020-11-25, Close, 115.86
      2020-11-27, Close, 116.42
      2020-11-30, Close, 118.87
      2020-12-01, Close, 122.54
      2020-12-02, Close, 122.90
      2020-12-03, Close, 122.76
      2020-12-04, Close, 122.07
      2020-12-04, BUY CREATE 122.07
      2020-12-04, Portfolio 100204.98
      2020-12-07, Close, 123.57
      2020-12-08, Close, 124.19
      2020-12-09, Close, 121.60
      2020-12-10, Close, 123.06
      2020-12-11, Close, 122.23
      2020-12-14, Close, 121.60
      2020-12-14, BUY CREATE 121.60
      2020-12-14, Portfolio 100195.99
      2020-12-15, Close, 127.69
      2020-12-16, Close, 127.62
      2020-12-17, Close, 128.51
      2020-12-18, Close, 126.47
      2020-12-21, Close, 128.04
      2020-12-22, Close, 131.68
      2020-12-23, Close, 130.76
      2020-12-24, Close, 131.77
      2020-12-28, Close, 136.49
      2020-12-29, Close, 134.67
      2020-12-30, Close, 133.52
      2020-12-30, BUY CREATE 133.52
      2020-12-30, Portfolio 100431.84
      2020-12-31, Close, 132.49
      2020-12-31, BUY CREATE 132.49
      2020-12-31, Portfolio 100409.85
      2021-01-04, Close, 129.22
      2021-01-04, BUY CREATE 129.22
      2021-01-04, Portfolio 100337.08
      2021-01-05, Close, 130.81
      2021-01-06, Close, 126.41
      2021-01-07, Close, 130.72
      2021-01-08, Close, 131.85
      2021-01-11, Close, 128.79
      2021-01-12, Close, 128.61
      2021-01-12, BUY CREATE 128.61
      2021-01-12, Portfolio 100323.57
      2021-01-13, Close, 130.69
      2021-01-14, Close, 128.72
      2021-01-15, Close, 126.95
      2021-01-15, BUY CREATE 126.95
      2021-01-15, Portfolio 100283.77
      2021-01-19, Close, 127.64
      2021-01-20, Close, 131.83
      2021-01-21, Close, 136.67
      2021-01-22, Close, 138.86
      2021-01-25, Close, 142.71
      2021-01-26, Close, 142.95
      2021-01-27, Close, 141.85
      2021-01-28, Close, 136.89
      2021-01-28, BUY CREATE 136.89
      2021-01-28, Portfolio 100531.63
      2021-01-29, Close, 131.76
      2021-01-29, BUY CREATE 131.76
      2021-01-29, Portfolio 100399.51
      2021-02-01, Close, 133.94
      2021-02-02, Close, 134.79
      2021-02-03, Close, 133.74
      2021-02-04, Close, 137.18
      2021-02-05, Close, 136.76
      2021-02-08, Close, 136.91
      2021-02-09, Close, 136.01
      2021-02-10, Close, 135.39
      2021-02-10, BUY CREATE 135.39
      2021-02-10, Portfolio 100495.73
      2021-02-11, Close, 135.13
      2021-02-11, BUY CREATE 135.13
      2021-02-11, Portfolio 100487.94
      2021-02-12, Close, 135.37
      2021-02-16, Close, 133.19
      2021-02-17, Close, 130.84
      2021-02-17, BUY CREATE 130.84
      2021-02-17, Portfolio 100364.31
      2021-02-18, Close, 129.71
      2021-02-18, BUY CREATE 129.71
      2021-02-18, Portfolio 100332.05
      2021-02-19, Close, 129.87
      2021-02-22, Close, 126.00
      2021-02-23, Close, 125.86
      2021-02-23, BUY CREATE 125.86
      2021-02-23, Portfolio 100212.17
      2021-02-24, Close, 125.35
      2021-02-24, BUY CREATE 125.35
      2021-02-24, Portfolio 100196.77
      2021-02-25, Close, 120.99
      2021-02-25, BUY CREATE 120.99
      2021-02-25, Portfolio 100053.56
      2021-02-26, Close, 121.26
      2021-03-01, Close, 127.79
      2021-03-02, Close, 125.12
      2021-03-03, Close, 122.06
      2021-03-03, BUY CREATE 122.06
      2021-03-03, Portfolio 100088.34
      2021-03-04, Close, 120.13
      2021-03-04, BUY CREATE 120.13
      2021-03-04, Portfolio 100021.10
      2021-03-05, Close, 121.42
      2021-03-08, Close, 116.36
      2021-03-09, Close, 121.09
      2021-03-10, Close, 119.98
      2021-03-11, Close, 121.96
      2021-03-12, Close, 121.03
      2021-03-15, Close, 123.99
      2021-03-16, Close, 125.57
      2021-03-17, Close, 124.76
      2021-03-18, Close, 120.53
      2021-03-18, BUY CREATE 120.53
      2021-03-18, Portfolio 100034.65
      2021-03-19, Close, 119.99
      2021-03-19, BUY CREATE 119.99
      2021-03-19, Portfolio 100015.30
      2021-03-22, Close, 123.39
      2021-03-23, Close, 122.54
      2021-03-24, Close, 120.09
      2021-03-24, BUY CREATE 120.09
      2021-03-24, Portfolio 100018.76
      2021-03-25, Close, 120.59
      2021-03-26, Close, 121.21
      2021-03-29, Close, 121.39
      2021-03-30, Close, 119.90
      2021-03-31, Close, 122.15
      2021-04-01, Close, 123.00
      2021-04-05, Close, 125.90
      2021-04-06, Close, 126.21
      2021-04-07, Close, 127.90
      2021-04-08, Close, 130.36
      2021-04-09, Close, 133.00
      2021-04-12, Close, 131.24
      2021-04-13, Close, 134.43
      2021-04-14, Close, 132.03
      2021-04-15, Close, 134.50
      2021-04-16, Close, 134.16
      2021-04-19, Close, 134.84
      2021-04-20, Close, 133.11
      2021-04-21, Close, 133.50
      2021-04-22, Close, 131.94
      2021-04-23, Close, 134.32
      2021-04-26, Close, 134.72
      Final Portfolio Value: 100589.88
      
      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @Mihir last edited by

        @mihir I could be wrong, but perhaps investing long in a stock that goes almost straight up from $69 to $134 in one year might be a determining factor?

        RunBacktest.com

        run-out 1 Reply Last reply Reply Quote 1
        • run-out
          run-out @run-out last edited by

          @run-out And also the fact that no shares are sold.

          RunBacktest.com

          1 Reply Last reply Reply Quote 1
          • A
            ab_trader last edited by

            @mihir said in Bactrader broker portfolio value increases instead of decreasing when only buying the stocks.:

            In the strategy I am only buying, thus the portfolio should only decrease

            Cash should be decreasing, but portfolio value depends on the stock prices. They go up, portfolio value goes up as well.

            • If my answer helped, hit reputation up arrow at lower right corner of the post.
            • Python Debugging With Pdb
            • New to python and bt - check this out
            1 Reply Last reply Reply Quote 1
            • M
              Mihir last edited by

              @run-out and @ab_trader Thank you for a prompt reply.

              So just to clarify, In portfolio, the stock price is also counted. Is it possible to separate the stock invested money and money which is not yet invested?

              A 1 Reply Last reply Reply Quote 0
              • A
                ab_trader @Mihir last edited by

                @mihir sure, it is possible, look at Docs - Broker, get_cash and get_value.

                I would recommend to read docs till the end before go deep with bt, will save a lot of time.

                • If my answer helped, hit reputation up arrow at lower right corner of the post.
                • Python Debugging With Pdb
                • New to python and bt - check this out
                1 Reply Last reply Reply Quote 1
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors