{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "867bfa18",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.core.display import display, HTML\n",
"display(HTML(\"\"))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "419ed573",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/bz/5cj1tzbj5xn319rpqrw6gpmh0000gn/T/ipykernel_9819/1974911599.py:17: 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.\n",
" x_rates.columns = x_rates.columns.str.replace(\"[\",\"\").str.replace(\"]\",\"\")\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" date | \n",
" australian_dollar | \n",
" bulgarian_lev | \n",
" brazilian_real | \n",
" canadian_dollar | \n",
" swiss_franc | \n",
" chinese_yuan | \n",
" cypriot_pound | \n",
" czech_koruna | \n",
" danish_krone | \n",
" estonian_kroon | \n",
" uk_pound | \n",
" greek_drachma | \n",
" hong_kong_dollar | \n",
" croatian_kuna | \n",
" hungarian_forint | \n",
" indonesian_rupiah | \n",
" israeli_shekel | \n",
" indian_rupee | \n",
" iceland_krona | \n",
" japanese_yen | \n",
" korean_won | \n",
" lithuanian_litas | \n",
" latvian_lats | \n",
" maltese_lira | \n",
" mexican_peso | \n",
" malaysian_ringgit | \n",
" norwegian_krone | \n",
" new_zealand_dollar | \n",
" philippine_peso | \n",
" polish_zloty | \n",
" romanian_leu | \n",
" russian_rouble | \n",
" swedish_krona | \n",
" singapore_dollar | \n",
" slovenian_tolar | \n",
" slovak_koruna | \n",
" thai_baht | \n",
" turkish_lira | \n",
" us_dollar | \n",
" south_african_rand | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1999-01-04 | \n",
" 1.9100 | \n",
" NaN | \n",
" NaN | \n",
" 1.8004 | \n",
" 1.6168 | \n",
" NaN | \n",
" 0.5823 | \n",
" 35.1070 | \n",
" 7.4501 | \n",
" 15.6466 | \n",
" 0.7111 | \n",
" 327.1500 | \n",
" 9.1332 | \n",
" NaN | \n",
" 251.4800 | \n",
" 9,433.6100 | \n",
" NaN | \n",
" NaN | \n",
" 81.4800 | \n",
" 133.7300 | \n",
" 1,398.5900 | \n",
" 4.7170 | \n",
" 0.6668 | \n",
" 0.4432 | \n",
" 11.6446 | \n",
" 4.4798 | \n",
" 8.8550 | \n",
" 2.2229 | \n",
" 45.5100 | \n",
" 4.0712 | \n",
" 1.3111 | \n",
" 25.2875 | \n",
" 9.4696 | \n",
" 1.9554 | \n",
" 189.0450 | \n",
" 42.9910 | \n",
" 42.6799 | \n",
" 0.3723 | \n",
" 1.1789 | \n",
" 6.9358 | \n",
"
\n",
" \n",
" 1 | \n",
" 1999-01-05 | \n",
" 1.8944 | \n",
" NaN | \n",
" NaN | \n",
" 1.7965 | \n",
" 1.6123 | \n",
" NaN | \n",
" 0.5823 | \n",
" 34.9170 | \n",
" 7.4495 | \n",
" 15.6466 | \n",
" 0.7122 | \n",
" 324.7000 | \n",
" 9.1341 | \n",
" NaN | \n",
" 250.8000 | \n",
" 9,314.5100 | \n",
" NaN | \n",
" NaN | \n",
" 81.5300 | \n",
" 130.9600 | \n",
" 1,373.0100 | \n",
" 4.7174 | \n",
" 0.6657 | \n",
" 0.4432 | \n",
" 11.5960 | \n",
" 4.4805 | \n",
" 8.7745 | \n",
" 2.2011 | \n",
" 44.7450 | \n",
" 4.0245 | \n",
" 1.3168 | \n",
" 26.5876 | \n",
" 9.4025 | \n",
" 1.9655 | \n",
" 188.7750 | \n",
" 42.8480 | \n",
" 42.5048 | \n",
" 0.3728 | \n",
" 1.1790 | \n",
" 6.7975 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 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 \n",
"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\n",
"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"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" date | \n",
" australian_dollar | \n",
" bulgarian_lev | \n",
" brazilian_real | \n",
" canadian_dollar | \n",
" swiss_franc | \n",
" chinese_yuan | \n",
" cypriot_pound | \n",
" czech_koruna | \n",
" danish_krone | \n",
" estonian_kroon | \n",
" uk_pound | \n",
" greek_drachma | \n",
" hong_kong_dollar | \n",
" croatian_kuna | \n",
" hungarian_forint | \n",
" indonesian_rupiah | \n",
" israeli_shekel | \n",
" indian_rupee | \n",
" iceland_krona | \n",
" japanese_yen | \n",
" korean_won | \n",
" lithuanian_litas | \n",
" latvian_lats | \n",
" maltese_lira | \n",
" mexican_peso | \n",
" malaysian_ringgit | \n",
" norwegian_krone | \n",
" new_zealand_dollar | \n",
" philippine_peso | \n",
" polish_zloty | \n",
" romanian_leu | \n",
" russian_rouble | \n",
" swedish_krona | \n",
" singapore_dollar | \n",
" slovenian_tolar | \n",
" slovak_koruna | \n",
" thai_baht | \n",
" turkish_lira | \n",
" us_dollar | \n",
" south_african_rand | \n",
"
\n",
" \n",
" \n",
" \n",
" 5697 | \n",
" 2021-01-07 | \n",
" 1.5836 | \n",
" 1.9558 | \n",
" 6.5172 | \n",
" 1.5601 | \n",
" 1.0833 | \n",
" 7.9392 | \n",
" NaN | \n",
" 26.1470 | \n",
" 7.4392 | \n",
" NaN | \n",
" 0.9019 | \n",
" NaN | \n",
" 9.5176 | \n",
" 7.5660 | \n",
" 357.7900 | \n",
" 17,259.9900 | \n",
" 3.9027 | \n",
" 90.0455 | \n",
" 155.3000 | \n",
" 127.1300 | \n",
" 1,342.2900 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" 24.2552 | \n",
" 4.9570 | \n",
" 10.3435 | \n",
" 1.6907 | \n",
" 59.0430 | \n",
" 4.4998 | \n",
" 4.8712 | \n",
" 91.2000 | \n",
" 10.0575 | \n",
" 1.6253 | \n",
" NaN | \n",
" NaN | \n",
" 36.8590 | \n",
" 8.9987 | \n",
" 1.2276 | \n",
" 18.7919 | \n",
"
\n",
" \n",
" 5698 | \n",
" 2021-01-08 | \n",
" 1.5758 | \n",
" 1.9558 | \n",
" 6.5748 | \n",
" 1.5543 | \n",
" 1.0827 | \n",
" 7.9184 | \n",
" NaN | \n",
" 26.1630 | \n",
" 7.4369 | \n",
" NaN | \n",
" 0.9013 | \n",
" NaN | \n",
" 9.4982 | \n",
" 7.5690 | \n",
" 359.6200 | \n",
" 17,247.3300 | \n",
" 3.8981 | \n",
" 89.7975 | \n",
" 155.5000 | \n",
" 127.2600 | \n",
" 1,337.9000 | \n",
" NaN | \n",
" NaN | \n",
" NaN | \n",
" 24.4718 | \n",
" 4.9359 | \n",
" 10.2863 | \n",
" 1.6883 | \n",
" 58.9470 | \n",
" 4.5113 | \n",
" 4.8708 | \n",
" 90.8000 | \n",
" 10.0510 | \n",
" 1.6228 | \n",
" NaN | \n",
" NaN | \n",
" 36.8480 | \n",
" 9.0146 | \n",
" 1.2250 | \n",
" 18.7212 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 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 \n",
"5697 2021-01-07 1.5836 1.9558 6.5172 1.5601 1.0833 7.9392 NaN 26.1470 7.4392 NaN 0.9019 NaN 9.5176 7.5660 357.7900 17,259.9900 3.9027 90.0455 155.3000 127.1300 1,342.2900 NaN NaN NaN 24.2552 4.9570 10.3435 1.6907 59.0430 4.4998 4.8712 91.2000 10.0575 1.6253 NaN NaN 36.8590 8.9987 1.2276 18.7919\n",
"5698 2021-01-08 1.5758 1.9558 6.5748 1.5543 1.0827 7.9184 NaN 26.1630 7.4369 NaN 0.9013 NaN 9.4982 7.5690 359.6200 17,247.3300 3.8981 89.7975 155.5000 127.2600 1,337.9000 NaN NaN NaN 24.4718 4.9359 10.2863 1.6883 58.9470 4.5113 4.8708 90.8000 10.0510 1.6228 NaN NaN 36.8480 9.0146 1.2250 18.7212"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import pandas as pd\n",
"import pprint\n",
"\n",
"pd.options.display.float_format = '{:20,.4f}'.format\n",
"pd.set_option('display.max_rows', None)\n",
"pd.set_option('display.max_columns', None)\n",
"pd.set_option('display.width', 1000)\n",
"pd.set_option('display.colheader_justify', 'center')\n",
"pd.set_option('display.precision', 3)\n",
"\n",
"# open file\n",
"x_rates = pd.read_csv(\"euro-daily-hist_1999_2020.csv\")\n",
"\n",
"# strip brackets and trailing space from country names\n",
"# replace remaining space with underscore\n",
"# lower case all column names\n",
"x_rates.columns = x_rates.columns.str.replace(\"[\",\"\").str.replace(\"]\",\"\")\n",
"x_rates.columns = x_rates.columns.str.rstrip()\n",
"x_rates.columns = x_rates.columns.str.replace(\" \",\"_\")\n",
"x_rates.columns = x_rates.columns.str.lower()\n",
"\n",
"# rename columns\n",
"x_rates.rename(columns={\"period\\\\unit:\":\"date\",\n",
" \"chinese_yuan_renminbi\":\"chinese_yuan\",\n",
" \"uk_pound_sterling\":\"uk_pound\"}, inplace=True)\n",
"\n",
"# convert datetime\n",
"x_rates[\"date\"] = pd.to_datetime(x_rates[\"date\"])\n",
"\n",
"# resort and reindex\n",
"x_rates.sort_values(\"date\", inplace=True)\n",
"x_rates.reset_index(drop=True, inplace=True)\n",
"\n",
"# convert hyphens in currency columns to NaN\n",
"import numpy as np\n",
"x_rates = x_rates.replace(\"-\", np.nan)\n",
"\n",
"# convert exchange rate values to float\n",
"x_rates.iloc[:,1:] = x_rates.iloc[:,1:].astype(float)\n",
"\n",
"display(x_rates.head(2))\n",
"display(x_rates.tail(2))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e57164fa",
"metadata": {},
"outputs": [],
"source": [
"# miniaturize x_rates\n",
"x_rates = x_rates.iloc[:,:6]\n",
"\n",
"# array with names of currencies\n",
"currency_names = x_rates.columns.values\n",
"currency_names = currency_names[1:]\n",
"\n",
"# create a list & dictionary of data frames for each currency with log rate of the exchange rate, 30 day rolling mean, and year\n",
"col_names = [] # list with names of currencies\n",
"df_list = [] # list of 40 dataframes\n",
"df_dict = {} # dictionary with 40 currency:currency_dataframe pairs\n",
"for currency in x_rates.columns[1:]:\n",
" df_name = currency\n",
" col_names.append(df_name)\n",
" df = x_rates[[\"date\", currency]].copy()\n",
" df = df[df[currency].notna()]\n",
" 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\n",
" df[\"rolling_mean_30\"] = df[currency].rolling(30).mean()\n",
" df[\"year\"] = df[\"date\"].dt.year\n",
" df_dict[currency] = df\n",
" df_list.append(df)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "27713ddc",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" bulgarian_lev_annual_vol | \n",
"
\n",
" \n",
" \n",
" \n",
" 2000 | \n",
" 0.0098 | \n",
"
\n",
" \n",
" 2001 | \n",
" 0.0058 | \n",
"
\n",
" \n",
" 2002 | \n",
" 0.0075 | \n",
"
\n",
" \n",
" 2003 | \n",
" 0.0047 | \n",
"
\n",
" \n",
" 2004 | \n",
" 0.0051 | \n",
"
\n",
" \n",
" 2005 | \n",
" 0.0097 | \n",
"
\n",
" \n",
" 2006 | \n",
" 0.0004 | \n",
"
\n",
" \n",
" 2007 | \n",
" 0.0001 | \n",
"
\n",
" \n",
" 2008 | \n",
" 0.0000 | \n",
"
\n",
" \n",
" 2009 | \n",
" 0.0000 | \n",
"
\n",
" \n",
" 2010 | \n",
" 0.0001 | \n",
"
\n",
" \n",
" 2011 | \n",
" 0.0000 | \n",
"
\n",
" \n",
" 2012 | \n",
" 0.0000 | \n",
"
\n",
" \n",
" 2013 | \n",
" 0.0001 | \n",
"
\n",
" \n",
" 2014 | \n",
" 0.0000 | \n",
"
\n",
" \n",
" 2015 | \n",
" 0.0001 | \n",
"
\n",
" \n",
" 2016 | \n",
" 0.0000 | \n",
"
\n",
" \n",
" 2017 | \n",
" 0.0000 | \n",
"
\n",
" \n",
" 2018 | \n",
" 0.0000 | \n",
"
\n",
" \n",
" 2019 | \n",
" 0.0000 | \n",
"
\n",
" \n",
" 2020 | \n",
" 0.0000 | \n",
"
\n",
" \n",
" 2021 | \n",
" 0.0000 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" bulgarian_lev_annual_vol\n",
"2000 0.0098 \n",
"2001 0.0058 \n",
"2002 0.0075 \n",
"2003 0.0047 \n",
"2004 0.0051 \n",
"2005 0.0097 \n",
"2006 0.0004 \n",
"2007 0.0001 \n",
"2008 0.0000 \n",
"2009 0.0000 \n",
"2010 0.0001 \n",
"2011 0.0000 \n",
"2012 0.0000 \n",
"2013 0.0001 \n",
"2014 0.0000 \n",
"2015 0.0001 \n",
"2016 0.0000 \n",
"2017 0.0000 \n",
"2018 0.0000 \n",
"2019 0.0000 \n",
"2020 0.0000 \n",
"2021 0.0000 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# function to create df with year and annual volatility for every row # i think i could use aggregation here but don't know how\n",
"def volatizer(currency):\n",
" annual_df_list = [currency[currency['year'] == y] for y in currency['year'].unique()] # list of annual dfs\n",
" c_name = currency.columns[1]\n",
" row_dict = {}\n",
" for frame in annual_df_list:\n",
" year_name = frame.iat[0,4] # first cell of the \"year\" column, becomes the \"year\" key for row_dict\n",
" annual_volatility = frame[\"log_rate\"].std()*253**.5 # volatility measured by standard deviation * 253 trading days per year raised to the 0.5 power\n",
" row_dict[year_name] = annual_volatility\n",
" df = pd.DataFrame.from_dict(row_dict, orient=\"index\", columns=[c_name+\"_annual_vol\"]) # indexing on year, not sure if this is cool\n",
" return df\n",
"\n",
"# apply volatizer to each currency df\n",
"for key in df_dict:\n",
" df_dict[key] = volatizer(df_dict[key])\n",
"\n",
"display(df_dict[\"bulgarian_lev\"])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}