{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.path.insert(0, \"../../\")\n",
"from pyecharts import online\n",
"online()\n",
"# 演示必要的准备代码,使用该库时不需重复此单元格命令"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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": [
"zz500 = xa.indexinfo('0000905')\n",
"zz500b = xa.indexinfo('0000831')\n",
"# 采用中证500和500低贝进行研究"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"zz500.bcmkset(xa.cashinfo())\n",
"zz500b.bcmkset(xa.cashinfo())"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.31811907574774867, 0.29669632893344722)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zz500.algorithm_volatility(),zz500b.algorithm_volatility()\n",
"# 低波波动性低的也没那么明显"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.12470000000000001, 0.1726)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zz500.total_annualized_returns(),zz500b.total_annualized_returns()\n",
"# 不过低波的收益率可是强多了"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"auto = xa.policy.scheduled(zz500,1000,pd.date_range('2011-01-01','2015-01-01',freq='W-THU'))\n",
"autob = xa.policy.scheduled(zz500b,1000,pd.date_range('2011-01-01','2015-01-01',freq='W-THU'))\n",
"# 每周四定投1000元"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"zz500t = xa.trade(zz500,auto.status)\n",
"zz500bt = xa.trade(zz500b, autob.status)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.45607187160394663, 0.46458845644291197)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zz500t.xirrrate('2015-06-01'),zz500bt.xirrrate('2015-06-01')\n",
"# 到牛市顶部时,年化收益率竟然差不多"
]
},
{
"cell_type": "code",
"execution_count": 23,
"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",
" 0 | \n",
" 中证500 | \n",
" 0000905 | \n",
" 10.627208 | \n",
" 3.9089 | \n",
" 53211.76 | \n",
" 565492.42 | \n",
" 208000.0 | \n",
" 208000.0 | \n",
" 208000.0 | \n",
" 0.0 | \n",
" 0.113566 | \n",
" 357492.42 | \n",
" 171.8714 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 基金名称 基金代码 当日净值 单位成本 持有份额 基金现值 基金总申购 历史最大占用 \\\n",
"0 中证500 0000905 10.627208 3.9089 53211.76 565492.42 208000.0 208000.0 \n",
"\n",
" 基金持有成本 基金分红与赎回 换手率 基金收益总额 投资收益率 \n",
"0 208000.0 0.0 0.113566 357492.42 171.8714 "
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zz500t.dailyreport('2015-06-01')"
]
},
{
"cell_type": "code",
"execution_count": 24,
"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",
" 0 | \n",
" 500µÍ±´ | \n",
" 0000831 | \n",
" 14.630401 | \n",
" 5.2911 | \n",
" 39311.42 | \n",
" 575141.83 | \n",
" 208000.0 | \n",
" 208000.0 | \n",
" 208000.0 | \n",
" 0.0 | \n",
" 0.113566 | \n",
" 367141.83 | \n",
" 176.5105 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 基金名称 基金代码 当日净值 单位成本 持有份额 基金现值 基金总申购 \\\n",
"0 500µÍ±´ 0000831 14.630401 5.2911 39311.42 575141.83 208000.0 \n",
"\n",
" 历史最大占用 基金持有成本 基金分红与赎回 换手率 基金收益总额 投资收益率 \n",
"0 208000.0 208000.0 0.0 0.113566 367141.83 176.5105 "
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zz500bt.dailyreport('2015-06-01')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"由此可见,虽然低波的一次性投入对应的年化收益很高,但最终定投下来和普通500指数的收益差不多。。。。"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"# 还是换创业板暴力一发吧,让你们领略一下15年神创的风采\n",
"cyb = xa.indexinfo('1399006')"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"cyb.bcmkset(xa.cashinfo())"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.30730000000000002, 0.30155185277351393)"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cyb.total_annualized_returns('2015-06-01'),cyb.algorithm_volatility('2015-06-01')\n",
"# 令人眼红的收益率"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"autoc = xa.policy.scheduled(cyb,1000,pd.date_range('2011-01-01','2015-01-01',freq='W-THU'))"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"cybt = xa.trade(cyb,autoc.status)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"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",
" 0 | \n",
" 创业板指 | \n",
" 1399006 | \n",
" 3.821031 | \n",
" 0.9779 | \n",
" 212690.33 | \n",
" 812696.24 | \n",
" 208000.0 | \n",
" 208000.0 | \n",
" 208000.0 | \n",
" 0.0 | \n",
" 0.113566 | \n",
" 604696.24 | \n",
" 290.7193 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 基金名称 基金代码 当日净值 单位成本 持有份额 基金现值 基金总申购 历史最大占用 \\\n",
"0 创业板指 1399006 3.821031 0.9779 212690.33 812696.24 208000.0 208000.0 \n",
"\n",
" 基金持有成本 基金分红与赎回 换手率 基金收益总额 投资收益率 \n",
"0 208000.0 0.0 0.113566 604696.24 290.7193 "
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cybt.dailyreport('2015-06-01')\n",
"# 就问你怕不怕,同样的投入现金流,收益绝对值接近翻倍"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.6452718352308852"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cybt.xirrrate('2015-06-01') #60%多的年化"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.07698746449319872"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 不过如果不幸没有止盈,持有到现在\n",
"cybt.xirrrate()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"让我这次定投一个大蓝筹"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"sz50 = xa.indexinfo('0000016')"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"autod = xa.policy.scheduled(sz50,1000,pd.date_range('2011-01-01','2015-01-01',freq='W-THU'))"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"sz50t = xa.trade(sz50,autod.status)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"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",
" 0 | \n",
" 上证50 | \n",
" 0000016 | \n",
" 3.207919 | \n",
" 1.6901 | \n",
" 123070.13 | \n",
" 394798.98 | \n",
" 208000.0 | \n",
" 208000.0 | \n",
" 208000.0 | \n",
" 0.0 | \n",
" 0.113566 | \n",
" 186798.98 | \n",
" 89.8072 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 基金名称 基金代码 当日净值 单位成本 持有份额 基金现值 基金总申购 历史最大占用 \\\n",
"0 上证50 0000016 3.207919 1.6901 123070.13 394798.98 208000.0 208000.0 \n",
"\n",
" 基金持有成本 基金分红与赎回 换手率 基金收益总额 投资收益率 \n",
"0 208000.0 0.0 0.113566 186798.98 89.8072 "
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sz50t.dailyreport('2015-06-01')"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.28189135734500326"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sz50t.xirrrate('2015-06-01')\n",
"# 不愧江湖诨号大烂臭,收益在15年的牛市中远小于中小盘"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"sz50.bcmkset(xa.cashinfo(),start='2011-01-01')"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.2333094161336661"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sz50.algorithm_volatility('2015-06-01')\n",
"# 真点低波"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.06558331152460622"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 持有到现在,即使这几年大盘股反杀,依旧不如创业板\n",
"sz50t.xirrrate()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 试一下定期不定额的加强效果"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"autoe = xa.policy.scheduled_tune(zz500,600,\n",
" times=pd.date_range('2011-01-01','2015-01-01',freq='W-THU'),piece=[(3,5),(3.5,3),(4,2),(5.5,1)])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"zz500t2=xa.trade(zz500,autoe.status)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zz500t2.v_tradevolume() # 每周的买入情况"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zz500t2.v_tradecost(end='2015-06-01') # 定投对于成本的平滑\n",
"# 看似收益很高,但你真的能承受住开始定投后长达两年多的浮亏么,甚至很多至暗时刻,浮亏超过20%,能坚持下来么"
]
},
{
"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",
" 基金持有成本 | \n",
" 基金分红与赎回 | \n",
" 换手率 | \n",
" 基金收益总额 | \n",
" 投资收益率 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 中证500 | \n",
" 0000905 | \n",
" 2.853925 | \n",
" 3.6405 | \n",
" 31643.62 | \n",
" 90308.51 | \n",
" 115200.0 | \n",
" 115200.0 | \n",
" 115200.0 | \n",
" 0.0 | \n",
" 0.262968 | \n",
" -24891.49 | \n",
" -21.6072 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 基金名称 基金代码 当日净值 单位成本 持有份额 基金现值 基金总申购 历史最大占用 \\\n",
"0 中证500 0000905 2.853925 3.6405 31643.62 90308.51 115200.0 115200.0 \n",
"\n",
" 基金持有成本 基金分红与赎回 换手率 基金收益总额 投资收益率 \n",
"0 115200.0 0.0 0.262968 -24891.49 -21.6072 "
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zz500t2.dailyreport('2012-11-30') # 看一下至暗时刻的投资表现"
]
},
{
"cell_type": "code",
"execution_count": 11,
"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",
" 0 | \n",
" 中证500 | \n",
" 0000905 | \n",
" 10.627208 | \n",
" 3.6838 | \n",
" 63684.82 | \n",
" 676791.8 | \n",
" 234600.0 | \n",
" 234600.0 | \n",
" 234600.0 | \n",
" 0.0 | \n",
" 0.113566 | \n",
" 442191.8 | \n",
" 188.4876 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 基金名称 基金代码 当日净值 单位成本 持有份额 基金现值 基金总申购 历史最大占用 \\\n",
"0 中证500 0000905 10.627208 3.6838 63684.82 676791.8 234600.0 234600.0 \n",
"\n",
" 基金持有成本 基金分红与赎回 换手率 基金收益总额 投资收益率 \n",
"0 234600.0 0.0 0.113566 442191.8 188.4876 "
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zz500t2.dailyreport('2015-06-01') # 收益率略有提高"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.49963825079451923"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"zz500t2.xirrrate('2015-06-01') # 年化也小幅提高,不过即使对投资变额如此过优化,收益提升幅度还如此小,似乎意义不是很大"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"最后,通过改变 pd.data_range() 中 freq 的标签,比如 W-WED,或M,可以调整定投在周几定投,或是按月定投,这里就不大篇幅回测其表现对比了,结论就是:收益都差不多"
]
}
],
"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
}