{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.path.insert(0, \"../../\")\n",
"from pyecharts import online\n",
"online()\n",
"# 演示必要的准备代码,使用该库时不需重复此单元格命令"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import xalpha as xa\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 指数与基金信息"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 指数信息类"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"zzhli = xa.indexinfo('1399922') \n",
"# 指数注意需要给定的代码为七位,第一位数字代表市场情况,0 代表沪市, 1 代表深市\n",
"# 本例即为深市代码为 399922 的指数获取,对应中证红利指数"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" comment | \n",
" date | \n",
" netvalue | \n",
" totvalue | \n",
"
\n",
" \n",
" \n",
" \n",
" 3300 | \n",
" 0 | \n",
" 2018-08-01 | \n",
" 4.206851 | \n",
" 4129.2559 | \n",
"
\n",
" \n",
" 3301 | \n",
" 0 | \n",
" 2018-08-02 | \n",
" 4.122989 | \n",
" 4046.9400 | \n",
"
\n",
" \n",
" 3302 | \n",
" 0 | \n",
" 2018-08-03 | \n",
" 4.070295 | \n",
" 3995.2183 | \n",
"
\n",
" \n",
" 3303 | \n",
" 0 | \n",
" 2018-08-06 | \n",
" 4.017390 | \n",
" 3943.2896 | \n",
"
\n",
" \n",
" 3304 | \n",
" 0 | \n",
" 2018-08-07 | \n",
" 4.119750 | \n",
" 4043.7615 | \n",
"
\n",
" \n",
" 3305 | \n",
" 0 | \n",
" 2018-08-08 | \n",
" 4.063663 | \n",
" 3988.7085 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" comment date netvalue totvalue\n",
"3300 0 2018-08-01 4.206851 4129.2559\n",
"3301 0 2018-08-02 4.122989 4046.9400\n",
"3302 0 2018-08-03 4.070295 3995.2183\n",
"3303 0 2018-08-06 4.017390 3943.2896\n",
"3304 0 2018-08-07 4.119750 4043.7615\n",
"3305 0 2018-08-08 4.063663 3988.7085"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zzhli.price[zzhli.price['date']>='2018-08-01'] \n",
"# 指数的价格表,数据类型为 pandas.DataFrame, 净值栏为初始值归一化的1的值,总值栏对应指数的真实值,注释栏均为空"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('中证红利', '1399922')"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zzhli.name, zzhli.code # 指数类的部分属性"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"zzhli.bcmkset(xa.indexinfo('0000300'),start = '2014-01-01') \n",
"# 设定中证红利的比较基准为沪深300指数,以便于接下来更多量化分析\n",
"# 同时设定比较分析的区间段是从13年开始的"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zzhli.v_netvalue()\n",
"# 指数与基准指数的可视化\n",
"#二者都在研究区间的初始日归一"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.050747854601604941, 0.98038626706999821)"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zzhli.alpha(), zzhli.beta() # 计算中证红利指数相对于沪深300的 alpha 和 beta 收益"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.95681872181058214"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zzhli.correlation_coefficient('2018-01-01') # 计算两个指数截止2017年底的相关系数"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.24951040441292685, 0.24367852612323274)"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zzhli.algorithm_volatility(), zzhli.benchmark_volatility() \n",
"# 计算中证红利和基准沪深300,截止目前的年化波动率 "
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.1303, 0.080399999999999999)"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zzhli.total_annualized_returns(), zzhli.benchmark_annualized_returns() \n",
"# 计算中证红利和基准沪深300的年化收益率"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2.1074014053052625, 3.8186585488765177)"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zzhli.information_ratio('2015-06-01'), zzhli.sharpe('2015-06-01') \n",
"# 计算指数截止 2015-06-01 的信息比率和夏普比率"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* 简单的研究已就得到了充分的量化数据和可视化\n",
"* 同时可以轻松得出中证红利可以视为沪深300增强的结论\n",
"* 以此类推,利用该模块功能的一角,就可 diy 很多好玩的研究"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 基金信息类"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"fgzzhl=xa.fundinfo('100032')"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" comment | \n",
" date | \n",
" netvalue | \n",
" totvalue | \n",
"
\n",
" \n",
" \n",
" \n",
" 243 | \n",
" 0.245 | \n",
" 2010-01-14 | \n",
" 1.410 | \n",
" 1.863 | \n",
"
\n",
" \n",
" 488 | \n",
" 0.130 | \n",
" 2011-01-19 | \n",
" 1.151 | \n",
" 1.718 | \n",
"
\n",
" \n",
" 729 | \n",
" 0.026 | \n",
" 2012-01-16 | \n",
" 0.969 | \n",
" 1.542 | \n",
"
\n",
" \n",
" 1458 | \n",
" 0.056 | \n",
" 2015-01-20 | \n",
" 1.385 | \n",
" 2.073 | \n",
"
\n",
" \n",
" 1712 | \n",
" 0.350 | \n",
" 2016-02-02 | \n",
" 1.118 | \n",
" 2.167 | \n",
"
\n",
" \n",
" 1945 | \n",
" 0.300 | \n",
" 2017-01-17 | \n",
" 1.087 | \n",
" 2.469 | \n",
"
\n",
" \n",
" 2191 | \n",
" 0.188 | \n",
" 2018-01-18 | \n",
" 1.215 | \n",
" 2.825 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" comment date netvalue totvalue\n",
"243 0.245 2010-01-14 1.410 1.863\n",
"488 0.130 2011-01-19 1.151 1.718\n",
"729 0.026 2012-01-16 0.969 1.542\n",
"1458 0.056 2015-01-20 1.385 2.073\n",
"1712 0.350 2016-02-02 1.118 2.167\n",
"1945 0.300 2017-01-17 1.087 2.469\n",
"2191 0.188 2018-01-18 1.215 2.825"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fgzzhl.special # 发生分红或折算的细节"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"fund name: 富国中证红利指数增强\n",
"fund code: 100032\n",
"fund purchase fee: 0.15%\n",
"fund redemption fee info: ['小于7天', '1.50%', '大于等于7天', '0.50%']\n"
]
}
],
"source": [
"fgzzhl.info()"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Timestamp('2010-01-14 00:00:00'),\n",
" Timestamp('2011-01-19 00:00:00'),\n",
" Timestamp('2012-01-16 00:00:00'),\n",
" Timestamp('2015-01-20 00:00:00'),\n",
" Timestamp('2016-02-02 00:00:00'),\n",
" Timestamp('2017-01-17 00:00:00'),\n",
" Timestamp('2018-01-18 00:00:00')]"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fgzzhl.fenhongdate \n",
"# 基金分红日期"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fgzzhl.zhesuandate\n",
"# 该基金未发生过份额折算"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" comment | \n",
" date | \n",
" netvalue | \n",
" totvalue | \n",
"
\n",
" \n",
" \n",
" \n",
" 243 | \n",
" 0.245 | \n",
" 2010-01-14 | \n",
" 1.410 | \n",
" 1.863 | \n",
"
\n",
" \n",
" 488 | \n",
" 0.130 | \n",
" 2011-01-19 | \n",
" 1.151 | \n",
" 1.718 | \n",
"
\n",
" \n",
" 729 | \n",
" 0.026 | \n",
" 2012-01-16 | \n",
" 0.969 | \n",
" 1.542 | \n",
"
\n",
" \n",
" 1458 | \n",
" 0.056 | \n",
" 2015-01-20 | \n",
" 1.385 | \n",
" 2.073 | \n",
"
\n",
" \n",
" 1712 | \n",
" 0.350 | \n",
" 2016-02-02 | \n",
" 1.118 | \n",
" 2.167 | \n",
"
\n",
" \n",
" 1945 | \n",
" 0.300 | \n",
" 2017-01-17 | \n",
" 1.087 | \n",
" 2.469 | \n",
"
\n",
" \n",
" 2191 | \n",
" 0.188 | \n",
" 2018-01-18 | \n",
" 1.215 | \n",
" 2.825 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" comment date netvalue totvalue\n",
"243 0.245 2010-01-14 1.410 1.863\n",
"488 0.130 2011-01-19 1.151 1.718\n",
"729 0.026 2012-01-16 0.969 1.542\n",
"1458 0.056 2015-01-20 1.385 2.073\n",
"1712 0.350 2016-02-02 1.118 2.167\n",
"1945 0.300 2017-01-17 1.087 2.469\n",
"2191 0.188 2018-01-18 1.215 2.825"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fgzzhl.price[fgzzhl.price['date'].isin(fgzzhl.specialdate)]\n",
"# 基金产生分红或折算的日期对应的price表格\n",
"# 其中 comment 栏正数表示每份额分红的金额, 负数表示现在每份额折算的份额数,常见于分级连接基金"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* 同样的和指数类相似,基金类也可以用 .bcmkset() 来设置比较基准,既可以是指数也可以是基金,从而进行多样的量化和可视化分析\n",
"* 但需要注意的是像这种出现过分红的基金用净值进行分析相当于无复权数据,会出现偏差,即使使用总值栏数据,也未处理分红的得到的现金\n",
"* 强烈建议使用 policy 模块的 buyandhold 类来处理这种基金的效果分析, 具体细节请参考相关示例"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 虚拟货币基金类"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"除了指数类和基金类,为了方便,还准备了虚拟的货币基金类。\n",
"需要注意的是真实的货币基金无法用基金类获取,基金类只支持净值法计价的基金。"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"vca = xa.cashinfo(interest=0.00012, start='2018-01-01')\n",
"# 初始化一个日利率 0.00012,建立于 2018 年的货币基金"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" comment | \n",
" date | \n",
" netvalue | \n",
" totvalue | \n",
"
\n",
" \n",
" \n",
" \n",
" 218 | \n",
" 0 | \n",
" 2018-08-07 | \n",
" 1.026504 | \n",
" 1.026504 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" comment date netvalue totvalue\n",
"218 0 2018-08-07 1.026504 1.026504"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vca.price.iloc[-2:-1]"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('mf', '货币基金')"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vca.code, vca.name \n",
"# 虚拟货币基金的代码默认为 mf, 名称为货币基金"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"注意:货币基金类也可以作为基准或是标的,进行同上的量化分析"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 真实货币基金类"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"huobi = xa.mfundinfo('004331') # 拉取真实的货币基金数据"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"fund name: 太平日日鑫货币B\n",
"fund code: 004331\n",
"fund purchase fee: 0%\n"
]
}
],
"source": [
"huobi.info()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"huobi.bcmkset(xa.cashinfo(interest=0.00011),start='2017-05-01') # 和一个虚拟的货币基金比较"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.042599999999999999"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"huobi.total_annualized_returns() # 该货币基金的平均年化是 4.26%"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.94913838927244121"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"huobi.correlation_coefficient() # 和一个平稳的虚拟货币基金的关联是 0.95"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 增量IO\n",
"为了提升基金数据的复用率,减少获取数据的时间,也可以开启本地增量 IO 的选项,将数据本地化\n",
"\n",
"* csv 格式"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"ioconf={'save':True,'fetch':True,'path':'../../../sknife/dataset/', 'form':'csv'}\n",
"# 可以先生成IO的设置字典,save 字段表示每一个info对象会自动将信息本地化,fetch 字段表示初始化时先尝试本地获取, \n",
"# path 字段是存储的路径和前缀, form 字段表示存储的格式是 csv\n",
"# 指数,基金和货币基金类都可以使用这种增量更新策略来提速"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"no saved copy of this index\n"
]
}
],
"source": [
"zzhli = xa.indexinfo('1399922', **ioconf) # 如果是 fetch 时还没有存档,会提醒打印消息,之后按原初始化方法获取"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"zzhli = xa.indexinfo('1399922', **ioconf) # 数据本地化后,对象初始化速度大幅提升"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* sql 格式"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 需要借助 sqlalchemy 库, 实现方式如下\n",
"from sqlalchemy import create_engine\n",
"engine = create_engine('mysql://user:pass@host/db_name?charset=utf8')\n",
"sqlconf={'save':True,'fetch':True,'path':engine, 'form':'sql'}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"zzhli = xa.indexinfo('1399922', **sqlconf) # 设定好 sql 选项后,其他部分均和 csv 类似"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}