Created
March 2, 2016 17:53
-
-
Save twiecki/23834f6ce30595ebe426 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import pandas as pd\n", | |
| "import numpy as np" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>Aidan</th>\n", | |
| " <th>Archelon</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>100</td>\n", | |
| " <td>NaN</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>110</td>\n", | |
| " <td>NaN</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>105</td>\n", | |
| " <td>100</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>95</td>\n", | |
| " <td>95</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td>98</td>\n", | |
| " <td>80</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>5</th>\n", | |
| " <td>110</td>\n", | |
| " <td>110</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " Aidan Archelon\n", | |
| "0 100 NaN\n", | |
| "1 110 NaN\n", | |
| "2 105 100\n", | |
| "3 95 95\n", | |
| "4 98 80\n", | |
| "5 110 110" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "account_value = pd.DataFrame({'Aidan': [100, 110, 105, 95, 98, 110], \n", | |
| " 'Archelon': [np.nan, np.nan, 100, 95, 80, 110]})\n", | |
| "account_value" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>Aidan</th>\n", | |
| " <th>Archelon</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>NaN</td>\n", | |
| " <td>NaN</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>0.100000</td>\n", | |
| " <td>NaN</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>-0.045455</td>\n", | |
| " <td>NaN</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>-0.095238</td>\n", | |
| " <td>-0.050000</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td>0.031579</td>\n", | |
| " <td>-0.157895</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>5</th>\n", | |
| " <td>0.122449</td>\n", | |
| " <td>0.375000</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " Aidan Archelon\n", | |
| "0 NaN NaN\n", | |
| "1 0.100000 NaN\n", | |
| "2 -0.045455 NaN\n", | |
| "3 -0.095238 -0.050000\n", | |
| "4 0.031579 -0.157895\n", | |
| "5 0.122449 0.375000" | |
| ] | |
| }, | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "account_returns = account_value.pct_change()\n", | |
| "account_returns" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "0 NaN\n", | |
| "1 0.100000\n", | |
| "2 -0.045455\n", | |
| "3 -0.072619\n", | |
| "4 -0.063158\n", | |
| "5 0.248724\n", | |
| "dtype: float64" | |
| ] | |
| }, | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "portfolio_returns = account_returns.mean(axis='columns')\n", | |
| "portfolio_returns" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 13, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "0.1391489158163266" | |
| ] | |
| }, | |
| "execution_count": 13, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "cum_portfolio_returns = (1 + portfolio_returns).prod() - 1\n", | |
| "cum_portfolio_returns" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 15, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>Aidan</th>\n", | |
| " <th>Archelon</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>2.0</td>\n", | |
| " <td>-2.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>-3.0</td>\n", | |
| " <td>-2.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>4.0</td>\n", | |
| " <td>1.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>0.5</td>\n", | |
| " <td>0.5</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td>1.2</td>\n", | |
| " <td>0.5</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>5</th>\n", | |
| " <td>-3.0</td>\n", | |
| " <td>-3.0</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " Aidan Archelon\n", | |
| "0 2.0 -2.0\n", | |
| "1 -3.0 -2.0\n", | |
| "2 4.0 1.0\n", | |
| "3 0.5 0.5\n", | |
| "4 1.2 0.5\n", | |
| "5 -3.0 -3.0" | |
| ] | |
| }, | |
| "execution_count": 15, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "minutely_pnl = pd.DataFrame({'Aidan': [2, -3, 4., 0.5, 1.2, -3], \n", | |
| " 'Archelon': [-2, -2, 1., 0.5, 0.5, -3]})\n", | |
| "minutely_pnl" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 24, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "0.0\n", | |
| "-0.00316247535946\n", | |
| "0.0\n", | |
| "0.000632495071892\n", | |
| "0.00170773669411\n", | |
| "-0.00208723373724\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# then this should happen every minute\n", | |
| "daily_pnl = pd.Series({'Aidan': 0, 'Archelon': 0})\n", | |
| "for minute, row in minutely_pnl.iterrows():\n", | |
| " # Start counting the cumulative pnl for this day\n", | |
| " daily_pnl += row\n", | |
| " \n", | |
| " # Calculate current daily return\n", | |
| " daily_return = daily_pnl / account_value.iloc[-1] \n", | |
| " daily_portfolio_return = daily_return.mean()\n", | |
| " daily_total_cumulative_return = cum_portfolio_returns * daily_portfolio_return\n", | |
| " print(\"Cumulative portfolio return on current day on minute {mcumudaily_total_cumulative_return)\n", | |
| " \n", | |
| "assert np.all(daily_return == minutely_pnl.sum() / account_value.iloc[-1])" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 2", | |
| "language": "python", | |
| "name": "python2" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 2 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython2", | |
| "version": "2.7.11" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment