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/

    Data for Multiple Securities

    General Code/Help
    2
    2
    42
    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.
    • Dimitar Dimitrov
      Dimitar Dimitrov last edited by

      Hi guys,

      I am trying to add data for multiple securities. I thought using a for loop could help, but it looks like something is wrong. It sais that "data" is not defined. If i define it as a DataFrame it does not work either. Can you please help?

      Many thanks and regards,
      Dimitar

      import backtrader as bt
      import datetime
      import pandas_datareader as wb
      import pandas as pd
      
      
      
      if __name__=="__main__":
      
          cerebro = bt.Cerebro()
      
          symbols = ["SPY", "IWM", "QQQ", "EFA", "EEM","VNQ", "LQD", "GLD", "SHY", "IEF", "TLT", "AGG"]
      
          for i in symbols:
             data[i] = bt.feeds.YahooFinanceData(dataname=i, fromdate=datetime.datetime(2000,1,1), todate=datetime.datetime.today())
      
          cerebro.adddata(data)
      
          cerebro.run()
      
          cerebro.plot()
      
      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        In

        cerebro.adddata(data)
        

        data should be bt data feed object. In your script data is a list of data feed objects, but not a single data feed object. Each data feed should be added to cerebro using separate adddata call. Search the forum, look up the docs and articles, this was discussed tons of times and described well.

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