{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Perfect predicton backtest\n",
    "What would the backtest look like if our predictions were perfect?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from dfply import *\n",
    "import simple_back_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "def perfect_model_data():\n",
    "    model_data = pd.read_feather( '../../data/backtesting/first_seen_dummy.feather')\n",
    "    model_data = (model_data >> \n",
    "            rename( spyid = 'index', streams = 'actual_streams') >> \n",
    "            mutate( streams = exp(X.streams), predicted_streams = exp(X.predicted_streams)))\n",
    "    \n",
    "    model_data = model_data >> mutate(predicted_streams = X.streams)\n",
    "\n",
    "    return model_data\n",
    "\n",
    "\n",
    "simple_back_test.get_model_data = perfect_model_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "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>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>average_positions</th>\n",
       "      <td>1.641469e+01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>average_position_size</th>\n",
       "      <td>2.368299e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>average_portfolio_return</th>\n",
       "      <td>9.508990e-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>portfolio_std</th>\n",
       "      <td>2.940501e-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>cumulative_return</th>\n",
       "      <td>5.482612e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>total_number_tracks</th>\n",
       "      <td>7.600000e+01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Backtest_days</th>\n",
       "      <td>4.620000e+02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max_drawdown</th>\n",
       "      <td>-2.369542e-16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sharpe</th>\n",
       "      <td>5.133498e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>annualised_ret</th>\n",
       "      <td>2.686089e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>annual_volatility</th>\n",
       "      <td>4.667900e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sortino</th>\n",
       "      <td>7.087955e+14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>downside_risk</th>\n",
       "      <td>3.380757e-16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tail_risk</th>\n",
       "      <td>inf</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     0\n",
       "average_positions         1.641469e+01\n",
       "average_position_size     2.368299e+05\n",
       "average_portfolio_return  9.508990e-04\n",
       "portfolio_std             2.940501e-03\n",
       "cumulative_return         5.482612e-01\n",
       "total_number_tracks       7.600000e+01\n",
       "Backtest_days             4.620000e+02\n",
       "max_drawdown             -2.369542e-16\n",
       "sharpe                    5.133498e+00\n",
       "annualised_ret            2.686089e-01\n",
       "annual_volatility         4.667900e-02\n",
       "sortino                   7.087955e+14\n",
       "downside_risk             3.380757e-16\n",
       "tail_risk                          inf"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "logging.basicConfig(format='%(message)s', level=logging.WARNING)\n",
    "\n",
    "ps, ps_summary = run_backtest(\n",
    "        payment_per_stream = 0.005\n",
    "        ,\n",
    "        artist_advance_fraction = 0.8\n",
    "        ,\n",
    "        pop_5_slippage = 0.15\n",
    "        , # Applied on both enter and exit\n",
    "        execution_slippage = 0.1\n",
    "        , # Applied only on the exit, ie how long does it take to sign up the artist\n",
    "        clip_individual_returns = 3\n",
    "        ,\n",
    "        min_price = 5e3\n",
    "        ,\n",
    "        max_price = 50e3\n",
    "        ,\n",
    "        duration_days = 365\n",
    "        ,\n",
    "        new_streams_per_date = 3/7\n",
    "        ,\n",
    "        capital = 250e3\n",
    "        )\n",
    "\n",
    "ps_summary.transpose()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:whitelist]",
   "language": "python",
   "name": "conda-env-whitelist-py"
  },
  "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.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
