{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import dautil as dl\n",
    "import ch7util\n",
    "from scipy.signal import argrelmin\n",
    "from scipy.signal import argrelmax\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "context = dl.nb.Context('calmar_sortino')\n",
    "lr = dl.nb.LatexRenderer(chapter=7, start=4, context=context)\n",
    "lr.render(r'S = \\frac{R-T}{DR}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def calc_sortino(rets):\n",
    "    # Returns below target\n",
    "    semi_var = rets[rets < 0] ** 2\n",
    "    semi_var = semi_var.sum()/len(rets)\n",
    "    sortino = np.sqrt(semi_var)\n",
    "\n",
    "    return rets.mean()/sortino"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def calc_calmar(rets):\n",
    "    # Peaks and bottoms indexes in sequence\n",
    "    mins = np.ravel(argrelmin(rets))\n",
    "    maxs = np.ravel(argrelmax(rets))\n",
    "    extrema = np.concatenate((mins, maxs))\n",
    "    extrema.sort()\n",
    "\n",
    "    return -rets.mean()/np.diff(rets[extrema]).min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ohlc = dl.data.OHLC()\n",
    "dfb = dl.report.DFBuilder(cols=['Ticker', 'Sortino', 'Calmar'])\n",
    "\n",
    "for symbol in ch7util.STOCKS:\n",
    "    stock = ohlc.get(symbol)\n",
    "    rets = ch7util.log_rets(stock['Adj Close'])\n",
    "    sortino = calc_sortino(rets)\n",
    "    calmar = calc_calmar(rets)\n",
    "    dfb.row([symbol, sortino, calmar])\n",
    "\n",
    "df = dfb.build(index=ch7util.STOCKS).dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "_, ax = plt.subplots()\n",
    "ax.scatter(df['Sortino'], df['Calmar'])\n",
    "dl.plotting.plot_polyfit(ax, df['Sortino'], df['Calmar'])\n",
    "dl.plotting.plot_text(ax, df['Sortino'], df['Calmar'], ch7util.STOCKS)\n",
    "ax.set_xlabel('Sortino')\n",
    "ax.set_ylabel('Calmar')\n",
    "ax.set_title('Sortino & Calmar Ratios')"
   ]
  }
 ],
 "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
