python计算股票净值和复利

# -*- coding: utf-8 -*-
"""
Created on Wed Mar 14 21:38:49 2018
E-mail: [email protected]
@author: DidiLv
"""

import pandas as pd
import numpy as np

from pandas import DataFrame

odata = pd.read_csv('investment_rate.csv')

Profit_pool = pd.DataFrame(odata)
# size

m, n = Profit_pool.shape
np_profit_pool = np.array(Profit_pool)

date = np_profit_pool[:,0]
num_date = m

# 确定跳跃点即时间不连续点
skip_id = [-1]
for id in range(num_date-1):
    if date[id + 1] - date[id] >= 3:
        skip_id.append(id)
skip_id.append(m-1)
skip_id = np.array(skip_id) 
# 确定添加复利和净值后的矩阵大小
num_skip = len(skip_id)-1
profit_pool_week_and_month_profit = np.zeros([1,n])

week_profit_month = np.ones([2,n-1])   
print("-----------skip-------------")    
print(skip_id)        
for count in range(len(skip_id)-1):
    np_week = np_profit_pool[skip_id[count]+1:skip_id[count+1]+1,1:n]
    profit_pool_week_and_month_profit = np.vstack((profit_pool_week_and_month_profit,np_profit_pool[skip_id[count]+1:skip_id[count+1]+1,:]))
    # 计算复利和净值
    mm,nn = np_week.shape
    np_week_new = np_week + np.ones([mm,nn])
    
    week_profit_month[0,:] = np_week_new.prod(axis = 0) 
    for ii in range(n-1):
        week_profit_month[0,ii] -= 1
        week_profit_month[1,ii] *= week_profit_month[0,ii] + 1  
        #这里只是用2e10和2e11做成一个表示符表示复利和净值
        temp = np.column_stack((np.array((2e10,2e11)),week_profit_month))
    profit_pool_week_and_month_profit = np.vstack((profit_pool_week_and_month_profit,temp))
    print("-----np_week-----------")
    print(np_week)    
    
        
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")    
np.savetxt('profit_pool_week_and_month_profit.csv', profit_pool_week_and_month_profit, delimiter = ',')        
        
    

 

你可能感兴趣的:(闲杂记录)