{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Trading Competition Optimization\n",
    "\n",
    "### Goal: Max return given maximum Sharpe and Drawdown"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from IPython.display import display\n",
    "import Quandl\n",
    "from datetime import datetime, timedelta\n",
    "\n",
    "tickers = ['XOM', 'CVX', 'CLB', 'OXY', 'SLB']\n",
    "market_ticker = 'GOOG/NYSE_VOO'\n",
    "lookback = 30\n",
    "d_col = 'Close'\n",
    "\n",
    "data = {tick: Quandl.get('YAHOO/{}'.format(tick))[-lookback:] for tick in tickers}\n",
    "market = Quandl.get(market_ticker)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Calculating the Return\n",
    "We first want to know how much each ticker returned over the prior period."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'CLB': -0.0016320202164526894,\n",
       " 'CVX': 0.0010319531629488911,\n",
       " 'OXY': 0.00093418904454400551,\n",
       " 'SLB': 0.00098431254720448159,\n",
       " 'XOM': 0.00044165797556096868}"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "returns = {tick: data[tick][d_col].pct_change() for tick in tickers}\n",
    "\n",
    "display({tick: returns[tick].mean() for tick in tickers})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Calculating the Sharpe ratio\n",
    "Sharpe: ${R - R_M \\over \\sigma}$\n",
    "\n",
    "We use the average return over the lookback period, minus the market average return, over the ticker standard deviation to calculate the Sharpe. Shorting a stock turns a negative Sharpe positive."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'CLB': -0.10578734457846127,\n",
       " 'CVX': 0.027303529817677398,\n",
       " 'OXY': 0.022622210057414487,\n",
       " 'SLB': 0.026950946344858676,\n",
       " 'XOM': -0.0053519259698605499}"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "market_returns = market.pct_change()\n",
    "\n",
    "sharpe = lambda ret: (ret.mean() - market_returns[d_col].mean()) / ret.std()\n",
    "sharpes = {tick: sharpe(returns[tick]) for tick in tickers}\n",
    "\n",
    "display(sharpes)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Calculating the drawdown\n",
    "This one is easy - what is the maximum daily change over the lookback period? That is, because we will allow short positions, we are not concerned strictly with maximum downturn, but in general, what is the largest 1-day change?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'CLB': 0.043551495607375035,\n",
       " 'CVX': 0.044894389686214398,\n",
       " 'OXY': 0.051424517867144637,\n",
       " 'SLB': 0.034774627850375328,\n",
       " 'XOM': 0.035851524605672758}"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "drawdown = lambda ret: ret.abs().max()\n",
    "drawdowns = {tick: drawdown(returns[tick]) for tick in tickers}\n",
    "\n",
    "display(drawdowns)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Performing the optimization\n",
    "\n",
    "$\\begin{align}\n",
    "max\\ \\ & \\mu \\cdot \\omega\\\\\n",
    "s.t.\\ \\ & \\vec{1} \\omega = 1\\\\\n",
    "& \\vec{S} \\omega \\ge s\\\\\n",
    "& \\vec{D} \\cdot | \\omega | \\le d\\\\\n",
    "& \\left|\\omega\\right| \\le l\\\\\n",
    "\\end{align}$\n",
    "\n",
    "We want to maximize average return subject to having a full portfolio, Sharpe above a specific level, drawdown below a level, and leverage not too high - that is, don't have huge long/short positions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Optimization terminated successfully.'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\"Holdings: [('XOM', 5.8337945679814904), ('CVX', 42.935064321851307), ('CLB', -124.5), ('OXY', 36.790387773552119), ('SLB', 39.940753336615096)]\""
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'Expected Return: 32.375%'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'Expected Max Drawdown: 4.34%'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "from scipy.optimize import minimize\n",
    "\n",
    "#sharpe_limit = .1\n",
    "drawdown_limit = .05\n",
    "leverage = 250\n",
    "\n",
    "# Use the map so we can guarantee we maintain the correct order\n",
    "# sharpe_a = np.array(list(map(lambda tick: sharpes[tick], tickers))) * -1 # So we can write as upper-bound\n",
    "dd_a = np.array(list(map(lambda tick: drawdowns[tick], tickers)))\n",
    "returns_a = np.array(list(map(lambda tick: returns[tick].mean(), tickers))) # Because minimizing\n",
    "\n",
    "meets_sharpe = lambda x: sum(abs(x) * sharpe_a) - sharpe_limit\n",
    "def meets_dd(x):\n",
    "    portfolio = sum(abs(x))\n",
    "    if portfolio < .1:\n",
    "        # If there are no stocks in the portfolio,\n",
    "        # we can accidentally induce division by 0,\n",
    "        # or division by something small enough to cause infinity\n",
    "        return 0\n",
    "    \n",
    "    return drawdown_limit - sum(abs(x) * dd_a) / sum(abs(x))\n",
    "\n",
    "is_portfolio = lambda x: sum(x) - 1\n",
    "\n",
    "def within_leverage(x):\n",
    "    return leverage - sum(abs(x))\n",
    "\n",
    "objective = lambda x: sum(x * returns_a) * -1 # Because we're minimizing\n",
    "bounds = ((None, None),) * len(tickers)\n",
    "x = np.zeros(len(tickers))\n",
    "\n",
    "constraints = [\n",
    "    {\n",
    "        'type': 'eq',\n",
    "        'fun': is_portfolio\n",
    "    }, {\n",
    "        'type': 'ineq',\n",
    "        'fun': within_leverage\n",
    "    #}, {\n",
    "    #    'type': 'ineq',\n",
    "    #    'fun': meets_sharpe\n",
    "    }, {\n",
    "        'type': 'ineq',\n",
    "        'fun': meets_dd\n",
    "    }\n",
    "]\n",
    "\n",
    "optimal = minimize(objective, x, bounds=bounds, constraints=constraints,\n",
    "                  options={'maxiter': 500})\n",
    "\n",
    "# Optimization time!\n",
    "display(optimal.message)\n",
    "\n",
    "display(\"Holdings: {}\".format(list(zip(tickers, optimal.x))))\n",
    "\n",
    "expected_return = optimal.fun * -100  # multiply by -100 to scale, and compensate for minimizing\n",
    "display(\"Expected Return: {:.3f}%\".format(expected_return))\n",
    "\n",
    "expected_drawdown = sum(abs(optimal.x) * dd_a) / sum(abs(optimal.x)) * 100\n",
    "display(\"Expected Max Drawdown: {0:.2f}%\".format(expected_drawdown))\n",
    "\n",
    "# TODO: Calculate expected Sharpe"
   ]
  }
 ],
 "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.5.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
