I defined a function (maybe I will divide it and put everything in a class) that takes data from pdf files, modify them and then print them in a new xlsx file.
The problem is that I have even a plot that I save in the desktop every time the script runs.
I have four variables inside the list for which I want to run it.
But every time I run it (with a for loop of the printing of the function), the graph is going to take the previous data and putting everything in a plot, but I want the plot only for the new data.
So I want 4 different file excel (I did it) and 4 different plots(each one with only one variable data series).
I tried to reset the input list of the plot to empty list, but it doesn't work.
How would you solve the problem? Maybe using other modules?
The picture I attached shows the mess I am doing, because it has inside the previous data and the new one, but I want only the three coloured time series of the new data.
PS: I would like to add a worksheet everytime I run the function, but I couldn't fine a way to do it and I opted to create a new xlsx file every time.
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)