{ "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": [ "path='../../tests/demo.csv'\n", "read = xa.record(path) \n", "# 依旧先读取交易单的 csv 文件" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "sysopen = xa.mul(status=read.status) \n", "# 基金投资组合既可以由一系列单个基金的交易类生成, 也可以直接依据给定的交易单一次性生成,这里采取后者" ] }, { "cell_type": "code", "execution_count": 5, "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", " \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", "
基金名称基金代码当日净值单位成本持有份额基金现值基金总申购历史最大占用基金持有成本基金分红与赎回换手率基金收益总额投资收益率
5总计totalNaNNaNNaN2545.384197.03775.092695.611501.390.366986-150.23-3.9795
2富国中证红利指数增强1000321.03300.99691638.711692.791895.01633.661633.66261.340.35373059.133.6195
0广发医药卫生联接A0011800.80900.8310630.39509.99600.0523.86523.8676.140.345889-13.87-2.6477
4工银中证传媒指数分级1648180.96071.3460356.62342.60480.0480.00480.000.000.267988-137.40-28.6250
1广发中债7-10年国开债指数A0033760.99840.00000.000.00742.0742.00-26.29768.291.25072826.293.5431
3广发中证全指金融地产联接A0014690.88910.00000.000.00480.0480.0084.38395.621.381403-84.38-17.5792
\n", "
" ], "text/plain": [ " 基金名称 基金代码 当日净值 单位成本 持有份额 基金现值 基金总申购 历史最大占用 \\\n", "5 总计 total NaN NaN NaN 2545.38 4197.0 3775.09 \n", "2 富国中证红利指数增强 100032 1.0330 0.9969 1638.71 1692.79 1895.0 1633.66 \n", "0 广发医药卫生联接A 001180 0.8090 0.8310 630.39 509.99 600.0 523.86 \n", "4 工银中证传媒指数分级 164818 0.9607 1.3460 356.62 342.60 480.0 480.00 \n", "1 广发中债7-10年国开债指数A 003376 0.9984 0.0000 0.00 0.00 742.0 742.00 \n", "3 广发中证全指金融地产联接A 001469 0.8891 0.0000 0.00 0.00 480.0 480.00 \n", "\n", " 基金持有成本 基金分红与赎回 换手率 基金收益总额 投资收益率 \n", "5 2695.61 1501.39 0.366986 -150.23 -3.9795 \n", "2 1633.66 261.34 0.353730 59.13 3.6195 \n", "0 523.86 76.14 0.345889 -13.87 -2.6477 \n", "4 480.00 0.00 0.267988 -137.40 -28.6250 \n", "1 -26.29 768.29 1.250728 26.29 3.5431 \n", "3 84.38 395.62 1.381403 -84.38 -17.5792 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sysopen.combsummary().sort_values(by=\"基金现值\",ascending=False) # 先看一下今日投资组合概述" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.07499131908930949" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sysopen.xirrrate() # 最重要的总是先问一下系统的整体收益年化如何\n", "# 需要注意由于QDII基金的净值会延迟两天,因此包含 QDII 基金投资的 xirrrate 计算,必须指定日期,最晚为前天,默认的昨天计算会报错" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sysopen.v_positions('2018-06-01') # 可视化指定日期的仓位分布" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sysopen.v_positions_history() # 基金组合就像一条河流。。。" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sysopen.v_tradevolume(bar_category_gap='75%',freq='W') \n", "# 整体交易情况一览, 以周为柱形图单位" ] }, { "cell_type": "code", "execution_count": 10, "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", "
cashdate
0-1170.002016-09-28
1157.542017-01-17
2-625.002017-10-17
3-442.062017-12-12
4-300.002018-01-18
5-1395.572018-07-05
61079.482018-07-26
\n", "
" ], "text/plain": [ " cash date\n", "0 -1170.00 2016-09-28\n", "1 157.54 2017-01-17\n", "2 -625.00 2017-10-17\n", "3 -442.06 2017-12-12\n", "4 -300.00 2018-01-18\n", "5 -1395.57 2018-07-05\n", "6 1079.48 2018-07-26" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sysopen.totcftable # 基金组合的合并现金流量表" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "ev=sysopen.evaluation(start='2018-01-01') # 还可以生成关于投资基金的 evaluate 类,以便对投资的基金本身进行分析" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ev.v_correlation(end='2018-08-01') #可以通过热力图来查看不同基金净值走势的相关性" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 封闭系统\n", "我们也可以研究一个封闭系统的组合投资,此时需要货币基金提供资金出入的功能,这样的系统净值更容易定义\n", "可以应用之前研究指数和基金时使用过的量化方法。" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(广发医药卫生联接A 交易情况,\n", " 广发中债7-10年国开债指数A 交易情况,\n", " 富国中证红利指数增强 交易情况,\n", " 广发中证全指金融地产联接A 交易情况,\n", " 工银中证传媒指数分级 交易情况)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sysopen.fundtradeobj" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# 这次我们使用基金交易类作为初始化参数\n", "sysclose = xa.mulfix(*sysopen.fundtradeobj, totmoney=5000) \n", "# totmoney 表示封闭系统初始的总钱数\n", "# 若不设置 cashobj 选项,则默认为 cashobj = cashinfo(interest=0.0001, start='2012-01-01')" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.016426" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sysclose.unitvalue('2018-07-04') # 封闭的基金投资组合整体就像一个单一基金交易类,有净值数据" ] }, { "cell_type": "code", "execution_count": 15, "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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
基金名称基金代码当日净值单位成本持有份额基金现值基金总申购历史最大占用基金持有成本基金分红与赎回换手率基金收益总额投资收益率
0广发医药卫生联接A0011800.8764000.8134275.20241.19300.00300.00223.8676.140.43419117.335.77670
1广发中债7-10年国开债指数A0033760.9403000.9424575.14540.80542.00542.00542.000.001.276224-1.20-0.22140
2富国中证红利指数增强1000321.1780000.9843877.471033.661125.00863.66863.66261.340.555878170.0019.68370
3广发中证全指金融地产联接A0014691.0593001.0758446.20472.66480.00480.00480.000.002.097701-7.34-1.52920
4工银中证传媒指数分级1648180.8296001.1980225.38186.98270.00270.00270.000.000.346300-83.02-30.74810
5货币基金mf1.2534361.16472249.862820.063987.543987.542620.481367.060.465023199.585.00510
6总计totalNaNNaNNaN5295.356704.545000.005000.001704.540.346300295.350.05907
\n", "
" ], "text/plain": [ " 基金名称 基金代码 当日净值 单位成本 持有份额 基金现值 基金总申购 \\\n", "0 广发医药卫生联接A 001180 0.876400 0.8134 275.20 241.19 300.00 \n", "1 广发中债7-10年国开债指数A 003376 0.940300 0.9424 575.14 540.80 542.00 \n", "2 富国中证红利指数增强 100032 1.178000 0.9843 877.47 1033.66 1125.00 \n", "3 广发中证全指金融地产联接A 001469 1.059300 1.0758 446.20 472.66 480.00 \n", "4 工银中证传媒指数分级 164818 0.829600 1.1980 225.38 186.98 270.00 \n", "5 货币基金 mf 1.253436 1.1647 2249.86 2820.06 3987.54 \n", "6 总计 total NaN NaN NaN 5295.35 6704.54 \n", "\n", " 历史最大占用 基金持有成本 基金分红与赎回 换手率 基金收益总额 投资收益率 \n", "0 300.00 223.86 76.14 0.434191 17.33 5.77670 \n", "1 542.00 542.00 0.00 1.276224 -1.20 -0.22140 \n", "2 863.66 863.66 261.34 0.555878 170.00 19.68370 \n", "3 480.00 480.00 0.00 2.097701 -7.34 -1.52920 \n", "4 270.00 270.00 0.00 0.346300 -83.02 -30.74810 \n", "5 3987.54 2620.48 1367.06 0.465023 199.58 5.00510 \n", "6 5000.00 5000.00 1704.54 0.346300 295.35 0.05907 " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sysclose.combsummary('2018-03-09') # 投资组合的分项纵览" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "sysclose.bcmkset(xa.indexinfo('0000905')) # 设置比较基准,开始对此封闭的基金投资系统进行量化评估" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sysclose.v_netvalue()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(-0.002218844426589677, 0.19121239655826425, -0.79728321041646089)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sysclose.alpha(),sysclose.beta(),sysclose.sharpe()" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(Timestamp('2018-01-24 00:00:00'),\n", " Timestamp('2018-08-06 00:00:00'),\n", " -0.06905136122288996)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sysclose.max_drawdown() # 系统的最大回撤" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0371724" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sysclose.riskfree # 进行量化计算中使用的无风险年化利率的参考值, 可在设置 benchmark 时调整该值" ] } ], "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 }