{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1b729e5b-09ec-42b6-ad6d-4b4c8947ae20",
   "metadata": {},
   "source": [
    "https://auto.gluon.ai/stable/tutorials/timeseries/forecasting-chronos.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1b98681b-7136-4726-bf1f-433910162939",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "from autogluon.timeseries import TimeSeriesDataFrame, TimeSeriesPredictor\n",
    "\n",
    "home = os.environ['HOME']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "be0a08ef-bd1e-4ef8-a776-d6289670a35b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_df_from_db(file: str, query: str) -> pd.DataFrame:\n",
    "    import sqlite3 \n",
    "    \n",
    "    conn = sqlite3.connect(file)\n",
    "    df = pd.read_sql_query(query, conn)\n",
    "    df['timestamp'] = pd.to_datetime(df['tstamp'])\n",
    "    df.set_index('timestamp', inplace=True)\n",
    "    return df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "16175ced-b6ed-4b6f-af7f-22208a867f71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>tstamp</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>PAIR-BTC-USDT</td>\n",
       "      <td>1717200000000000000</td>\n",
       "      <td>67566.835981</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>PAIR-BTC-USDT</td>\n",
       "      <td>1717200060000000000</td>\n",
       "      <td>67575.014916</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>PAIR-BTC-USDT</td>\n",
       "      <td>1717200120000000000</td>\n",
       "      <td>67572.550101</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>PAIR-BTC-USDT</td>\n",
       "      <td>1717200180000000000</td>\n",
       "      <td>67600.476125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>PAIR-BTC-USDT</td>\n",
       "      <td>1717200240000000000</td>\n",
       "      <td>67636.363585</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1435</th>\n",
       "      <td>PAIR-BTC-USDT</td>\n",
       "      <td>1717286100000000000</td>\n",
       "      <td>67753.782261</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1436</th>\n",
       "      <td>PAIR-BTC-USDT</td>\n",
       "      <td>1717286160000000000</td>\n",
       "      <td>67755.991209</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1437</th>\n",
       "      <td>PAIR-BTC-USDT</td>\n",
       "      <td>1717286220000000000</td>\n",
       "      <td>67755.991295</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1438</th>\n",
       "      <td>PAIR-BTC-USDT</td>\n",
       "      <td>1717286280000000000</td>\n",
       "      <td>67763.063589</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1439</th>\n",
       "      <td>PAIR-BTC-USDT</td>\n",
       "      <td>1717286340000000000</td>\n",
       "      <td>67768.060160</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1440 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                 id               tstamp        target\n",
       "0     PAIR-BTC-USDT  1717200000000000000  67566.835981\n",
       "1     PAIR-BTC-USDT  1717200060000000000  67575.014916\n",
       "2     PAIR-BTC-USDT  1717200120000000000  67572.550101\n",
       "3     PAIR-BTC-USDT  1717200180000000000  67600.476125\n",
       "4     PAIR-BTC-USDT  1717200240000000000  67636.363585\n",
       "...             ...                  ...           ...\n",
       "1435  PAIR-BTC-USDT  1717286100000000000  67753.782261\n",
       "1436  PAIR-BTC-USDT  1717286160000000000  67755.991209\n",
       "1437  PAIR-BTC-USDT  1717286220000000000  67755.991295\n",
       "1438  PAIR-BTC-USDT  1717286280000000000  67763.063589\n",
       "1439  PAIR-BTC-USDT  1717286340000000000  67768.060160\n",
       "\n",
       "[1440 rows x 3 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "file_path = \"/workspace/data/crypto_md/20240601.mktdata.ohlcv.db\"\n",
    "instrument_id='PAIR-BTC-USDT'\n",
    "query = f\"select '{instrument_id}' as id, tstamp, vwap from bnbspot_ohlcv_1min where instrument_id = '{instrument_id}'\"\n",
    "df = load_df_from_db(file=file_path, query=query)\n",
    "df.rename(columns={'vwap': 'target'}, inplace=True)\n",
    "# df[\"tstamp2\"] = df.index\n",
    "df = df.reset_index()\n",
    "df = df.drop([\"timestamp\"], axis=1) \n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b23fbb91-d5a2-40d2-85fe-30581d745ac6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>item_id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">PAIR-BTC-USDT</th>\n",
       "      <th>2024-06-01 00:00:00</th>\n",
       "      <td>67566.835981</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-01 00:01:00</th>\n",
       "      <td>67575.014916</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-01 00:02:00</th>\n",
       "      <td>67572.550101</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-01 00:03:00</th>\n",
       "      <td>67600.476125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-01 00:04:00</th>\n",
       "      <td>67636.363585</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                         target\n",
       "item_id       timestamp                        \n",
       "PAIR-BTC-USDT 2024-06-01 00:00:00  67566.835981\n",
       "              2024-06-01 00:01:00  67575.014916\n",
       "              2024-06-01 00:02:00  67572.550101\n",
       "              2024-06-01 00:03:00  67600.476125\n",
       "              2024-06-01 00:04:00  67636.363585"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = TimeSeriesDataFrame.from_data_frame(\n",
    "    df,\n",
    "    id_column=\"id\",\n",
    "    timestamp_column=\"tstamp\"\n",
    ")\n",
    "data.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "cd7b6927-1666-4a59-8f47-4c6a66b230e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Beginning AutoGluon training...\n",
      "AutoGluon will save models to 'AutogluonModels/ag-20240606_225207'\n",
      "=================== System Info ===================\n",
      "AutoGluon Version:  1.1.0\n",
      "Python Version:     3.10.13\n",
      "Operating System:   Linux\n",
      "Platform Machine:   x86_64\n",
      "Platform Version:   #117-Ubuntu SMP Fri Apr 26 12:26:49 UTC 2024\n",
      "CPU Count:          8\n",
      "GPU Count:          1\n",
      "Memory Avail:       54.82 GB / 62.72 GB (87.4%)\n",
      "Disk Space Avail:   429.42 GB / 476.94 GB (90.0%)\n",
      "===================================================\n",
      "Setting presets to: chronos_small\n",
      "\n",
      "Fitting with arguments:\n",
      "{'enable_ensemble': True,\n",
      " 'eval_metric': WQL,\n",
      " 'hyperparameters': {'Chronos': {'model_path': 'small'}},\n",
      " 'known_covariates_names': [],\n",
      " 'num_val_windows': 1,\n",
      " 'prediction_length': 10,\n",
      " 'quantile_levels': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],\n",
      " 'random_seed': 123,\n",
      " 'refit_every_n_windows': 1,\n",
      " 'refit_full': False,\n",
      " 'skip_model_selection': True,\n",
      " 'target': 'target',\n",
      " 'verbosity': 2}\n",
      "\n",
      "Inferred time series frequency: 'min'\n",
      "Provided train_data has 1430 rows, 1 time series. Median time series length is 1430 (min=1430, max=1430). \n",
      "\n",
      "Provided data contains following columns:\n",
      "\ttarget: 'target'\n",
      "\n",
      "AutoGluon will gauge predictive performance using evaluation metric: 'WQL'\n",
      "\tThis metric's sign has been flipped to adhere to being higher_is_better. The metric score can be multiplied by -1 to get the metric value.\n",
      "===================================================\n",
      "\n",
      "Starting training. Start time is 2024-06-06 22:52:07\n",
      "Models that will be trained: ['Chronos[small]']\n",
      "Training timeseries model Chronos[small]. \n",
      "\t0.00    s     = Training runtime\n",
      "Training complete. Models trained: ['Chronos[small]']\n",
      "Total runtime: 0.00 s\n",
      "Best model: Chronos[small]\n"
     ]
    }
   ],
   "source": [
    "prediction_length = 10\n",
    "train_data, test_data = data.train_test_split(prediction_length)\n",
    "\n",
    "predictor = TimeSeriesPredictor(prediction_length=prediction_length).fit(\n",
    "    train_data, presets=\"chronos_small\",\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "36b063a8-365f-45cb-80f7-8b5b625f42e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Model not specified in predict, will default to the model with the best validation score: Chronos[small]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 72980 characters]",
      "text/plain": [
       "<Figure size 2000x350 with 1 Axes>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictions = predictor.predict(train_data)\n",
    "predictor.plot(\n",
    "    data=data, \n",
    "    predictions=predictions, \n",
    "    max_history_length=200,\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
