python金融历史模拟法

python金融历史模拟法

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

# 打开表格取出里边的数据
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为各自天数的收益率
a = stock_return.loc[:,"收盘/最新价"]
# print(a)

# 设P0为固值
P0 = 1000

b = []
for i in a * P0:
    b.append(i)
b.sort()
b1=[]
for i in b:
    i = "%.2f"%i
    b1.append(i)
print(b1)

arf = float(input("请输入置信水平:"))
# f为分位数
f = (1-arf)*len(b1)
#f若为1则取排序下来的第一个值(索引第一个是0)
var = b1[int(f-1)]
var = np.abs(float(var))
print(var)

# N为持有期
N = int(input("请输入持有天数:"))
var1 = np.sqrt(N)*float(var)
print("%.8f"%var1)
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页