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.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697'''
Perform the analysis of 2) and 3) up to -5 days before a key event date, and up to +5 daysafter a key event date. Plot the prices and returns of days T-5, T-4, T-3, T-2, T-1, T, T+1,T+2, T+3, T+4, T+5 for the stock markets, the exchange rates and the sovereign bondyields. What do you notice? Is there any movement in the markets before key event dates?Do financial markets react positively or negatively before, after, and on the days of keyevents? Repeat the steps 2) and 3) using cumulative returns from five days before the eventto one day after the event. What do you observe? What happens if you consider thecumulative returns from five days before the event to three days after the event? What doyou learn about the impact of these events? Comment on the sign and persistence of theinformation shock.'''
import pandas as pdimport statsmodels.formula.api as smimport matplotlib.pyplot as mpfrom pandas.tseries.offsets import *
def load_returns(): df = pd.read_pickle("returns.csv") return dfdef load_prices(): df = pd.read_pickle("data.csv") return dfdef t10(date, returns, boffset = 5, eoffset = 5): ''' This function returns a dataframe with the returns +/- 5 days from a specific date. ''' boff = boffset * BDay() eoff = eoffset * BDay() b = date - boff e = date + eoff df = returns.loc[b:e] return dfdef cumu_returns(df): cumu = df.cumsum(axis = 0) return cumudef plot_returns(tf, num, title = "No title"): mp.clf() mp.plot(tf.index, tf["FTSE"]) mp.plot(tf.index, tf["SNP"]) mp.plot(tf.index, tf["DAX"]) mp.plot(tf.index, tf["UKGILT"]) mp.plot(tf.index, tf["GBPUSD"]) mp.plot(tf.index, tf["GBPEUR"]) mp.xlabel("Date") mp.ylabel("Daily Return") mp.xticks(rotation=45) mp.title(title) mp.grid(True) mp.legend(loc='center left', bbox_to_anchor=(1, 0.5)) mp.gcf().subplots_adjust(bottom=0.15, right = .75) mp.savefig("{}.png".format(num)) #mp.show() return 0def create_matrix(date, returns): t = t10(date, returns)
if __name__ == '__main__': labels = ["Cameron promises referendum", "Conservative party wins", "Referendum date selected", "Referendum held", "Results announced", "Cameron resigns, May steps in"] returns = load_returns() prices = load_prices() num_dates = len(returns[returns.KY == 1].index) #This iterates throughout each requirement of the question and produces #all the necessary graphs. for i in range(0,1): date = returns[returns.KY == 1].index[4] tf = t10(date, returns)
plot_returns(tf, 1, title = "{}, +/- 5 days, actual returns".format(labels[4])) ''' Repeat the steps 2) and 3) using cumulative returns from five days before the event to one day after the event. ''' tf2 = t10(date, returns, eoffset = 1) cumu = cumu_returns(tf2) plot_returns(cumu, 2, title = "{}, -5, +1 day, cumulative sum".format(labels[4]))
''' What happens if you consider the cumulative returns from five days before the event to three days after the event? ''' tf3 = t10(date, returns, eoffset = 3) cumu2 = cumu_returns(tf3) plot_returns(cumu2, 3, title = "{}, -5, +3 days, cumulative sum".format(labels[4]))
tf4 = t10(date, returns, eoffset = 5) cumu3 = cumu_returns(tf4) cumu3 plot_returns(cumu3, 4, title = "{}, +/- 5 days, cumulative sum".format(labels[4]))
print(date)