指数投资方式中有四种基本的方法,分别是定期定额、定期不定额、不定期定额和不定期不定额,这四种方式投资效果不同,对投资者的要求也不同,定期定额最简单,但收益不算高,不定期不定额最复杂,对投资者的要求最高,特别是对情绪的要求非常高,同时收益也是最好的。
这里先介绍第一种定期定额的情况,下面会通过量化的过程来反应投资的整体过程。
定期定额就是按日、按周或者按月进行投资,每次投资的资金是一样的,比如每周买入1000块的沪深300基金,这种方式是不管指数涨跌,到点就买;
假设每次投入的资金是1000块,按周定投,下面是通过量化的过程跑出来的情况(源码附在后面),这里既然是定期定额就不考虑卖出。数据是通过中证全指(指数代码1000002)进行计算的。
上半部分的图中蓝线是中证全指的走势图,红点是每周定投的位置,下半部分的图中蓝线是累计投入的资金,红线是持有基金的市值,整个过程投入的总资金是500000,最终的基金总市值是680424.75,最后获得的收益是36.08%,从下半部分图中可以很清晰的看出,当指数下跌的过程,基金市值会低于投入的资金,这个过程收益为负,随着指数的上涨,基金市值上涨的幅度会高于投入的资金,然后在某些点超过投入资金的总值,这个过程收益就会转负为正,这就是在低点积累的份额更多造成的。
整体来说定期定额的投资效果并不是很好,接下来还会分享定期不定额、不定期定额和不定期不定额来进行比较。
源码
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math as math
name_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 = 0
def RegularQuota(val_percentage,val_data_p, buy_cnt, buy_total_share):
global sell_flag_regular_quota
if val_percentage <= 1:
sell_flag_regular_quota = 0
buy_each_regular_quota = 1000
buy_each_share = buy_each_regular_quota / val_data_p
buy_total_share = buy_total_share + buy_each_share
buy_cnt = buy_cnt + 1
plot_y = val_data_p
plot_x = i
plot_flag = 1
else:
if sell_flag_regular_quota == 0:
sell_flag_regular_quota = 1
buy_each_share = -buy_total_share
buy_total_share = 0
plot_y = val_data_p
plot_x = i
plot_flag = -1
else:
buy_each_share = 0
plot_y = val_data_p
plot_x = i
plot_flag = 0
return buy_each_share, buy_cnt, [plot_flag, plot_x, plot_y],buy_total_share
gap = 5 # invest each week
cnt = 0
buy_each_share_regular_quota =np.zeros((len(data_index_p), 1))
buy_total_share_list_regular_quota =np.zeros((len(data_index_p), 1))
buy_total_money_list_regular_quota =np.zeros((len(data_index_p), 1))
buy_cnt_regular_quota = 0
plot_regular_quota =np.zeros((len(data_index_p), 3))
buy_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))
buy_each_share_no_regular_quota =np.zeros((len(data_index_p), 1))
buy_total_share_list_no_regular_quota =np.zeros((len(data_index_p), 1))
buy_total_money_list_no_regular_quota =np.zeros((len(data_index_p), 1))
buy_cnt_no_regular_quota = 0
plot_no_regular_quota =np.zeros((len(data_index_p), 3))
buy_each_share_no_regular_no_quota =np.zeros((len(data_index_p), 1))
buy_total_share_list_no_regular_no_quota =np.zeros((len(data_index_p), 1))
buy_total_money_list_no_regular_no_quota =np.zeros((len(data_index_p), 1))
buy_cnt_no_regular_no_quota = 0
plot_no_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_regular_quota = 1000
buy_each_share_regular_quota[i], buy_cnt_regular_quota,plot_regular_quota[i], buy_total_share_regular_quota\
= RegularQuota(val_percentage, data_index_p[i], buy_cnt_regular_quota,sum(buy_each_share_regular_quota))
buy_total_share_list_regular_quota[i] =sum(buy_each_share_regular_quota) * data_index_p[i]
buy_total_money_list_regular_quota[i] = buy_cnt_regular_quota *buy_each_regular_quota
earn_total_money_no_regular_quota =np.zeros((len(data_index_p), 1))
money_sell_no_regular_quota = 0
for i in range(len(data_index_p)):
if buy_each_share_no_regular_quota[i] < 0:
money_sell_no_regular_quota = money_sell_no_regular_quota -buy_each_share_no_regular_quota[i] * data_index_p[i]
earn_total_money_no_regular_quota[i] =sum(buy_each_share_no_regular_quota[0:i+1]) * data_index_p[i] +money_sell_no_regular_quota
earn_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_quota
earn_total_money_regular_quota =np.zeros((len(data_index_p), 1))
money_sell_regular_quota = 0
for i in range(len(data_index_p)):
if buy_each_share_regular_quota[i] < 0:
money_sell_regular_quota = money_sell_regular_quota -buy_each_share_regular_quota[i] * data_index_p[i]
print('')
earn_total_money_regular_quota[i] =sum(buy_each_share_regular_quota[0:i+1]) * data_index_p[i] +money_sell_regular_quota
print('')
earn_total_money_no_regular_no_quota =np.zeros((len(data_index_p), 1))
money_sell_no_regular_no_quota = 0
for i in range(len(data_index_p)):
if buy_each_share_no_regular_no_quota[i] < 0:
money_sell_no_regular_no_quota = money_sell_no_regular_no_quota -buy_each_share_no_regular_no_quota[i] * data_index_p[i]
earn_total_money_no_regular_no_quota[i] =sum(buy_each_share_no_regular_no_quota[0:i+1]) * data_index_p[i] +money_sell_no_regular_no_quota
plt_gap = 10
size_title = 28
size_label = 15
size_line = 3
size_rotation = 15
size_buy_plot = 5
#------------------------------------------------------------- #
plt.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_quota[-1][0] -buy_total_money_list_regular_quota[-1][0]) /buy_total_money_list_regular_quota[-1][0]
plt.title('定期定额 | 投资收益= ' + str("{:.2f}".format(income)) + '%',size=15)
v_max = max(data_index_p)
v_min = min(data_index_p)
for i in range(len(plot_regular_quota)):
if plot_regular_quota[i][0] == 1:
plt.plot(plot_regular_quota[i][1], plot_regular_quota[i][2],color='tomato',marker='o',ms=(size_buy_plot*v_max/plot_regular_quota[i][2]))
elif plot_regular_quota[i][0] == -1:
plt.plot(plot_regular_quota[i][1], plot_regular_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_quota,color='tomato')
font = {'size': 15, 'color': 'tomato','weight': 'black'}
plt.text(len(buy_total_share_list_regular_quota),buy_total_share_list_regular_quota[-1][0], str("{:.2f}".format(buy_total_share_list_regular_quota[-1][0])),fontdict=font)
plt.plot(len(buy_total_share_list_regular_quota)-1,buy_total_share_list_regular_quota[-1][0],color='tomato', marker='o')
plt.plot(buy_total_money_list_regular_quota,color='cornflowerblue')
font = {'size': 15, 'color':'cornflowerblue', 'weight': 'black'}
plt.text(len(buy_total_money_list_regular_quota),buy_total_money_list_regular_quota[-1][0],str("{:.2f}".format(buy_total_money_list_regular_quota[-1][0])),fontdict=font)
plt.plot(len(buy_total_money_list_regular_quota)-1,buy_total_money_list_regular_quota[-1][0],color='cornflowerblue', marker='o')
plt.plot(earn_total_money_regular_quota,color='red')
font = {'size': 15, 'color': 'red','weight': 'black'}
plt.text(len(earn_total_money_regular_quota),earn_total_money_regular_quota[-1][0],str("{:.2f}".format(earn_total_money_regular_quota[-1][0])),fontdict=font)
plt.plot(len(earn_total_money_regular_quota)-1,earn_total_money_regular_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/13alPKvTP7Rw061UMcgtMXQ?pwd=s6dr 提取码: s6dr
程序中用到的数据如果有问题,大家可以留言获取,欢迎大家一起交流沟通^_^
课程参考:网易云课堂 基于Python的量化指数基金投资
网友评论