# 用python教你计算定投能获得多少收益

## 分析

1. 需要获得沪深指数的数据。

2. 模拟投资，进行计算。比如从2015-01-01开始投资，每周定投500元，到2018-10-01止，计算投资本金和收益，进而得出投资收益率。

## 过程

1. 获取沪深数据。

http://quotes.money.163.com/service/chddata.html?code=0000001&start=19901219&end=20181003&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;VOTURNOVER;VATURNOVER

code表示股票代码，0000001为上证指数。沪深300指数编号为0000300。

start为开始日期，格式yyyyMMdd

end为结束日期，格式为yyyyMMdd

fields为返回字段，比如TCLOSE表示收盘价,HIGH表示最高价等等。

```日期,股票代码,名称,收盘价,最高价,最低价,开盘价,前收盘,涨跌额,涨跌幅,成交量,成交金额
2018-09-28,000300,沪深300,3438.8649,3444.9884,3404.6973,3409.9598,3403.5902,35.2747,1.0364,84377756,94880274571.6
2018-09-27,000300,沪深300,3403.5902,3423.71,3394.732,3415.9532,3417.2413,-13.6511,-0.3995,69765241,87573300844.9
2018-09-26,000300,沪深300,3417.2413,3448.2595,3383.2352,3388.5576,3379.8043,37.437,1.1077,93173343,1.19113319089e+11
2018-09-25,000300,沪深300,3379.8043,3394.3771,3367.0253,3375.9717,3410.4856,-30.6813,-0.8996,69054693,82870029933.1
2018-09-21,000300,沪深300,3410.4856,3410.4856,3307.767,3320.6958,3310.1258,100.3598,3.0319,108416005,1.23573117858e+11
2018-09-20,000300,沪深300,3310.1258,3332.3093,3303.073,3314.8758,3312.4823,-2.3565,-0.0711,64561057,76660845175.0
2018-09-19,000300,沪深300,3312.4823,3335.9913,3258.3097,3261.997,3269.4319,43.0504,1.3168,89170666,1.09395423838e+11
2018-09-18,000300,沪深300,3269.4319,3270.107,3193.6423,3193.6423,3204.9224,64.5095,2.0128,70425035,83899137593.0
2018-09-17,000300,沪深300,3204.9224,3230.7878,3200.5096,3224.9267,3242.0902,-37.1678,-1.1464,52259547,60650940907.6
2018-09-14,000300,沪深300,3242.0902,3254.1043,3227.9439,3242.8307,3236.5662,5.524,0.1707,60130640,76481211955.9```

2. 计算

```#!/usr/bin/python
# -*- coding: UTF-8 -*-
import operator as op
from datetime import datetime,timedelta

'''

input:
index_no: 指数编号,比如沪深300指数的编号为 000300
start_date：开始日期,格式为 2018-04-03
end_date：结束日期,格式为 2018-04-03
output:
指数列表，只包含日期和收盘价
'''
def load(index_no, start_date, end_date):
file = '../../data/stock/' + index_no + '.csv'
f = open(file,'r')
indices = {}
for line in f.readlines():
arr = line.strip().split(',')
dt = arr[0]
if op.lt(start_date,dt) and op.lt(dt,end_date) :
indices[arr[0]] = arr[3]
return indices

'''

input:
date: 投日日期
amount: 投资金额
data: 指数数据
output:
获得基金份数
'''
def purchase(date, amount, data):
index_value = data[date]
return amount/float(index_value)

def getIndexValue(date,data):
return data[date]

'''

input:
start_date: 起投日期
end_date: 终止日期
amount: 每次投资金额
freq: 投资频率(每几天投资一次)
output:
投资总额,收益总额
'''
def invest(index_no,start_date, end_date, amount, feq):
data = load(index_no,start_date,end_date)
sd = datetime.strptime(start_date, '%Y-%m-%d')
ed = datetime.strptime(end_date, '%Y-%m-%d')
curr = sd
total_share = 0; total_amount = 0;total_profit=0
last_index = 0
while True:
#如果当天不是交易日，则往后延一天.
while curr.strftime('%Y-%m-%d') not in data.keys():
curr += timedelta(days = 1)
if curr > ed: break
if curr > ed: # 超过结束日期则停止
break

last_index = float(getIndexValue(curr.strftime('%Y-%m-%d'),data))
#购买基金
shares = purchase(curr.strftime('%Y-%m-%d'), amount, data)
total_share += shares
total_amount += amount
#计算下一个投资日期
curr = curr + timedelta(days = feq)
total_profit = last_index*total_share - total_amount
return total_amount,total_profit

start_date = '2008-10-01'
end_date = '2018-10-01'
total_amount,total_profit = invest('000300',start_date, end_date,500,7)
print('起投日期',start_date)
print('结束日期',end_date)
print('总投资额',total_amount)
print('总收益',round(total_profit))
print('投资收益率',round(total_profit/total_amount*100,2),"%")```

3.执行结果

```\$ python3 investment.py

1年 (2017.10.01~2018.10.01)

25000

-2439

-9.76%

-9.76%

2年 (2016.10.01~2018.10.01)

50500

-3081

-6.1 %

-3.1%

3年 (2015.10.01~2018.10.01)

76000

-2001

-2.63%

-1%

5年 (2013.10.01~2018.10.01)

127000

9948

7.83 %

1.52%

10年 (2008.10.01~2018.10.01)

252500

45320

17.95 %

1.66%

## 问题2:

1年 (2017.01.24~2018.01.24)

26000

4478

17.22 %

17.22 %

2年 (2016.01.24~2018.01.24)

51000

13117

25.72 %

12.12%

3年 (2015.01.24~2018.01.24)

77000

16593

21.55 %

6.72%

5年 (2013.01.24~2018.01.24)

129000

58404

45.27%

7.75%

10年 (2008.01.24~2018.01.24)

256500

129950

50.66%

4.18%

## 问题3

```起投日期 2008-01-24

37 篇文章11 人订阅

0 条评论