免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
empyrical、pyfolio工具介紹及在backtrader量化框架中使用

簡介

  • empyrical -- Quantopian開源的常見金融風(fēng)險(xiǎn)指標(biāo)lib庫,會調(diào)用pandas_datareader從yahoo或google獲取股票數(shù)據(jù)
  • pyfolio -- Quantopian開源的用圖形表示的金融投資組合性能和風(fēng)險(xiǎn)分析的Python庫,基于empyrical獲取數(shù)據(jù)和計(jì)算基礎(chǔ)指標(biāo),由各種各樣的獨(dú)立圖組成,這些圖提供了交易策略表現(xiàn)的綜合圖像。圖形示例可以參考full_tear_sheet_example.ipynb

empyrical安裝&使用

  • 安裝:pip install empyrical
  • 使用方法:以計(jì)算最大回撤和alpha、beta為例,代碼如下。
import numpy as np
from empyrical import max_drawdown, alpha_beta
returns = np.array([.01.02.03-.4-.06-.02])
benchmark_returns = np.array([.02.02.03-.35-.05-.01])
max_drawdown(returns) # 計(jì)算最大回撤
alpha, beta = alpha_beta(returns, benchmark_returns) # 計(jì)算alpha和beta
returns = pd.Series([.01.02.03-.4-.06-.02]) #支持pd.Series()
max_drawdown(returns)

查看empyrical支持的api列表,如下。

import empyrical
print(dir(empyrical))

結(jié)果是:

['DAILY''MONTHLY''QUARTERLY''WEEKLY''YEARLY''__builtins__''__cached__''__doc__''__file__''__loader__''__name__''__package__''__path__''__spec__''__version__''_version''aggregate_returns''alpha''alpha_aligned''alpha_beta''alpha_beta_aligned''annual_return''annual_volatility''beta''beta_aligned''beta_fragility_heuristic''beta_fragility_heuristic_aligned''cagr''calmar_ratio''capture''compute_exposures''conditional_value_at_risk''cum_returns''cum_returns_final''deprecate''down_alpha_beta''down_capture''downside_risk''excess_sharpe''gpd_risk_estimates''gpd_risk_estimates_aligned''max_drawdown''omega_ratio''perf_attrib''periods''roll_alpha''roll_alpha_aligned''roll_alpha_beta''roll_alpha_beta_aligned''roll_annual_volatility''roll_beta''roll_beta_aligned''roll_down_capture''roll_max_drawdown''roll_sharpe_ratio''roll_sortino_ratio''roll_up_capture''roll_up_down_capture''sharpe_ratio''simple_returns''sortino_ratio''stability_of_timeseries''stats''tail_ratio''up_alpha_beta''up_capture''up_down_capture''utils''value_at_risk']

順便說明下,empyrical官方api手冊不全,看api文檔使用help來看比較方便,如help(empyrical.excess_sharpe)

pyfolio安裝&使用

pyfolio只能在jupyter notebook環(huán)境下完整繪圖,在命令行環(huán)境或py文件下可以單獨(dú)畫圖,但不能顯示表格數(shù)據(jù)。

通常的誤區(qū)是:pyfolio只能在jupyter下使用。正確的說法是pyfolio對jupyter環(huán)境下支持的最好。

  • 安裝:
pip install git+https://github.com/quantopian/pyfolio

使用pip install pyfolio,在使用的時(shí)候會報(bào)錯(cuò),下面會說明。

  • 使用方法:
  1. 構(gòu)造數(shù)據(jù) 產(chǎn)出如下數(shù)據(jù),格式為pd.series。
data.head()
Date
2017-04-25 00:00:00+00:00    0.018699
2017-04-26 00:00:00+00:00   -0.011536
2017-04-27 00:00:00+00:00   -0.004965
2017-04-28 00:00:00+00:00    0.017626
2017-05-01 00:00:00+00:00    0.027892
dtype: float64
  1. 使用pyfolio
import pyfolio as pf
pf.create_returns_tear_sheet(data)
pf.plot_drawdown_periods(data)

在jupyter中運(yùn)行,結(jié)果如下:

在ipython運(yùn)行,結(jié)果如下:

In [8]: pf.create_returns_tear_sheet(data)
<IPython.core.display.HTML object>
<IPython.core.display.HTML object>

In [9]: ret = pf.create_returns_tear_sheet(data)
<IPython.core.display.HTML object>
<IPython.core.display.HTML object>

pf.plot_drawdown_periods(data)

import matplotlib.pyplot as plt

plt.show()
  • 查看支持的函數(shù):
print(dir(pf))

['APPROX_BDAYS_PER_MONTH', 'FACTOR_PARTITIONS', 'FigureCanvasAgg', 'FuncFormatter', 'MM_DISPLAY_UNIT', 'Markdown', 'OrderedDict', 'STAT_FUNCS_PCT', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_seaborn', '_version', 'axes_style', 'capacity', 'create_capacity_tear_sheet', 'create_full_tear_sheet', 'create_interesting_times_tear_sheet', 'create_perf_attrib_tear_sheet', 'create_position_tear_sheet', 'create_returns_tear_sheet', 'create_round_trip_tear_sheet', 'create_simple_tear_sheet', 'create_txn_tear_sheet', 'customize', 'datetime', 'deprecate', 'display', 'division', 'ep', 'figure', 'gridspec', 'interesting_periods', 'matplotlib', 'np', 'patches', 'pd', 'perf_attrib', 'plot_annual_returns', 'plot_capacity_sweep', 'plot_cones', 'plot_daily_turnover_hist', 'plot_daily_volume', 'plot_drawdown_periods', 'plot_drawdown_underwater', 'plot_exposures', 'plot_gross_leverage', 'plot_holdings', 'plot_long_short_holdings', 'plot_max_median_position_concentration', 'plot_monthly_returns_dist', 'plot_monthly_returns_heatmap', 'plot_monthly_returns_timeseries', 'plot_perf_stats', 'plot_prob_profit_trade', 'plot_return_quantiles', 'plot_returns', 'plot_rolling_beta', 'plot_rolling_returns', 'plot_rolling_sharpe', 'plot_rolling_volatility', 'plot_round_trip_lifetimes', 'plot_sector_allocations', 'plot_slippage_sensitivity', 'plot_slippage_sweep', 'plot_turnover', 'plot_txn_time_hist', 'plotting', 'plotting_context', 'plt', 'pos', 'pytz', 'round_trips', 'show_and_plot_top_positions', 'show_perf_stats', 'show_profit_attribution', 'show_worst_drawdown_periods', 'sns', 'sp', 'tears', 'time', 'timer', 'timeseries', 'txn', 'utils', 'warnings', 'wraps']

  • 常見錯(cuò)誤
  1. 報(bào)錯(cuò):ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

解決辦法:pf.create_returns_tear_sheet()的參數(shù)是pd.series,不能是pd.dataframe

  1. 報(bào)錯(cuò):AttributeError: 'numpy.int64' object has no attribute 'to_pydatetime'

解決辦法:

pip install git+https://github.com/quantopian/pyfolio

在backtrader中使用PyFolio

pyfolio改變過api接口,create_full_tear_sheet()函數(shù)后面沒有了gross_lev參數(shù),使用backtrade網(wǎng)站的示例代碼會報(bào)錯(cuò)。參考pyfolio官方的使用文檔即可。

backtrader使用pyfolio示例代碼如下:

cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio')
strats = cerebro.run()
strat0 = strats[0]
pyfolio = strats.analyzers.getbyname('pyfolio')
returns, positions, transactions, gross_lev = pyfoliozer.get_pf_items()
import pyfolio as pf
pf.create_full_tear_sheet(returns)

交流

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Backtrader版本“積木式”AI量化回測系統(tǒng)(全部源代碼+數(shù)據(jù)下載)
細(xì)數(shù)7大算法交易工具
beta收益和alpha收益是什么意思?
文件同步更新之Rsync算法的探討
太陽活動預(yù)報(bào)中心
七葉皂苷鈉
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服