{"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"4.0.5"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Tidyverse + Highcharter with financial data\nThis notebook shows some pre-processing in R and EDA plots using highcharts. Hope it's useful!","metadata":{"_kg_hide-input":false,"_kg_hide-output":false}},{"cell_type":"markdown","source":"### Setup","metadata":{}},{"cell_type":"code","source":"options(warn=-1)\noptions(dplyr.summarise.inform = FALSE)\nsuppressMessages(library(tidyverse))\nsuppressMessages(library(highcharter))\nsuppressMessages(library(lubridate))\nsuppressMessages(library(xts))\nd_assets = read_csv(\"../input/g-research-crypto-forecasting//asset_details.csv\", show_col_types = FALSE)\nd_train = read_csv(\"../input/g-research-crypto-forecasting//train.csv\", show_col_types = FALSE)","metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","_kg_hide-input":true,"_kg_hide-output":false,"execution":{"iopub.status.busy":"2021-11-03T10:51:13.183208Z","iopub.execute_input":"2021-11-03T10:51:13.184875Z","iopub.status.idle":"2021-11-03T10:51:20.271984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Good EDA memory saver and to render in kaggle - chunk down to daily/weekly/monthly","metadata":{}},{"cell_type":"code","source":"v_window = \"month\"\n\nd_train_reduced = d_train %>%\n  mutate(\n    day = as.Date(as.POSIXct(timestamp, origin=\"1970-01-01\")),\n    week = floor_date(day, \"weeks\", week_start = 1),\n    month = floor_date(day, \"months\")\n  ) %>%\n  rename(date_window = !!v_window) %>%\n  group_by(date_window, Asset_ID) %>%\n  summarise(\n    VWAP = weighted.mean(VWAP, Volume),\n    High = max(High, na.rm = TRUE),\n    Low = min(Low, na.rm = TRUE),\n    Open = first(Open),\n    Close = last(Close),\n    Volume = sum(Volume, na.rm = TRUE),\n    Count = sum(Count, na.rm = TRUE)\n  ) %>%\n  ungroup() %>%\n  left_join(\n    d_assets,\n    by = \"Asset_ID\"\n  )\n\nd_train_reduced %>%\n    slice(1:10)","metadata":{"execution":{"iopub.status.busy":"2021-11-03T10:51:20.284305Z","iopub.execute_input":"2021-11-03T10:51:20.286247Z","iopub.status.idle":"2021-11-03T10:52:28.251200Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Summary by Asset","metadata":{}},{"cell_type":"code","source":"d_summaries = d_train_reduced %>% \n  filter(!is.na(VWAP) & is.finite(VWAP)) %>%\n  group_by(Asset_ID) %>%\n  summarise(\n    High = max(High),\n    VWAP = weighted.mean(VWAP, Volume),\n    Volume = sum(Volume)\n  ) %>%\n  ungroup() %>%\n  arrange(desc(High)) %>%\n  mutate(max_rank = row_number()) %>%\n  left_join(d_assets, by = \"Asset_ID\") %>%\n  select(Asset_ID, Asset_Name, everything())\n\nd_summaries","metadata":{"execution":{"iopub.status.busy":"2021-11-03T10:52:28.253503Z","iopub.execute_input":"2021-11-03T10:52:28.254855Z","iopub.status.idle":"2021-11-03T10:52:28.308968Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Price History by Assets","metadata":{}},{"cell_type":"code","source":"# plotting function to save repeating\nhc_stock_plot = function(id) {\n  d_plot = d_train_reduced %>%\n  filter(Asset_ID == id) %>%\n  mutate(datetime = datetime_to_timestamp(as.POSIXlt(date_window))) #for highcharts\n    \n  v_title = d_plot %>%\n    distinct(Asset_Name) %>%\n    pull(Asset_Name) %>%\n    paste(sep = \", \")\n  \n  hc = d_plot %>%\n    hchart(\n      type = \"line\",\n      hcaes(\n        x = datetime, \n        y = VWAP\n      ),\n        name = \"Price ($)\",\n      yAxis = 0\n    ) %>%\n    hc_yAxis_multiples(\n      list(\n        title = list(text = \"Price ($)\"), \n        top = \"0%\", \n        height = \"70%\"\n      ),\n      list(\n        title = list(text = \"Volume\"), \n        top = \"70%\", \n        height = \"30%\", \n      opposite = TRUE\n      )\n    ) %>%\n    hc_add_series(\n      d_plot, \n      type = \"column\", \n        name = \"Volume\",\n      yAxis = 1,\n      hcaes(\n        x = datetime, \n        y = Volume\n      )\n    ) %>%\n    hc_xAxis(\n     type = 'datetime',\n     labels = list(format = '{value: %Y-%m-%d}')\n    ) %>%\n    hc_tooltip(\n        sort = FALSE, \n        crosshairs = TRUE, \n        shared = TRUE,\n        valueDecimals = 2\n        #pointFormat= \"{point.x:.2f}, {point.y:.2f}\"\n    ) %>%\n    hc_scrollbar(enabled=TRUE, height = 10) %>%\n    hc_rangeSelector(enabled=TRUE) %>%\n    hc_navigator(enabled = TRUE) %>%\n    hc_title(text = v_title)\n  \n  hc\n}","metadata":{"execution":{"iopub.status.busy":"2021-11-03T10:52:28.312842Z","iopub.execute_input":"2021-11-03T10:52:28.314419Z","iopub.status.idle":"2021-11-03T10:52:28.329453Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hc_stock_plot_candle = function(id) {\n    d_plot = d_train_reduced %>%\n      filter(Asset_ID == id) %>%\n      mutate(datetime = datetime_to_timestamp(as.POSIXlt(date_window))) #for highcharts\n    \n    v_title = d_plot %>%\n        distinct(Asset_Name) %>%\n        pull(Asset_Name) %>%\n        paste(sep = \", \")\n\n    m_plot = d_plot %>%\n      select(High, Low, Open, Close, Volume) %>% \n      as.matrix()\n\n    v_dates = d_plot %>%\n      mutate(date = as.POSIXct(date_window)) %>%\n      pull(date)\n\n    x_plot = xts(m_plot, order.by = v_dates) \n\n    highchart(type = \"stock\") %>% \n      hc_yAxis_multiples(create_yaxis(2, height = c(2, 1), turnopposite = TRUE, min = 0)) %>% \n      # series :D\n      hc_add_series(x_plot, yAxis = 0, name = v_title) %>%\n      hc_add_series(x_plot$Volume, color = \"gray\", yAxis = 1, name = \"Volume\", type = \"column\") %>%\n      hc_tooltip(valueDecimals = 2) %>%\n      hc_plotOptions(\n        candlestick = list(\n            upColor = \"green\", \n            color = \"red\"\n        )\n    ) %>%\n    hc_title(text = v_title)\n}","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-11-03T11:07:11.955305Z","iopub.execute_input":"2021-11-03T11:07:11.956880Z","iopub.status.idle":"2021-11-03T11:07:11.969542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hc_stock_plot(0)","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2021-11-03T11:07:20.055517Z","iopub.execute_input":"2021-11-03T11:07:20.057086Z","iopub.status.idle":"2021-11-03T11:07:20.477211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hc_stock_plot_candle(1)","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2021-11-03T11:07:25.625797Z","iopub.execute_input":"2021-11-03T11:07:25.627341Z","iopub.status.idle":"2021-11-03T11:07:25.924655Z"},"trusted":true},"execution_count":null,"outputs":[]}]}