{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "31d0d7e3",
   "metadata": {},
   "source": [
    "# Running this noteboook\n",
    "\n",
    "Create a new Python Venv using:\n",
    "\n",
    "```bash\n",
    "python -m venv .venv\n",
    "# source .venv/bin/activate\n",
    "./.venv/Scripts/activate\n",
    "```\n",
    "\n",
    "Install `evcxr_jupyter` and `jupyterlab` using:\n",
    "\n",
    "```bash\n",
    "cargo install evcxr_jupyter\n",
    "evcxr_jupyter --install\n",
    "pip install jupyterlab\n",
    "jupyter lab\n",
    "```\n",
    "\n",
    "Or try following this guide here: [DataCrayon - Setup Jupyter with Rust](https://datacrayon.com/data-analysis-with-rust-notebooks/setup-anaconda-jupyter-and-rust/)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d04a212-4025-41d7-809e-864649b08ab5",
   "metadata": {},
   "source": [
    "## Setup kernel\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9edf8272-762c-48c6-a003-2b4f484471b1",
   "metadata": {},
   "outputs": [],
   "source": [
    ":dep msyrs = { path = \"../../msyrs\" }\n",
    ":dep polars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22e1ae9e-14b8-4be4-b852-8f0fb420eaca",
   "metadata": {},
   "outputs": [],
   "source": [
    "use msyrs::download::{JPMaQSDownload, JPMaQSDownloadGetIndicatorArgs};\n",
    "use msyrs::utils as msyrs_utils;\n",
    "use msyrs::utils::qdf as msyrs_qdf;\n",
    "use polars::frame::DataFrame;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c8ac34a-d0e5-458d-81a3-f7825ad50612",
   "metadata": {},
   "source": [
    "## Download data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57a15e6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "let cids_dm = vec![\n",
    "    \"AUD\", \"CAD\", \"CHF\", \"EUR\", \"GBP\", \"JPY\", \"NOK\", \"NZD\", \"SEK\", \"USD\",\n",
    "];\n",
    "let cids_em = vec![\n",
    "    \"CLP\", \"COP\", \"CZK\", \"HUF\", \"IDR\", \"ILS\", \"INR\", \"KRW\", \"MXN\", \"PLN\", \"THB\", \"TRY\", \"TWD\",\n",
    "    \"ZAR\",\n",
    "];\n",
    "\n",
    "// cids = cids_dm + cids_em\n",
    "let cids: Vec<String> = cids_dm\n",
    "    .iter()\n",
    "    .chain(cids_em.iter())\n",
    "    .map(|s| s.to_string())\n",
    "    .collect();\n",
    "\n",
    "let non_dux = [\"IDR\", \"NZD\"];\n",
    "let cids_dux: Vec<String> = cids\n",
    "    .iter()\n",
    "    .filter(|s| !non_dux.contains(&s.as_str()))\n",
    "    .map(|s| s.to_string())\n",
    "    .collect();\n",
    "let ecos = vec![\n",
    "    \"CPIC_SA_P1M1ML12\",\n",
    "    \"CPIC_SJA_P3M3ML3AR\",\n",
    "    \"CPIC_SJA_P6M6ML6AR\",\n",
    "    \"CPIH_SA_P1M1ML12\",\n",
    "    \"CPIH_SJA_P3M3ML3AR\",\n",
    "    \"CPIH_SJA_P6M6ML6AR\",\n",
    "    \"INFTEFF_NSA\",\n",
    "    \"INTRGDP_NSA_P1M1ML12_3MMA\",\n",
    "    \"INTRGDPv5Y_NSA_P1M1ML12_3MMA\",\n",
    "    \"PCREDITGDP_SJA_D1M1ML12\",\n",
    "    \"RGDP_SA_P1Q1QL4_20QMA\",\n",
    "    \"RYLDIRS02Y_NSA\",\n",
    "    \"RYLDIRS05Y_NSA\",\n",
    "    \"PCREDITBN_SJA_P1M1ML12\",\n",
    "];\n",
    "let mkts = vec![\n",
    "    \"DU02YXR_NSA\",\n",
    "    \"DU05YXR_NSA\",\n",
    "    \"DU02YXR_VT10\",\n",
    "    \"DU05YXR_VT10\",\n",
    "    \"EQXR_NSA\",\n",
    "    \"EQXR_VT10\",\n",
    "    \"FXXR_NSA\",\n",
    "    \"FXXR_VT10\",\n",
    "    \"FXCRR_NSA\",\n",
    "    \"FXTARGETED_NSA\",\n",
    "    \"FXUNTRADABLE_NSA\",\n",
    "];\n",
    "\n",
    "let xcats: Vec<String> = ecos\n",
    "    .iter()\n",
    "    .chain(mkts.iter())\n",
    "    .map(|s| s.to_string())\n",
    "    .collect();\n",
    "\n",
    "let cids_str: Vec<&str> = cids.iter().map(AsRef::as_ref).collect();\n",
    "let xcats_str: Vec<&str> = xcats.iter().map(AsRef::as_ref).collect();\n",
    "let download_tickers = msyrs_utils::misc::create_interesecting_tickers(&cids_str, &xcats_str);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ba562d3-2f32-4da3-891e-451288077530",
   "metadata": {},
   "source": [
    "### Import download functions\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "137d6b3e-498d-48b1-8e3c-2f9fedfb64e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "use msyrs::download::{JPMaQSDownload, JPMaQSDownloadGetIndicatorArgs};\n",
    "\n",
    "use msyrs::utils as msyrs_utils;\n",
    "use msyrs::utils::qdf as msyrs_qdf;\n",
    "\n",
    "use polars::prelude::*;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "677da413-44b1-4746-b723-c17b0ff0c8a8",
   "metadata": {},
   "source": [
    "### Check connection\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6bcbfc0c-8249-4253-80b9-257115b50055",
   "metadata": {},
   "outputs": [],
   "source": [
    "let mut jpamqs_download = JPMaQSDownload::default();\n",
    "\n",
    "match jpamqs_download.check_connection() {\n",
    "   Ok(_) => println!(\"Connection to DataQuery API successful\"),\n",
    "  Err(e) => println!(\"Error connecting to DataQuery API: {:?}\", e),\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "612e565f-4fa6-400b-a5ac-a4358dbc05ef",
   "metadata": {},
   "source": [
    "### Get catalogue\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ac7096a-fae8-43e9-9584-c5f055a9f14e",
   "metadata": {},
   "outputs": [],
   "source": [
    "let tickers: Vec<String> = jpamqs_download.get_catalogue().unwrap();\n",
    "let some_tickers = tickers.iter().take(100).map(|s| s.to_string()).collect::<Vec<String>>();\n",
    "for ticker in some_tickers.clone() {\n",
    "        print!(\"{}, \", ticker);\n",
    "}\n",
    "println!(\"\");"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3a0bb91-3b3a-4c7e-959e-37311827b884",
   "metadata": {},
   "source": [
    "### Setup download args\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6a9506c",
   "metadata": {},
   "source": [
    "#### Define tickers for download\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd10ce0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "// cids = cids_dm + cids_em\n",
    "let cids: Vec<String> = cids_dm\n",
    "    .iter()\n",
    "    .chain(cids_em.iter())\n",
    "    .map(|s| s.to_string())\n",
    "    .collect();\n",
    "\n",
    "let non_dux = [\"IDR\", \"NZD\"];\n",
    "#[allow(unused_variables)]\n",
    "let cids_dux: Vec<String> = cids\n",
    "    .iter()\n",
    "    .filter(|s| !non_dux.contains(&s.as_str()))\n",
    "    .map(|s| s.to_string())\n",
    "    .collect();\n",
    "let ecos = vec![\n",
    "    \"CPIC_SA_P1M1ML12\",\n",
    "    \"CPIC_SJA_P3M3ML3AR\",\n",
    "    \"CPIC_SJA_P6M6ML6AR\",\n",
    "    \"CPIH_SA_P1M1ML12\",\n",
    "    \"CPIH_SJA_P3M3ML3AR\",\n",
    "    \"CPIH_SJA_P6M6ML6AR\",\n",
    "    \"INFTEFF_NSA\",\n",
    "    \"INTRGDP_NSA_P1M1ML12_3MMA\",\n",
    "    \"INTRGDPv5Y_NSA_P1M1ML12_3MMA\",\n",
    "    \"PCREDITGDP_SJA_D1M1ML12\",\n",
    "    \"RGDP_SA_P1Q1QL4_20QMA\",\n",
    "    \"RYLDIRS02Y_NSA\",\n",
    "    \"RYLDIRS05Y_NSA\",\n",
    "    \"PCREDITBN_SJA_P1M1ML12\",\n",
    "];\n",
    "let mkts = vec![\n",
    "    \"DU02YXR_NSA\",\n",
    "    \"DU05YXR_NSA\",\n",
    "    \"DU02YXR_VT10\",\n",
    "    \"DU05YXR_VT10\",\n",
    "    \"EQXR_NSA\",\n",
    "    \"EQXR_VT10\",\n",
    "    \"FXXR_NSA\",\n",
    "    \"FXXR_VT10\",\n",
    "    \"FXCRR_NSA\",\n",
    "    \"FXTARGETED_NSA\",\n",
    "    \"FXUNTRADABLE_NSA\",\n",
    "];\n",
    "\n",
    "let xcats: Vec<String> = ecos\n",
    "    .iter()\n",
    "    .chain(mkts.iter())\n",
    "    .map(|s| s.to_string())\n",
    "    .collect();\n",
    "\n",
    "let cids_str: Vec<&str> = cids.iter().map(AsRef::as_ref).collect();\n",
    "let xcats_str: Vec<&str> = xcats.iter().map(AsRef::as_ref).collect();\n",
    "let download_tickers = msyrs_utils::misc::create_interesecting_tickers(&cids_str, &xcats_str);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45b1f5fa-d99b-4f24-9b0b-1e04f484efc2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "52d4ec91-a024-4f83-aaec-692f5d2645f9",
   "metadata": {},
   "source": [
    "### Download data as a dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12e7055b-c68f-4b6e-9bb3-43c1886dab97",
   "metadata": {},
   "outputs": [],
   "source": [
    "let all_metrics: Vec<String> = [\"value\", \"grading\", \"eop_lag\", \"mop_lag\"]\n",
    "        .iter()\n",
    "        .map(|x| x.to_string())\n",
    "        .collect();\n",
    "\n",
    "let download_args = JPMaQSDownloadGetIndicatorArgs {\n",
    "    tickers: download_tickers,\n",
    "    metrics: all_metrics // defaults to all metrics if not set\n",
    "    ..Default::default()\n",
    "};\n",
    "\n",
    "let mut jpmaqs_client = JPMaQSDownload::default();\n",
    "let downloaded_df = jpmaqs_client\n",
    "    .get_indicators_qdf(JPMaQSDownloadGetIndicatorArgs {\n",
    "        tickers: download_tickers,\n",
    "        ..Default::default()\n",
    "    })\n",
    "    .unwrap();\n",
    "\n",
    "downloaded_df.head(Some(100))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0193ded",
   "metadata": {},
   "source": [
    "## Utility functions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f6c6455",
   "metadata": {},
   "source": [
    "### Reduce DF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10b264fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "let eq_df = msyrs_qdf::reduce_dataframe(\n",
    "    downloaded_df.clone(),\n",
    "    None,\n",
    "    Some(vec![\"EQXR_NSA\", \"EQXR_VT10\"]),\n",
    "    None,\n",
    "    // None,\n",
    "    None,\n",
    "    None,\n",
    "    false,\n",
    ")\n",
    ".unwrap();\n",
    "\n",
    "println!(\"{:?}\", eq_df.head(Some(10)));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3f3b0c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "let fx_df = msyrs_qdf::reduce_dataframe(\n",
    "    downloaded_df,\n",
    "    None,\n",
    "    Some(vec![\n",
    "        \"FXXR_NSA\",\n",
    "        \"FXXR_VT10\",\n",
    "        \"FXCRR_NSA\",\n",
    "        \"FXTARGETED_NSA\",\n",
    "        \"FXUNTRADABLE_NSA\",\n",
    "    ]),\n",
    "    None,\n",
    "    None,\n",
    "    None,\n",
    "    true, // intersect = true \n",
    ")\n",
    ".unwrap();\n",
    "\n",
    "println!(\"{:?}\", eq_df.head(Some(10)));"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "722063d1",
   "metadata": {},
   "source": [
    "### Update DF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00ecdb90",
   "metadata": {},
   "outputs": [],
   "source": [
    "let custom_df = msyrs_qdf::update_dataframe(&fx_df, &eq_df).unwrap();\n",
    "\n",
    "println!(\"{:?}\", custom_df.head(Some(10)));"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Rust",
   "language": "rust",
   "name": "rust"
  },
  "language_info": {
   "codemirror_mode": "rust",
   "file_extension": ".rs",
   "mimetype": "text/rust",
   "name": "rust",
   "pygment_lexer": "rust",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
