#!/usr/bin/env python # coding: utf-8 # In[1]: # import libraries and set display options import pandas as pd import pprint import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as mdates # 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 x_rates = x_rates.replace("-", np.nan) # convert exchange rate values to float x_rates.iloc[:,1:] = x_rates.iloc[:,1:].astype(float) display(x_rates.head()) # In[2]: # create a list of data frames for each currency with 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["rolling_mean_30"] = df[currency].rolling(30).mean() df_dict[currency] = df # create a stand-alone date series chart_date = (x_rates["date"]) # In[3]: # line graph showing Euro to four high frequency trading pairs overlapped get_ipython().run_line_magic('matplotlib', 'widget') fig, ax = plt.subplots(figsize=(8,6)) ax1 = ax.twinx() ax2 = ax.twinx() ax3 = ax.twinx() line = ax.plot(chart_date, df_dict["us_dollar"]["rolling_mean_30"], 'tab:blue') line_1 = ax1.plot(chart_date, df_dict["japanese_yen"]["rolling_mean_30"], 'tab:orange') line_2 = ax2.plot(chart_date, df_dict["uk_pound"]["rolling_mean_30"], 'tab:green') line_3 = ax3.plot(chart_date, df_dict["australian_dollar"]["rolling_mean_30"], 'tab:red') ax.axvline(x=chart_date.iloc[3560], color="gold", linewidth=2) # vline to highlight point on chart for ax in ax, ax1, ax2, ax3: ax.set(yticks=[]) ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%y')) plt.show() # In[4]: display(chart_date.iloc[0]) display(chart_date.iloc[701]) display(chart_date.iloc[2499]) display(chart_date.iloc[3560]) # In[5]: display(chart_date)