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

Run Multiple Data in For Loop



  • I am running multiple data in For loop Like this but I can't What's Wrong!
    The code

    result = []
    modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
    datapath = os.path.join(modpath, 'data')
    for file in tqdm(os.listdir(datapath), position=0):
        if file.endswith(".txt"):
            with open(f'data/{file}') as csv_file:
                row_count = sum(1 for row in csv_file)
                if row_count < 200:
                    continue
            path = os.path.join(modpath, f'data/{file}')
            data = bt.feeds.YahooFinanceCSVData(dataname=path,
                                                name=file,
                                                fromdate=datetime.datetime(2019, 1, 1),
                                                todate=datetime.datetime.now()
                                                )
            cerebro = bt.Cerebro()
            cerebro.broker.setcash(100000000)
            cerebro.addstrategy(TestStrategy)
            cerebro.addsizer(bt.sizers.FixedSize, stake=0.05)
            cerebro.broker.setcommission(commission=0.005)
            cerebro.addanalyzer(trade_list, _name='trade_list')
            cerebro.adddata(data)
            strats = cerebro.run(tradehistory=True)
            trade_list = strats[0].analyzers.trade_list.get_analysis()
            result.append(trade_list)
    

    and error is

    TypeError                                 Traceback (most recent call last)
    <ipython-input-5-7660bd8f46b7> in <module>
         21         cerebro.addanalyzer(trade_list, _name='trade_list')
         22         cerebro.adddata(data)
    ---> 23         strats = cerebro.run(tradehistory=True)
         24         trade_list = strats[0].analyzers.trade_list.get_analysis()
         25         result.append(trade_list)
    
    F:\Anaconda\lib\site-packages\backtrader\cerebro.py in run(self, **kwargs)
       1125             # let's skip process "spawning"
       1126             for iterstrat in iterstrats:
    -> 1127                 runstrat = self.runstrategies(iterstrat)
       1128                 self.runstrats.append(runstrat)
       1129                 if self._dooptimize:
    
    F:\Anaconda\lib\site-packages\backtrader\cerebro.py in runstrategies(self, iterstrat, predata)
       1255 
       1256                 for ancls, anargs, ankwargs in self.analyzers:
    -> 1257                     strat._addanalyzer(ancls, *anargs, **ankwargs)
       1258 
       1259                 sizer, sargs, skwargs = self.sizers.get(idx, defaultsizer)
    
    F:\Anaconda\lib\site-packages\backtrader\strategy.py in _addanalyzer(self, ancls, *anargs, **ankwargs)
        245         nsuffix = next(self._alnames[anname])
        246         anname += str(nsuffix or '')  # 0 (first instance) gets no suffix
    --> 247         analyzer = ancls(*anargs, **ankwargs)
        248         self.analyzers.append(analyzer, anname)
        249 
    
    TypeError: 'list' object is not callable
    


  • @sadegh said in Run Multiple Data in For Loop:

    cerebro.addanalyzer(trade_list, _name='trade_list')

    addanalyzer method expects the class name as a first argument. It seem the list was passed instead.


Log in to reply
 

});