Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/just-cameron/misc-python. All my Python code for class or other things.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485# The following is copied from the work we did in the last couple of weeks.import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport urllib.request as urrimport datetime as dtimport statsmodels.formula.api as smimport statsmodels.tools
#The URL's for our datasetses_url = "https://www.stoxx.com/document/Indices/Current/HistoricalData/hbrbcpe.txt" #This one has a problem with semicolonsvs_url = "https://www.stoxx.com/document/Indices/Current/HistoricalData/h_vstoxx.txt"
#Print a concantenated output of the two URLs.#print(es_url + '\n' + vs_url)
#Retrieve the page as a file.#urr.urlretrieve(es_url, "es.txt")#urr.urlretrieve(vs_url, "vs.txt")
lines = open("es.txt", "r").readlines()lines = [line.replace(" ", "") for line in lines]
# Open a file, set it to write.# Then we put in a header, which is the header of the "lines" list.# Then we post the remaining sections of lines, and close the file.new_file = open("es50.txt", "w")new_file.writelines("Date" + lines[3][:-1] + ";DEL" + lines[3][-1])new_file.writelines(lines[4:])new_file.close()
# new_lines = open("es50.txt", "r").readlines()
es = pd.read_csv("es50.txt", sep = ";", parse_dates=True, dayfirst=True, index_col=0)# print(pdl.ix[0:4,:]) # A look before we delete the DEL columndel es["DEL"] # This removes the column we're talking about.#print(es.head()) # Now we have a normal ass data frame.
#Read in our other file.vs = pd.read_csv("vs.txt", sep = ",", parse_dates=True, header = 2, index_col=0, dayfirst=True)#print(vs.head())
data1 = pd.DataFrame({"EUROSTOXX":es['SX5E'][es.index > dt.datetime(1999, 1, 1)]})data1 = pd.DataFrame({"EUROSTOXX":data1['EUROSTOXX'][data1.index < dt.datetime(2016,2,13)]})
data2 = pd.DataFrame({"VSTOXX":vs["V2TX"][vs.index > dt.datetime(1999, 1, 1)]})data2 = pd.DataFrame({"VSTOXX":data2["VSTOXX"][vs.index < dt.datetime(2016, 2, 13)]})
"""The tutor recommends using .fillna('ffill') to pull down theprevious day's values, though that doesn't seem right to me.
The code for the recommended thing is as follows, though fromhere the seminar tutor and I have diverged substantially.If you want to revert to the "actual" method, removethe inner join argument from the joiner below, and uncommentthe statement below the join.
It may also be that it's more 'realistic', but I would haveto know more about VSTOXX. I have no idea what it represents."""data = data1.join(data2)data = data.fillna(method = 'ffill')
data.plot(subplots=True, grid=True, style = 'b', figsize=(8,6))
rets = np.log(data/data.shift(-1))#print(rets.head())
rets = rets[1:]
rets.plot(subplots=True, grid=True, style = 'b', figsize=(8,6))
#REVEAL#plt.show()
xdata = rets['EUROSTOXX']ydata = rets['VSTOXX']
print(xdata.head(), '\n', ydata.head())
result = sm.OLS(ydata, statsmodels.tools.add_constant(xdata)).fit()print(result.summary())