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 (i.e. amount of gold vs currency in circulation) 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).
The modern foreign exchange market (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).
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.
Text with conclusion.
The first steps are to import the basic libraries, open the file, and take an initial look at the dataset.
# 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())
Period\Unit: | [Australian dollar ] | [Bulgarian lev ] | [Brazilian real ] | [Canadian dollar ] | [Swiss franc ] | [Chinese yuan renminbi ] | [Cypriot pound ] | [Czech koruna ] | [Danish krone ] | [Estonian kroon ] | [UK pound sterling ] | [Greek drachma ] | [Hong Kong dollar ] | [Croatian kuna ] | [Hungarian forint ] | [Indonesian rupiah ] | [Israeli shekel ] | [Indian rupee ] | [Iceland krona ] | [Japanese yen ] | [Korean won ] | [Lithuanian litas ] | [Latvian lats ] | [Maltese lira ] | [Mexican peso ] | [Malaysian ringgit ] | [Norwegian krone ] | [New Zealand dollar ] | [Philippine peso ] | [Polish zloty ] | [Romanian leu ] | [Russian rouble ] | [Swedish krona ] | [Singapore dollar ] | [Slovenian tolar ] | [Slovak koruna ] | [Thai baht ] | [Turkish lira ] | [US dollar ] | [South African rand ] | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2021-01-08 | 1.5758 | 1.9558 | 6.5748 | 1.5543 | 1.0827 | 7.9184 | NaN | 26.163 | 7.4369 | NaN | 0.90128 | NaN | 9.4982 | 7.5690 | 359.62 | 17247.33 | 3.8981 | 89.7975 | 155.5000 | 127.26 | 1337.90 | NaN | NaN | NaN | 24.4718 | 4.9359 | 10.2863 | 1.6883 | 58.947 | 4.5113 | 4.8708 | 90.8000 | 10.0510 | 1.6228 | NaN | NaN | 36.8480 | 9.0146 | 1.2250 | 18.7212 |
1 | 2021-01-07 | 1.5836 | 1.9558 | 6.5172 | 1.5601 | 1.0833 | 7.9392 | NaN | 26.147 | 7.4392 | NaN | 0.90190 | NaN | 9.5176 | 7.5660 | 357.79 | 17259.99 | 3.9027 | 90.0455 | 155.3000 | 127.13 | 1342.29 | NaN | NaN | NaN | 24.2552 | 4.9570 | 10.3435 | 1.6907 | 59.043 | 4.4998 | 4.8712 | 91.2000 | 10.0575 | 1.6253 | NaN | NaN | 36.8590 | 8.9987 | 1.2276 | 18.7919 |
2 | 2021-01-06 | 1.5824 | 1.9558 | 6.5119 | 1.5640 | 1.0821 | 7.9653 | NaN | 26.145 | 7.4393 | NaN | 0.90635 | NaN | 9.5659 | 7.5595 | 357.86 | 17168.20 | 3.9289 | 90.2040 | 156.3000 | 127.03 | 1339.30 | NaN | NaN | NaN | 24.3543 | 4.9482 | 10.3810 | 1.6916 | 59.296 | 4.5160 | 4.8720 | 90.8175 | 10.0653 | 1.6246 | NaN | NaN | 36.9210 | 9.0554 | 1.2338 | 18.5123 |
3 | 2021-01-05 | 1.5927 | 1.9558 | 6.5517 | 1.5651 | 1.0803 | 7.9315 | NaN | 26.227 | 7.4387 | NaN | 0.90333 | NaN | 9.5136 | 7.5588 | 360.27 | 17075.10 | 3.9277 | 89.8670 | 156.1000 | 126.25 | 1335.85 | NaN | NaN | NaN | 24.5860 | 4.9293 | 10.4713 | 1.7036 | 59.020 | 4.5473 | 4.8721 | 91.6715 | 10.0570 | 1.6180 | NaN | NaN | 36.7760 | 9.0694 | 1.2271 | 18.4194 |
4 | 2021-01-04 | 1.5928 | 1.9558 | 6.3241 | 1.5621 | 1.0811 | 7.9484 | NaN | 26.141 | 7.4379 | NaN | 0.90160 | NaN | 9.5330 | 7.5565 | 361.32 | 17062.67 | 3.9430 | 89.7890 | 156.1000 | 126.62 | 1332.03 | NaN | NaN | NaN | 24.3031 | 4.9264 | 10.4440 | 1.7065 | 59.058 | 4.5475 | 4.8713 | 90.3420 | 10.0895 | 1.6198 | NaN | NaN | 36.7280 | 9.0579 | 1.2296 | 17.9214 |
Period\Unit: | [Australian dollar ] | [Bulgarian lev ] | [Brazilian real ] | [Canadian dollar ] | [Swiss franc ] | [Chinese yuan renminbi ] | [Cypriot pound ] | [Czech koruna ] | [Danish krone ] | [Estonian kroon ] | [UK pound sterling ] | [Greek drachma ] | [Hong Kong dollar ] | [Croatian kuna ] | [Hungarian forint ] | [Indonesian rupiah ] | [Israeli shekel ] | [Indian rupee ] | [Iceland krona ] | [Japanese yen ] | [Korean won ] | [Lithuanian litas ] | [Latvian lats ] | [Maltese lira ] | [Mexican peso ] | [Malaysian ringgit ] | [Norwegian krone ] | [New Zealand dollar ] | [Philippine peso ] | [Polish zloty ] | [Romanian leu ] | [Russian rouble ] | [Swedish krona ] | [Singapore dollar ] | [Slovenian tolar ] | [Slovak koruna ] | [Thai baht ] | [Turkish lira ] | [US dollar ] | [South African rand ] | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
5694 | 1999-01-08 | 1.8406 | NaN | NaN | 1.7643 | 1.6138 | NaN | 0.58187 | 34.938 | 7.4433 | 15.6466 | 0.70940 | 324.00 | 9.0302 | NaN | 250.15 | 9321.63 | NaN | NaN | 80.9900 | 130.09 | 1366.73 | 4.66430 | 0.6654 | 0.4419 | 11.4414 | 4.4295 | 8.5900 | 2.1557 | 44.295 | 4.0363 | 1.3143 | 27.2075 | 9.1650 | 1.9537 | 188.8400 | 42.560 | 42.5590 | 0.3718 | 1.1659 | 6.7855 |
5695 | 1999-01-07 | 1.8474 | NaN | NaN | 1.7602 | 1.6165 | NaN | 0.58187 | 34.886 | 7.4431 | 15.6466 | 0.70585 | 324.40 | 9.0131 | NaN | 250.09 | 9218.77 | NaN | NaN | 81.0600 | 129.43 | 1337.16 | 4.65480 | 0.6627 | 0.4413 | 11.5511 | 4.4203 | 8.6295 | 2.1531 | 44.436 | 4.0165 | 1.3092 | 26.9876 | 9.1800 | 1.9436 | 188.8000 | 42.765 | 42.1678 | 0.3701 | 1.1632 | 6.8283 |
5696 | 1999-01-06 | 1.8820 | NaN | NaN | 1.7711 | 1.6116 | NaN | 0.58200 | 34.850 | 7.4452 | 15.6466 | 0.70760 | 324.72 | 9.1010 | NaN | 250.67 | 9337.68 | NaN | NaN | 81.5400 | 131.42 | 1359.54 | 4.69940 | 0.6649 | 0.4420 | 11.4705 | 4.4637 | 8.7335 | 2.1890 | 44.872 | 4.0065 | 1.3168 | 27.4315 | 9.3050 | 1.9699 | 188.7000 | 42.778 | 42.6949 | 0.3722 | 1.1743 | 6.7307 |
5697 | 1999-01-05 | 1.8944 | NaN | NaN | 1.7965 | 1.6123 | NaN | 0.58230 | 34.917 | 7.4495 | 15.6466 | 0.71220 | 324.70 | 9.1341 | NaN | 250.80 | 9314.51 | NaN | NaN | 81.5300 | 130.96 | 1373.01 | 4.71740 | 0.6657 | 0.4432 | 11.5960 | 4.4805 | 8.7745 | 2.2011 | 44.745 | 4.0245 | 1.3168 | 26.5876 | 9.4025 | 1.9655 | 188.7750 | 42.848 | 42.5048 | 0.3728 | 1.1790 | 6.7975 |
5698 | 1999-01-04 | 1.9100 | NaN | NaN | 1.8004 | 1.6168 | NaN | 0.58231 | 35.107 | 7.4501 | 15.6466 | 0.71110 | 327.15 | 9.1332 | NaN | 251.48 | 9433.61 | NaN | NaN | 81.4800 | 133.73 | 1398.59 | 4.71700 | 0.6668 | 0.4432 | 11.6446 | 4.4798 | 8.8550 | 2.2229 | 45.510 | 4.0712 | 1.3111 | 25.2875 | 9.4696 | 1.9554 | 189.0450 | 42.991 | 42.6799 | 0.3723 | 1.1789 | 6.9358 |
<class 'pandas.core.frame.DataFrame'> RangeIndex: 5699 entries, 0 to 5698 Data columns (total 41 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Period\Unit: 5699 non-null object 1 [Australian dollar ] 5699 non-null object 2 [Bulgarian lev ] 5297 non-null object 3 [Brazilian real ] 5431 non-null object 4 [Canadian dollar ] 5699 non-null object 5 [Swiss franc ] 5699 non-null object 6 [Chinese yuan renminbi ] 5431 non-null object 7 [Cypriot pound ] 2346 non-null object 8 [Czech koruna ] 5699 non-null object 9 [Danish krone ] 5699 non-null object 10 [Estonian kroon ] 3130 non-null object 11 [UK pound sterling ] 5699 non-null object 12 [Greek drachma ] 520 non-null object 13 [Hong Kong dollar ] 5699 non-null object 14 [Croatian kuna ] 5431 non-null object 15 [Hungarian forint ] 5699 non-null object 16 [Indonesian rupiah ] 5699 non-null object 17 [Israeli shekel ] 5431 non-null object 18 [Indian rupee ] 5431 non-null object 19 [Iceland krona ] 3292 non-null float64 20 [Japanese yen ] 5699 non-null object 21 [Korean won ] 5699 non-null object 22 [Lithuanian litas ] 4159 non-null object 23 [Latvian lats ] 3904 non-null object 24 [Maltese lira ] 2346 non-null object 25 [Mexican peso ] 5699 non-null object 26 [Malaysian ringgit ] 5699 non-null object 27 [Norwegian krone ] 5699 non-null object 28 [New Zealand dollar ] 5699 non-null object 29 [Philippine peso ] 5699 non-null object 30 [Polish zloty ] 5699 non-null object 31 [Romanian leu ] 5637 non-null float64 32 [Russian rouble ] 5699 non-null object 33 [Swedish krona ] 5699 non-null object 34 [Singapore dollar ] 5699 non-null object 35 [Slovenian tolar ] 2085 non-null object 36 [Slovak koruna ] 2608 non-null object 37 [Thai baht ] 5699 non-null object 38 [Turkish lira ] 5637 non-null float64 39 [US dollar ] 5699 non-null object 40 [South African rand ] 5699 non-null object dtypes: float64(3), object(38) memory usage: 1.8+ MB
None
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.
The first steps will be:
datetime
data type# 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)
# set style for cells with centered output
from IPython.core.display import HTML
HTML("""
<style>
.output_png {
display: table-cell;
text-align: center;
vertical-align: middle;
}
</style>
""")
# x_rates head
display(x_rates.head())
/var/folders/bz/5cj1tzbj5xn319rpqrw6gpmh0000gn/T/ipykernel_4258/1330394742.py:4: FutureWarning: The default value of regex will change from True to False in a future version. In addition, single character regular expressions will *not* be treated as literal strings when regex=True. x_rates.columns = x_rates.columns.str.replace("[","").str.replace("]","")
date | australian_dollar | bulgarian_lev | brazilian_real | canadian_dollar | swiss_franc | chinese_yuan | cypriot_pound | czech_koruna | danish_krone | estonian_kroon | uk_pound | greek_drachma | hong_kong_dollar | croatian_kuna | hungarian_forint | indonesian_rupiah | israeli_shekel | indian_rupee | iceland_krona | japanese_yen | korean_won | lithuanian_litas | latvian_lats | maltese_lira | mexican_peso | malaysian_ringgit | norwegian_krone | new_zealand_dollar | philippine_peso | polish_zloty | romanian_leu | russian_rouble | swedish_krona | singapore_dollar | slovenian_tolar | slovak_koruna | thai_baht | turkish_lira | us_dollar | south_african_rand | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1999-01-04 | 1.9100 | NaN | NaN | 1.8004 | 1.6168 | NaN | 0.5823 | 35.1070 | 7.4501 | 15.6466 | 0.7111 | 327.1500 | 9.1332 | NaN | 251.4800 | 9,433.6100 | NaN | NaN | 81.4800 | 133.7300 | 1,398.5900 | 4.7170 | 0.6668 | 0.4432 | 11.6446 | 4.4798 | 8.8550 | 2.2229 | 45.5100 | 4.0712 | 1.3111 | 25.2875 | 9.4696 | 1.9554 | 189.0450 | 42.9910 | 42.6799 | 0.3723 | 1.1789 | 6.9358 |
1 | 1999-01-05 | 1.8944 | NaN | NaN | 1.7965 | 1.6123 | NaN | 0.5823 | 34.9170 | 7.4495 | 15.6466 | 0.7122 | 324.7000 | 9.1341 | NaN | 250.8000 | 9,314.5100 | NaN | NaN | 81.5300 | 130.9600 | 1,373.0100 | 4.7174 | 0.6657 | 0.4432 | 11.5960 | 4.4805 | 8.7745 | 2.2011 | 44.7450 | 4.0245 | 1.3168 | 26.5876 | 9.4025 | 1.9655 | 188.7750 | 42.8480 | 42.5048 | 0.3728 | 1.1790 | 6.7975 |
2 | 1999-01-06 | 1.8820 | NaN | NaN | 1.7711 | 1.6116 | NaN | 0.5820 | 34.8500 | 7.4452 | 15.6466 | 0.7076 | 324.7200 | 9.1010 | NaN | 250.6700 | 9,337.6800 | NaN | NaN | 81.5400 | 131.4200 | 1,359.5400 | 4.6994 | 0.6649 | 0.4420 | 11.4705 | 4.4637 | 8.7335 | 2.1890 | 44.8720 | 4.0065 | 1.3168 | 27.4315 | 9.3050 | 1.9699 | 188.7000 | 42.7780 | 42.6949 | 0.3722 | 1.1743 | 6.7307 |
3 | 1999-01-07 | 1.8474 | NaN | NaN | 1.7602 | 1.6165 | NaN | 0.5819 | 34.8860 | 7.4431 | 15.6466 | 0.7058 | 324.4000 | 9.0131 | NaN | 250.0900 | 9,218.7700 | NaN | NaN | 81.0600 | 129.4300 | 1,337.1600 | 4.6548 | 0.6627 | 0.4413 | 11.5511 | 4.4203 | 8.6295 | 2.1531 | 44.4360 | 4.0165 | 1.3092 | 26.9876 | 9.1800 | 1.9436 | 188.8000 | 42.7650 | 42.1678 | 0.3701 | 1.1632 | 6.8283 |
4 | 1999-01-08 | 1.8406 | NaN | NaN | 1.7643 | 1.6138 | NaN | 0.5819 | 34.9380 | 7.4433 | 15.6466 | 0.7094 | 324.0000 | 9.0302 | NaN | 250.1500 | 9,321.6300 | NaN | NaN | 80.9900 | 130.0900 | 1,366.7300 | 4.6643 | 0.6654 | 0.4419 | 11.4414 | 4.4295 | 8.5900 | 2.1557 | 44.2950 | 4.0363 | 1.3143 | 27.2075 | 9.1650 | 1.9537 | 188.8400 | 42.5600 | 42.5590 | 0.3718 | 1.1659 | 6.7855 |
The change over time in Euro exchange rate can be visualized in a line graph. The US dollar is the first currency to be examined.
# create a us dollar to euro 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 dollar to euro exchange rate
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
%matplotlib inline
years = mdates.YearLocator() # every year
years_fmt = mdates.DateFormatter('%Y')
# ticks = [2000,2005,2010,2015,2020]
fig, ax = plt.subplots(figsize=(10,3.75))
ax.plot(us_dollar["date"], us_dollar["rolling_mean_30"])
ax.xaxis.set_major_locator(years) # format the ticks
ax.xaxis.set_major_formatter(years_fmt)
fmt_three_years = mdates.MonthLocator(interval = 36) # Major ticks every 3 years
ax.xaxis.set_major_locator(fmt_three_years)
# num_ticks = 5
# plt.xticks(range(0, len(us_dollar["date"].sort_values().unique()), num_ticks),
# us_dollar["date"].sort_values().unique()[::num_ticks], rotation = 0)
ax.set(xlabel="Year",
ylabel="US dollar",
title="US Dollar to Euro Exhcange Rates")
plt.show()
The line graph shows that the exchange rate between the Euro and US dollar is pretty 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. This is the most accurate representation of the data, but it's not very easy to look at. A quick visual check can illustrate this overlap and then compare the original data frame with one where the missing dates are removed.
# 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 dollar to euro showing NaN values with and without highlight
fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(10,7.5))
for ax in ax1,ax2:
ax.xaxis.set_major_locator(years) # format the x ticks
ax.xaxis.set_major_formatter(years_fmt)
fmt_three_years = mdates.MonthLocator(interval = 36) # Major ticks every 3 years
ax.xaxis.set_major_locator(fmt_three_years)
ax.set(yticks=[],
xlabel="Year")
ax1.plot(us_dollar["date"], us_dollar["rolling_mean_30"])
ax1.set(title="dataset w/ NaN values")
ax2.plot(us_dollar["date"], us_dollar["rolling_mean_30"])
ax2.set(title="dataset w/ corresponding dates in light blue")
for xc in nan_days:
plt.axvline(x=xc, color="lightcyan")
fig.suptitle("US dollar to Euro exchange rates", fontsize=20)
fig.tight_layout(pad=3.0)
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 rows
fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(10,7.5))
for ax in ax1,ax2:
ax.xaxis.set_major_locator(years) # format the x ticks
ax.xaxis.set_major_formatter(years_fmt)
fmt_three_years = mdates.MonthLocator(interval = 36) # Major ticks every 3 years
ax.xaxis.set_major_locator(fmt_three_years)
ax.set(yticks=[],
xlabel="Year")
ax1.plot(us_dollar["date"], us_dollar["rolling_mean_30"])
ax1.set(title="NaN rows included")
ax2.plot(US_dollar_nonull["date"], US_dollar_nonull["rolling_mean_30"])
ax2.set(title="NaN rows removed")
fig.suptitle("US dollar to Euro exchange rates", fontsize=20)
fig.tight_layout(pad=3.0)
plt.show()
The third graph shows vertical blue stripes for all dates with missing information. These correspond perfectly with the gaps in the original chart. The final chart, with these rows removed, appears to be good match and is easier to look at. This also demonstrates how the rolling mean handles NaN values within a data frame. In either case the shape of the line is identical where the information exists, therefor both are similarly accurate represenations of the availalble data. All further currency data frames will be handled the same.
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).
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, they comprise less than 0.4% of daily trades (4).
# building the rolling 30 mean into the data frame
mean_rates = x_rates.rolling(30).mean()
mean_rates = mean_rates.add_suffix("_rolling_30")
new_frame = pd.concat([x_rates, mean_rates], axis=1)
new_frame = new_frame[[item for items in zip(x_rates.columns, mean_rates.columns) for item in items]]
# display(new_frame.head())
# # how do I get the currency columns before the rolling mean column
# function to build data frame for Euro to other half of trading pair
# this one uses new_frame, which has the 30 day rolling mean incorporated, and then removes the NaN dates
framer_list = []
def framer(col):
framer_list.append(col)
col_names = ["date", (str(col)), (str(col)+"_rolling_30")]
df = pd.DataFrame(columns=col_names)
df.iloc[:,0] = new_frame["date"]
df.iloc[:,1] = new_frame[col]
df.iloc[:,2] = new_frame[(str(col)+"_rolling_30")]
df = df[df.iloc[:,2].notna()]
return df
# australian_dollar = framer("australian_dollar")
# display(australian_dollar.head())
# print(framer_list)
# plt.plot(australian_dollar["date"], australian_dollar["australian_dollar_rolling_30"])
# plt.show()
# function to build data frame for Euro to other half of trading pair
# this one uses x_rates, removes the NaN values, and then adds a rolling 30 mean
xchange_list = []
def xchange(col):
xchange_list.append(col)
df = x_rates[["date",col]].copy()
df = df[df[col].notna()]
df["rolling_mean_30"] = df[col].rolling(30).mean()
return df
# canadian_dollar = xchange("canadian_dollar")
# display(canadian_dollar.head())
# print(xchange_list)
# plt.plot(canadian_dollar["date"], canadian_dollar["rolling_mean_30"])
# plt.show()
# is there a way to apply either function to all the columns in x_rates?
# one drawback could be having 42 data frames
# 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")
# 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")
# it would be cool to convert or use xchange_list for this but I can't get the string off
c_list = [us_dollar, japanese_yen, uk_pound, australian_dollar,
israeli_shekel, philippine_peso, malaysian_ringgit, romanian_leu]
# line graph showing Euro to each high frequency trading pairs
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(12,8))
fig.tight_layout(pad=3)
for ax in ax1, ax2, ax3, ax4:
ax.xaxis.set_major_locator(years) # format the x ticks
ax.xaxis.set_major_formatter(years_fmt)
fmt_three_years = mdates.MonthLocator(interval = 36) # Major ticks every 3 years
ax.xaxis.set_major_locator(fmt_three_years)
ax.set(yticks=[],
xlabel="Year")
for ax, currency in zip((ax1, ax2, ax3, ax4), c_list[:5]):
ax.plot(currency["date"], currency["rolling_mean_30"])
# ax.set_ylabel(str(currency)) # why not, i can get it to change to a string "currency", but not a variable
ax1.set(ylabel="US dollar")
ax2.set(ylabel="Japanese yen")
ax3.set(ylabel="UK pound")
ax4.set(ylabel="Australian dollar")
fig.suptitle("Euro to High Frequency Pairs", fontsize=20)
plt.show()
# line graph showing Euro to each low frequency trading pairs
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(12,8))
fig.tight_layout(pad=3)
for ax in ax1, ax2, ax3, ax4:
ax.xaxis.set_major_locator(years) # format the x ticks
ax.xaxis.set_major_formatter(years_fmt)
fmt_three_years = mdates.MonthLocator(interval = 36) # Major ticks every 3 years
ax.xaxis.set_major_locator(fmt_three_years)
ax.set(yticks=[],
xlabel="Year")
for ax, currency in zip((ax1, ax2, ax3, ax4), c_list[4:]):
ax.plot(currency["date"], currency["rolling_mean_30"])
# ax.set_ylabel(str(currency)) # why not, i can get it to change to a string "currency", but not a variable
ax1.set(ylabel="Israeli shekel")
ax2.set(ylabel="Philippine peso")
ax3.set(ylabel="Malaysian ringgit")
ax4.set(ylabel="Romanian leu")
fig.suptitle("Euro to Low Frequency Pairs", fontsize=20)
plt.show()
An overlay of all currencies focused on the first decade of the Euro shows it's initial struggle and eventual success.
x_values1 = us_dollar["date"]
y_values1 = us_dollar["rolling_mean_30"]
x_values2 = japanese_yen["date"]
y_values2 = japanese_yen["rolling_mean_30"]
x_values3 = uk_pound["date"]
y_values3 = uk_pound["rolling_mean_30"]
# x_values4 = uk_pound["date"]
# y_values4 = uk_pound["rolling_mean_30"]
fig = plt.figure()
ax = fig.add_subplot(111, label = "1")
ax2 = fig.add_subplot(111, label = "2", frame_on = False)
ax3 = fig.add_subplot(111, label = "3", frame_on = False)
for ax in ax, ax2, ax3:
ax.xaxis.set_major_locator(years) # format the x ticks
ax.xaxis.set_major_formatter(years_fmt)
fmt_three_years = mdates.MonthLocator(interval = 36) # Major ticks every 3 years
ax.xaxis.set_major_locator(fmt_three_years)
ax.set(yticks=[],
xlabel="Year")
# ax.set_xlim(right=2009)
ax.plot(x_values1, y_values1, color = "blue")
ax.set_yticks([])
# ax.set_xlabel("x label 1", color = "C0")
# ax.set_ylabel("y label 1", color = "C0")
# ax.tick_params(axis = 'x', colors = "C0")
# ax.tick_params(axis = 'y', colors = "C0")
ax2.plot(x_values2, y_values2, color = "green")
ax2.set_yticks([])
# ax2.xaxis.tick_top()
# ax2.yaxis.tick_right()
# ax2.set_xlabel('x label 2', color = "C1")
# ax2.set_ylabel('y label 2', color = "C1")
# ax2.xaxis.set_label_position('top')
# ax2.yaxis.set_label_position('right')
# ax2.tick_params(axis = 'x', colors = "C1")
# ax2.tick_params(axis = 'y', colors = "C1")
ax3.plot(x_values3, y_values3, color = "yellow")
ax3.set_yticks([])
plt.show()
# [us_dollar, japanese_yen, uk_pound, australian_dollar,
# israeli_shekel, philippine_peso, malaysian_ringgit, romanian_leu]
for country in c_list:
plt.plot(country["date"],country["rolling_mean_30"])
# plt.legend()
plt.show()
first_decade = x_rates[x_rates["date"].dt.year < 2009]
display(first_decade.head())
display(first_decade.tail())
date | australian_dollar | bulgarian_lev | brazilian_real | canadian_dollar | swiss_franc | chinese_yuan | cypriot_pound | czech_koruna | danish_krone | estonian_kroon | uk_pound | greek_drachma | hong_kong_dollar | croatian_kuna | hungarian_forint | indonesian_rupiah | israeli_shekel | indian_rupee | iceland_krona | japanese_yen | korean_won | lithuanian_litas | latvian_lats | maltese_lira | mexican_peso | malaysian_ringgit | norwegian_krone | new_zealand_dollar | philippine_peso | polish_zloty | romanian_leu | russian_rouble | swedish_krona | singapore_dollar | slovenian_tolar | slovak_koruna | thai_baht | turkish_lira | us_dollar | south_african_rand | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1999-01-04 | 1.9100 | NaN | NaN | 1.8004 | 1.6168 | NaN | 0.5823 | 35.1070 | 7.4501 | 15.6466 | 0.7111 | 327.1500 | 9.1332 | NaN | 251.4800 | 9,433.6100 | NaN | NaN | 81.4800 | 133.7300 | 1,398.5900 | 4.7170 | 0.6668 | 0.4432 | 11.6446 | 4.4798 | 8.8550 | 2.2229 | 45.5100 | 4.0712 | 1.3111 | 25.2875 | 9.4696 | 1.9554 | 189.0450 | 42.9910 | 42.6799 | 0.3723 | 1.1789 | 6.9358 |
1 | 1999-01-05 | 1.8944 | NaN | NaN | 1.7965 | 1.6123 | NaN | 0.5823 | 34.9170 | 7.4495 | 15.6466 | 0.7122 | 324.7000 | 9.1341 | NaN | 250.8000 | 9,314.5100 | NaN | NaN | 81.5300 | 130.9600 | 1,373.0100 | 4.7174 | 0.6657 | 0.4432 | 11.5960 | 4.4805 | 8.7745 | 2.2011 | 44.7450 | 4.0245 | 1.3168 | 26.5876 | 9.4025 | 1.9655 | 188.7750 | 42.8480 | 42.5048 | 0.3728 | 1.1790 | 6.7975 |
2 | 1999-01-06 | 1.8820 | NaN | NaN | 1.7711 | 1.6116 | NaN | 0.5820 | 34.8500 | 7.4452 | 15.6466 | 0.7076 | 324.7200 | 9.1010 | NaN | 250.6700 | 9,337.6800 | NaN | NaN | 81.5400 | 131.4200 | 1,359.5400 | 4.6994 | 0.6649 | 0.4420 | 11.4705 | 4.4637 | 8.7335 | 2.1890 | 44.8720 | 4.0065 | 1.3168 | 27.4315 | 9.3050 | 1.9699 | 188.7000 | 42.7780 | 42.6949 | 0.3722 | 1.1743 | 6.7307 |
3 | 1999-01-07 | 1.8474 | NaN | NaN | 1.7602 | 1.6165 | NaN | 0.5819 | 34.8860 | 7.4431 | 15.6466 | 0.7058 | 324.4000 | 9.0131 | NaN | 250.0900 | 9,218.7700 | NaN | NaN | 81.0600 | 129.4300 | 1,337.1600 | 4.6548 | 0.6627 | 0.4413 | 11.5511 | 4.4203 | 8.6295 | 2.1531 | 44.4360 | 4.0165 | 1.3092 | 26.9876 | 9.1800 | 1.9436 | 188.8000 | 42.7650 | 42.1678 | 0.3701 | 1.1632 | 6.8283 |
4 | 1999-01-08 | 1.8406 | NaN | NaN | 1.7643 | 1.6138 | NaN | 0.5819 | 34.9380 | 7.4433 | 15.6466 | 0.7094 | 324.0000 | 9.0302 | NaN | 250.1500 | 9,321.6300 | NaN | NaN | 80.9900 | 130.0900 | 1,366.7300 | 4.6643 | 0.6654 | 0.4419 | 11.4414 | 4.4295 | 8.5900 | 2.1557 | 44.2950 | 4.0363 | 1.3143 | 27.2075 | 9.1650 | 1.9537 | 188.8400 | 42.5600 | 42.5590 | 0.3718 | 1.1659 | 6.7855 |
date | australian_dollar | bulgarian_lev | brazilian_real | canadian_dollar | swiss_franc | chinese_yuan | cypriot_pound | czech_koruna | danish_krone | estonian_kroon | uk_pound | greek_drachma | hong_kong_dollar | croatian_kuna | hungarian_forint | indonesian_rupiah | israeli_shekel | indian_rupee | iceland_krona | japanese_yen | korean_won | lithuanian_litas | latvian_lats | maltese_lira | mexican_peso | malaysian_ringgit | norwegian_krone | new_zealand_dollar | philippine_peso | polish_zloty | romanian_leu | russian_rouble | swedish_krona | singapore_dollar | slovenian_tolar | slovak_koruna | thai_baht | turkish_lira | us_dollar | south_african_rand | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2603 | 2008-12-25 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
2604 | 2008-12-26 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
2605 | 2008-12-29 | 2.0595 | 1.9558 | 3.3777 | 1.7408 | 1.4931 | 9.7788 | NaN | 26.4550 | 7.4503 | 15.6466 | 0.9786 | NaN | 11.0599 | 7.3438 | 266.6400 | 15,839.7000 | 5.4998 | 68.6360 | NaN | 128.9800 | 1,828.4900 | 3.4528 | 0.7061 | NaN | 19.1860 | 4.9660 | 9.9450 | 2.4559 | 67.7400 | 4.1400 | 4.0649 | 41.6925 | 10.9795 | 2.0532 | NaN | 30.1700 | 49.9380 | 2.1628 | 1.4270 | 13.6136 |
2606 | 2008-12-30 | 2.0435 | 1.9558 | 3.3127 | 1.7331 | 1.4963 | 9.6364 | NaN | 26.6300 | 7.4506 | 15.6466 | 0.9742 | NaN | 10.9261 | 7.3348 | 266.3300 | 15,507.8000 | 5.3021 | 67.8530 | NaN | 127.4000 | 1,902.4300 | 3.4528 | 0.7079 | NaN | 19.3143 | 4.9040 | 9.8400 | 2.4448 | 67.0400 | 4.1465 | 4.0202 | 41.4035 | 10.9500 | 2.0325 | NaN | 30.1450 | 48.9980 | 2.1606 | 1.4098 | 13.3931 |
2607 | 2008-12-31 | 2.0274 | 1.9558 | 3.2436 | 1.6998 | 1.4850 | 9.4956 | NaN | 26.8750 | 7.4506 | 15.6466 | 0.9525 | NaN | 10.7858 | 7.3555 | 266.7000 | 15,239.1200 | 5.2919 | 67.4370 | NaN | 126.1400 | 1,839.1300 | 3.4528 | 0.7083 | NaN | 19.2333 | 4.8048 | 9.7500 | 2.4191 | 65.9300 | 4.1535 | 4.0225 | 41.2830 | 10.8700 | 2.0040 | NaN | 30.1260 | 48.2850 | 2.1488 | 1.3917 | 13.0667 |
An overlay of the Japanese yen and Australian dollar will show if the changes in 2011 are concurrent.
# line graph showing Euro to yen and aus dollar
fig, ax = plt.subplots(figsize=(9,5.5))
ax.plot(japanese_yen["date"], japanese_yen["rolling_mean_30"], color="green")
ax.set_ylabel('Japanese yen', color="green")
ax.set(yticks=[])
ax2 = ax.twinx()
ax2.plot(australian_dollar["date"], australian_dollar["rolling_mean_30"], color="blue")
ax2.set_ylabel('Australian dollar', color="blue")
ax2.set(yticks=[])
ax.set(xlabel='Year')
ax.xaxis.set_major_locator(years) # format the x ticks
ax.xaxis.set_major_formatter(years_fmt)
fmt_three_years = mdates.MonthLocator(interval = 36) # Major ticks every 3 years
ax.xaxis.set_major_locator(fmt_three_years)
fig.suptitle("Euro to Two Traded Currencies", fontsize=20)
plt.show()
days = new_frame
fig, ax = plt.subplots()
ax.plot(new_frame["date"],new_frame["chinese_yuan_rolling_30"])
plt.show()
# big_list = []
# for col in x_rates.columns:
# big_list.append(col)
# big_list
late_2000s = x_rates[x_rates["date"].dt.year.between(2009,2013)]
late_2000s.head()
date | australian_dollar | bulgarian_lev | brazilian_real | canadian_dollar | swiss_franc | chinese_yuan | cypriot_pound | czech_koruna | danish_krone | estonian_kroon | uk_pound | greek_drachma | hong_kong_dollar | croatian_kuna | hungarian_forint | indonesian_rupiah | israeli_shekel | indian_rupee | iceland_krona | japanese_yen | korean_won | lithuanian_litas | latvian_lats | maltese_lira | mexican_peso | malaysian_ringgit | norwegian_krone | new_zealand_dollar | philippine_peso | polish_zloty | romanian_leu | russian_rouble | swedish_krona | singapore_dollar | slovenian_tolar | slovak_koruna | thai_baht | turkish_lira | us_dollar | south_african_rand | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2608 | 2009-01-01 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
2609 | 2009-01-02 | 1.9884 | 1.9558 | 3.2456 | 1.6942 | 1.4874 | 9.4608 | NaN | 26.8250 | 7.4499 | 15.6466 | 0.9610 | NaN | 10.7472 | 7.3346 | 265.4800 | 15,391.2600 | 5.2522 | 67.1250 | NaN | 126.6400 | 1,833.0900 | 3.4528 | 0.7083 | NaN | 19.1718 | 4.8080 | 9.6950 | 2.3969 | 65.3100 | 4.1638 | 4.0350 | 40.7819 | 10.8425 | 2.0265 | NaN | NaN | 48.2120 | 2.1380 | 1.3866 | 13.0618 |
2610 | 2009-01-05 | 1.9189 | 1.9558 | 3.1619 | 1.6475 | 1.5060 | 9.2781 | NaN | 26.7600 | 7.4488 | 15.6466 | 0.9358 | NaN | 10.5303 | 7.3026 | 267.1300 | 15,076.0200 | 5.2355 | 65.8930 | NaN | 126.8100 | 1,799.5200 | 3.4528 | 0.7070 | NaN | 18.5292 | 4.7503 | 9.3950 | 2.3301 | 64.0800 | 4.1360 | 4.0490 | 39.5915 | 10.7075 | 1.9987 | NaN | NaN | 47.4620 | 2.0897 | 1.3582 | 12.8655 |
2611 | 2009-01-06 | 1.8727 | 1.9558 | 2.9517 | 1.5850 | 1.5018 | 9.1138 | NaN | 26.4100 | 7.4530 | 15.6466 | 0.9176 | NaN | 10.3369 | 7.2835 | 266.6700 | 14,665.2000 | 5.1477 | 64.8270 | NaN | 125.8400 | 1,753.5800 | 3.4528 | 0.7070 | NaN | 17.7949 | 4.6789 | 9.3850 | 2.2700 | 62.5700 | 4.0200 | 4.0675 | 38.8859 | 10.5675 | 1.9719 | NaN | NaN | 46.7550 | 2.0256 | 1.3332 | 12.4521 |
2612 | 2009-01-07 | 1.8831 | 1.9558 | 2.9977 | 1.6061 | 1.5006 | 9.2908 | NaN | 26.1170 | 7.4522 | 15.6466 | 0.9043 | NaN | 10.5398 | 7.3019 | 266.1400 | 14,750.5800 | 5.2685 | 66.3640 | NaN | 126.7700 | 1,780.1300 | 3.4528 | 0.7069 | NaN | 18.2717 | 4.7589 | 9.3915 | 2.2717 | 63.1500 | 3.9747 | 4.0995 | 39.9068 | 10.5763 | 1.9990 | NaN | NaN | 47.3510 | 2.0672 | 1.3595 | 12.7150 |
Determining volatility of the eight currencies for the past two decades illustrates part of the story of the twenty-first century.
# 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
# calling logarizer on all eight currencies
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"]) + "%")
volatility_dict
{'us_dollar': 9.66, 'japanese_yen': 11.74, 'uk_pound': 8.110000000000001, 'australian_dollar': 10.56, 'israeli_shekel': 9.83, 'philippine_peso': 10.57, 'malaysian_ringgit': 9.47, 'romanian_leu': 8.16}
# 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")