#!/usr/bin/env python
# coding: utf-8
# # The Euro on Forex
# ## Exploring Twenty Years of Volatility
# ### History
# The currency exchange market has been around since coins were first used. At the end of the 1800s the development of the gold standard fixed currency values to a certain amount of gold, allowing for an internationally stable monetary system (1). Eventually the amount of gold was not enough to keep up with the demand for currency. After World War II the Bretton Woods Accord set the US Dollar as the global fixed currency. This was an adjustable rate, but still determined by central banks and state actors (2). The US dollar was pegged to gold at the time, but during the 1970s the same problem occurred with the dollar. In August of 1971 President Richard Nixon unilaterally canceled direct international convertability of the US dollar to gold, inadvertently beginning a regime of free-floating currencies (3).
#
# ### Today
# The modern foreign exchange market (also called Forex, FX, or currency market) is a direct broker to dealer global marketplace that determines the market value and exchange rate of currencies all over the world. It is open 24 hours a day, 5 days a week. Currencies are always traded in pairs, and the value expressed is of one currency relative to another. For example, at the end of the day on 11/19/21 the Euro to US dollar rate was 1.13, or $1.13 equals €1. All of the twenty most traded currencies are free-floating, their value is determined by political conditions, economic factors and market psychology.
#
# Forex is by far the largest financial market in the world, in 2019 having a daily volume of over $6.6 trillion (4). The Euro to US dollar (EURUSD) is the most traded currency pair, taking about 30% of the multi-billion dollar Forex turnover (5).
#
# ### Methodology
# This study examines the volatility of the value of the Euro to eight other currencies. It uses the 2019 Triennial Central Bank Survey by the Bank for International Settlements to determine four currencies that represent large portions of the market and four currencies that represent small portions of the market. The highly traded currencies are the US dollar, Japanese yen, UK pound, and Australian dollar. The Israeli shekel, Phillipine peso, Malaysian ringgit, and Romanian leu comprise the group of least traded currencies.
#
# The data set used is based on daily closing values provided by the European Central Bank and compiled by Daria Chemkaeva. It is updated weekly, the version used for this study was downloaded from Kaggle in November 2021.
#
# ### Summary
# Text with conclusion.
#
#
# ## Initial Look
# The first steps are to import the basic libraries, open the file, and take an initial look at the dataset.
#
#
# In[1]:
# import libraries and set display options
import pandas as pd
import pprint
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', 1000)
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")
# head, tail, and basic info
display(x_rates.head())
display(x_rates.tail())
display(x_rates.info())
# ## Initial Look Continued
# The dataset covers the time period from January 1999 to January 2021. It has 5699 rows and 41 columns. Some of these columns have less than half the number of entries as other columns. In the original data set, cells with missing entries have a hyphen (-). This causes problems with a lot of the functions that will be used. For the time being the hyphens will be converted to the value ```NaN```, which preserves the row of information.
# ## Data Cleaning
# The first steps will be:
# * Clean and modify column names
# * Convert to `datetime` data type
# * Sort the values with oldest first and reset the index
# * Clean currency columns
# * Convert exchange rates to floats
# In[2]:
# strip brackets and trailing space from country names
# replace remaining space with underscore
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(" ","_")
# 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)
# ## Euro to Dollar
# The change in the Euro exchange rate over time can be visualized in a line graph. The US dollar is the first currency to be examined.
# In[3]:
# create a euro to dollar dataframe and add 30 day rolling mean column
us_dollar = x_rates[["Date","US_dollar"]].copy()
us_dollar["rolling_mean_30"] = us_dollar["US_dollar"].rolling(30).mean()
# line graph for euro to dollar exchange rate
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
plt.plot(us_dollar["Date"], us_dollar["rolling_mean_30"])
plt.show()
# In[4]:
# create nan group and get dates
nan_dollar_group = us_dollar[us_dollar["US_dollar"].isnull()]
nan_days = nan_dollar_group["Date"]
# line graph showing 30 day rolling mean with and without vertical bars representing null days
plt.figure(figsize=(10,3.75))
plt.subplot(1,2,1)
plt.plot(us_dollar["Date"], us_dollar["rolling_mean_30"])
plt.yticks([])
plt.subplot(1,2,2)
plt.plot(us_dollar["Date"], us_dollar["rolling_mean_30"])
plt.yticks([])
for xc in nan_days:
plt.axvline(x=xc, color="lightcyan")
plt.suptitle("Euro to US dollar exchange rates, dataset w/ NaN values and corresponding dates in light blue")
plt.show()
# create a dollar to euro dataframe with NaN dates removed
US_dollar_nonull = x_rates[x_rates["US_dollar"].notna()].copy()
US_dollar_nonull["rolling_mean_30"] = US_dollar_nonull["US_dollar"].rolling(30).mean()
# line graph showing 30 day rolling mean with and without NaN dates
plt.figure(figsize=(10,3.75))
plt.subplot(1,2,1)
plt.plot(us_dollar["Date"], us_dollar["rolling_mean_30"])
plt.yticks([])
plt.subplot(1,2,2)
plt.plot(US_dollar_nonull["Date"], US_dollar_nonull["rolling_mean_30"])
plt.yticks([])
plt.suptitle("Euro to US dollar exchange rates, with NaN dates and corresponding dates removed")
plt.show()
# ## Euro to Dollar Continued
# The line graph shows that the exchange rate between the Euro and US dollar is highly variable. There are also a lot of gaps in the graph. These represent points in the data frame where information for a particular day is missing. The third chart shows vertical blue stripes for all dates with missing information. These correspond perfectly with the gaps in the original chart. One option is to remove all the missing rows. The final chart, with these rows removed, appears to be good match and is easier to look at. All further currency data frames will be handled the same.
#
# ## Euro to Highly Traded Currencies
# The line graphs represent the exchange rates for the Euro to the US dollar, Japanese yen, UK pound, and Australian dollar. These four currencies, in addition to the Euro, are the top five currenceis in the Forex market. All five together comprise over 75% of daily trades (4).
# In[5]:
column_names = list(x_rates)
column_names = column_names[1:]
# us_dollar = x_rates[["Date","US_dollar"]].copy()
# us_dollar["rolling_mean_30"] = us_dollar["US_dollar"].rolling(30).mean()
# def rounder(element):
# element = element
# rf = x_rates
# rf.tail()
# In[6]:
# function to build data frame for Euro to other half of trading pair
xchange_list = []
def xchange(column):
xchange_list.append(column)
dataset = x_rates[["Date",column]].copy()
dataset = dataset[dataset[column].notna()]
dataset["rolling_mean_30"] = dataset[column].rolling(30).mean()
return dataset
# is there a way to apply this function to all the columns in x_rates?
# one drawback would be having 42 data frames
# but I don't know how else to deal with NaN cells being different for each currency
# i should see if there's a way to add the rolling mean column for each currency
# calling xchange function on four highly traded currencies
us_dollar = xchange("US_dollar")
japanese_yen = xchange("Japanese_yen")
uk_pound = xchange("UK_pound")
australian_dollar = xchange("Australian_dollar")
# line graph showing Euro to each trading pair
plt.figure(figsize=(10,7.5))
plt.subplot(2,2,1)
plt.plot(us_dollar["Date"], us_dollar["rolling_mean_30"])
plt.title("Euro to US dollar")
plt.yticks([])
plt.subplot(2,2,2)
plt.plot(japanese_yen["Date"], japanese_yen["rolling_mean_30"])
plt.title("Euro to Japanese yen")
plt.yticks([])
plt.subplot(2,2,3)
plt.plot(uk_pound["Date"], uk_pound["rolling_mean_30"])
plt.title("Euro to UK pound")
plt.yticks([])
plt.subplot(2,2,4)
plt.plot(australian_dollar["Date"], australian_dollar["rolling_mean_30"])
plt.title("Euro to Australian dollar")
plt.yticks([])
plt.subplots_adjust(hspace=0.3)
plt.show()
# with less code?
# plt.figure(figsize=(10,7.5))
# for i in range(1, 5):
# plt.subplot(2,2,i)
# plt.show()
# ## Euro to Highly Traded Currencies Continued
# stuff
# stuff
# ## Euro to Least Traded Currencies
# The line graphs represent the exchange rates for the Euro to the Israeli shekel, Philippine peso, Malaysian ringgit, and Romanian leu. These four currencies have low trade volumes, all five together comprise less than 0.4% of daily trades (4).
# In[7]:
# calling xchange function on four least traded currencies
israeli_shekel = xchange("Israeli_shekel")
philippine_peso = xchange("Philippine_peso")
malaysian_ringgit = xchange("Malaysian_ringgit")
romanian_leu = xchange("Romanian_leu")
# line graph showing Euro to each trading pair
plt.figure(figsize=(10,7.5))
plt.subplot(2,2,1)
plt.plot(israeli_shekel["Date"], israeli_shekel["rolling_mean_30"])
plt.title("Euro to Israeli shekel")
plt.yticks([])
plt.subplot(2,2,2)
plt.plot(philippine_peso["Date"], philippine_peso["rolling_mean_30"])
plt.title("Euro to Philippine peso")
plt.yticks([])
plt.subplot(2,2,3)
plt.plot(malaysian_ringgit["Date"], malaysian_ringgit["rolling_mean_30"])
plt.title("Euro to Malaysian ringgit")
plt.yticks([])
plt.subplot(2,2,4)
plt.plot(romanian_leu["Date"], romanian_leu["rolling_mean_30"])
plt.title("Euro to Romanian leu")
plt.yticks([])
plt.subplots_adjust(hspace=0.3)
plt.show()
# ## Euro to Least Traded Currencies Continued
# Israeli shekel and Philppine peso both appear to be increasing in value and the Romanian leu is definitely loosing value. The shekel and leu also appear to be fairly stable, or have low volatility. Determining volatility of the eight currencies for the past two decades illustrates part of the story of the twenty-first century.
#
# ## Volatility
# stuff
# In[8]:
# function to get log of daily rate
def logarizer(dataset):
dataset["log_rate"] = np.log(dataset.iloc[:,1]/dataset.iloc[:,1].shift()) # getting the log of the exchange rate
return dataset["log_rate"]
# function to get overall volatility
volatility_dict = {}
def volatizer(dataset):
column_headers = list(dataset.columns.values)
currency_name = column_headers[1]
volatility = dataset["log_rate"].std()*253**.5 # volatility measured by standard deviation * 253 trading days per year
volatility = round(volatility, 4)*100
volatility_dict[currency_name] = volatility
# it would be cool to convert or use xchange_list
# i didn't really think .replace would work
# currency_list = []
# for currency in xchange_list:
# currency = currency.replace("'","").lower()
# currency_list.append(currency)
# currency_list
# ['us_dollar',
# 'japanese_yen',
# 'uk_pound',
# 'australian_dollar',
# 'israeli_shekel',
# 'philippine_peso',
# 'malaysian_ringgit',
# 'romanian_leu']
# calling logarizer on all eight currencies
c_list = [us_dollar, japanese_yen, uk_pound, australian_dollar,
israeli_shekel, philippine_peso, malaysian_ringgit, romanian_leu]
for currency in c_list:
logarizer(currency)
# calling volatizer on all eight currencies
for currency in c_list:
volatizer(currency)
# line graph showing log exchange rates of each currency
plt.figure(figsize=(10,14))
for i, currency in zip(range(1,9), c_list):
plt.subplot(4,2,i)
currency["log_rate"].plot.hist(bins=50)
# plt.title(currency) # will get ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
plt.show()
# plt.title("Romanian volatility: " + str(romanian_leu["volatility"]) + "%")
# In[9]:
volatility_dict
# In[10]:
# import geopandas and other libraries
import geopandas as gpd
import seaborn as sns
import os
map_data = gpd.read_file("World_Countries__Generalized_.shp")
map_data["Volatility"] = None
map_data.head()
map_data.shape
# map_data.plot(figsize=(15,13))
# map_data[map_data["COUNTRY"] == "United States"].plot(color = "green")
# ## Volatility Continued
# stuff
# ## Mapping
# stuff
# ## Citations
# 1. Eichengreen, Barry (2019). Globalizing Capital: A History of the International Monetary System (3rd ed.). Princeton University Press. pp. 7, 79. ISBN 978-0-691-19390-8. JSTOR j.ctvd58rxg.
# 2. https://en.wikipedia.org/wiki/Bretton_Woods_system
# 3. Lowenstein, Roger (August 5, 2011). "The Nixon Shock". www.bloomberg.com. Bloomberg. Retrieved March 25, 2019.
# 4. Bank for International Settlements. "Foreign Exchange Turnover in April 2019." Accessed Oct. 3, 2020.
# 5. https://fxssi.com/the-most-traded-currency-pairs