【发布时间】:2017-12-20 13:42:41
【问题描述】:
我定义了一个函数(也许我会将它划分并将所有内容放在一个类中),它从 pdf 文件中获取数据,修改它们,然后将它们打印到一个新的 xlsx 文件中。
问题是我什至有一个情节,每次脚本运行时我都会保存在桌面上。
我要运行它的列表中有四个变量。
但是每次我运行它(使用函数打印的 for 循环)时,该图都会获取以前的数据并将所有内容放在一个图中,但我只想要新数据的图。
所以我想要 4 个不同的文件 excel(我做到了)和 4 个不同的图(每个图只有一个变量数据系列)。
我试图将绘图的输入列表重置为空列表,但它不起作用。
你会如何解决这个问题?也许使用其他模块?
我附上的图片显示了我正在做的混乱,因为它包含以前的数据和新的数据,但我只想要新数据的三色时间序列。
PS:我想在每次运行该函数时添加一个工作表,但我无法做到这一点,我选择每次都创建一个新的 xlsx 文件。
import pandas as pd
import pandas_datareader as pdr
import datetime as dt
from datetime import timedelta
from tabula import read_pdf
from scipy.stats import norm
import numpy
import math
import matplotlib.pyplot as plt
import xlsxwriter
date=dt.datetime.now().date()
date_ok=str(date-timedelta(days=1))
#digits_list=[5,6,7,6]
#range_PDF_list=[range(0,28),range(1,30),range(0,21),range(1,26)]
#range_black_list=[range(0,28),range(0,29),range(0,21),range(0,25)]
#pages_list=[5,3,2,3]
#initial_list=[0,1,0,1]
strike_PDF={'eurostoxx':['^STOXX50E',5,range(0,28),range(0,28),5,0],'dax':['^GDAXI',6,range(1,30),range(0,29),3,1],'sensex':
['^BSESN',7,range(0,21),range(0,21),2,0],'smi':['^SSMI',6,range(1,26),range(0,25),3,1]}
#print (strike_PDF['eurostoxx'])[1]
#print strike_PDF['eurostoxx']
#print strike_PDF.keys()
def define_the_index(vol_GBM,step_dt,vol,T):
index=raw_input()
strike_price_list=[]
real_price=[]
if index in strike_PDF.keys():
df=pdr.get_data_yahoo((strike_PDF[index])[0],date_ok)
raw_initial=read_pdf((index+'.pdf'),pages=[1])
if index=='eurostoxx':
initial=float((raw_initial.ix[(strike_PDF[index])[5]][0])[:(strike_PDF[index])[1]].decode('utf-8').replace(',','').encode('utf-8'))
initial=initial/2
else:
initial=float((raw_initial.ix[(strike_PDF[index])[5]][0])[:(strike_PDF[index])[1]].decode('utf-8').replace(',','').encode('utf-8'))
table=read_pdf((index+'.pdf'),pages=[(strike_PDF[index])[4]])
for j in reversed((strike_PDF[index])[2]):
raw_strike_price=table.ix[j][0]
strike_price=raw_strike_price[:(strike_PDF[index])[1]].decode('utf-8').replace(',','').encode('utf-8')
strike_price_list.append(float(strike_price))
for j in reversed((strike_PDF[index])[2]):
raw_last_price=table.ix[j][10]
if ',' in str(raw_last_price):
last_price=raw_last_price[:(strike_PDF[index])[1]].decode('utf-8').replace(',','').encode('utf-8')
else:
last_price=raw_last_price
real_price.append(float(last_price))
close_price=float(df['Close'][0])
change_step=[]
index_level=[close_price]
#vol_GBM=0.015
#step_dt=0.01
a=numpy.random.randn(1,1000)
uncertainty_steps=a.tolist()[0]
f_uncertainty_steps=uncertainty_steps[:]
for i in range(0,1000):
change_step.append( float( f_uncertainty_steps[i] )
* mat.sqrt( vol_GBM )
* step_dt
* ( close_price )
) # ? do parentheses match an intent
# this should be indented
# I don't understand how to adjust it in here,
# but in my code works fine with indentation of the for loop
for i in range(0,1000):
index_level.append(float(index_level[i])+float(change_step[i]))
# vol = 0.35
# T = 0.25
Black_list=[]
Black_price=[]
for j in (strike_PDF[index])[3]:
K=float(strike_price_list[j])
for i in range(0,1000):
S=index_level[i]
d1=(math.log(S/K)+0.5*T*vol*vol)/vol*T
d2=d1-vol*T
Black_p=((S*norm.cdf(d1)-K*norm.cdf(d2))/K)*initial
if Black_p>0:
Black_list.append(Black_p)
else:
Black_list.append(0)
only_price=numpy.percentile(Black_list,0.01)
Black_price.append(only_price)
# ELN
ELN_price=[]
for i in (strike_PDF[index])[3]:
ELN_price.append(Black_price[i]+100)
Black_list_mod=[]
Black_price_mod=[]
for j in (strike_PDF[index])[3]:
Km=float(strike_price_list[j])
for i in range(0,1000):
Sm=(index_level[i+1]/index_level[i])*close_price
d1=(math.log(Sm/Km)+0.5*T*vol*vol)/vol*T
d2=d1-vol*T
Black_p_mod=((Sm*norm.cdf(d1)-Km*norm.cdf(d2))/Km)*initial
if Black_p_mod>0:
Black_list_mod.append(Black_p_mod)
else:
Black_list_mod.append(0)
only_price_mod=numpy.percentile(Black_list_mod,0.01)
Black_price_mod.append(only_price_mod)
# ELN mod
ELN_price_mod=[]
for i in (strike_PDF[index])[3]:
ELN_price_mod.append(Black_price_mod[i]+100)
# plot
plt.plot(Black_price,strike_price_list,'o-',label='Normal')
plt.plot(Black_price_mod,strike_price_list,'o-',color='r',label='Modified')
plt.plot(real_price,strike_price_list,'o-',color='grey',label='Last Price')
plt.xlabel('Option Price')
plt.ylabel('Strike Price')
plt.title(index + ' Option Price')
plt.legend()
plt.savefig(index + ' Option.png')
[enter image description here][1]
# excel
workbook = xlsxwriter.Workbook(index+'.xlsx')
worksheet = workbook.add_worksheet(index)
for i in (strike_PDF[index])[3]:
worksheet.write_number(i+1,0,strike_price_list[i])
worksheet.write_string(0,0,index + ' Strike')
for i in (strike_PDF[index])[3]:
worksheet.write_number(i+1,1,Black_price[i])
worksheet.write_string(0,1,index + ' Call Option Price')
for i in (strike_PDF[index])[3]:
worksheet.write_number(i+1,2,ELN_price[i])
worksheet.write_string(0,2,index+' ELN Price')
for i in (strike_PDF[index])[3]:
worksheet.write_number(i+1,3,Black_price_mod[i])
worksheet.write_string(0,3,index+' Call Option Price Mod')
for i in (strike_PDF[index])[3]:
worksheet.write_number(i+1,4,ELN_price_mod[i])
worksheet.write_string(0,4,index+' ELN Price Mod')
worksheet.insert_image('K5',index+' Option.png')
workbook.close()
Black_price=[]
Black_price_mod=[]
strike_price_list=[]
real_price=[]
for z in range(0,4):
print define_the_index(0.015,0.01,0.35,0.25)
【问题讨论】:
-
你在清理轴吗?我在你的代码中找不到它,也许这会起作用
-
我只是重置了作为绘图输入的列表,即在函数的末尾,我放置了空列表。但是,如果有一些特定的命令可以清理绘图的轴,请告诉我;)谢谢
-
保存图形后尝试
plt.clf()(清除图形)或plt.cla()(清除轴)!让我知道这是否有帮助 -
我把 plt.cla() 放在函数的末尾并且工作了!!!非常感谢伙计。您对在同一个 xlsx 文件中添加工作表有什么建议吗?如果我这样做,我会只用一个名称调用文件,然后删除 index+'.xls'。但是在运行循环时,它不是添加工作表,而是在现有的工作表上书写,最后我运行了最后一个工作表。 myabe 是 xlsxwirte 模块的一个功能?再次感谢
-
我在我的回答中展示了它 :) 如果这是您正在寻找的 - 否则,请告诉我!
标签: python excel matplotlib plot quantitative-finance