From 94f079e469507d29f3decc63e44b851ce7462b16 Mon Sep 17 00:00:00 2001 From: codez0mb1e Date: Tue, 29 Mar 2022 09:12:29 +0000 Subject: [PATCH] Currencies Analysis (beta) --- src/fx_currencies_anlysis.Rmd | 58 +- src/fx_currencies_anlysis.md | 1999 +++++++++++++++++++++++++++++++++ 2 files changed, 2035 insertions(+), 22 deletions(-) create mode 100644 src/fx_currencies_anlysis.md diff --git a/src/fx_currencies_anlysis.Rmd b/src/fx_currencies_anlysis.Rmd index 4779f3e..b9141d2 100755 --- a/src/fx_currencies_anlysis.Rmd +++ b/src/fx_currencies_anlysis.Rmd @@ -9,43 +9,54 @@ knitr::opts_chunk$set(echo = T, warning = F) ***Analysis price of the my list of ~~the most promised cryptotokens~~ currencies.*** -## Set envinroment :earth_asia: +## Prepare + +Install packages and set environment :earth_asia: `install.packages("azuremlsdk")` ```{r set_envinroment, message=FALSE} -library(dplyr) -library(tidyr) +suppressPackageStartupMessages({ + library(dplyr) + library(tidyr) + + library(lubridate) + library(stringr) + + library(gt) + library(tidyverse) + library(glue) + + library(ggplot2) + + library(azuremlsdk) +}) +``` -library(lubridate) -library(stringr) -library(gt) -library(tidyverse) -library(glue) - -library(ggplot2) - -library(azuremlsdk) - -## set params +```{r set_params} .azureml_dataset_name <- "Currencies" +``` -## connect to Azure ML workspace + +Connect to Azure ML workspace: + +```{r azureml_connect} ws <- azuremlsdk::load_workspace_from_config() sprintf( - "%s workspace located in %s region",ws$name, ws$location + "%s workspace located in %s region", ws$name, ws$location ) - ``` -## Load dataset +## Load dataset + ```{r get_azure_dataset} currencies_ds <- azuremlsdk::get_dataset_by_name(ws, name = .azureml_dataset_name) currencies_ds$name currencies_ds$description ``` +Get USD/RUB top higher rates: ```{r prepare_dataframe} quotes_df <- currencies_ds$to_pandas_dataframe() @@ -71,6 +82,10 @@ quotes_df %>% ) ``` +## Preprocessing data + +Calculate `Return` and `Log Return` for last 10 years: + ```{r preprocessing} quotes_df %<>% transmute( @@ -125,7 +140,7 @@ quotes_stats %>% gt() %>% tab_header( - title = "Least Volatility Currencies", + title = "The Least Volatile Currencies", subtitle = glue("{min(quotes_df$date)} to {max(quotes_df$date)}") ) %>% fmt_number( @@ -138,7 +153,7 @@ My broker available pairs: symbols <- c( 'RUB', 'EUR', 'GBP', 'CHF', 'CNY', 'HKD', 'JPY', 'SEK', 'SGD', 'AUD', - 'KZT', 'BYN', 'TRY', 'MXN' + 'AED', 'KZT', 'BYN', 'TRY', 'MXN' ) quotes_stats %>% @@ -147,14 +162,13 @@ quotes_stats %>% ) %>% mutate( `100x Volatility` = volatility*100 - #risk = if_else(volatility > volatility_threshold, "High", "Medium or low", NA_character_) ) %>% arrange(volatility) %>% select(-volatility) %>% gt() %>% tab_header( - title = "Least Volatility Currencies", + title = "The Most Promised Currencies", subtitle = glue("{min(quotes_df$date)} to {max(quotes_df$date)}") ) %>% fmt_number( diff --git a/src/fx_currencies_anlysis.md b/src/fx_currencies_anlysis.md new file mode 100644 index 0000000..8d8e24e --- /dev/null +++ b/src/fx_currencies_anlysis.md @@ -0,0 +1,1999 @@ +Currencies Analysis +================ + +***Analysis price of the my list of ~~the most promised cryptotokens~~ +currencies.*** + +## Prepare + +Install packages and set environment :earth\_asia: + +`install.packages("azuremlsdk")` + +``` r +suppressPackageStartupMessages({ + library(dplyr) + library(tidyr) + + library(lubridate) + library(stringr) + + library(gt) + library(tidyverse) + library(glue) + + library(ggplot2) + + library(azuremlsdk) +}) +``` + +``` r +.azureml_dataset_name <- "Currencies" +``` + +Connect to Azure ML workspace: + +``` r +ws <- azuremlsdk::load_workspace_from_config() +sprintf( + "%s workspace located in %s region", ws$name, ws$location +) +``` + + ## [1] "portf-opt-ws workspace located in westeurope region" + +## Load dataset + +``` r +currencies_ds <- azuremlsdk::get_dataset_by_name(ws, name = .azureml_dataset_name) +currencies_ds$name +``` + + ## [1] "Currencies" + +``` r +currencies_ds$description +``` + + ## [1] "Source: https://www.kaggle.com/datasets/dhruvildave/currency-exchange-rates" + +Get USD/RUB top higher rates: + +``` r +quotes_df <- currencies_ds$to_pandas_dataframe() + +# ~ 20 years, 150 currencies and 1.5M rows + +quotes_df %>% + filter(slug == "USD/RUB") %>% + select(-slug) %>% + top_n(10) %>% + + gt() %>% + tab_header( + title = "USD/RUB Rate", + subtitle = glue("{min(quotes_df$date)} to {max(quotes_df$date)}") + ) %>% + fmt_date( + columns = date, + date_style = 6 + ) %>% + fmt_number( + columns = c(open, high, low, close) + ) +``` + + ## Selecting by close + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
USD/RUB Rate
1996-10-30 to 2021-08-30
dateopenhighlowclose
Jan 21, 201682.0685.8282.0681.82
Jan 22, 201680.6181.2677.9482.90
Jan 26, 201681.5482.1678.3379.84
Feb 3, 201679.5679.7577.8779.71
Feb 10, 201679.3979.4977.6579.59
Feb 12, 201679.3679.7478.5979.77
Mar 19, 202080.9282.0779.2480.92
Mar 23, 202079.7281.3479.4979.84
Mar 31, 202079.5979.6977.6679.59
Nov 3, 202080.5580.5779.0580.52
+
+ +## Preprocessing data + +Calculate `Return` and `Log Return` for last 10 years: + +``` r +quotes_df %<>% + transmute( + symbol = slug, + price = close, + date + ) %>% + + filter( + str_detect(symbol, "USD/") & + date > max(date) - lubridate::years(10) + ) %>% + + filter(!(symbol == "USD/RUB" & price < 1)) %>% + + arrange(date) %>% + group_by(symbol) %>% + + mutate( + return = c(NA_real_, diff(price))/lag(price), + log_return = log(1 + return) + ) %>% + na.omit +``` + +Base and quote currencies: + +``` r +quotes_stats <- quotes_df %>% + + summarise( + max_price = max(price), + min_price = min(price), + last_price = last(price), + max_min_rate = max(price)/min(price), + volatility = sd(log_return) + ) + +volatility_threshold <- quotes_stats %>% pull(volatility) %>% quantile(probs = .75) + + +quotes_stats %>% + filter( + volatility <= volatility_threshold | + symbol == "USD/RUB" + ) %>% + mutate( + `100x Volatility` = volatility*100 + ) %>% + arrange(volatility) %>% + select(-volatility) %>% + + gt() %>% + tab_header( + title = "The Least Volatile Currencies", + subtitle = glue("{min(quotes_df$date)} to {max(quotes_df$date)}") + ) %>% + fmt_number( + columns = c(max_price, min_price, max_min_rate, `100x Volatility`) + ) +``` + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
The Least Volatile Currencies
2011-09-01 to 2021-08-30
symbolmax_pricemin_pricelast_pricemax_min_rate100x Volatility
USD/AED3.673.673.672801.000.01
USD/HKD7.857.757.786941.010.03
USD/KWD0.310.270.300601.160.16
USD/CNY7.186.036.465801.190.23
USD/DJF177.72172.00177.500001.030.28
USD/SGD1.461.201.344481.210.33
USD/SAR3.773.303.750501.140.39
USD/GTQ7.897.047.727501.120.41
USD/ILS4.073.133.204901.300.45
USD/TTD6.785.936.763201.140.47
USD/CAD1.460.971.260501.510.47
USD/MYR4.492.964.155001.520.50
USD/DKK7.155.186.299861.380.51
USD/EUR0.960.700.847001.380.51
USD/CRC619.70478.54619.700011.290.53
USD/PHP54.2339.7549.705001.360.54
USD/INR77.5745.7073.292001.700.54
USD/RON4.542.934.179201.550.55
USD/JPY125.6375.74109.902001.660.55
USD/GBP0.870.580.726611.490.55
USD/JMD153.8883.37150.530001.850.56
USD/MKD58.9242.0751.980001.400.58
USD/MDL20.3111.0917.580001.830.61
USD/BDT84.7272.3984.720001.170.62
USD/AUD1.740.931.369951.880.63
USD/SEK10.446.298.618401.660.64
USD/CHF1.030.790.916911.310.64
USD/CZK26.0316.7521.671201.550.64
USD/BWP12.196.5811.118501.850.66
USD/NZD1.781.131.427101.570.66
USD/THB36.4328.0732.447001.300.67
USD/LKR199.43106.22199.429991.880.67
USD/KRW1,262.93999.831165.890011.260.70
USD/RSD118.4770.0599.288201.690.70
USD/UYU45.3118.0842.530002.510.71
USD/PLN4.282.873.861401.490.72
USD/HUF338.26188.61294.660001.790.74
USD/MUR42.5526.5042.550001.610.79
USD/MXN25.3411.9820.135002.110.80
USD/NIO35.1322.0535.000001.590.84
USD/KZT454.34174.15427.179992.610.84
USD/QAR3.903.003.640001.300.95
USD/TRY8.781.718.376905.120.97
USD/ZAR19.256.9814.661102.760.99
USD/RUB82.9028.7973.504002.881.05
USD/ZMW22.645.1115.820004.431.06
USD/BRL5.891.585.193803.721.08
USD/ARS97.704.1097.7000023.851.11
USD/TND3.061.372.790002.231.17
USD/BGN1.871.211.657341.551.28
USD/EGP19.605.8315.650003.371.29
USD/NOK11.765.368.664352.191.31
USD/PEN4.112.384.069401.721.34
USD/BYN3.080.512.510006.041.37
USD/MAD10.297.898.953001.301.43
USD/UAH33.507.8026.920004.301.83
USD/SDG451.001.39440.03491324.465.96
USD/BND1.430.661.344702.186.29
USD/XOF647.0058.00555.4699711.166.44
USD/IDR16,504.80892.0014370.0000018.506.58
USD/HNL24.903.0023.825608.308.14
+
+ +My broker available pairs: + +``` r +symbols <- c( + 'RUB', + 'EUR', 'GBP', 'CHF', 'CNY', 'HKD', 'JPY', 'SEK', 'SGD', 'AUD', + 'AED', 'KZT', 'BYN', 'TRY', 'MXN' +) + +quotes_stats %>% + filter( + symbol %in% str_c("USD", symbols, sep = "/") + ) %>% + mutate( + `100x Volatility` = volatility*100 + ) %>% + arrange(volatility) %>% + select(-volatility) %>% + + gt() %>% + tab_header( + title = "The Most Promised Currencies", + subtitle = glue("{min(quotes_df$date)} to {max(quotes_df$date)}") + ) %>% + fmt_number( + columns = c(max_price, min_price, max_min_rate, `100x Volatility`) + ) +``` + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
The Most Promised Currencies
2011-09-01 to 2021-08-30
symbolmax_pricemin_pricelast_pricemax_min_rate100x Volatility
USD/AED3.673.673.672801.000.01
USD/HKD7.857.757.786941.010.03
USD/CNY7.186.036.465801.190.23
USD/SGD1.461.201.344481.210.33
USD/EUR0.960.700.847001.380.51
USD/JPY125.6375.74109.902001.660.55
USD/GBP0.870.580.726611.490.55
USD/AUD1.740.931.369951.880.63
USD/SEK10.446.298.618401.660.64
USD/CHF1.030.790.916911.310.64
USD/MXN25.3411.9820.135002.110.80
USD/KZT454.34174.15427.179992.610.84
USD/TRY8.781.718.376905.120.97
USD/RUB82.9028.7973.504002.881.05
USD/BYN3.080.512.510006.041.37
+
+ +Plot exchange rate for out favorites: + +Define low risk symbols: + +``` r +low_risk_symbols <- quotes_stats %>% + filter( + symbol %in% str_c("USD", symbols, sep = "/") + ) %>% + filter( + volatility <= volatility_threshold | + symbol == "USD/RUB" + ) %>% + pull(symbol) %>% + unique + +cat( + sprintf( + "['%s']", + paste(low_risk_symbols, collapse = "', '") +)) +``` + + ## ['USD/AED', 'USD/AUD', 'USD/BYN', 'USD/CHF', 'USD/CNY', 'USD/EUR', 'USD/GBP', 'USD/HKD', 'USD/JPY', 'USD/KZT', 'USD/MXN', 'USD/RUB', 'USD/SEK', 'USD/SGD', 'USD/TRY']