用随机函数模拟出一段四年的走势,在均匀的时间点买入走势基金:
import numpy as np
import matplotlib.pylab as plt
TOTALMONEY = 1e5
def genData():
data = np.random.normal(0,1,2000).cumsum()[1000:]
if data.min()<0:
data+=-data.min()+1
return data
def test_mindata():
minData = 1
for _ in range(10000):
data = genData()
if data.min()<minData:
minData = data.min()
assert minData>0
test_mindata()
模拟出的走势大概是这样的:
plt.plot(genData())
模拟走势
三种不同的时间段投资策略:
def simulate_market():
# 无交易手续费,每日定投
data = genData()
money_for_each = TOTALMONEY/len(data)
number_of_purchases = (money_for_each/data).sum()
market_value = number_of_purchases*data[-1]
growth_rate_of_funds = market_value/TOTALMONEY-1
# plt.plot(data)
return growth_rate_of_funds
def simulate_market_with_trading_fee(fee=0.0015):
# 有交易手续费,每日定投
data = genData()
money_for_each = TOTALMONEY/len(data)*(1-fee)
number_of_purchases = (money_for_each/data).sum()
market_value = number_of_purchases*data[-1]
growth_rate_of_funds = market_value/TOTALMONEY-1
# plt.plot(data)
return growth_rate_of_funds
def simulate_market_with_trading_fee_months(fee=0.0015):
# 有交易手续费,每月定投
data = genData()
data_for_trading = data[::5]
money_for_each = TOTALMONEY/len(data_for_trading)*(1-fee)
number_of_purchases = (money_for_each/data_for_trading).sum()
market_value = number_of_purchases*data[-1]
growth_rate_of_funds = market_value/TOTALMONEY-1
return growth_rate_of_funds
按照三种策略模拟十万次走势生成、投资操作:
ax1 = plt.subplot(311) # 无交易手续费,每日定投
ax2 = plt.subplot(312) # 有交易手续费,每日定投
ax3 = plt.subplot(313) # 有交易手续费,每月定投
growth_rate_of_funds_set = []
for _ in range(100000):
growth_rate_of_funds_set.append(simulate_market())
ax1.hist(growth_rate_of_funds_set,bins=np.linspace(-7.5,7.5,300))
growth_rate_of_funds_set = []
for _ in range(100000):
growth_rate_of_funds_set.append(simulate_market_with_trading_fee())
ax2.hist(growth_rate_of_funds_set,bins=np.linspace(-7.5,7.5,300))
growth_rate_of_funds_set = []
for _ in range(100000):
growth_rate_of_funds_set.append(simulate_market_with_trading_fee_months())
ax3.hist(growth_rate_of_funds_set,bins=np.linspace(-7.5,7.5,300))
四年的总投资收益率是这样的:
定投收益率
注意有一半可能是负收益。
网友评论