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

general help to set Datafeed correct (Multitimeframe & Multismymbols)



  • Hey guys,
    I am new to backtrader and I have trouble to fill in my data feed correct.

    So first of all I found really good Informations about Multitimeframe and Multisymbol Datafeed in the forum ( https://community.backtrader.com/topic/2285/multiple-timeframes-with-multiple-datafeeds
    ). In my case I orientated my coding style on this forum post.

    Here is my whole script to get an complete overview:

    import backtrader as bt
    import backtrader.feeds as btfeeds
    #import backtrader.indicators as btind
    import pandas as pd 
    import math
    
    
    class simpleStrategy1307(bt.Strategy):
        params = (
            ('Risk_perTrade', 0.01),
        )
    
        def __init__(self):
            self.m60_data = self.datas[::2]
            self.d1_data = self.datas[1::2]
    
            self.inds = dict()
            for i, d in enumerate(self.m60_data):
                self.inds[d] = dict()
                self.inds[d]['atr'] = bt.indicators.ATR(d)
                self.inds[d]['stoch'] = bt.indicators.Stochastic(d)
            self.daily_inds = dict()
            for i, d in enumerate(self.d1_data):
                self.daily_inds[d] = dict()
                self.daily_inds[d]['d_atr'] = bt.indicators.ATR(d)
    
    
            def next(self):
                # d = 60min & d1 = Daily Data
                # dn = 60Minuten Sock Name & d1n ist Daily Stock Name
                for i, (d, d1) in enumerate(zip(self.m60_data, self.d1_data)):
                    dt, (dn, d1n) = self.datetime.datetime(), (d._name, d1._name)
    
                    if self.inds[d]['atr'] > self.inds[d1]['atr']:
                        self.buy(d)
                    else:
                        self.sell(d)
                
    def runstrat():
    
        cerebro = bt.Cerebro()
    
        cerebro.addstrategy(simpleStrategy1307)
    
        
        # get all symbols
        # read symbols from csv and read path of Stock and Index 
        sheet = pd.read_csv("/Users/christian/Desktop/allgemein/Visual Studio/Grap StockData/Aktiensymbole/SPX_für_Tiingo.csv")
        Stock_60min_path = "/Users/christian/Desktop/allgemein/Visual Studio/Stock Data/S&P500 Aktien 1H/.csv"
        Index_Daily_path = "/Users/christian/Desktop/allgemein/Visual Studio/Stock Data/Index EOD Data/.csv"
        #make excel
        symbols 
        for i in range (0,len(sheet.index)):
            # in first column path f.e. "/Users/christianbrendel/Desktop/allgemein/Visual Studio/Stock Data/S&P500 Aktien 1H/A"
            symbols[i][0] = Stock_60min_path + sheet.iloc[i][0]
            # in second column  name f.e. "A" einfügen
            symbols[i][1] = sheet.iloc[i][0]
    
    
    # here I want to add Datafeed of all Stocks 60min
        for i in range(len(symbols)):
            dataframe = pandas.read_csv(symbols[i][0], index_col=0, 
                                        parse_dates=True, 
                                        dayfirst=True
                                        )
            data = PandasData(dataname=dataframe, timeframe=bt.TimeFrame.Minutes, compression=60)
            cerebro.adddata(data, name=symbols[i][1])
    
    # here I want to add the IndexDatafeed (Daily)
        Indexdataframe = pandas.read_csv(Index_Daily_path, index_col=0, 
                                    parse_dates=True, 
                                    dayfirst=True
                                    )
        Indexdata = PandasData(dataname=dataframe, timeframe=bt.TimeFrame.Days)
        cerebro.adddata(Indexdata, name="SPX")
    
    
        cerebro.run()
    
    if __name__ == "__main__":
        runstrat()
    
    

    Because I have a lot trouble in more parts, I am going to show you now each part with my detailed thinkings.
    1. I First I just added two Indicators into the strategy in the init. The ATR indicator and the stochastic indicator. I added them to the 60 Minutes and Daily timeframe.
    Is this way to implement 2 Dataframes correct? I just copied this passage but I am not 100% sure..

    
        def __init__(self):
            self.m60_data = self.datas[::2]
            self.d1_data = self.datas[1::2]
    
            self.inds = dict()
            for i, d in enumerate(self.m60_data):
                self.inds[d] = dict()
                self.inds[d]['atr'] = bt.indicators.ATR(d)
                self.inds[d]['stoch'] = bt.indicators.Stochastic(d)
            self.daily_inds = dict()
            for i, d in enumerate(self.d1_data):
                self.daily_inds[d] = dict()
                self.daily_inds[d]['d_atr'] = bt.indicators.ATR(d)
    
    

    2. in the Next function I make a for loop, which should go through every data feed and calculates just a simple buy for the strategy. Is this way to go through all datafeeds in the "next" function correct?

    def next(self):
        # d = 60min & d1 = Daily Data
        # dn = 60Minuten Sock Name & d1n ist Daily Stock Name
        for i, (d, d1) in enumerate(zip(self.m60_data, self.d1_data)):
            dt, (dn, d1n) = self.datetime.datetime(), (d._name, d1._name)
    
            if self.inds[d]['atr'] > self.inds[d1]['atr']:
                self.buy(d)
            else:
                self.sell(d)
    
    1. In the last part I want t run the strategy with Cerebro. I think here are the most errors... (the compiler also gives me the error message "Undefined variable 'symbols'")
      In This part I want to add all the data feeds to the cerebro. I tried this with looping through my list of symbols and than filling the "symbol" array with the path in the first column and the name in the second column. Than I additionally added the IndexData of the S&P500 as Daily Data. Is this code correct? And how can I solve the problem with the "Undefined variable 'symbols'" error message
    def runstrat():
    
        cerebro = bt.Cerebro()
    
        cerebro.addstrategy(simpleStrategy1307)
    
        
        # get all symbols
        # read symbols from csv and read path of Stock and Index 
        sheet = pd.read_csv("/Users/christianbrendel/Desktop/allgemein/Visual Studio/Grap StockData/Aktiensymbole/SPX_für_Tiingo.csv")
        Stock_60min_path = "/Users/christianbrendel/Desktop/allgemein/Visual Studio/Stock Data/S&P500 Aktien 1H/.csv"
        Index_Daily_path = "/Users/christianbrendel/Desktop/allgemein/Visual Studio/Stock Data/Index EOD Data/.csv"
        #erstelle excel
        symbols 
        for i in range (0,len(sheet.index)):
            # in erste Spalte path z.B. "/Users/christianbrendel/Desktop/allgemein/Visual Studio/Stock Data/S&P500 Aktien 1H/A"
            symbols[i][0] = Stock_60min_path + sheet.iloc[i][0]
            # in der zeiten spalte den Name z.B. "A" einfügen
            symbols[i][1] = sheet.iloc[i][0]
    
    
    
    
        for i in range(len(symbols)):
            dataframe = pandas.read_csv(symbols[i][0], index_col=0, 
                                        parse_dates=True, 
                                        dayfirst=True
                                        )
            data = PandasData(dataname=dataframe, timeframe=bt.TimeFrame.Minutes, compression=60)
            cerebro.adddata(data, name=symbols[i][1])
    
        Indexdataframe = pandas.read_csv(Index_Daily_path, index_col=0, 
                                    parse_dates=True, 
                                    dayfirst=True
                                    )
        Indexdata = PandasData(dataname=dataframe, timeframe=bt.TimeFrame.Days)
        cerebro.adddata(Indexdata, name="SPX")
    
    
        cerebro.run()
    

    thank you in advance, I know the question is a long one, but I just can't get anywhere with this problem alone. I have tried to ask the question as structured as possible.

    best regards
    Christian



  • I managed it to pass the error message with the symbol:
    I just declared the symbol like this:

        w, h = 2, len(sheet.index)
        symbols = [[0 for x in range(w)] for y in range(h)] 
    

    The code is running now, but I think it stops anywhere, because if I plot the equity it just plotts it ones and than nothing happens and the code don't stop running.

    What do you think about the 3 functions? Do you see something what is wrong?



  • Couple observations:

    • you add data the following way: first you add N 60-min data feeds, then add single SPX daily data, so you have N+1 data feeds in the system
    • in the init() you split them and consider every even data feed as 60 min data feed, and every odd data feed as daily data feed. which doesn't correlate with the way data feeds added.
    • in the next() you call ATR for 60 min data feed using the name from the init() initiaited daily data feed
    if self.inds[d]['atr'] > self.inds[d1]['atr']:
    
    • use [0] to get last ATR line value

    In general when you work on the complex scripts you try to split them on the pieces and code - debug separately. Also it would be useful to debug the script on the 2-3 data feeds and add more logging messages (sometimes after each operation or script line) in order to understand what is going on in the script.

    Statements like
    @chhrissi2909 said in general help to set Datafeed correct (Multitimeframe & Multismymbols):

    The code is running now, but I think it stops anywhere, because if I plot the equity it just plotts it ones and than nothing happens and the code don't stop running.

    doesn't bring any worth.



  • Hey @ab_trader
    thank you or your help. :) You are absolutely right.. I should add more logging messages..
    I think you know a lot about coding with Backtrader. Do you have a concrete tip for me how to set 3 different timeframes in one script with some symbols?

    Can you maby give me something like an example code, how I could do that.. Just the logic.. So that I understand how to do this in a simple war.

    best regards
    Christian



  • @chhrissi2909 said in general help to set Datafeed correct (Multitimeframe & Multismymbols):

    Do you have a concrete tip for me how to set 3 different timeframes in one script with some symbols?

    You may want to put more details in what are you inputs and what you want to achieve, cause (for example) one way to do this:

    # add data feeds into backtrader
    datas = ['ticker1_1min.txt', 'ticker2_1min.txt', 'ticker3_1min.txt']
    for d in datas:
        data = YourDataFeed(d)
        cerebro.adddata(data, timeframe=bt.TimeFrame.Minutes)
        cerebro.resample(data, timeframe=bt.TimeFrame.Minutes, compression=5) # 5 min data feed
        cerebro.resample(data, timeframe=bt.TimeFrame.Minutes, compression=30) # 30 min data feed
    

    Now you have self.datas list with length of 9 in the strategy where:
    items 1, 2, 3 - ticker1 1 min feed, ticker1 5 min feed and ticker1 30 min feed
    items 4, 5, 6 - ticker2 1 min feed, ticker2 5 min feed and ticker2 30 min feed
    items 7, 8, 9 - ticker3 1 min feed, ticker3 5 min feed and ticker3 30 min feed



  • cerebro.adddata should go with no timeframe parameter.



  • @chhrissi2909 said in general help to set Datafeed correct (Multitimeframe & Multismymbols):

    if self.inds[d]['atr'] > self.inds[d1]['atr']:

    Just trying to understand your code's logic. Shouldn't the above line be:

    if self.inds[d]['atr'] > self.daily_inds[d1]['d_atr']:
    

    definitely I misunderstand something, sorry.


Log in to reply
 

});