指数投资方式中有四种基本的方法,分别是定期定额、定期不定额、不定期定额和不定期不定额,这四种方式投资效果不同,对投资者的要求也不同,定期定额最简单,但收益不算高,不定期不定额最复杂,对投资者的要求最高,特别是对情绪的要求非常高,同时收益也是最好的。

在上一篇《基于Python的指数基金量化投资- 指数投资技巧(一)定期定额》中已经介绍了定期定额的方式,这里接着介绍第而种定期不定额的情况喝量化的过程。

定期不定额还是按日、按周或者按月进行投资,但每次投资的资金不一样,如果指数高就少买,如果指数低就多买,例如每周都会买入沪深300基金,当指数是3000的时候买入300块,当指数是2000的时候买入600块,当指数是1000的时候买入900块,这样相当于在低位买入了更多的份额,高位买入了更少的份额,这样有利于在低于积累份额,在未来会获得更多的收益,比第一种定期定额方案会更优。

下面通过用中证全指的数据进行量化测试来看看具体的过程。

具体的策略是下面的三个条件:

1)按周进行投资;

2)当估值为80%时投入400元,估值70%时投入600元,估值为60%时投入800元,估值50%时投入1000元,估值为40%时投入1200元,估值30%时投入1400元,估值为20%时投入1600元,估值10%时投入1800元,估值等于0%时投入2000元。

3)当估值高于80%时全仓卖出;

通过这种方式可以得到下面的量化结果。

图中上半部分蓝线是指数走势,红点是按周定投的位置,但是红点不是一样大的,指数位置越高红点越小,指数位置越低红点越大,表示低点买得多,高点买得少。而几个紫色的点表示估值高于80%卖出的位置。

下半部分的图表示总资产、已投入资金和持有基金份额,其中红线时总资产,蓝线是已投入资金,橙线是持有基金份额。开始阶段不断买入持有份额和总资产是重合的,随着买入的增多同时指数上涨,红线橙线逐步高于蓝线,在2015年初左右估值高于80%则卖出,可以看见橙线变为0,也就是全仓卖出,然后红线、蓝线和橙线持续保持了一段时间的水平走势,也就是这区间没有任何投入,资产、资金和份额都没有变化,接下来也有不同时期的买入和卖出。

最后可以看出投入的资金是447800元,整体资产是666224.42,收益是48.78%,比定期定额的收益高出了不少。

结果显示定期不定额的投资效果要高于定期定额,接下来还会分享不定期定额和和不定期不定额来进行比较。

源码

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math as mathname_index = 'lxr_1000002'
name_index_g = 'g_lxr'
all_data_index = pd.read_csv('./exportfile/indexDataAll/' + name_index + '.csv')
all_data_index_g = pd.read_csv('./importfile/indexSeries/indexValuation/g/' + name_index_g + '.csv')calc_range = 2500
calc_gap = 5
data_index_p = all_data_index['close'].values[len(all_data_index['close']) - calc_range:len(all_data_index['close']):calc_gap]
data_index_g = all_data_index_g['pe'].values[len(all_data_index_g['pe']) - calc_range:len(all_data_index_g['pe']):calc_gap]
val_percentage_list = list()sell_flag_no_regular_no_quota = [0, 0]
sell_flag_regular_quota = 0
sell_flag_regular_no_quota = 0
sell_flag_no_regular_quota = 0def RegularNoQuota(val_percentage, val_data_p, buy_cnt, buy_total_share):global sell_flag_regular_no_quotathd_valuation = [0.0, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80]each_ratio =    [2.0, 1.80, 1.60, 1.40, 1.20, 1.00, 0.80, 0.60, 0.40]if val_percentage == thd_valuation[0]:each_ratio_todo = each_ratio[0]elif thd_valuation[0] < val_percentage <= thd_valuation[1]:each_ratio_todo = each_ratio[1]elif thd_valuation[1] < val_percentage <= thd_valuation[2]:each_ratio_todo = each_ratio[2]elif thd_valuation[2] < val_percentage <= thd_valuation[3]:each_ratio_todo = each_ratio[3]elif thd_valuation[3] < val_percentage <= thd_valuation[4]:each_ratio_todo = each_ratio[4]elif thd_valuation[4] < val_percentage <= thd_valuation[5]:each_ratio_todo = each_ratio[5]elif thd_valuation[5] < val_percentage <= thd_valuation[6]:each_ratio_todo = each_ratio[6]elif thd_valuation[6] < val_percentage <= thd_valuation[7]:each_ratio_todo = each_ratio[7]elif thd_valuation[7] < val_percentage <= thd_valuation[8]:each_ratio_todo = each_ratio[8]else:each_ratio_todo = 0if each_ratio_todo > 0:sell_flag_regular_no_quota = 0buy_each_regular_quota = 1000 * each_ratio_todobuy_each_share = buy_each_regular_quota / val_data_pbuy_cnt = buy_cnt + buy_each_regular_quotaplot_y = val_data_pplot_x = iplot_flag = 1else:if sell_flag_regular_no_quota == 0:sell_flag_regular_no_quota = 1buy_each_share = -buy_total_sharebuy_total_share = 0plot_y = val_data_pplot_x = iplot_flag = -1else:buy_each_share = 0plot_y = val_data_pplot_x = iplot_flag = 0return buy_each_share, buy_cnt, [plot_flag, plot_x, plot_y], buy_total_sharegap = 5  # invest each week
cnt = 0buy_each_share_regular_no_quota = np.zeros((len(data_index_p), 1))
buy_total_share_list_regular_no_quota = np.zeros((len(data_index_p), 1))
buy_total_money_list_regular_no_quota = np.zeros((len(data_index_p), 1))
buy_cnt_regular_no_quota = 0
plot_regular_no_quota = np.zeros((len(data_index_p), 3))# idx_start = 974 #2011-1-4
idx_start = 1
for i in range(len(data_index_p)):valuation_len = all_data_index_g['pe'].values[len(all_data_index['close']) - calc_range-500:len(all_data_index['close']) - calc_range+i*calc_gap:calc_gap]val_loc = np.where(valuation_len < data_index_g[i])val_percentage = len(val_loc[0]) / (len(valuation_len))val_percentage_list.append(val_percentage)buy_each_share_regular_no_quota[i], buy_cnt_regular_no_quota, plot_regular_no_quota[i], buy_total_share_regular_no_quota\= RegularNoQuota(val_percentage, data_index_p[i],buy_cnt_regular_no_quota, sum(buy_each_share_regular_no_quota))buy_total_share_list_regular_no_quota[i] = sum(buy_each_share_regular_no_quota) * data_index_p[i]buy_total_money_list_regular_no_quota[i] = buy_cnt_regular_no_quotaearn_total_money_regular_no_quota = np.zeros((len(data_index_p), 1))
money_sell_regular_no_quota = 0
for i in range(len(data_index_p)):if buy_each_share_regular_no_quota[i] < 0:money_sell_regular_no_quota = money_sell_regular_no_quota - buy_each_share_regular_no_quota[i] * data_index_p[i]earn_total_money_regular_no_quota[i] = sum(buy_each_share_regular_no_quota[0:i+1]) * data_index_p[i] + money_sell_regular_no_quotaplt_gap = 10
size_title = 28
size_label = 15
size_line = 3
size_rotation = 15
size_buy_plot = 5plt.figure()
plt.rcParams["axes.grid"] = True
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams["grid.linestyle"] = (3, 5)
plt.subplot(211)income = 100 * (earn_total_money_regular_no_quota[-1][0] - buy_total_money_list_regular_no_quota[-1][0]) / buy_total_money_list_regular_no_quota[-1][0]
plt.title('定期不定额 | 投资收益 = ' + str("{:.2f}".format(income)) + '%',size=15)
# plt.plot(buy_each_share_no_regular_quota)v_max = max(data_index_p)
v_min = min(data_index_p)for i in range(len(plot_regular_no_quota)):if plot_regular_no_quota[i][0] == 1:plt.plot(plot_regular_no_quota[i][1], plot_regular_no_quota[i][2],color='tomato',marker='o',ms=(size_buy_plot*v_max/plot_regular_no_quota[i][2]))elif plot_regular_no_quota[i][0] == -1:plt.plot(plot_regular_no_quota[i][1], plot_regular_no_quota[i][2], color='purple', marker='o',ms=10)
plt.plot(data_index_p)
plt_xticks = all_data_index['date'].values[len(all_data_index['close']) - calc_range:len(all_data_index['close']):calc_gap].tolist()
plt.xticks(range(len(plt_xticks),0,-math.floor(len(plt_xticks)/plt_gap)),plt_xticks[len(plt_xticks):0:-math.floor(len(plt_xticks)/plt_gap)],rotation=size_rotation)
plt.tick_params(labelsize=size_label)plt.subplot(212)
plt.plot(buy_total_share_list_regular_no_quota,color='tomato')
font = {'size': 15, 'color': 'tomato', 'weight': 'black'}
plt.text(len(buy_total_share_list_regular_no_quota), buy_total_share_list_regular_no_quota[-1][0], str("{:.2f}".format(buy_total_share_list_regular_no_quota[-1][0])), fontdict=font)
plt.plot(len(buy_total_share_list_regular_no_quota)-1,buy_total_share_list_regular_no_quota[-1][0], color='tomato', marker='o')plt.plot(buy_total_money_list_regular_no_quota,color='cornflowerblue')
font = {'size': 15, 'color': 'cornflowerblue', 'weight': 'black'}
plt.text(len(buy_total_money_list_regular_no_quota), buy_total_money_list_regular_no_quota[-1][0], str("{:.2f}".format(buy_total_money_list_regular_no_quota[-1][0])), fontdict=font)
plt.plot(len(buy_total_money_list_regular_no_quota)-1,buy_total_money_list_regular_no_quota[-1][0], color='cornflowerblue', marker='o')plt.plot(earn_total_money_regular_no_quota,color='red')
font = {'size': 15, 'color': 'red', 'weight': 'black'}
plt.text(len(earn_total_money_regular_no_quota), earn_total_money_regular_no_quota[-1][0], str("{:.2f}".format(earn_total_money_regular_no_quota[-1][0])), fontdict=font)
plt.plot(len(earn_total_money_regular_no_quota)-1,earn_total_money_regular_no_quota[-1][0], color='red', marker='o')plt_xticks = all_data_index['date'].values[len(all_data_index['close']) - calc_range:len(all_data_index['close']):calc_gap].tolist()
plt.xticks(range(len(plt_xticks),0,-math.floor(len(plt_xticks)/plt_gap)),plt_xticks[len(plt_xticks):0:-math.floor(len(plt_xticks)/plt_gap)],rotation=size_rotation)
plt.tick_params(labelsize=size_label)plt.show()

文中用到的两个文件下载链接: https://pan.baidu.com/s/14vRxb08jRIRPhM-_8i8W3A?pwd=iefi
提取码: iefi
程序中用到的数据如果有问题,大家可以留言获取也可以添加小将前行的微信xjqx_666进行获取,欢迎大家一起交流沟通

课程参考:基于Python的量化指数基金投资

基于Python的指数基金量化投资 - 指数投资技巧(二)定期不定额相关推荐

  1. 基于Python的指数基金量化投资——指数数据获取

    做基金的量化,最最重要的是要有数据,所以指数的数据是所有分析的源头. Baostock就提供比较全面的指数数据,具体可以参考<基于Python的指数基金量化投资 - 股票数据源baostock& ...

  2. 基于Python的指数基金量化投资 ——A股所有个股名称和证券代码获取

    前面介绍过怎么获取A股个股的数据<基于Python的指数基金量化投资 - 股票数据源baostock>,里面包含了个股的各种历史数据,包含:股价.市盈率.市净率.成交量.换手率等等. 但是 ...

  3. 基于Python的指数基金量化投资-股票数据源baostock

    基于Python的指数基金量化投资-股票数据源baostock 课程参考:基于Python的量化指数基金投资 微信公众号: 量化用到的数据源来自baostock,可以通过www.baostock.co ...

  4. 基于Python的指数基金量化投资——指数基金偏离度计算

    什么是指数偏离度 它是指数涨跌的快慢和偏离幅度指标. 当指数快速上涨,偏离度数据会迅速的向上偏离,当快速下跌时,偏离度数据会迅速的向下偏离. 而持续的上涨中出现下跌,偏离度就会急转直下,另一种持续的下 ...

  5. 基于Python的指数基金量化投资——指数基金间相关度计算

    每一种指数基金都是由一篮子股票组成的,少的有几十个成分股,多的有几百上千个成分股,而整个A股目前有四千多家上市公司,每种指数基金都从A股这个大篮子里面选取成分股,那就会有个问题,不同的指数基金选择的成 ...

  6. 基于Python的指数基金量化投资——A股全市场成交量计算

    成交量是反映市场情绪和流动性一个很重要的指标,当出现牛市时成交量会急剧放大,当出现熊市时成交量会急剧缩小. 通过成交量可以反映出市场的情绪是处于正常.平淡还是疯狂,可以在一定程度上指导我们的投资操作, ...

  7. 基于Python的指数基金量化投资-为什么量化指数基金投资

    上一次写了基于Python的指数基金量化投资-股票数据源baostock 这次来说一下为什么要量化指数基金投资. 进行指数基金投资我们需要清楚几个关键点:该投资哪些指数品种,为什么投资这些品种.这些品 ...

  8. 基于Python的指数基金量化投资 - 指数投资技巧(三)不定期定额

    指数投资方式中有四种基本的方法,分别是定期定额.定期不定额.不定期定额和不定期不定额,这四种方式投资效果不同,对投资者的要求也不同,定期定额最简单,但收益不算高,不定期不定额最复杂,对投资者的要求最高 ...

  9. 《共同基金常识》书中的精髓:如何用好指数基金,做好理财投资?

    <共同基金常识>书中的精髓:如何用好指数基金,做好理财投资? 财务自由可能是我们每个人的梦想,但是要想实现财务自由,我们就需要一个稳定增长的路径. 说到投资,很多朋友首先想到的应该是理财产 ...

  10. 基于Python的指数基金量化投资——指数包含的个股数据获取

    要计算指数的加权值,指数的市盈率.市净率,或者指数的净资产收益率,都需要用到指数所包含的个股信息,前面分享的<指数的净资产收益率计算>和<指数的市盈率和市净率计算>等文中都有提 ...

最新文章

  1. Luogu P6055 [RC-02] GCD(莫比乌斯反演,杜教筛)(这题乐死我了,真就图一乐呗)
  2. 2021第十二届蓝桥杯国赛总结-java大学c组
  3. druid抛出的异常------javax.management.InstanceAlreadyExistsException引发的一系列探索
  4. strace使用详解(未研究)
  5. 大数据之“用户行为分析”
  6. 【spring boot】新建项目,实现HelloWorld
  7. Java多线程之线程封闭(三)
  8. c语言判断一个点在长方体内部_21个入门练手项目,让你轻松玩转C语言
  9. Qt编译错误:无法解析的外部符号 __imp__CloseServiceHandle __imp__OpenSCManager
  10. 使用flot.js 发现x轴y轴无法显示轴名称
  11. 创梦天地通过聆讯:上半年经营利润1.3亿 腾讯持股超20%
  12. 四色着色问题 c语言编程,数据结构-图着色问题
  13. Wpf之Tree使用Dictionary作为数据源
  14. Security+ 学习笔记56 增强隐私保护的技术
  15. 基础连接已关闭解决办法_解决|罗技蓝牙键盘连接ipad后打不出字?
  16. vs2015连接oracle(11g)的方法
  17. php模板引擎smarty案例下载,Smarty下载|Smarty(php模板引擎) v3.1.30官方版 - 121下载站...
  18. 如何有效阅读英文数据手册?
  19. 阿里矢量图库 当前页全选
  20. 飞信linux下载文件,在Linux上安装飞信

热门文章

  1. Rhadoop集群搭建
  2. Multisim14.0详细安装教程图文
  3. 解释杨中科随机数为什么会骗人?
  4. 系统设计=基于表面肌电信号的不同手势识别【MATLAB】
  5. 华为HCNA好考吗?
  6. ubuntu进入桌面自动启动脚本_Ubuntu程序开机自动启动设置(服务和自动运行配置文件)的几种方法...
  7. java简历模板来了!!
  8. 语音识别软件测试面试,软件测试之ASR(语音识别)评测学习
  9. 软件需求说明书/ 概要设计说明书/项目开发计划/详细设计说明书模版(说明要点及要点解释)
  10. centos安装open-jdk8