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

bt.analyzers.PyFolio



  • Hi everyone, I came across a problem ! I have tow datas one is minutes and the other is resampled from minutes data.
    then I add "cerebro.addanalyzer(bt.analyzers.PyFolio)" and I got exceptions below. thank you for helping me!

    Traceback (most recent call last):
    File "E:/STOCK/test.py", line 109, in <module>
    cerebro.run()
    File "D:\Python\lib\site-packages\backtrader\cerebro.py", line 1127, in run
    runstrat = self.runstrategies(iterstrat)
    File "D:\Python\lib\site-packages\backtrader\cerebro.py", line 1298, in runstrategies
    self._runnext(runstrats)
    File "D:\Python\lib\site-packages\backtrader\cerebro.py", line 1630, in _runnext
    strat._next()
    File "D:\Python\lib\site-packages\backtrader\strategy.py", line 350, in _next
    self._next_analyzers(minperstatus)
    File "D:\Python\lib\site-packages\backtrader\strategy.py", line 388, in _next_analyzers
    analyzer._prenext()
    File "D:\Python\lib\site-packages\backtrader\analyzer.py", line 150, in _prenext
    child._prenext()
    File "D:\Python\lib\site-packages\backtrader\analyzer.py", line 152, in _prenext
    self.prenext()
    File "D:\Python\lib\site-packages\backtrader\analyzer.py", line 229, in prenext
    self.next()
    File "D:\Python\lib\site-packages\backtrader\analyzers\positions.py", line 78, in next
    pvals = [self.strategy.broker.get_value([d]) for d in self.datas]
    File "D:\Python\lib\site-packages\backtrader\analyzers\positions.py", line 78, in <listcomp>
    pvals = [self.strategy.broker.get_value([d]) for d in self.datas]
    File "D:\Python\lib\site-packages\backtrader\brokers\bbroker.py", line 422, in get_value
    return self._get_value(datas=datas, lever=lever)
    File "D:\Python\lib\site-packages\backtrader\brokers\bbroker.py", line 446, in _get_value
    dvalue = comminfo.getvaluesize(position.size, data.close[0])
    File "D:\Python\lib\site-packages\backtrader\linebuffer.py", line 163, in getitem
    return self.array[self.idx + ago]
    IndexError: array index out of range

    Process finished with exit code 1



  • Show us your datafeed code, make sure to use the three backticks ;)



  • data = pd.read_csv('Data/stock.csv', index_col=0, parse_dates=True)

    b1 = bt.feeds.PandasData(dataname=data,timeframe=bt.TimeFrame.Minutes )

    cerebro.adddata(b1)

    cerebro.resampledata(b1, timeframe=bt.TimeFrame.Days, compression=1)

    Thank you!



  • Best copy and paste your full code, and use the ```, so it looks like this and makes it easier to read:

    cerebro.adddata(b1)
    


  • @benmercerdev

    class MyStrategy(bt.Strategy):
        pass
    
    cerebro = bt.Cerebro()
    
    data = pd.read_csv('Data/stock.csv', index_col=0, parse_dates=True)
    
    b1 = bt.feeds.PandasData(dataname=data,timeframe=bt.TimeFrame.Minutes )
    
    cerebro.adddata(b1)
    cerebro.resampledata(b1,timeframe=bt.TimeFrame.Days )
    
    
    cerebro.addstrategy(MyStrategy)
    cerebro.addanalyzer(bt.analyzers.PyFolio)
    cerebro.run()
    
    cerebro.plot(style='candle')
    

    Traceback (most recent call last):
    File "E:/STOCK/test.py", line 52, in <module>
    cerebro.run()
    File "D:\Python\lib\site-packages\backtrader\cerebro.py", line 1127, in run
    runstrat = self.runstrategies(iterstrat)
    File "D:\Python\lib\site-packages\backtrader\cerebro.py", line 1298, in runstrategies
    self._runnext(runstrats)
    File "D:\Python\lib\site-packages\backtrader\cerebro.py", line 1630, in _runnext
    strat._next()
    File "D:\Python\lib\site-packages\backtrader\strategy.py", line 350, in _next
    self._next_analyzers(minperstatus)
    File "D:\Python\lib\site-packages\backtrader\strategy.py", line 388, in _next_analyzers
    analyzer._prenext()
    File "D:\Python\lib\site-packages\backtrader\analyzer.py", line 150, in _prenext
    child._prenext()
    File "D:\Python\lib\site-packages\backtrader\analyzer.py", line 152, in _prenext
    self.prenext()
    File "D:\Python\lib\site-packages\backtrader\analyzer.py", line 229, in prenext
    self.next()
    File "D:\Python\lib\site-packages\backtrader\analyzers\positions.py", line 78, in next
    pvals = [self.strategy.broker.get_value([d]) for d in self.datas]
    File "D:\Python\lib\site-packages\backtrader\analyzers\positions.py", line 78, in <listcomp>
    pvals = [self.strategy.broker.get_value([d]) for d in self.datas]
    File "D:\Python\lib\site-packages\backtrader\brokers\bbroker.py", line 422, in get_value
    return self._get_value(datas=datas, lever=lever)
    File "D:\Python\lib\site-packages\backtrader\brokers\bbroker.py", line 446, in _get_value
    dvalue = comminfo.getvaluesize(position.size, data.close[0])
    File "D:\Python\lib\site-packages\backtrader\linebuffer.py", line 163, in getitem
    return self.array[self.idx + ago]
    IndexError: array index out of range

    Process finished with exit code 1



  • I don't believe Pyfolio is supported any more. article



  • Pyfolio works with trades, profits etc. You have empty strategy, than what do you want to track with pyfolio?


Log in to reply
 

});