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/

    How can I plot the broker value vs a stock?

    General Discussion
    2
    3
    63
    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.
    • Eduardo De La Garza
      Eduardo De La Garza last edited by

      I have been able to plot the AAPL stock and the value on different charts. I would like to have them on the same chart so I can compare them. How can I do that? I don't know if I can add a value indicator to the data or what. This is what I have.

      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
      from _extensions.data_definitions import DOHLCVData
      
      AAPL_PATH = "./../../data/datasets/02_processed/eod/AAPL.csv"
      
      
      class LongAAPL(bt.Strategy):
      
          def next(self):
              self.order_target_percent(self.data, 1)
      
      
      if __name__ == '__main__':
          # Create a cerebro entity
          cerebro = bt.Cerebro(stdstats=False)
      
          # Create a Data Feed
          data = DOHLCVData(
              dataname=AAPL_PATH,
              headers=True,
              fromdate=datetime.datetime(2020, 1, 1),
              todate=datetime.datetime(2020, 12, 31),  # month was in 12
              reverse=False
          )
      
          cerebro.adddata(data, name="AAPL")
      
          cerebro.broker.setcash(100000.0)
      
          cerebro.addobserver(bt.observers.Value)
          cerebro.addstrategy(LongAAPL)
      
          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())
      
          cerebro.plot()
      

      Screen Shot 2020-07-15 at 21.07.29.png

      1 Reply Last reply Reply Quote 0
      • Eduardo De La Garza
        Eduardo De La Garza last edited by

        Of course right now the lines would be the same since I'm buying AAPL 100% but I want so I can see the results after modifying the strategy. Thanks!

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

          Check out benchmark observer -
          Articles - Benchmarking
          Docs - Observers - Benchmarking

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }