{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9bdd5e0b",
   "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>date</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>open</th>\n",
       "      <th>volume</th>\n",
       "      <th>ingestion_timestamp</th>\n",
       "      <th>day_of_week</th>\n",
       "      <th>cleaned_timestamp</th>\n",
       "      <th>return</th>\n",
       "      <th>volatility</th>\n",
       "      <th>feature_timestamp</th>\n",
       "      <th>trend_50</th>\n",
       "      <th>trend_200</th>\n",
       "      <th>momentum</th>\n",
       "      <th>market_regime</th>\n",
       "      <th>market_regime_label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2015-07-24</td>\n",
       "      <td>288.278015</td>\n",
       "      <td>289.252991</td>\n",
       "      <td>275.253998</td>\n",
       "      <td>276.005005</td>\n",
       "      <td>37199400</td>\n",
       "      <td>2026-02-26 13:18:59.227458</td>\n",
       "      <td>4</td>\n",
       "      <td>2026-02-26 13:19:05.664586+00:00</td>\n",
       "      <td>0.044300</td>\n",
       "      <td>0.022061</td>\n",
       "      <td>2026-02-26 13:19:10.312302+00:00</td>\n",
       "      <td>0.119213</td>\n",
       "      <td>0.168323</td>\n",
       "      <td>0.104996</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2015-07-25</td>\n",
       "      <td>288.696991</td>\n",
       "      <td>290.733002</td>\n",
       "      <td>286.002014</td>\n",
       "      <td>288.164001</td>\n",
       "      <td>20662200</td>\n",
       "      <td>2026-02-26 13:18:59.227458</td>\n",
       "      <td>5</td>\n",
       "      <td>2026-02-26 13:19:05.664586+00:00</td>\n",
       "      <td>0.001453</td>\n",
       "      <td>0.021716</td>\n",
       "      <td>2026-02-26 13:19:10.312302+00:00</td>\n",
       "      <td>0.115319</td>\n",
       "      <td>0.169962</td>\n",
       "      <td>0.061726</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2015-07-26</td>\n",
       "      <td>292.686005</td>\n",
       "      <td>293.052002</td>\n",
       "      <td>287.705994</td>\n",
       "      <td>288.640015</td>\n",
       "      <td>16032300</td>\n",
       "      <td>2026-02-26 13:18:59.227458</td>\n",
       "      <td>6</td>\n",
       "      <td>2026-02-26 13:19:05.664586+00:00</td>\n",
       "      <td>0.013817</td>\n",
       "      <td>0.019169</td>\n",
       "      <td>2026-02-26 13:19:10.312302+00:00</td>\n",
       "      <td>0.124900</td>\n",
       "      <td>0.186167</td>\n",
       "      <td>0.087931</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2015-07-27</td>\n",
       "      <td>293.623993</td>\n",
       "      <td>297.773987</td>\n",
       "      <td>287.450012</td>\n",
       "      <td>292.639008</td>\n",
       "      <td>30592000</td>\n",
       "      <td>2026-02-26 13:18:59.227458</td>\n",
       "      <td>0</td>\n",
       "      <td>2026-02-26 13:19:05.664586+00:00</td>\n",
       "      <td>0.003205</td>\n",
       "      <td>0.019353</td>\n",
       "      <td>2026-02-26 13:19:10.312302+00:00</td>\n",
       "      <td>0.122402</td>\n",
       "      <td>0.189721</td>\n",
       "      <td>0.102991</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2015-07-28</td>\n",
       "      <td>294.427002</td>\n",
       "      <td>296.648987</td>\n",
       "      <td>293.423004</td>\n",
       "      <td>293.632996</td>\n",
       "      <td>25453600</td>\n",
       "      <td>2026-02-26 13:18:59.227458</td>\n",
       "      <td>1</td>\n",
       "      <td>2026-02-26 13:19:05.664586+00:00</td>\n",
       "      <td>0.002735</td>\n",
       "      <td>0.018131</td>\n",
       "      <td>2026-02-26 13:19:10.312302+00:00</td>\n",
       "      <td>0.119827</td>\n",
       "      <td>0.192877</td>\n",
       "      <td>0.087309</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        date       close        high         low        open    volume  \\\n",
       "0 2015-07-24  288.278015  289.252991  275.253998  276.005005  37199400   \n",
       "1 2015-07-25  288.696991  290.733002  286.002014  288.164001  20662200   \n",
       "2 2015-07-26  292.686005  293.052002  287.705994  288.640015  16032300   \n",
       "3 2015-07-27  293.623993  297.773987  287.450012  292.639008  30592000   \n",
       "4 2015-07-28  294.427002  296.648987  293.423004  293.632996  25453600   \n",
       "\n",
       "         ingestion_timestamp  day_of_week                cleaned_timestamp  \\\n",
       "0 2026-02-26 13:18:59.227458            4 2026-02-26 13:19:05.664586+00:00   \n",
       "1 2026-02-26 13:18:59.227458            5 2026-02-26 13:19:05.664586+00:00   \n",
       "2 2026-02-26 13:18:59.227458            6 2026-02-26 13:19:05.664586+00:00   \n",
       "3 2026-02-26 13:18:59.227458            0 2026-02-26 13:19:05.664586+00:00   \n",
       "4 2026-02-26 13:18:59.227458            1 2026-02-26 13:19:05.664586+00:00   \n",
       "\n",
       "     return  volatility                feature_timestamp  trend_50  trend_200  \\\n",
       "0  0.044300    0.022061 2026-02-26 13:19:10.312302+00:00  0.119213   0.168323   \n",
       "1  0.001453    0.021716 2026-02-26 13:19:10.312302+00:00  0.115319   0.169962   \n",
       "2  0.013817    0.019169 2026-02-26 13:19:10.312302+00:00  0.124900   0.186167   \n",
       "3  0.003205    0.019353 2026-02-26 13:19:10.312302+00:00  0.122402   0.189721   \n",
       "4  0.002735    0.018131 2026-02-26 13:19:10.312302+00:00  0.119827   0.192877   \n",
       "\n",
       "   momentum  market_regime market_regime_label  \n",
       "0  0.104996            NaN                None  \n",
       "1  0.061726            NaN                None  \n",
       "2  0.087931            NaN                None  \n",
       "3  0.102991            NaN                None  \n",
       "4  0.087309            NaN                None  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_parquet('data/regimes/BTC-USD_regimes.parquet')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a7f90945",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                       return           volatility          \n",
      "                         mean       std       mean       std\n",
      "market_regime_label                                         \n",
      "Bear                -0.005352  0.034112   0.030718  0.017073\n",
      "Bull                 0.002615  0.026245   0.024523  0.009287\n",
      "Sideways             0.002312  0.017013   0.015930  0.007129\n"
     ]
    }
   ],
   "source": [
    "summary = df.groupby(\"market_regime_label\")[[\"return\", \"volatility\"]].agg([\"mean\", \"std\"])\n",
    "print(summary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5e0449c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "market_regime_label\n",
       "Bear       -0.141299\n",
       "Bull       -0.057462\n",
       "Sideways   -0.045840\n",
       "Name: return, dtype: float64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(\"market_regime_label\")[\"return\"].min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "870e4319",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Strategy mean: 5.810377227931221e-05\n",
      "Market mean: 0.0020426368691019918\n"
     ]
    }
   ],
   "source": [
    "df[\"strategy_return\"] = df[\"return\"] * (df[\"market_regime_label\"] == \"Bull\")\n",
    "print(\"Strategy mean:\", df[\"strategy_return\"].mean())\n",
    "print(\"Market mean:\", df[\"return\"].mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e0349e36",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Strategy final value: 1.21618898373469\n",
      "Market final value: 246.48128156377766\n"
     ]
    }
   ],
   "source": [
    "df[\"strategy_cum\"] = (1 + df[\"strategy_return\"]).cumprod()\n",
    "df[\"market_cum\"] = (1 + df[\"return\"]).cumprod()\n",
    "print(\"Strategy final value:\", df[\"strategy_cum\"].iloc[-1])\n",
    "print(\"Market final value:\", df[\"market_cum\"].iloc[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64c0bf96",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time in market: 0.02221648152932059\n"
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mThe Kernel crashed while executing code in the current cell or a previous cell. \n",
      "\u001b[1;31mPlease review the code in the cell(s) to identify a possible cause of the failure. \n",
      "\u001b[1;31mClick <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. \n",
      "\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
     ]
    }
   ],
   "source": [
    "time_in_market = (df[\"market_regime_label\"] == \"Bull\").mean()\n",
    "print(\"Time in market:\", time_in_market)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
