{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "source": [
    "Please go through the \"building_strategies\" notebook first before you go through this notebook\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Some Prebuilt Reporting ##\n",
    "\n",
    "Lets first build the strategy described in that notebook, add it to a portfolio and run the portfolio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-04T00:54:49.486068Z",
     "start_time": "2022-12-04T00:54:49.210132Z"
    },
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "from types import SimpleNamespace\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import pyqstrat as pq\n",
    "from pyqstrat.notebooks.support.build_example_strategy import build_example_strategy\n",
    "\n",
    "strategy = build_example_strategy(SimpleNamespace(lookback_period = 10, num_std = 2))\n",
    "strategy.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Many objects have functions that return pandas dataframes for ease of use.  Any function that returns a dataframe starts with df_ so its easy to tell which dataframes an object returns.\n",
    "\n",
    "Probably the most common function you will use is df_data on the strategy object.  This returns the market data, indicators, signal values and P&L at each market data bar.  The last column, i is the integer index of that bar, and can be used to query data in other dataframes or objects for that strategy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-04T00:54:49.500368Z",
     "start_time": "2022-12-04T00:54:49.486726Z"
    },
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "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>timestamp</th>\n",
       "      <th>c</th>\n",
       "      <th>h</th>\n",
       "      <th>l</th>\n",
       "      <th>lower_band</th>\n",
       "      <th>o</th>\n",
       "      <th>sma</th>\n",
       "      <th>upper_band</th>\n",
       "      <th>bb_signal</th>\n",
       "      <th>position</th>\n",
       "      <th>unrealized</th>\n",
       "      <th>realized</th>\n",
       "      <th>commission</th>\n",
       "      <th>fee</th>\n",
       "      <th>net_pnl</th>\n",
       "      <th>equity</th>\n",
       "      <th>i</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>550</th>\n",
       "      <td>2019-02-13 10:45:00</td>\n",
       "      <td>113.74</td>\n",
       "      <td>113.980</td>\n",
       "      <td>113.720</td>\n",
       "      <td>113.317057</td>\n",
       "      <td>113.9500</td>\n",
       "      <td>113.93401</td>\n",
       "      <td>114.550963</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>551</th>\n",
       "      <td>2019-02-13 11:00:00</td>\n",
       "      <td>113.90</td>\n",
       "      <td>113.905</td>\n",
       "      <td>113.680</td>\n",
       "      <td>113.325960</td>\n",
       "      <td>113.7500</td>\n",
       "      <td>113.94001</td>\n",
       "      <td>114.554060</td>\n",
       "      <td>-1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>551</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>552</th>\n",
       "      <td>2019-02-13 11:15:00</td>\n",
       "      <td>113.99</td>\n",
       "      <td>114.045</td>\n",
       "      <td>113.870</td>\n",
       "      <td>113.367005</td>\n",
       "      <td>113.8700</td>\n",
       "      <td>113.96501</td>\n",
       "      <td>114.563015</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>552</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>553</th>\n",
       "      <td>2019-02-13 11:30:00</td>\n",
       "      <td>114.04</td>\n",
       "      <td>114.130</td>\n",
       "      <td>113.935</td>\n",
       "      <td>113.394339</td>\n",
       "      <td>113.9800</td>\n",
       "      <td>113.98600</td>\n",
       "      <td>114.577661</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>553</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>554</th>\n",
       "      <td>2019-02-13 11:45:00</td>\n",
       "      <td>114.04</td>\n",
       "      <td>114.040</td>\n",
       "      <td>113.850</td>\n",
       "      <td>113.539625</td>\n",
       "      <td>114.0400</td>\n",
       "      <td>114.03700</td>\n",
       "      <td>114.534375</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>555</th>\n",
       "      <td>2019-02-13 12:00:00</td>\n",
       "      <td>113.98</td>\n",
       "      <td>114.010</td>\n",
       "      <td>113.920</td>\n",
       "      <td>113.594123</td>\n",
       "      <td>113.9949</td>\n",
       "      <td>114.05800</td>\n",
       "      <td>114.521877</td>\n",
       "      <td>-1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>555</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>556</th>\n",
       "      <td>2019-02-13 12:15:00</td>\n",
       "      <td>113.86</td>\n",
       "      <td>114.000</td>\n",
       "      <td>113.810</td>\n",
       "      <td>113.724008</td>\n",
       "      <td>114.0000</td>\n",
       "      <td>113.98200</td>\n",
       "      <td>114.239992</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>557</th>\n",
       "      <td>2019-02-13 12:30:00</td>\n",
       "      <td>113.87</td>\n",
       "      <td>113.870</td>\n",
       "      <td>113.790</td>\n",
       "      <td>113.730101</td>\n",
       "      <td>113.8500</td>\n",
       "      <td>113.95000</td>\n",
       "      <td>114.169899</td>\n",
       "      <td>-1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>557</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>558</th>\n",
       "      <td>2019-02-13 12:45:00</td>\n",
       "      <td>113.81</td>\n",
       "      <td>113.890</td>\n",
       "      <td>113.770</td>\n",
       "      <td>113.719122</td>\n",
       "      <td>113.8650</td>\n",
       "      <td>113.91900</td>\n",
       "      <td>114.118878</td>\n",
       "      <td>-1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>558</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>559</th>\n",
       "      <td>2019-02-13 13:00:00</td>\n",
       "      <td>113.86</td>\n",
       "      <td>113.860</td>\n",
       "      <td>113.790</td>\n",
       "      <td>113.708235</td>\n",
       "      <td>113.8000</td>\n",
       "      <td>113.90900</td>\n",
       "      <td>114.109765</td>\n",
       "      <td>-1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>559</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              timestamp       c        h        l  lower_band         o  \\\n",
       "550 2019-02-13 10:45:00  113.74  113.980  113.720  113.317057  113.9500   \n",
       "551 2019-02-13 11:00:00  113.90  113.905  113.680  113.325960  113.7500   \n",
       "552 2019-02-13 11:15:00  113.99  114.045  113.870  113.367005  113.8700   \n",
       "553 2019-02-13 11:30:00  114.04  114.130  113.935  113.394339  113.9800   \n",
       "554 2019-02-13 11:45:00  114.04  114.040  113.850  113.539625  114.0400   \n",
       "555 2019-02-13 12:00:00  113.98  114.010  113.920  113.594123  113.9949   \n",
       "556 2019-02-13 12:15:00  113.86  114.000  113.810  113.724008  114.0000   \n",
       "557 2019-02-13 12:30:00  113.87  113.870  113.790  113.730101  113.8500   \n",
       "558 2019-02-13 12:45:00  113.81  113.890  113.770  113.719122  113.8650   \n",
       "559 2019-02-13 13:00:00  113.86  113.860  113.790  113.708235  113.8000   \n",
       "\n",
       "           sma  upper_band  bb_signal  position  unrealized  realized  \\\n",
       "550  113.93401  114.550963          1       NaN         NaN       NaN   \n",
       "551  113.94001  114.554060         -1       NaN         NaN       NaN   \n",
       "552  113.96501  114.563015          1       NaN         NaN       NaN   \n",
       "553  113.98600  114.577661          1       NaN         NaN       NaN   \n",
       "554  114.03700  114.534375          1       NaN         NaN       NaN   \n",
       "555  114.05800  114.521877         -1       NaN         NaN       NaN   \n",
       "556  113.98200  114.239992          1       NaN         NaN       NaN   \n",
       "557  113.95000  114.169899         -1       NaN         NaN       NaN   \n",
       "558  113.91900  114.118878         -1       NaN         NaN       NaN   \n",
       "559  113.90900  114.109765         -1       NaN         NaN       NaN   \n",
       "\n",
       "     commission  fee  net_pnl  equity    i  \n",
       "550         NaN  NaN      NaN     NaN  550  \n",
       "551         NaN  NaN      NaN     NaN  551  \n",
       "552         NaN  NaN      NaN     NaN  552  \n",
       "553         NaN  NaN      NaN     NaN  553  \n",
       "554         NaN  NaN      NaN     NaN  554  \n",
       "555         NaN  NaN      NaN     NaN  555  \n",
       "556         NaN  NaN      NaN     NaN  556  \n",
       "557         NaN  NaN      NaN     NaN  557  \n",
       "558         NaN  NaN      NaN     NaN  558  \n",
       "559         NaN  NaN      NaN     NaN  559  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "strategy.df_data().iloc[550:560]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also look at just the PNL or just the marketdata by themselves."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-04T00:54:49.508585Z",
     "start_time": "2022-12-04T00:54:49.501843Z"
    },
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "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>timestamp</th>\n",
       "      <th>position</th>\n",
       "      <th>unrealized</th>\n",
       "      <th>realized</th>\n",
       "      <th>commission</th>\n",
       "      <th>fee</th>\n",
       "      <th>net_pnl</th>\n",
       "      <th>equity</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-01-14 15:00:00</td>\n",
       "      <td>930.0</td>\n",
       "      <td>51.150</td>\n",
       "      <td>-144.30500</td>\n",
       "      <td>4650.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-4743.15500</td>\n",
       "      <td>9.952568e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-01-15 15:00:00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>720.59500</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>720.59500</td>\n",
       "      <td>1.000721e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-01-16 15:00:00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>215.01495</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>215.01495</td>\n",
       "      <td>1.000215e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-01-17 15:00:00</td>\n",
       "      <td>917.0</td>\n",
       "      <td>-128.380</td>\n",
       "      <td>459.50495</td>\n",
       "      <td>4585.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-4253.87505</td>\n",
       "      <td>9.957461e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-01-18 15:00:00</td>\n",
       "      <td>917.0</td>\n",
       "      <td>747.355</td>\n",
       "      <td>459.50495</td>\n",
       "      <td>4585.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-3378.14005</td>\n",
       "      <td>9.966219e+05</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            timestamp  position  unrealized   realized  commission  fee  \\\n",
       "0 2019-01-14 15:00:00     930.0      51.150 -144.30500      4650.0  0.0   \n",
       "1 2019-01-15 15:00:00       0.0       0.000  720.59500         0.0  0.0   \n",
       "2 2019-01-16 15:00:00       0.0       0.000  215.01495         0.0  0.0   \n",
       "3 2019-01-17 15:00:00     917.0    -128.380  459.50495      4585.0  0.0   \n",
       "4 2019-01-18 15:00:00     917.0     747.355  459.50495      4585.0  0.0   \n",
       "\n",
       "      net_pnl        equity  \n",
       "0 -4743.15500  9.952568e+05  \n",
       "1   720.59500  1.000721e+06  \n",
       "2   215.01495  1.000215e+06  \n",
       "3 -4253.87505  9.957461e+05  \n",
       "4 -3378.14005  9.966219e+05  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "strategy.df_pnl().head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can look at orders and trades that were created during this run"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-04T00:54:49.514405Z",
     "start_time": "2022-12-04T00:54:49.509310Z"
    },
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "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>symbol</th>\n",
       "      <th>type</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>qty</th>\n",
       "      <th>reason_code</th>\n",
       "      <th>order_props</th>\n",
       "      <th>contract_props</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>PEP</td>\n",
       "      <td>StopLimitOrder</td>\n",
       "      <td>2019-01-14 09:45:00</td>\n",
       "      <td>931.0</td>\n",
       "      <td>enter long</td>\n",
       "      <td>{'trigger_price': 107.38, 'limit_price': nan}</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>PEP</td>\n",
       "      <td>MarketOrder</td>\n",
       "      <td>2019-01-14 12:15:00</td>\n",
       "      <td>-931.0</td>\n",
       "      <td>exit long</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>PEP</td>\n",
       "      <td>StopLimitOrder</td>\n",
       "      <td>2019-01-14 13:45:00</td>\n",
       "      <td>930.0</td>\n",
       "      <td>enter long</td>\n",
       "      <td>{'trigger_price': 107.56, 'limit_price': nan}</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>PEP</td>\n",
       "      <td>MarketOrder</td>\n",
       "      <td>2019-01-15 13:30:00</td>\n",
       "      <td>-930.0</td>\n",
       "      <td>exit long</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>PEP</td>\n",
       "      <td>StopLimitOrder</td>\n",
       "      <td>2019-01-15 15:30:00</td>\n",
       "      <td>920.0</td>\n",
       "      <td>enter long</td>\n",
       "      <td>{'trigger_price': 108.755, 'limit_price': nan}</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  symbol            type           timestamp    qty reason_code  \\\n",
       "0    PEP  StopLimitOrder 2019-01-14 09:45:00  931.0  enter long   \n",
       "1    PEP     MarketOrder 2019-01-14 12:15:00 -931.0   exit long   \n",
       "2    PEP  StopLimitOrder 2019-01-14 13:45:00  930.0  enter long   \n",
       "3    PEP     MarketOrder 2019-01-15 13:30:00 -930.0   exit long   \n",
       "4    PEP  StopLimitOrder 2019-01-15 15:30:00  920.0  enter long   \n",
       "\n",
       "                                      order_props contract_props  \n",
       "0   {'trigger_price': 107.38, 'limit_price': nan}                 \n",
       "1                                                                 \n",
       "2   {'trigger_price': 107.56, 'limit_price': nan}                 \n",
       "3                                                                 \n",
       "4  {'trigger_price': 108.755, 'limit_price': nan}                 "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "strategy.df_orders().head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-04T00:54:49.522257Z",
     "start_time": "2022-12-04T00:54:49.515138Z"
    },
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "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>symbol</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>qty</th>\n",
       "      <th>price</th>\n",
       "      <th>fee</th>\n",
       "      <th>commission</th>\n",
       "      <th>order_date</th>\n",
       "      <th>order_qty</th>\n",
       "      <th>reason_code</th>\n",
       "      <th>order_props</th>\n",
       "      <th>contract_props</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>PEP</td>\n",
       "      <td>2019-01-14 10:00:00</td>\n",
       "      <td>931.0</td>\n",
       "      <td>107.4800</td>\n",
       "      <td>0</td>\n",
       "      <td>4655.0</td>\n",
       "      <td>2019-01-14 09:45:00</td>\n",
       "      <td>931.0</td>\n",
       "      <td>enter long</td>\n",
       "      <td>{'trigger_price': 107.38, 'limit_price': nan}</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>PEP</td>\n",
       "      <td>2019-01-14 12:30:00</td>\n",
       "      <td>-931.0</td>\n",
       "      <td>107.3250</td>\n",
       "      <td>0</td>\n",
       "      <td>-4655.0</td>\n",
       "      <td>2019-01-14 12:15:00</td>\n",
       "      <td>-931.0</td>\n",
       "      <td>exit long</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>PEP</td>\n",
       "      <td>2019-01-14 14:00:00</td>\n",
       "      <td>930.0</td>\n",
       "      <td>107.5750</td>\n",
       "      <td>0</td>\n",
       "      <td>4650.0</td>\n",
       "      <td>2019-01-14 13:45:00</td>\n",
       "      <td>930.0</td>\n",
       "      <td>enter long</td>\n",
       "      <td>{'trigger_price': 107.56, 'limit_price': nan}</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>PEP</td>\n",
       "      <td>2019-01-15 13:45:00</td>\n",
       "      <td>-930.0</td>\n",
       "      <td>108.5050</td>\n",
       "      <td>0</td>\n",
       "      <td>-4650.0</td>\n",
       "      <td>2019-01-15 13:30:00</td>\n",
       "      <td>-930.0</td>\n",
       "      <td>exit long</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>PEP</td>\n",
       "      <td>2019-01-15 15:45:00</td>\n",
       "      <td>920.0</td>\n",
       "      <td>108.8225</td>\n",
       "      <td>0</td>\n",
       "      <td>4600.0</td>\n",
       "      <td>2019-01-15 15:30:00</td>\n",
       "      <td>920.0</td>\n",
       "      <td>enter long</td>\n",
       "      <td>{'trigger_price': 108.755, 'limit_price': nan}</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  symbol           timestamp    qty     price  fee  commission  \\\n",
       "0    PEP 2019-01-14 10:00:00  931.0  107.4800    0      4655.0   \n",
       "1    PEP 2019-01-14 12:30:00 -931.0  107.3250    0     -4655.0   \n",
       "2    PEP 2019-01-14 14:00:00  930.0  107.5750    0      4650.0   \n",
       "3    PEP 2019-01-15 13:45:00 -930.0  108.5050    0     -4650.0   \n",
       "4    PEP 2019-01-15 15:45:00  920.0  108.8225    0      4600.0   \n",
       "\n",
       "           order_date  order_qty reason_code  \\\n",
       "0 2019-01-14 09:45:00      931.0  enter long   \n",
       "1 2019-01-14 12:15:00     -931.0   exit long   \n",
       "2 2019-01-14 13:45:00      930.0  enter long   \n",
       "3 2019-01-15 13:30:00     -930.0   exit long   \n",
       "4 2019-01-15 15:30:00      920.0  enter long   \n",
       "\n",
       "                                      order_props contract_props  \n",
       "0   {'trigger_price': 107.38, 'limit_price': nan}                 \n",
       "1                                                                 \n",
       "2   {'trigger_price': 107.56, 'limit_price': nan}                 \n",
       "3                                                                 \n",
       "4  {'trigger_price': 108.755, 'limit_price': nan}                 "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "strategy.df_trades().head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-10T21:09:59.531818Z",
     "start_time": "2018-08-10T21:09:59.499471Z"
    }
   },
   "source": [
    "You can also look at the returns at the portfolio level (i.e. summing up several strategies)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-04T00:54:49.533158Z",
     "start_time": "2022-12-04T00:54:49.522895Z"
    },
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "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>timestamp</th>\n",
       "      <th>net_pnl</th>\n",
       "      <th>equity</th>\n",
       "      <th>ret</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-01-14</td>\n",
       "      <td>-4743.15500</td>\n",
       "      <td>9.952568e+05</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-01-15</td>\n",
       "      <td>720.59500</td>\n",
       "      <td>1.000721e+06</td>\n",
       "      <td>0.005490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-01-16</td>\n",
       "      <td>215.01495</td>\n",
       "      <td>1.000215e+06</td>\n",
       "      <td>-0.000505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-01-17</td>\n",
       "      <td>-4253.87505</td>\n",
       "      <td>9.957461e+05</td>\n",
       "      <td>-0.004468</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-01-18</td>\n",
       "      <td>-3378.14005</td>\n",
       "      <td>9.966219e+05</td>\n",
       "      <td>0.000879</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   timestamp     net_pnl        equity       ret\n",
       "0 2019-01-14 -4743.15500  9.952568e+05       NaN\n",
       "1 2019-01-15   720.59500  1.000721e+06  0.005490\n",
       "2 2019-01-16   215.01495  1.000215e+06 -0.000505\n",
       "3 2019-01-17 -4253.87505  9.957461e+05 -0.004468\n",
       "4 2019-01-18 -3378.14005  9.966219e+05  0.000879"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "strategy.df_returns().head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also get data as native Python objects as opposed to pandas dataframes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-04T00:54:49.536647Z",
     "start_time": "2022-12-04T00:54:49.534054Z"
    },
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[PEP 2019-01-15 13:45:00 qty: -930.0 prc: 108.505 commission: -4650 order: PEP 2019-01-15 13:30:00 qty: -930.0 exit long filled,\n",
       " PEP 2019-01-15 15:45:00 qty: 920.0 prc: 108.822 commission: 4600 order: PEP 2019-01-15 15:30:00 qty: 920.0 trigger_prc: 108.755 limit_prc: nan enter long namespace(trigger_price=108.755, limit_price=nan) filled,\n",
       " PEP 2019-01-16 10:15:00 qty: -920.0 prc: 108.458 commission: -4600 order: PEP 2019-01-16 10:00:00 qty: -920.0 exit long filled,\n",
       " PEP 2019-01-16 11:30:00 qty: -927.0 prc: 107.902 commission: -4635 order: PEP 2019-01-16 11:15:00 qty: -927.0 trigger_prc: 107.91 limit_prc: nan enter short namespace(trigger_price=107.91, limit_price=nan) filled,\n",
       " PEP 2019-01-16 13:30:00 qty: 927.0 prc: 108.085 commission: 4635 order: PEP 2019-01-16 13:15:00 qty: 927.0 exit short filled,\n",
       " PEP 2019-01-17 09:45:00 qty: 923.0 prc: 108.5 commission: 4615 order: PEP 2019-01-16 16:00:00 qty: 923.0 trigger_prc: 108.33 limit_prc: nan enter long namespace(trigger_price=108.33, limit_price=nan) filled,\n",
       " PEP 2019-01-17 12:30:00 qty: -923.0 prc: 108.73 commission: -4615 order: PEP 2019-01-17 12:15:00 qty: -923.0 exit long filled,\n",
       " PEP 2019-01-17 12:45:00 qty: -920.0 prc: 108.74 commission: -4600 order: PEP 2019-01-17 12:30:00 qty: -920.0 trigger_prc: 108.76 limit_prc: nan enter short namespace(trigger_price=108.76, limit_price=nan) filled,\n",
       " PEP 2019-01-17 14:30:00 qty: 920.0 prc: 108.705 commission: 4600 order: PEP 2019-01-17 14:15:00 qty: 920.0 exit short filled,\n",
       " PEP 2019-01-17 15:00:00 qty: 917.0 prc: 109.3 commission: 4585 order: PEP 2019-01-17 14:45:00 qty: 917.0 trigger_prc: 109.16 limit_prc: nan enter long namespace(trigger_price=109.16, limit_price=nan) filled]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "strategy.trades(start_date = np.datetime64('2019-01-15'), end_date = np.datetime64('2019-01-20'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding your Own Metrics ##\n",
    "\n",
    "Each strategy may have metrics that you want to measure that are specific to that strategy.  To add these, you can use the Evaluator object which can make things easier.\n",
    "\n",
    "To evaluate a strategy we use the evaluate returns function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-04T00:54:49.552910Z",
     "start_time": "2022-12-04T00:54:49.537565Z"
    },
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "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>gmean</th>\n",
       "      <th>amean</th>\n",
       "      <th>std</th>\n",
       "      <th>shrp</th>\n",
       "      <th>srt</th>\n",
       "      <th>k</th>\n",
       "      <th>calmar</th>\n",
       "      <th>mar</th>\n",
       "      <th>mdd_pct</th>\n",
       "      <th>mdd_dates</th>\n",
       "      <th>dd_3y_pct</th>\n",
       "      <th>dd_3y_timestamps</th>\n",
       "      <th>up_dwn</th>\n",
       "      <th>2019</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <td>-0.05588</td>\n",
       "      <td>-0.05525</td>\n",
       "      <td>0.004225</td>\n",
       "      <td>-0.8238</td>\n",
       "      <td>-1.385</td>\n",
       "      <td>4.845</td>\n",
       "      <td>-3.432</td>\n",
       "      <td>-3.432</td>\n",
       "      <td>0.0161</td>\n",
       "      <td>2019-01-22/2019-02-26</td>\n",
       "      <td>0.0161</td>\n",
       "      <td>2019-01-22/2019-02-26</td>\n",
       "      <td>10/19/0.345</td>\n",
       "      <td>-0.05588</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     gmean     amean       std     shrp     srt      k  calmar     mar  \\\n",
       "  -0.05588  -0.05525  0.004225  -0.8238  -1.385  4.845  -3.432  -3.432   \n",
       "\n",
       " mdd_pct              mdd_dates dd_3y_pct       dd_3y_timestamps       up_dwn  \\\n",
       "  0.0161  2019-01-22/2019-02-26    0.0161  2019-01-22/2019-02-26  10/19/0.345   \n",
       "\n",
       "      2019  \n",
       "  -0.05588  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "strategy.evaluate_returns(plot = False);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What if we want to add some more metrics to this.  For example, lets say we want to add a metric that looks at how many long trades we had versus short trades.  We can do this using an Evaluator object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-04T00:54:49.557094Z",
     "start_time": "2022-12-04T00:54:49.553865Z"
    },
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Long Trades: 29 Short Trades: 20\n"
     ]
    }
   ],
   "source": [
    "def compute_num_long_trades(trades):\n",
    "    return len([trade for trade in trades if trade.order.reason_code == pq.ReasonCode.ENTER_LONG])\n",
    "\n",
    "def compute_num_short_trades(trades):\n",
    "    return len([trade for trade in trades if trade.order.reason_code == pq.ReasonCode.ENTER_SHORT])\n",
    "\n",
    "evaluator = pq.Evaluator(initial_metrics = {'trades' : strategy.trades()})\n",
    "\n",
    "evaluator.add_metric('num_long_trades', compute_num_long_trades, dependencies = ['trades'])\n",
    "evaluator.add_metric('num_short_trades', compute_num_short_trades, dependencies = ['trades'])\n",
    "\n",
    "evaluator.compute()\n",
    "\n",
    "print('Long Trades: {} Short Trades: {}'.format(evaluator.metric('num_long_trades'), evaluator.metric('num_short_trades')))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-10T21:35:46.005492Z",
     "start_time": "2018-08-10T21:35:46.002539Z"
    }
   },
   "source": [
    "The Evaluator takes care of dependency management so that if you want to compute a metric that relies on other metrics, it will compute the metrics in the right order.\n",
    "\n",
    "Lets compute Maximum Adverse Execution for each trade.  MAE tells you the maximum loss each trade in its lifetime.  It's useful for figuring out where to put trailing stops. For example, if most of your profitable trades had a maximum loss during their life up to 5% but many losing trades had losses of 50% and 60%, it might make sense to place a trailing stop around 6% or 7% so you don't get stopped out of your profitable trades but get out of the losing ones quickly.  See Jaekle and Tomasini, page 66 for details\n",
    "\n",
    "To do this we first have to first pair up entry and exit trades into a single \"round-trip\" trade."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-04T00:54:49.564257Z",
     "start_time": "2022-12-04T00:54:49.558060Z"
    },
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "import collections\n",
    "\n",
    "trades = strategy.trades()\n",
    "\n",
    "entry_trades = [trade for trade in trades if trade.order.reason_code == pq.ReasonCode.ENTER_LONG]\n",
    "exit_trades = [trade for trade in trades if trade.order.reason_code == pq.ReasonCode.EXIT_LONG]\n",
    "\n",
    "def compute_mae(entry_trades, exit_trades, c, timestamps):\n",
    "    mae = np.empty(len(exit_trades)) * np.nan\n",
    "    \n",
    "    round_trip_pnl = np.empty(len(exit_trades)) * np.nan\n",
    "\n",
    "    for i, exit in enumerate(exit_trades):\n",
    "        #if i == len(exit_trades): break\n",
    "        entry = entry_trades[i]\n",
    "        _round_trip_pnl = entry.qty * (exit.price - entry.price)\n",
    "        running_price = c[(timestamps >= entry.timestamp) & (timestamps <= exit.timestamp)]\n",
    "        running_pnl = entry.qty * (running_price - entry.price)\n",
    "        _mae = -1 * min(_round_trip_pnl, np.min(running_pnl))\n",
    "        _mae = _mae / entry.price # Get mae in % terms\n",
    "        _mae = max(0, _mae) # If we have no drawdown for this trade, set it to 0\n",
    "        mae[i] = _mae\n",
    "        round_trip_pnl[i] = _round_trip_pnl / entry.price # Also store round trip pnl for this trade since we will have to plot it\n",
    "    return mae, round_trip_pnl\n",
    "        \n",
    "def get_trades(trades, entry):\n",
    "    rc = [pq.ReasonCode.ENTER_LONG, pq.ReasonCode.ENTER_SHORT] if entry else [pq.ReasonCode.EXIT_LONG, pq.ReasonCode.EXIT_SHORT]\n",
    "    return [trade for trade in trades if trade.order.reason_code in rc]\n",
    "\n",
    "contract_group = strategy.contract_groups[0]\n",
    "\n",
    "evaluator = pq.Evaluator(initial_metrics = {'trades' : strategy.trades(),\n",
    "                                            'c' : strategy.indicator_values[contract_group].c,\n",
    "                                            'timestamps' : strategy.timestamps})\n",
    "evaluator.add_metric('entry_trades', lambda trades : get_trades(trades, True), dependencies=['trades'])\n",
    "evaluator.add_metric('exit_trades', lambda trades : get_trades(trades, False), dependencies=['trades'])\n",
    "evaluator.add_metric('mae', compute_mae, dependencies=['entry_trades', 'exit_trades', 'c', 'timestamps'])\n",
    "\n",
    "evaluator.compute()\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We could have easily done the same computation without using the Evaluator.  The main advantage of using the Evaluator is that you can reuse other metrics you are dependent on without having to recompute them each time, i.e it provides a local cache of metrics."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-10T22:14:20.705460Z",
     "start_time": "2018-08-10T22:14:20.700666Z"
    },
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "source": [
    "## Plotting ##\n",
    "\n",
    "Now that we have maximum adverse execution, lets plot it.\n",
    "\n",
    "pyqstrat plots are built on top of matplotlib.  Each pyqstrat Plot object contains a list of Subplot objects which in turn each contain a list of the data you want to plot in that subplot.\n",
    "\n",
    "In this case, we want to plot round trip PNL for each trade versus its max drawdown (MAE).  The goal is to find out where to set a stop loss so we keep most of the winning trades while limiting our losses from the biggest losing trades.  Since we want to look at the effect of setting a stop loss on both winning and losing trades, we flip the sign of the round trip pnl on the losing trades and mark them in a different color to show the negative P&L."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-04T00:54:49.646134Z",
     "start_time": "2022-12-04T00:54:49.565141Z"
    },
    "run_control": {
     "frozen": false,
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 576x504 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 50400 characters]\n",
      "text/plain": [
       "<Figure size 1080x576 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "mae = evaluator.metric('mae')[0]\n",
    "round_trip_pnl = evaluator.metric('mae')[1]\n",
    "\n",
    "# Separate out positive trades from negative trades\n",
    "round_trip_profit = round_trip_pnl[round_trip_pnl >= 0]\n",
    "mae_profit = mae[round_trip_pnl >= 0]\n",
    "\n",
    "round_trip_loss = round_trip_pnl[round_trip_pnl <= 0]\n",
    "mae_loss = mae[round_trip_pnl <= 0]\n",
    "\n",
    "subplot = pq.Subplot([\n",
    "    pq.XYData('Profitable Trade', mae_profit, round_trip_profit, display_attributes = pq.ScatterPlotAttributes(marker = '^', marker_color = 'green')),\n",
    "    pq.XYData('Losing Trade', mae_loss, -1 * round_trip_loss, display_attributes = pq.ScatterPlotAttributes(marker = 'v', marker_color = 'red'))],\n",
    "    horizontal_lines = [pq.HorizontalLine(y = 0, color = 'black')],\n",
    "    vertical_lines = [pq.VerticalLine(x = 0, color = 'black')],\n",
    "    xlabel = 'Drawdown in %', ylabel = 'Profit / Loss in %')\n",
    "\n",
    "plot = pq.Plot([subplot])\n",
    "plot.draw();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It looks like a good place to put a stop loss so we keep most of the winning trades but don't take big losses might be around 4%.  The next step would be to re-test the system with this stop loss inserted.  Lets plot the same data with a line showing the stop loss."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-12-04T00:54:49.722760Z",
     "start_time": "2022-12-04T00:54:49.648869Z"
    },
    "run_control": {
     "frozen": false,
     "read_only": false
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 55672 characters]\n",
      "text/plain": [
       "<Figure size 1080x576 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "subplot = pq.Subplot([\n",
    "    pq.XYData('Profitable Trade', mae_profit, round_trip_profit, display_attributes = pq.ScatterPlotAttributes(marker = '^', marker_color = 'green')),\n",
    "    pq.XYData('Losing Trade', mae_loss, -1 * round_trip_loss, display_attributes = pq.ScatterPlotAttributes(marker = 'v', marker_color = 'red'))],\n",
    "    horizontal_lines = [pq.HorizontalLine(y = 0, color = 'black')],\n",
    "    vertical_lines = [pq.VerticalLine(x = 0, color = 'black'), pq.VerticalLine(name = 'Stop Loss', x = 4, color = 'blue')],\n",
    "    xlabel = 'Drawdown in %', ylabel = 'Profit / Loss in %')\n",
    "\n",
    "plot = pq.Plot([subplot])\n",
    "plot.draw();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are several different kinds of subplots, including 3d subplots that you can include in the same plot.  See the code of the plot function in the Strategy class or the Evaluator class for examples."
   ]
  }
 ],
 "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.9.13"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
