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

Cant get multiple data feeds to work



  • Iv been trying to import multiple data feeds into cerebro.addddata and it seems to work however when i took a closer look, my trades were being clipped off by the shortest data feed. I am importing my data in this way because I am going to be importing many tickers at once and it wont always be the same number. So it would be tedious to do each one individually without the loop. Below i have an example of a very simplified version of what im working with.

    import datetime  
    import os.path  
    import sys 
    import pandas as pd
    import backtrader.feeds as btfeeds
    import pandas_datareader.data as web
    import backtrader as bt 
    import backtrader.indicators as btind
    import backtrader.analyzers as btanalyzers
    
    class Algo(bt.Strategy):
        
        def __init__(self):
            pass
    
        def next(self):
            for i, d in enumerate(self.datas): 
                self.symbol = self.datas[i]._name
                self.Date = self.datas[i].datetime.date
                print (self.Date(0), self.symbol)
    
    if __name__ == '__main__': 
      
    #Cerebro start + add strat:    
        cerebro = bt.Cerebro()
        cerebro.addstrategy(Algo)
    
        stocks = ['FB','AAPL']
        start_date = [datetime.date(2017,7,24),datetime.date(2017,8,2)]
        
        for i,b in zip(stocks,start_date): 
            stock = i
            start = b
            end_date = datetime.date(2017,8,7)
    
            DF_1 = web.DataReader(stock, "morningstar", start, end_date)
            DF_1= pd.DataFrame(DF_1)
            DF_1.reset_index(level = 'Symbol', inplace=True,drop=False)
            data = bt.feeds.PandasData(dataname = DF_1)
            cerebro.adddata(data, name = i)   
              
        cerebro.run()        
               
    

    Here you can see that i am only getting prints for as far back as the AAPL data goes. However the facebook data goes back further and when I am running trades it is omitting the trades on those dates.

    [Running] python "c:\Python36\.vscode\forum qustion.py"
    2017-08-02 FB
    2017-08-02 AAPL
    2017-08-03 FB
    2017-08-03 AAPL
    2017-08-04 FB
    2017-08-04 AAPL
    2017-08-07 FB
    2017-08-07 AAPL
    

    When I run facebook on its own, it produces all of the dates the data has.

    [Running] python "c:\Python36\.vscode\forum qustion.py"
    2017-07-24 FB
    2017-07-25 FB
    2017-07-26 FB
    2017-07-27 FB
    2017-07-28 FB
    2017-07-31 FB
    2017-08-01 FB
    2017-08-02 FB
    2017-08-03 FB
    2017-08-04 FB
    2017-08-07 FB
    

    Any clue on what to do? Also does anyone have any idea how to get all the trades/outputs from each data set to output by group instead of by date? Id like to see all the Facebook Prints and then all of the AAPL ones after that.

    Thanks for the help in advance.



  • It was already discussed several times. Use prenext method along with next method, but check if data available in prenext.


  • administrators

    @sagittarius19 said in Cant get multiple data feeds to work:

    Also does anyone have any idea how to get all the trades/outputs from each data set to output by group instead of by date?

    Things in trading happen by date. If you want some other grouping, you'll have to store the information and do whatever grouping you want to do, after trading has ended (be it real or backtesting)

    @sagittarius19 said in Cant get multiple data feeds to work:

    Here you can see that i am only getting prints for as far back as the AAPL data goes. However the facebook data goes back further and when I am running trades it is omitting the trades on those dates.

    No. You haven't understood the basics of the platform. The system won't enter the next state (as indicated by @ab_trader) until everything has reached its minimum period (aka warm-up period). This is a guarantee that access to the data feeds and indicators is possible.



  • i do the following, lots of ways to do it though and all depends on what type of indicators you have. this solution is just a data check example, you still need to do indicator checks.

    def prenext(self):
        self.next()
        
    def next(self):
        if (len(self.datas) > 0):

  • administrators

    @blonc said in Cant get multiple data feeds to work:

    def next(self):
        if (len(self.datas) > 0):
    

    That check will always return: True. Because if the system contains no data feeds it won't start (and it will therefore never get to the next method of the strategy)

    You have to check each data feed (and each indicator) individually.

    > def next(self):
      for i in enumerate(self.datas):
            if (len(self.datas[i]) > 0):
    

    Where i obviously comes out of a loop.



  • @backtrader I just got it. Thanks for pointing this out!