---
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.***

## Prepare

Install packages and set environment :earth_asia:

`install.packages("azuremlsdk")`

```{r set_envinroment, message=FALSE}
suppressPackageStartupMessages({
  library(dplyr)
  library(tidyr)
  
  library(lubridate)
  library(stringr)
  
  library(gt)
  library(tidyverse)
  library(glue)
  
  library(ggplot2)
  
  library(azuremlsdk)
})
```


```{r set_params}
.azureml_dataset_name <- "Currencies"
```


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
)
```


## 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()

# ~ 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)
  )
```

## Preprocessing data

Calculate `Return` and `Log Return` for last 10 years: 

```{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 = "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`)
  )
```
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`)
  )

```
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 = "', '")
))
```



