{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import sys\n", "\n", "sys.path.insert(0, \"../../\")\n", "from pyecharts import online\n", "\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": 8, "metadata": {}, "outputs": [], "source": [ "# 选取几个典型的500增强进行分析\n", "jianxin = xa.fundinfo(\"000478\")\n", "chuangjinhexin = xa.fundinfo(\"002311\")\n", "nanfang = xa.fundinfo(\"002906\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# 再选取中证500指数本身和普通500指数基金做比较\n", "zz500 = xa.indexinfo(\"0000905\")\n", "guangfa = xa.fundinfo(\"162711\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "analysis = xa.evaluate(\n", " jianxin, chuangjinhexin, nanfang, guangfa, zz500, start=\"2017-01-01\"\n", ")\n", "# 导入 evaluate 类统一比较" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "analysis.v_netvalue()\n", "# 同一个起点不同的轨迹,但还好都稳稳跑赢指数本身,说明增强确实有效果" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
0004780023110029061627110000905
0004781.0000000.9848140.9772780.9892580.988675
0023110.9848141.0000000.9698180.9836620.983937
0029060.9772780.9698181.0000000.9709490.970567
1627110.9892580.9836620.9709491.0000000.999471
00009050.9886750.9839370.9705670.9994711.000000
\n", "
" ], "text/plain": [ " 000478 002311 002906 162711 0000905\n", "000478 1.000000 0.984814 0.977278 0.989258 0.988675\n", "002311 0.984814 1.000000 0.969818 0.983662 0.983937\n", "002906 0.977278 0.969818 1.000000 0.970949 0.970567\n", "162711 0.989258 0.983662 0.970949 1.000000 0.999471\n", "0000905 0.988675 0.983937 0.970567 0.999471 1.000000" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "analysis.correlation_table() # 看一看各家之间的关联系数,在保持了对500的高相关的情形下,实现了超额收益" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "# 以表现最好的一只单独研究\n", "chuangjinhexin.bcmkset(zz500, start=\"2017-01-01\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(0.085534596312940553, 2.897434705553295)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chuangjinhexin.alpha(), chuangjinhexin.information_ratio() # 超额收益指标显著" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 按持有分析\n", "有些基金会大量分红,如果只按照净值来分析,就无法客观的描述其关联系数和超额收益,按总值分析,又忽略了分红再投入的收益,因此我们直接持有,按照分红再投入的策略模版交易,并形成可以净值刻画的封闭组合类来分析。" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# 这次以喜欢分红的跟踪中证红利的基金为例\n", "fuguo = xa.fundinfo(\"100032\")\n", "dacheng = xa.fundinfo(\"090010\")\n", "zzhl = xa.indexinfo(\"1399922\")" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
commentdatenetvaluetotvalue
2430.2452010-01-141.4101.863
4880.1302011-01-191.1511.718
7290.0262012-01-160.9691.542
14580.0562015-01-201.3852.073
17120.3502016-02-021.1182.167
19450.3002017-01-171.0872.469
21910.1882018-01-181.2152.825
\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": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fuguo.special # 富国多次分红,因此我们需要额外构造封闭组合类来模拟其真实的收益情况" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
commentdatenetvaluetotvalue
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: [comment, date, netvalue, totvalue]\n", "Index: []" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dacheng.special # 大成这只没有分过红" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "st = xa.policy.buyandhold(fuguo, \"2011-01-01\") # 制定一个一次性买入并一直持有,分红始终选择再投入的账单" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "fuguot = xa.trade(fuguo, st.status) # 按账单买入富国红利" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "fuguotclose = xa.mulfix(fuguot) # 为了可以净值刻画,将这笔交易嵌入总值和上笔相同的封闭投资系统中" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "fuguotclose.bcmkset(zzhl, start=\"2011-01-01\") # 通过设定基准,来生成该投资系统的净值" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "fuguotclose.name = \"富国中证红利增强\"\n", "fuguotclose.code = \"100032\"\n", "# 为了使该系统被 evaluate 类接受,还需要手动添加名字和代码属性" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "analysis_zzhl = xa.evaluate(fuguotclose, dacheng, zzhl, start=\"2011-01-05\")" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "analysis_zzhl.v_netvalue() # 富国很强,大家还是都能跑赢基准,\n", "# 这种高分红的指数跑赢基准很容易,因为普通指数不是全收益指数,没有计入分红部分,而指数基金投资是能收到分红的" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
1000320900101399922
1000321.0000000.9923890.99514
0900100.9923891.0000000.99686
13999220.9951400.9968601.00000
\n", "
" ], "text/plain": [ " 100032 090010 1399922\n", "100032 1.000000 0.992389 0.99514\n", "090010 0.992389 1.000000 0.99686\n", "1399922 0.995140 0.996860 1.00000" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "analysis_zzhl.correlation_table() # 关联性自然是杠杠的" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "# 你可能会问,如果直接有净值比会咋样,我们看一下可不可行\n", "analysis_wrong = xa.evaluate(fuguo, dacheng, zzhl, start=\"2011-01-01\")" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "analysis_wrong.v_netvalue() # 完全没办法比,富国的几次分红使得净值很低,无法看出真实的投资收益" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
1000320900101399922
1000321.0000000.8296110.831255
0900100.8296111.0000000.996860
13999220.8312550.9968601.000000
\n", "
" ], "text/plain": [ " 100032 090010 1399922\n", "100032 1.000000 0.829611 0.831255\n", "090010 0.829611 1.000000 0.996860\n", "1399922 0.831255 0.996860 1.000000" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "analysis_wrong.correlation_table() # 用净值比较,富国的关联性出现严重失真" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "对于出现过基金份额折算的基金,常见于各种分级基金母基,也需要按照上面的按持有分析的思路,构造净值系统进行比较,这里不再赘述" ] } ], "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 }