Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. STEVEN 0
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    S
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 1
    • Best 0
    • Controversial 0
    • Groups 0

    STEVEN 0

    @STEVEN 0

    0
    Reputation
    1
    Profile views
    1
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    STEVEN 0 Unfollow Follow

    Latest posts made by STEVEN 0

    • value of cerebro.broker.getvalue() never changed

      Hi All,

      I'm following the quickguide. I've already feed my data, and my code can print buy log. however, value of cerebro.broker.getvalue() has never changed. Anyone knows why?

      Here is the result:
      it can show buy/create,but cerebro.broker.getvalue() is still 100000.

      Starting Portfolio Value: 100000.00
      2022-07-15, Close, 32.31
      2022-07-14, Close, 32.82
      2022-07-13, Close, 30.56
      2022-07-12, Close, 29.44
      2022-07-12, BUY CREATE, 29.44
      2022-07-11, Close, 31.43
      2022-07-08, Close, 31.90
      ……
      2018-12-07, BUY CREATE, 25.75
      2018-12-06, Close, 26.16
      2018-12-05, Close, 25.91
      2018-12-04, Close, 25.47
      2018-12-04, BUY CREATE, 25.47
      2018-12-03, Close, 25.63
      Final Portfolio Value: 100000.00
      

      Here is my 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, 'D:/300568_TTL.csv')
      
          # Create a Data Feed
          data = bt.feeds.GenericCSVData(
              dataname=datapath,
              datetime=2,
              open=3,
              high=4,
              low=5,
              close=6,
              volume=10,
              K_value=12,
              J_value=14,
              dtformat=('%Y%m%d'),
              # Do not pass values before this date
              fromdate=datetime.datetime(2018, 12, 1),
              # Do not pass values before this date
              todate=datetime.datetime(2022, 7, 16),
              # Do not pass values after this date
              reverse=True)
      
          # 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())![alt text](![image url](image url))
      
      posted in General Code/Help
      S
      STEVEN 0