Ad currencies analysis

This commit is contained in:
codez0mb1e 2022-03-29 08:42:43 +00:00
parent 27f5d141db
commit dfd136c380

193
src/fx_currencies_anlysis.Rmd Executable file
View File

@ -0,0 +1,193 @@
---
title: "Currencies Analysis"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = T, warning = F)
```
***Analysis price of the my list of ~~the most promised cryptotokens~~ currencies.***
## Set envinroment :earth_asia:
`install.packages("azuremlsdk")`
```{r set_envinroment, message=FALSE}
library(dplyr)
library(tidyr)
library(lubridate)
library(stringr)
library(gt)
library(tidyverse)
library(glue)
library(ggplot2)
library(azuremlsdk)
## set params
.azureml_dataset_name <- "Currencies"
## connect to Azure ML workspace
ws <- azuremlsdk::load_workspace_from_config()
sprintf(
"%s workspace located in %s region",ws$name, ws$location
)
```
## Load dataset
```{r get_azure_dataset}
currencies_ds <- azuremlsdk::get_dataset_by_name(ws, name = .azureml_dataset_name)
currencies_ds$name
currencies_ds$description
```
```{r prepare_dataframe}
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)
)
```
```{r preprocessing}
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 discover}
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 = "Least Volatility 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`)
)
```
My broker available pairs:
```{r}
symbols <- c(
'RUB',
'EUR', 'GBP', 'CHF', 'CNY', 'HKD', 'JPY', 'SEK', 'SGD', 'AUD',
'KZT', 'BYN', 'TRY', 'MXN'
)
quotes_stats %>%
filter(
symbol %in% str_c("USD", symbols, sep = "/")
) %>%
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",
subtitle = glue("{min(quotes_df$date)} to {max(quotes_df$date)}")
) %>%
fmt_number(
columns = c(max_price, min_price, max_min_rate, `100x Volatility`)
)
```
Plot exchange rate for out favorites:
```{r}
```
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 = "', '")
))
```