For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Data for Multiple Securities

  • 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,

    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),

  • In


    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.

Log in to reply