#!/usr/bin/env python # coding: utf-8 # In[1]: get_ipython().run_cell_magic('javascript', '', 'IPython.OutputArea.prototype._should_scroll = function(lines) {\n return false;\n}\n') # In[2]: # import libraries and set display options import pandas as pd import pprint import matplotlib.pyplot as plt import matplotlib.dates as mdates from matplotlib.pyplot import cm import matplotlib.dates as mdates plt.style.use('ggplot') # set style for graphs # set frame width and center output from IPython.core.display import display, HTML display(HTML(""" """)) # pretty print options pd.options.display.float_format = '{:20,.4f}'.format pd.set_option('display.max_rows', None) pd.set_option('display.max_columns', None) pd.set_option('display.width', 3000) pd.set_option('display.colheader_justify', 'center') pd.set_option('display.precision', 3) # open file x_rates = pd.read_csv("euro-daily-hist_1999_2020.csv") # strip brackets and trailing space from country names # replace remaining space with underscore # lower case all column names x_rates.columns = x_rates.columns.str.replace("[","").str.replace("]","") x_rates.columns = x_rates.columns.str.rstrip() x_rates.columns = x_rates.columns.str.replace(" ","_") x_rates.columns = x_rates.columns.str.lower() # rename columns x_rates.rename(columns={"period\\unit:":"date", "chinese_yuan_renminbi":"chinese_yuan", "uk_pound_sterling":"uk_pound"}, inplace=True) # convert datetime x_rates["date"] = pd.to_datetime(x_rates["date"]) # resort and reindex x_rates.sort_values("date", inplace=True) x_rates.reset_index(drop=True, inplace=True) # convert hyphens in currency columns to NaN import numpy as np x_rates = x_rates.replace("-", np.nan) # convert exchange rate values to float x_rates.iloc[:,1:] = x_rates.iloc[:,1:].astype(float) # create a list of data frames for each currency with # the log rate of the exchange rate, 30 day rolling mean, and year df_dict = {} for currency in x_rates.columns[1:]: df_name = currency df = x_rates[["date", currency]].copy() # df = df[df[currency].notna()] df["log_rate"] = np.log(df.iloc[:,1]/df.iloc[:,1].shift()) # getting the log of the exchange rate # double check this is the correct way to get log df["rolling_mean_30"] = df[currency].rolling(30).mean() df["year"] = df["date"].dt.year df_dict[currency] = df # currencies for comparison high_freq = [df_dict["us_dollar"], df_dict["japanese_yen"], df_dict["uk_pound"], df_dict["australian_dollar"]] low_freq = [df_dict["israeli_shekel"], df_dict["philippine_peso"], df_dict["malaysian_ringgit"], df_dict["romanian_leu"]] print("x-rates head:") display(x_rates.head()) # In[3]: # widget can be turned on and off to discover specific dates # %matplotlib widget # line graph showing Euro to four high frequency trading pairs overlapped chart_date_high = (df_dict["us_dollar"]["date"]) colors = iter([plt.cm.tab10(i) for i in range(0,4)]) fig, ax = plt.subplots(figsize=(18,10)) ax1 = ax.twinx() ax2 = ax.twinx() ax3 = ax.twinx() for ax, currency in zip ((ax, ax1,ax2,ax3), high_freq): ax.plot(chart_date_high, currency["rolling_mean_30"], linewidth=3, c=next(colors)) # for ax in ax, ax1, ax2, ax3: ax.set_xticks(["2000-01-04 00:00:00", "2005-01-04 00:00:00", "2010-01-04 00:00:00", "2015-01-04 00:00:00", "2020-01-04 00:00:00"]) ax.set_xticklabels([2000,2005,2010,2015,2020], fontsize=18) ax.set(yticks=[]) # Sept 11, 2001 ax.axvline(x=chart_date_high.iloc[701], alpha=0.8, color="gold", linewidth=2) ax.axvspan(xmin=chart_date_high.iloc[11], xmax=chart_date_high.iloc[701], ymax=0.97, ymin=0.86, alpha=0.4, color='gold') ax.text(0.165, 0.82, "9/11 attacks", color="black", fontsize=16, transform=fig.transFigure) ax.text(0.165, 0.8, "in US", color="black", fontsize=16, transform=fig.transFigure) # EESA # https://en.wikipedia.org/wiki/Emergency_Economic_Stabilization_Act_of_2008 ax.axvline(x=chart_date_high.iloc[2544], color="gold", linewidth=2) ax.axvspan(xmin=chart_date_high.iloc[1644], xmax=chart_date_high.iloc[2544], ymax=0.19, ymin=0.10, alpha=0.4, color='gold') ax.text(0.365, 0.23, "E.E.S.A. enacted", color="black", fontsize=16, transform=fig.transFigure) ax.text(0.365, 0.21, "in US", color="black", fontsize=16, transform=fig.transFigure) # European Debt Crisis # https://www.businessinsider.com/s?q=euro+forex+2012 # https://www.ecb.europa.eu/pub/pdf/scpwps/ecbwp1532.pdf ax.axvline(x=chart_date_high.iloc[3568], color="gold", linewidth=2) ax.axvspan(xmin=chart_date_high.iloc[3568], xmax=chart_date_high.iloc[4488], ymax=0.97, ymin=0.86, alpha=0.4, color='gold') ax.text(0.60, 0.82, "ECB announces", color="black", fontsize=16, transform=fig.transFigure) ax.text(0.60, 0.8, "unlimited support", color="black", fontsize=16, transform=fig.transFigure) fig.suptitle(" "*12 + "Alignment of Euro/High Frequency Pairs" + " "*12, color = "#f0f0f0", backgroundcolor = "#4d4d4d", fontsize=35, weight="bold") ax.text(0.75, 0.24, "US dollar", color="tab:blue", fontsize=18, weight="bold", transform=fig.transFigure) ax.text(0.75, 0.21, "Japanese yen", color="tab:orange", fontsize=18, weight="bold", transform=fig.transFigure) ax.text(0.75, 0.17, "UK pound", color="tab:green", fontsize=18, weight="bold", transform=fig.transFigure) ax.text(0.75, 0.14, "Australian dollar", color="tab:red", fontsize=18, weight="bold", transform=fig.transFigure) ax.text(0.08, 0.0, "© B McMinn" + " "*174 + "Source: European Central Bank", color = "#f0f0f0", backgroundcolor = "#4d4d4d", fontsize=14, transform=fig.transFigure) plt.show() # In[4]: # ***** # Here is all the same stuff but with NaN excluded # In[5]: # create a list of data frames for each currency with # the log rate of the exchange rate, 30 day rolling mean, and year df_dict = {} for currency in x_rates.columns[1:]: df_name = currency df = x_rates[["date", currency]].copy() df = df[df[currency].notna()] df["log_rate"] = np.log(df.iloc[:,1]/df.iloc[:,1].shift()) # getting the log of the exchange rate # double check this is the correct way to get log df["rolling_mean_30"] = df[currency].rolling(30).mean() df["year"] = df["date"].dt.year df_dict[currency] = df # currencies for comparison high_freq = [df_dict["us_dollar"], df_dict["japanese_yen"], df_dict["uk_pound"], df_dict["australian_dollar"]] low_freq = [df_dict["israeli_shekel"], df_dict["philippine_peso"], df_dict["malaysian_ringgit"], df_dict["romanian_leu"]] # In[6]: # the rows for the three vlines sept11 = df_dict["us_dollar"]["date"] == "2001-09-11" sept11 = df_dict["us_dollar"][sept11] eesa = df_dict["us_dollar"]["date"] == "2008-10-03" eesa = df_dict["us_dollar"][eesa] edc = df_dict["us_dollar"]["date"] == "2012-09-06" edc = df_dict["us_dollar"][edc] print("the rows for the three vlines") display(sept11) display(eesa) display(edc) # In[7]: # widget can be turned on and off to discover specific dates # %matplotlib widget # line graph showing Euro to four high frequency trading pairs overlapped chart_date_high = (df_dict["us_dollar"]["date"]) colors = iter([plt.cm.tab10(i) for i in range(0,4)]) fig, ax = plt.subplots(figsize=(18,10)) ax1 = ax.twinx() ax2 = ax.twinx() ax3 = ax.twinx() for ax, currency in zip ((ax, ax1,ax2,ax3), high_freq): ax.plot(chart_date_high, currency["rolling_mean_30"], linewidth=3, c=next(colors)) ax.set_xticks(["2000-01-04 00:00:00", "2005-01-04 00:00:00", "2010-01-04 00:00:00", "2015-01-04 00:00:00", "2020-01-04 00:00:00"]) ax.set_xticklabels([2000,2005,2010,2015,2020], fontsize=18) ax.set(yticks=[]) # Sept 11, 2001 sept11 = df_dict["us_dollar"]["date"] == "2001-09-11" sept11 = df_dict["us_dollar"][sept11] sept11 = sept11["date"] ax.axvline(x=sept11, alpha=0.8, color="gold", linewidth=2) ax.axvspan(xmin=chart_date_high.iloc[11], xmax=chart_date_high.iloc[701], ymax=0.97, ymin=0.86, alpha=0.4, color='gold') ax.text(0.165, 0.82, "9/11 attacks", color="black", fontsize=16, transform=fig.transFigure) ax.text(0.165, 0.8, "in US", color="black", fontsize=16, transform=fig.transFigure) # EESA # https://en.wikipedia.org/wiki/Emergency_Economic_Stabilization_Act_of_2008 eesa = df_dict["us_dollar"]["date"] == "2008-10-03" eesa = df_dict["us_dollar"][eesa] eesa = eesa["date"] ax.axvline(x=eesa, color="gold", linewidth=2) ax.axvspan(xmin=chart_date_high.iloc[1644], xmax=chart_date_high.iloc[2544], ymax=0.19, ymin=0.10, alpha=0.4, color='gold') ax.text(0.365, 0.23, "E.E.S.A. enacted", color="black", fontsize=16, transform=fig.transFigure) ax.text(0.365, 0.21, "in US", color="black", fontsize=16, transform=fig.transFigure) # European Debt Crisis # https://www.businessinsider.com/s?q=euro+forex+2012 # https://www.ecb.europa.eu/pub/pdf/scpwps/ecbwp1532.pdf edc = df_dict["us_dollar"]["date"] == "2012-09-06" edc = df_dict["us_dollar"][edc] edc = edc["date"] ax.axvline(x=edc, color="gold", linewidth=2) ax.axvspan(xmin=chart_date_high.iloc[3568], xmax=chart_date_high.iloc[4488], ymax=0.97, ymin=0.86, alpha=0.4, color='gold') ax.text(0.60, 0.82, "ECB announces", color="black", fontsize=16, transform=fig.transFigure) ax.text(0.60, 0.8, "unlimited support", color="black", fontsize=16, transform=fig.transFigure) # other text fig.suptitle(" "*12 + "Alignment of Euro/High Frequency Pairs" + " "*12, color = "#f0f0f0", backgroundcolor = "#4d4d4d", fontsize=35, weight="bold") ax.text(0.75, 0.24, "US dollar", color="tab:blue", fontsize=18, weight="bold", transform=fig.transFigure) ax.text(0.75, 0.21, "Japanese yen", color="tab:orange", fontsize=18, weight="bold", transform=fig.transFigure) ax.text(0.75, 0.17, "UK pound", color="tab:green", fontsize=18, weight="bold", transform=fig.transFigure) ax.text(0.75, 0.14, "Australian dollar", color="tab:red", fontsize=18, weight="bold", transform=fig.transFigure) ax.text(0.08, 0.0, "© B McMinn" + " "*174 + "Source: European Central Bank", color = "#f0f0f0", backgroundcolor = "#4d4d4d", fontsize=14, transform=fig.transFigure) plt.show()