# python金融正太var方差-协方差代码

import numpy as np
import pandas as pd
import scipy.stats as st
from pylab import mpl
mpl.rcParams["font.sans-serif"] = ["KaiTi"]
mpl.rcParams["axes.unicode_minus"] = False

#打开表格取出里边的数据 D后边那一长串为存放路径
stock_price = pd.read_excel("D:\python代码\\first\daorutry.xlsx",sheet_name="Sheet1",header =0,index_col=0)
#print(stock_price)

#股票日收益
stock_return = np.log(stock_price/stock_price.shift(1))
#删除缺失行
stock_return = stock_return.dropna()
#print(stock_return)
a = stock_return.loc[:,"收盘/最新价"]
#print(a)
#a_mean为平均收益率  b为保留小数点后四位的a_mean
a_mean =a.mean()
b = "%.4f" % a_mean
#a_mean = "%.2f%%" % (a.mean()*100)
print(b)

#p的中间的值为置信水平为95%情况下的收益率的波动率σ
p = np.percentile(a, (1, 5, 10), interpolation='midpoint')
p = np.abs(p[1])
print(("%.4f")%p)

#z为置信水平在90%或者95%或者99%下的值
arf = float(input("请输入置信水平:"))
z = st.norm.ppf(q=1-arf)
#z取绝对值
z = np.abs(z)

#设P0为固值
P0 = 1000
#N为持有期
N = int(input("请输入持有月份:"))
N = N*30

#var = np.sqrt(N) * P0 * (z*p-float(b))
var = np.sqrt(N)*P0*(z*p-float(b))
print(var)