资源加载中... loading...

利用pyfolio工具评价回测资金曲线

Author: 雨幕(youquant), Created: 2020-03-11 15:34:16, Updated: 2024-06-13 14:24:56

img

1、前言

前些天发现优宽策略回测结果输出的盈亏曲线结果比较简单,故想着是否获取收益结果数据后自己再进行处理,得到更详细的资金曲线评估报告,并且用图形可视化展示出来。着手把想法写出来的时候发现并不是那么容易,故想是否有人也有相同的想法,并且已经做出相应的工具呢?故上网搜罗了一圈,发现确实有这类工具,在GitHub上看了好几个项目,最终选择了pyfolio这个工具。

2、pyfolio是什么

pyfolio是一个由quantinc .开发的用于金融投资组合的性能和风险分析的Python库。它可以很好地与Zipline开源回溯测试库一起工作。quant还为专业人士提供全面管理的服务,包括Zipline、Alphalens、Pyfolio、FactSet数据等。 pyfolio的核心是所谓的“ so-called tear sheet ”,它由各种各样的独立图组成,这些图提供了交易算法表现的综合图像。 GitHub地址:https://github.com/quantopian/pyfolio

3、学习使用pyfolio

由于该工具网上的学习资料比较少,自己摸索了好些时间才会简单的使用。 PyFolio API参考:https://www.quantopian.com/docs/api-reference/pyfolio-api-reference#pyfolio-api-reference 这里比较详细介绍了pyfolio的API,该平台可以做美股回测,回测结果可以直接通过pyfolio展示,我只粗略地学习了一下,貌似其他功能都挺强大的。

4、安装pyfolio

pyfolio的安装比较简单,按照GitHub上的说明安装即可。

5、优宽回测结果通过pyfolio展示

好了介绍就到这了,开始进入正题。首先在优宽获取回测资金曲线数据。 img 在回测结果的浮动盈亏图表中点击上图中全屏旁边的按钮,然后选择‘Download CSV’。 获取到的CSV数据,格式如下图(文件名根据自己需求更改): img 如果想要分析结果有比较的基准的话,则还需要准备一份交易标的的K线日线数据,当然如果没有K线数据只有收益数据也是可以分析的,只是有基准数据分析结果会多几个指标,例如:阿尔法(Alpha)、贝塔(Beta)等,下文按提供基准K线数据来写。 K线数据我们可以通过优宽研究环境直接从平台获取:

#使用优宽研究环境提供的API获取与收益数据等长的K线数据
dfh = get_bars('bitfinex.btc_usd', '1d', start=str(startd), end=str(endd))

数据准备完毕后,就可以写代码了。我们需要将获取到的数据进行处理,以使其符合pyfolio所需要的数据结构,然后调用pyfolio的create_returns_tear_sheet接口计算并输出结果。我们主要需要传入(returns,benchmark_rets=None,live_start_date=None)三个参数。 returns参数为必须的收益数据; benchmark_rets为基准数据,不是必须的; live_start_datelive_start_date,不是必须的,这个参数的意思就是,你的returns啥时候是开始实盘的?比如我们上面这一串returns,假设我们在2019-12-01之后是实盘,而前面是模拟盘或者是回测的结果,那么我们就可以这样设置:live_start_date = ‘2019-12-01’。通过设置该参数获得的结果,理论上我们可以分析一下我们的策略有没有过拟合。如果样本内外的差异很大,那么大概率这就是过拟合了。 我们可以在优宽研究环境下实现该分析功能,也可以在自己本地实现,下面以在优宽研究环境实现为例:

首先在自己本地新建“csv转成py代码.py”python文件,并将如下代码复制进去,以生成包含从FMZ下载的资金曲线CSV文件的py代码,本地运行新建的py文件会生成“chart_hex.py”文件。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import binascii

#文件名可根据需要自定义,本示例使用默认文件名
filename = 'chart.csv'
with open(filename, 'rb') as f:
    content = f.read()
#csv转py
wFile = open(filename.split('.')[0] + '_hex.py', "w")
wFile.write("hexstr = bytearray.fromhex('" +
            bytes.decode(binascii.hexlify(content))
            + "').decode()\nwFile = open('" + filename + "', 'w')\nwFile.write(hexstr)\nwFile.close()")
wFile.close()
打开上面生成的“chart_hex.py”文件,将里面的内容复制出来全部替换下面的代码块,然后逐一运行下面代码块,即可获取chart.csv文件
hexstr = bytearray.fromhex('efbbbf224461746554696d65222c22e6b5aee58aa8e79b88e4ba8f222c22e4ba8be4bbb6220a22323031392d31302d33312030303a30303a3030222c300a22323031392d31312d30312030303a30303a3030222c300a22323031392d31312d30322030303a30303a3030222c2d302e3032383434353837303635373338383930350a22323031392d31312d30332030303a30303a3030222c302e3030373431393439393432333839363936390a22323031392d31312d30342030303a30303a3030222c2d302e30323234373732373731373434313231370a22323031392d31312d30352030303a30303a3030222c2d302e30323033393930383333363836353735390a22323031392d31312d30362030303a30303a3030222c2d302e3034393935353039333230393332303435360a22323031392d31312d30372030303a30303a3030222c2d302e303434333232333634383035363033370a22323031392d31312d30382030303a30303a3030222c2d302e3032353631313934393330353935313637360a22323031392d31312d30392030303a30303a3030222c302e3032363331303433393432313739303536360a22323031392d31312d31302030303a30303a3030222c302e3033303232303332383333303436333137350a22323031392d31312d31312030303a30303a3030222c302e3033313230373133363936363633313133330a22323031392d31312d31322030303a30303a3030222c2d302e3031383533323831363136363038333135350a22323031392d31312d31332030303a30303a3030222c2d302e30313736393032353136363738333732320a22323031392d31312d31342030303a30303a3030222c2d302e3032323339313034373338373637393338360a22323031392d31312d31352030303a30303a3030222c2d302e3030383433363137313736363631333438370a22323031392d31312d31362030303a30303a3030222c302e3031373430363536343033313836383133330a22323031392d31312d31372030303a30303a3030222c302e303232393131353234343739303732330a22323031392d31312d31382030303a30303a3030222c302e3033323032363631303538383035373131340a22323031392d31312d31392030303a30303a3030222c302e303138393230323836383338373438380a22323031392d31312d32302030303a30303a3030222c302e30363632363938393337393232363738390a22323031392d31312d32312030303a30303a3030222c302e3036303835343430303337353130313033370a22323031392d31312d32322030303a30303a3030222c302e31343432363035363831333031303231330a22323031392d31312d32332030303a30303a3030222c302e32343239343037303935353332323336370a22323031392d31312d32342030303a30303a3030222c302e32313133303432303033353237373934310a22323031392d31312d32352030303a30303a3030222c302e323735363433303736313138343937380a22323031392d31312d32362030303a30303a3030222c302e323532343832323739343237363235360a22323031392d31312d32372030303a30303a3030222c302e32343931313136313839303039383437370a22323031392d31312d32382030303a30303a3030222c302e31313038373135373939323036393134310a22323031392d31312d32392030303a30303a3030222c302e313633343530313533373233393139390a22323031392d31312d33302030303a30303a3030222c302e31393838303132323332343735393737350a22323031392d31322d30312030303a30303a3030222c302e31363633373536393939313635393038350a22323031392d31322d30322030303a30303a3030222c302e32303638323732383333323337393630370a22323031392d31322d30332030303a30303a3030222c302e32303434323831303032303830393033320a22323031392d31322d30342030303a30303a3030222c302e323030353636323836353230383830360a22323031392d31322d30352030303a30303a3030222c302e31323434363439343330303739303635360a22323031392d31322d30362030303a30303a3030222c302e31303032343339383239393236303637332c302e31303032343339383239393236303637330a22323031392d31322d30372030303a30303a3030222c302e31303637313232383937343130373831360a22323031392d31322d30382030303a30303a3030222c302e31323839363336313133333032313036310a22323031392d31322d30392030303a30303a3030222c302e313337393030323234303239323136320a22323031392d31322d31302030303a30303a3030222c302e31313432333735383637323436303130350a22323031392d31322d31312030303a30303a3030222c302e31323638353037323134353130343038320a22323031392d31322d31322030303a30303a3030222c302e31343139333631313738343432333234330a22323031392d31322d31332030303a30303a3030222c302e31333838333632383537383138383536370a22323031392d31322d31342030303a30303a3030222c302e313136323031343031393435393734350a22323031392d31322d31352030303a30303a3030222c302e31363135333931303631363930313932330a22323031392d31322d31362030303a30303a3030222c302e31343937383138343836363238323231380a22323031392d31322d31372030303a30303a3030222c302e31353734393833333435363438393438320a22323031392d31322d31382030303a30303a3030222c302e32343234393031303233333139323635380a22323031392d31322d31392030303a30303a3030222c302e32313830363838353631363039303035350a22323031392d31322d32302030303a30303a3030222c302e323938383636303034333936303139340a22323031392d31322d32312030303a30303a3030222c302e33303135333036303934383834370a22323031392d31322d32322030303a30303a3030222c302e323938363835393334383634363038370a22323031392d31322d32332030303a30303a3030222c302e333039333035323733383735393130310a22323031392d31322d32342030303a30303a3030222c302e333834363231343935353136383931320a22323031392d31322d32352030303a30303a3030222c302e33343532373534363233383138313130360a22323031392d31322d32362030303a30303a3030222c302e33363235323332383833363737313035330a22323031392d31322d32372030303a30303a3030222c302e33343937363331393933333834333133360a22323031392d31322d32382030303a30303a3030222c302e33303732393733373234353434373938360a22323031392d31322d32392030303a30303a3030222c302e33323238383132323432363135363530370a22323031392d31322d33302030303a30303a3030222c302e33343134363537343239333438363535330a22323031392d31322d33312030303a30303a3030222c302e333435323733393139363237303738320a22323032302d30312d30312030303a30303a3030222c302e33353730313633323035353433343337340a22323032302d30312d30322030303a30303a3030222c302e33343937353937393034363236373934370a22323032302d30312d30332030303a30303a3030222c302e33373032333633333138303534353335370a22323032302d30312d30342030303a30303a3030222c302e33383636373137373837343037313635370a22323032302d30312d30352030303a30303a3030222c302e33383834373536373836393031343634330a22323032302d30312d30362030303a30303a3030222c302e34313331323236353139383433373731340a22323032302d30312d30372030303a30303a3030222c302e34323335323332383237303436333733350a22323032302d30312d30382030303a30303a3030222c302e34363837333531323838353035333330330a22323032302d30312d30392030303a30303a3030222c302e353436373135313832363033383332380a22323032302d30312d31302030303a30303a3030222c302e353530373037323136333937383830310a22323032302d30312d31312030303a30303a3030222c302e35353531373436393236393938310a22323032302d30312d31322030303a30303a3030222c302e353632323130363337343737323731330a22323032302d30312d31332030303a30303a3030222c302e353734373831373030393536383631370a22323032302d30312d31342030303a30303a3030222c302e353632383330303731353536353831350a22323032302d30312d31352030303a30303a3030222c302e363538323839383038313031393136380a22323032302d30312d31362030303a30303a3030222c302e363732323034393830303331333936370a22323032302d30312d31372030303a30303a3030222c302e363537313832383237323238323335380a22323032302d30312d31382030303a30303a3030222c302e363734393831383838383639373536330a22323032302d30312d31392030303a30303a3030222c302e363739373632303637393239383131330a22323032302d30312d32302030303a30303a3030222c302e363334313332373332393636313231370a22323032302d30312d32312030303a30303a3030222c302e363237353837313436323430323734370a22323032302d30312d32322030303a30303a3030222c302e363331313336373230353334393834370a22323032302d30312d32332030303a30303a3030222c302e3630313936323331393931343334360a22323032302d30312d32342030303a30303a3030222c302e363036343239313935383633313431360a22323032302d30312d32352030303a30303a3030222c302e35383130363933393531373337390a22323032302d30312d32362030303a30303a3030222c302e363133313034353130383436353937380a22323032302d30312d32372030303a30303a3030222c302e3632393938323638373737383035350a22323032302d30312d32382030303a30303a3030222c302e363831333134363734333130313533350a22323032302d30312d32392030303a30303a3030222c302e373134303533393533383834313233350a22323032302d30312d33302030303a30303a3030222c302e373433383032353331363031313135360a22323032302d30312d33312030303a30303a3030222c302e373535393639303935383539313330370a22323032302d30322d30312030303a30303a3030222c302e373533383030313630323737353438310a22323032302d30322d30322030303a30303a3030222c302e373534343434333437323732343132350a22323032302d30322d30332030303a30303a3030222c302e373435373138393532343434373738330a22323032302d30322d30342030303a30303a3030222c302e3738373636303035313130343530340a22323032302d30322d30352030303a30303a3030222c302e373935393939343930353732393834360a22323032302d30322d30362030303a30303a3030222c302e373935323037323039363636373034390a22323032302d30322d30372030303a30303a3030222c302e3832393234363232343838363336350a22323032302d30322d30382030303a30303a3030222c302e383239393034373635353939363035350a22323032302d30322d30392030303a30303a3030222c302e383338363639323137313033313436350a22323032302d30322d31302030303a30303a3030222c302e38353830313634373631380a22323032302d30322d31312030303a30303a3030222c302e383130323530393437393936313938330a22323032302d30322d31322030303a30303a3030222c302e383433323631313436333636313030320a22323032302d30322d31332030303a30303a3030222c302e383535383536353834363731333632320a22323032302d30322d31342030303a30303a3030222c302e383337323730363631383738303935360a22323032302d30322d31352030303a30303a3030222c302e383333353332343038383538303234330a22323032302d30322d31362030303a30303a3030222c302e383636383832343034353334343633320a22323032302d30322d31372030303a30303a3030222c302e383836363634323232323038333831310a22323032302d30322d31382030303a30303a3030222c302e393032363430303937303731373033390a22323032302d30322d31392030303a30303a3030222c302e383832373838333631373939333438380a22323032302d30322d32302030303a30303a3030222c302e383530303035363732363738333734320a22323032302d30322d32312030303a30303a3030222c302e3737383436363530373530313739360a22323032302d30322d32322030303a30303a3030222c302e373737383734393835393335313437350a22323032302d30322d32332030303a30303a3030222c302e373731333834393530303532383132330a22323032302d30322d32342030303a30303a3030222c302e373937383030363936353434323134340a22323032302d30322d32352030303a30303a3030222c302e373736383231373934313333363939370a22323032302d30322d32362030303a30303a3030222c302e373938353333313136353336313831310a22323032302d30322d32372030303a30303a3030222c302e383530343335363139343238353239390a22323032302d30322d32382030303a30303a3030222c302e383734333333393138383334393638310a22323032302d30322d32392030303a30303a3030222c302e3838383336363333393338343837380a22323032302d30332d30312030303a30303a3030222c302e383933393737393637343631333438380a22323032302d30332d30322030303a30303a3030222c302e3931323431323035313530303336362c302e3931323431323035313530303336360a22323032302d30332d30332030303a30303a3030222c302e383733353632323939353238363532330a22323032302d30332d30342030303a30303a3030222c302e383532353336353235333030343039310a22323032302d30332d30352030303a30303a3030222c302e383633323633313830363733313335350a22323032302d30332d30362030303a30303a3030222c302e383734303237343632353730373730350a22323032302d30332d30372030303a30303a3030222c302e383634323439323631363431353135360a22323032302d30332d30382030303a30303a3030222c302e38373630353132313331363135333031').decode()
wFile = open('chart.csv', 'w')
wFile.write(hexstr)
wFile.close()
!ls -la
cat chart.csv
# 在研究环境下安装pyfolio库
#在研究环境中安装pyfolio
!pip3 install --user pyfolio
import pandas as pd
import sys
sys.path.append('/home/quant/.local/lib/python3.6/site-packages')
import pyfolio as pf
import matplotlib.pyplot as plt
%matplotlib inline 
import warnings
warnings.filterwarnings('ignore')
from fmz import * # 导入所有FMZ函数

#读取资金曲线数据,FMZ平台下载,累计收益数据
df=pd.read_csv(filepath_or_buffer='chart.csv')
#转换为日期格式
df['Date'] = pd.to_datetime(df['DateTime'],format='%Y-%m-%d %H:%M:%S')
#获取开始与结束时间
startd = df.at[0,'Date']
endd = df.at[df.shape[0]-1,'Date']

#读取标的资产日K线数据,用作分析中的基准收益数据
#使用FMZ研究环境提供的API获取与收益数据等长的K线数据
dfh = get_bars('bitfinex.btc_usd', '1d', start=str(startd), end=str(endd))
dfh=dfh[['close']]
#以k线数据的收盘价计算日涨跌幅
dfh['close_shift'] = dfh['close'].shift(1)
dfh = dfh.fillna(method='bfill') # 向下寻找最近的一个非空值,以该值来填充确实的位置,全称backward fill
dfh['changeval']=dfh['close']-dfh['close_shift']
dfh['change']=dfh['changeval']/dfh['close_shift']
#涨跌幅保留6位小数
dfh = dfh.round({'change': 6})

#收益数据处理,FMZ平台获取的是累计收益,将其转换为日收益变化率
df['return_shift'] = df['浮动盈亏'].shift(1)
df['dayly']=df['浮动盈亏']-df['return_shift']
chushizichan = 3 #FMZ回测中的初始资产值
df['returns'] = df['dayly']/(df['return_shift']+chushizichan)
df=df[['Date','浮动盈亏','return_shift','dayly','returns']]
df = df.fillna(value=0.0)
df = df.round({'dayly': 3}) #保留三位小数
df = df.round({'returns': 6})

#将pd.DataFrame,转换为pyfolio收益所需的pd.Series
df['Date'] = pd.to_datetime(df['Date'])
df=df[['Date','returns']]
df.set_index('Date', inplace=True)
#处理好的收益数据
returns = df['returns'].tz_localize('UTC')

#将pd.DataFrame,转换为pyfolio基准收益所需的pd.Series
dfh=dfh[['change']]
dfh = pd.Series(dfh['change'].values, index=dfh.index)
#处理好的基准数据
benchmark_rets = dfh

#策略的回测期后开始进行实时交易的时间点。
live_start_date = '2020-02-01'

#调用pyfolio的API计算并输出资金曲线分析结果图
#returns参数是必须的,其余参数可以不输入
pf.create_returns_tear_sheet(returns,benchmark_rets=benchmark_rets,live_start_date=live_start_date)

输出的分析结果: img img

6、结果解读

输出的结果数据比较多,我们需要静下心来学习一下这些指标都是啥意思,输出结果都是英文显示的,英文比较少我们对照着翻译来看还是比较容易理解的,下面我介绍几个我找到的对相关指标的介绍,明白了指标的意思,我们才能解读我们的策略状态。

  • 年化收益(Annual return) 年化收益率是指把当前的收益率(日收益率、周收益率、月收益率等)换算成年收益率来计算的,是一种理论收益,并不是真正已经取得的收益率。年化收益率需要和年收益率区分开,年收益率是指策略执行一年的收益率,是实际的收益。
  • 累计收益(Cumulative returns) 最容易理解的一个概念,策略收益也就是策略开始到结束,总资产的变化率。 年化波动率(Annual volatility) 年化波动率用来衡量投资标的的波动风险。
  • 夏普率(Sharpe ratio) 描述的是策略在单位总风险下所能获得的超额收益。
  • 最大回撤(Max Drawdown) 描述的策略最大的亏损情况。最大回撤通常越小越好。
  • 欧米伽比率(Omega ratio) 另一种风险回报绩效指标。它相对于Sharpe比率的最大优势是-通过构造-它考虑了所有统计时刻,而Sharpe比率仅考虑前两个时刻。
  • 索提诺比率(Sortino) 描述的是策略在单位下行风险下所能获得的超额收益。
  • 每日风险价值(daily Value-at-Risk ) 每日风险价值-另一个非常流行的风险指标。在这种情况下,这表明在95%的情况下,将头寸(投资组合)再保留1天,损失不会超过1.8%。 参考:https://towardsdatascience.com/the-easiest-way-to-evaluate-the-performance-of-trading-strategies-in-python-4959fd798bb3
  • Tail ratio 对daily return的分布选取95分位和5分位,然后相除取绝对值。本质的含义就是赚取的return比亏钱的大多少倍。
  • 稳定性(stability) 也就是所谓的稳定性。其实很简单,就是时间增量对累计净值的解释力度是多少,也就是回归的r平方。这样说有点抽象,我们简单解释一下。 参考:https://blog.csdn.net/qtlyx/article/details/88724236

7、小小的建议

希望优宽能够也能够增加丰富资金曲线的评价功能,并且增加历史回测结果的储存功能,这样能够更方便和更专业地展示回测结果,帮助大家创造出更优秀的策略。

8、感谢优宽平台

让我在这里学到了很多量化知识以及各种技能,感谢小小梦老师、张超大神悉心指导!希望优宽平台越来越好! 本人菜鸟一枚,初次作文,还望大佬们多多指教。!


相关内容

更多内容