Backtrader 文档学习- Analyzers - PyFolio

Backtrader 文档学习- Analyzers - PyFolio

As of (at least) 2017-07-25 the pyfolio APIs have changed and create_full_tear_sheet no longer has a gross_lev as a named argument. Consequently the sample for integration doesn’t work

自2017-07-25开始,pyfolio APIs发生了变化,create_full_tear_sheet不再将gross_lev作为命名参数。 因此集成的示例不起作用 。
就是 PyFolio 和 PyFolio Integration 中的示例都不能正常运行。

测试验证一下:

cerebro = bt.Cerebro()

# data
dataname = './datas/2005-2006-day-001.txt'
data = btfeeds.BacktraderCSVData(dataname=dataname)

ret_data = cerebro.adddata(data)

# strategy
cerebro.addstrategy(btstrats.SMA_CrossOver)
# add analyser
cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio')
results = cerebro.run()
strat = results[0]
pyfoliozer = strat.analyzers.getbyname('pyfolio')
returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items()

import pyfolio as pf
# create_returns_tear_sheet  create_full_tear_sheet
pf.create_full_tear_sheet(
    returns,
    positions=positions,
    transactions=transactions,
    gross_lev=gross_lev,
    live_start_date='2005-05-01' ,  # This date is sample specific
    round_trips=True)

报错信息:

TypeError                                 Traceback (most recent call last)
Cell In[15], line 20
     18 import pyfolio as pf
     19 # create_returns_tear_sheet  create_full_tear_sheet
---> 20 pf.create_full_tear_sheet(
     21     returns,
     22     positions=positions,
     23     transactions=transactions,
     24     gross_lev=gross_lev,
     25     live_start_date='2005-05-01' ,  # This date is sample specific
     26     round_trips=True)

TypeError: create_full_tear_sheet() got an unexpected keyword argument 'gross_lev'

参考 pyfolio 主页:

http://quantopian.github.io/pyfolio/:

看pyfolio示例,的确没有create_full_tear_sheet方法了。
示例:

# 导入PyFolio
import pyfolio as pf
%matplotlib inline

# silence warnings
import warnings
warnings.filterwarnings('ignore')

# 加载日线数据,facebook
stock_rets = pf.utils.get_symbol_rets('FB')

# 数据分析返回结果,并且做图分析
pf.create_returns_tear_sheet(stock_rets, live_start_date='2015-12-1')

尝试更换为BT的数据源,看看能否兼容:

#stock_rets = pf.utils.get_symbol_rets('FB')
ret_data = cerebro.adddata(data)
pf.create_returns_tear_sheet(ret_data, live_start_date='2015-12-1')

结果不出所料,报错:
还是数据源结构不同,不兼容。

AttributeError: 'NoneType' object has no attribute 'addindicator'

你可能感兴趣的:(数据分析,BackTrader,量化,BackTrader)