{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:13:44.393089Z",
"start_time": "2018-10-06T17:13:44.379164Z"
}
},
"outputs": [],
"source": [
"#Imports\n",
"%matplotlib notebook\n",
"import latex\n",
"import pandas as pd\n",
"import numpy as np\n",
"from typing import Dict, List\n",
"from collections import OrderedDict\n",
"import datetime as datetime\n",
"from pandas.tseries.offsets import *\n",
"from IPython.core.debugger import set_trace\n",
"import matplotlib\n",
"matplotlib.style.use('seaborn-darkgrid')\n",
"#import pixiedust\n",
"#ctrl-shift-p for the command palette\n",
"pd.set_option(\"max_colwidth\", 100)\n",
"pd.set_option(\"display.max_rows\", 100000)\n",
"pd.set_option(\"display.max_columns\", 1000)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:13:44.416102Z",
"start_time": "2018-10-06T17:13:44.395083Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" \n",
" Date \n",
" Adj Close \n",
" \n",
" \n",
" \n",
" \n",
" 0 \n",
" 2002-07-30 \n",
" 48.837349 \n",
" \n",
" \n",
" 1 \n",
" 2002-07-31 \n",
" 49.285275 \n",
" \n",
" \n",
" 2 \n",
" 2002-08-01 \n",
" 49.488335 \n",
" \n",
" \n",
" 3 \n",
" 2002-08-02 \n",
" 49.870586 \n",
" \n",
" \n",
" 4 \n",
" 2002-08-05 \n",
" 50.121433 \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Date Adj Close\n",
"0 2002-07-30 48.837349\n",
"1 2002-07-31 49.285275\n",
"2 2002-08-01 49.488335\n",
"3 2002-08-02 49.870586\n",
"4 2002-08-05 50.121433"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bonds_file='../data/Options/IEF.csv'\n",
"#bonds_file='../data/Options/TLT.csv'\n",
"#bonds_file='../data/Options/SHY.csv'\n",
"\n",
"bond = pd.read_csv(\n",
" bonds_file, header=0, parse_dates=[\"Date\"], usecols=['Date','Adj Close'])\n",
"bond.head()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:13:44.421014Z",
"start_time": "2018-10-06T17:13:44.417063Z"
}
},
"outputs": [],
"source": [
"bond.rename(columns={\"Adj Close\": \"bond\"},inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:13:44.492859Z",
"start_time": "2018-10-06T17:13:44.423013Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" \n",
" bond \n",
" \n",
" \n",
" Date \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" 2006-02-27 \n",
" 58.414406 \n",
" \n",
" \n",
" 2006-02-28 \n",
" 58.647667 \n",
" \n",
" \n",
" 2006-03-01 \n",
" 58.532089 \n",
" \n",
" \n",
" 2006-03-02 \n",
" 58.298084 \n",
" \n",
" \n",
" 2006-03-03 \n",
" 58.156284 \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
" bond\n",
"Date \n",
"2006-02-27 58.414406\n",
"2006-02-28 58.647667\n",
"2006-03-01 58.532089\n",
"2006-03-02 58.298084\n",
"2006-03-03 58.156284"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bond=bond[(bond.Date>='2006-02-27')]\n",
"bond.index=bond.Date\n",
"bond.drop(columns=['Date'],inplace=True)\n",
"bond.head()\n"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:13:44.526730Z",
"start_time": "2018-10-06T17:13:44.494898Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" \n",
" Date \n",
" i \n",
" quote_date \n",
" expiration \n",
" strike \n",
" trade_volume \n",
" price \n",
" newMonth \n",
" targetExpiration \n",
" ExpDiff \n",
" mid_underlying \n",
" futures \n",
" strikeTarget \n",
" strikeLocation \n",
" StrikeDiff \n",
" entry \n",
" exit \n",
" \n",
" \n",
" \n",
" \n",
" 0 \n",
" 2006-04-19 \n",
" 714 \n",
" 2006-04-19 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 9.90 \n",
" 2006-04-19 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.32 \n",
" 22.5 \n",
" 11.32 \n",
" 714 \n",
" 11.18 \n",
" False \n",
" False \n",
" \n",
" \n",
" 1 \n",
" 2006-04-20 \n",
" 721 \n",
" 2006-04-20 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 12.55 \n",
" 2006-04-19 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.64 \n",
" 12.5 \n",
" 11.32 \n",
" 714 \n",
" 11.18 \n",
" False \n",
" False \n",
" \n",
" \n",
" 2 \n",
" 2006-04-21 \n",
" 729 \n",
" 2006-04-21 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 12.35 \n",
" 2006-04-19 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.59 \n",
" 12.5 \n",
" 11.32 \n",
" 714 \n",
" 11.18 \n",
" False \n",
" False \n",
" \n",
" \n",
" 3 \n",
" 2006-04-24 \n",
" 768 \n",
" 2006-04-24 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 20.0 \n",
" 12.45 \n",
" 2006-04-19 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.75 \n",
" 12.5 \n",
" 11.32 \n",
" 714 \n",
" 11.18 \n",
" False \n",
" False \n",
" \n",
" \n",
" 4 \n",
" 2006-04-25 \n",
" 807 \n",
" 2006-04-25 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 9.90 \n",
" 2006-04-19 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.76 \n",
" 12.5 \n",
" 11.32 \n",
" 714 \n",
" 11.18 \n",
" False \n",
" False \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Date i quote_date expiration strike trade_volume price \\\n",
"0 2006-04-19 714 2006-04-19 2006-05-17 22.5 0.0 9.90 \n",
"1 2006-04-20 721 2006-04-20 2006-05-17 22.5 0.0 12.55 \n",
"2 2006-04-21 729 2006-04-21 2006-05-17 22.5 0.0 12.35 \n",
"3 2006-04-24 768 2006-04-24 2006-05-17 22.5 20.0 12.45 \n",
"4 2006-04-25 807 2006-04-25 2006-05-17 22.5 0.0 9.90 \n",
"\n",
" newMonth targetExpiration ExpDiff mid_underlying \\\n",
"0 2006-04-19 2006-11-05 172 days 00:00:00.000000000 11.32 \n",
"1 2006-04-19 2006-11-05 172 days 00:00:00.000000000 11.64 \n",
"2 2006-04-19 2006-11-05 172 days 00:00:00.000000000 11.59 \n",
"3 2006-04-19 2006-11-05 172 days 00:00:00.000000000 11.75 \n",
"4 2006-04-19 2006-11-05 172 days 00:00:00.000000000 11.76 \n",
"\n",
" futures strikeTarget strikeLocation StrikeDiff entry exit \n",
"0 22.5 11.32 714 11.18 False False \n",
"1 12.5 11.32 714 11.18 False False \n",
"2 12.5 11.32 714 11.18 False False \n",
"3 12.5 11.32 714 11.18 False False \n",
"4 12.5 11.32 714 11.18 False False "
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#puts_file='../data/Options/vxxRolling_puts.csv'\n",
"puts_file = '../data/Options/vix_rolling_puts.csv'\n",
"\n",
"puts = pd.read_csv(\n",
" puts_file,\n",
" header=0,\n",
" parse_dates=[\n",
" \"quote_date\",\n",
" \"expiration\",\n",
" \"newMonth\",\n",
" \"targetExpiration\",\n",
" ])\n",
"puts.head()"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:13:44.625466Z",
"start_time": "2018-10-06T17:13:44.528725Z"
},
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" \n",
" Date \n",
" i \n",
" quote_date \n",
" expiration \n",
" strike \n",
" trade_volume \n",
" price \n",
" newMonth \n",
" targetExpiration \n",
" ExpDiff \n",
" mid_underlying \n",
" futures \n",
" strikeTarget \n",
" strikeLocation \n",
" StrikeDiff \n",
" entry \n",
" exit \n",
" \n",
" \n",
" \n",
" \n",
" 0 \n",
" 2006-04-19 \n",
" 714 \n",
" 2006-04-19 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 9.90 \n",
" 2006-04-19 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.32 \n",
" 22.5 \n",
" 11.32 \n",
" 714 \n",
" 11.18 \n",
" False \n",
" False \n",
" \n",
" \n",
" 1 \n",
" 2006-04-20 \n",
" 721 \n",
" 2006-04-20 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 12.55 \n",
" 2006-04-19 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.64 \n",
" 12.5 \n",
" 11.32 \n",
" 714 \n",
" 11.18 \n",
" False \n",
" False \n",
" \n",
" \n",
" 2 \n",
" 2006-04-21 \n",
" 729 \n",
" 2006-04-21 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 12.35 \n",
" 2006-04-19 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.59 \n",
" 12.5 \n",
" 11.32 \n",
" 714 \n",
" 11.18 \n",
" False \n",
" False \n",
" \n",
" \n",
" 3 \n",
" 2006-04-24 \n",
" 768 \n",
" 2006-04-24 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 20.0 \n",
" 12.45 \n",
" 2006-04-19 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.75 \n",
" 12.5 \n",
" 11.32 \n",
" 714 \n",
" 11.18 \n",
" False \n",
" False \n",
" \n",
" \n",
" 4 \n",
" 2006-04-25 \n",
" 807 \n",
" 2006-04-25 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 9.90 \n",
" 2006-04-19 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.76 \n",
" 12.5 \n",
" 11.32 \n",
" 714 \n",
" 11.18 \n",
" False \n",
" False \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Date i quote_date expiration strike trade_volume price \\\n",
"0 2006-04-19 714 2006-04-19 2006-05-17 22.5 0.0 9.90 \n",
"1 2006-04-20 721 2006-04-20 2006-05-17 22.5 0.0 12.55 \n",
"2 2006-04-21 729 2006-04-21 2006-05-17 22.5 0.0 12.35 \n",
"3 2006-04-24 768 2006-04-24 2006-05-17 22.5 20.0 12.45 \n",
"4 2006-04-25 807 2006-04-25 2006-05-17 22.5 0.0 9.90 \n",
"\n",
" newMonth targetExpiration ExpDiff mid_underlying \\\n",
"0 2006-04-19 2006-11-05 172 days 00:00:00.000000000 11.32 \n",
"1 2006-04-19 2006-11-05 172 days 00:00:00.000000000 11.64 \n",
"2 2006-04-19 2006-11-05 172 days 00:00:00.000000000 11.59 \n",
"3 2006-04-19 2006-11-05 172 days 00:00:00.000000000 11.75 \n",
"4 2006-04-19 2006-11-05 172 days 00:00:00.000000000 11.76 \n",
"\n",
" futures strikeTarget strikeLocation StrikeDiff entry exit \n",
"0 22.5 11.32 714 11.18 False False \n",
"1 12.5 11.32 714 11.18 False False \n",
"2 12.5 11.32 714 11.18 False False \n",
"3 12.5 11.32 714 11.18 False False \n",
"4 12.5 11.32 714 11.18 False False "
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"filter1=puts['entry'].isin([False]) \n",
"filter2=puts['exit'].isin([False]) \n",
"dealing_days=puts[(filter1)| (filter2)]\n",
"\n",
"dealing_days.to_csv('../data/Options/strikes.csv')\n",
"dealing_days.head()"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:13:44.659375Z",
"start_time": "2018-10-06T17:13:44.627460Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" \n",
" Date \n",
" i \n",
" quote_date \n",
" expiration \n",
" strike \n",
" trade_volume \n",
" price \n",
" newMonth \n",
" targetExpiration \n",
" ExpDiff \n",
" mid_underlying \n",
" futures \n",
" strikeTarget \n",
" strikeLocation \n",
" StrikeDiff \n",
" entry \n",
" exit \n",
" \n",
" \n",
" \n",
" \n",
" 0 \n",
" 2006-04-19 \n",
" 714 \n",
" 2006-04-19 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 0.075 \n",
" 2006-04-19 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.32 \n",
" 22.5 \n",
" 22.64 \n",
" 714 \n",
" 0.14 \n",
" False \n",
" False \n",
" \n",
" \n",
" 1 \n",
" 2006-04-20 \n",
" 721 \n",
" 2006-04-20 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 0.075 \n",
" 2006-04-19 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.64 \n",
" 12.5 \n",
" 22.64 \n",
" 714 \n",
" 0.14 \n",
" False \n",
" False \n",
" \n",
" \n",
" 2 \n",
" 2006-04-21 \n",
" 729 \n",
" 2006-04-21 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 0.075 \n",
" 2006-04-19 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.59 \n",
" 12.5 \n",
" 22.64 \n",
" 714 \n",
" 0.14 \n",
" False \n",
" False \n",
" \n",
" \n",
" 3 \n",
" 2006-04-24 \n",
" 768 \n",
" 2006-04-24 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 0.075 \n",
" 2006-04-19 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.75 \n",
" 12.5 \n",
" 22.64 \n",
" 714 \n",
" 0.14 \n",
" False \n",
" False \n",
" \n",
" \n",
" 4 \n",
" 2006-04-25 \n",
" 807 \n",
" 2006-04-25 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 0.050 \n",
" 2006-04-19 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.76 \n",
" 12.5 \n",
" 22.64 \n",
" 714 \n",
" 0.14 \n",
" False \n",
" False \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Date i quote_date expiration strike trade_volume price \\\n",
"0 2006-04-19 714 2006-04-19 2006-05-17 22.5 0.0 0.075 \n",
"1 2006-04-20 721 2006-04-20 2006-05-17 22.5 0.0 0.075 \n",
"2 2006-04-21 729 2006-04-21 2006-05-17 22.5 0.0 0.075 \n",
"3 2006-04-24 768 2006-04-24 2006-05-17 22.5 0.0 0.075 \n",
"4 2006-04-25 807 2006-04-25 2006-05-17 22.5 0.0 0.050 \n",
"\n",
" newMonth targetExpiration ExpDiff mid_underlying \\\n",
"0 2006-04-19 2006-11-05 172 days 00:00:00.000000000 11.32 \n",
"1 2006-04-19 2006-11-05 172 days 00:00:00.000000000 11.64 \n",
"2 2006-04-19 2006-11-05 172 days 00:00:00.000000000 11.59 \n",
"3 2006-04-19 2006-11-05 172 days 00:00:00.000000000 11.75 \n",
"4 2006-04-19 2006-11-05 172 days 00:00:00.000000000 11.76 \n",
"\n",
" futures strikeTarget strikeLocation StrikeDiff entry exit \n",
"0 22.5 22.64 714 0.14 False False \n",
"1 12.5 22.64 714 0.14 False False \n",
"2 12.5 22.64 714 0.14 False False \n",
"3 12.5 22.64 714 0.14 False False \n",
"4 12.5 22.64 714 0.14 False False "
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#calls_file='../data/Options/vxxRolling_calls.csv'\n",
"calls_file='../data/Options/vix_rolling_calls.csv'\n",
"\n",
"calls = pd.read_csv(\n",
" calls_file,\n",
" header=0,\n",
" parse_dates=[\n",
" \"quote_date\",\n",
" \"expiration\",\n",
" \"newMonth\",\n",
" \"targetExpiration\",\n",
" ])\n",
"calls.head()"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:13:44.679322Z",
"start_time": "2018-10-06T17:13:44.661370Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" \n",
" put_quote_date \n",
" put_expiration \n",
" put_strike \n",
" put_trade_volume \n",
" put_price \n",
" put_targetExpiration \n",
" put_ExpDiff \n",
" put_mid_underlying \n",
" futures \n",
" put_entry \n",
" put_exit \n",
" \n",
" \n",
" put_quote_date \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" 2006-04-19 \n",
" 2006-04-19 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 9.90 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.32 \n",
" 22.5 \n",
" False \n",
" False \n",
" \n",
" \n",
" 2006-04-20 \n",
" 2006-04-20 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 12.55 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.64 \n",
" 12.5 \n",
" False \n",
" False \n",
" \n",
" \n",
" 2006-04-21 \n",
" 2006-04-21 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 12.35 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.59 \n",
" 12.5 \n",
" False \n",
" False \n",
" \n",
" \n",
" 2006-04-24 \n",
" 2006-04-24 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 20.0 \n",
" 12.45 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.75 \n",
" 12.5 \n",
" False \n",
" False \n",
" \n",
" \n",
" 2006-04-25 \n",
" 2006-04-25 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 9.90 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.76 \n",
" 12.5 \n",
" False \n",
" False \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
" put_quote_date put_expiration put_strike put_trade_volume \\\n",
"put_quote_date \n",
"2006-04-19 2006-04-19 2006-05-17 22.5 0.0 \n",
"2006-04-20 2006-04-20 2006-05-17 22.5 0.0 \n",
"2006-04-21 2006-04-21 2006-05-17 22.5 0.0 \n",
"2006-04-24 2006-04-24 2006-05-17 22.5 20.0 \n",
"2006-04-25 2006-04-25 2006-05-17 22.5 0.0 \n",
"\n",
" put_price put_targetExpiration put_ExpDiff \\\n",
"put_quote_date \n",
"2006-04-19 9.90 2006-11-05 172 days 00:00:00.000000000 \n",
"2006-04-20 12.55 2006-11-05 172 days 00:00:00.000000000 \n",
"2006-04-21 12.35 2006-11-05 172 days 00:00:00.000000000 \n",
"2006-04-24 12.45 2006-11-05 172 days 00:00:00.000000000 \n",
"2006-04-25 9.90 2006-11-05 172 days 00:00:00.000000000 \n",
"\n",
" put_mid_underlying futures put_entry put_exit \n",
"put_quote_date \n",
"2006-04-19 11.32 22.5 False False \n",
"2006-04-20 11.64 12.5 False False \n",
"2006-04-21 11.59 12.5 False False \n",
"2006-04-24 11.75 12.5 False False \n",
"2006-04-25 11.76 12.5 False False "
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"put_prices = puts.filter(\n",
" [\n",
" 'quote_date', 'expiration', 'strike', 'trade_volume', 'price',\n",
" 'targetExpiration', 'ExpDiff', 'mid_underlying','futures','entry', 'exit'\n",
" ],\n",
" axis=1)\n",
"put_prices.rename(\n",
" columns={\n",
" 'quote_date': 'put_quote_date',\n",
" 'expiration': 'put_expiration',\n",
" 'strike': 'put_strike',\n",
" 'trade_volume': 'put_trade_volume',\n",
" 'price': 'put_price',\n",
" 'targetExpiration': 'put_targetExpiration',\n",
" 'ExpDiff': 'put_ExpDiff',\n",
" 'mid_underlying': 'put_mid_underlying',\n",
" 'futures':'futures',\n",
" 'entry': 'put_entry',\n",
" 'exit': 'put_exit'\n",
" },\n",
" inplace=True)\n",
"put_prices.index = put_prices.put_quote_date\n",
"put_prices.head()"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:13:44.699269Z",
"start_time": "2018-10-06T17:13:44.680319Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" \n",
" call_quote_date \n",
" call_expiration \n",
" call_strike \n",
" call_trade_volume \n",
" call_price \n",
" call_targetExpiration \n",
" call_ExpDiff \n",
" call_mid_underlying \n",
" futures \n",
" call_entry \n",
" call_exit \n",
" \n",
" \n",
" call_quote_date \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" 2006-04-19 \n",
" 2006-04-19 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 0.075 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.32 \n",
" 22.5 \n",
" False \n",
" False \n",
" \n",
" \n",
" 2006-04-20 \n",
" 2006-04-20 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 0.075 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.64 \n",
" 12.5 \n",
" False \n",
" False \n",
" \n",
" \n",
" 2006-04-21 \n",
" 2006-04-21 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 0.075 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.59 \n",
" 12.5 \n",
" False \n",
" False \n",
" \n",
" \n",
" 2006-04-24 \n",
" 2006-04-24 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 0.075 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.75 \n",
" 12.5 \n",
" False \n",
" False \n",
" \n",
" \n",
" 2006-04-25 \n",
" 2006-04-25 \n",
" 2006-05-17 \n",
" 22.5 \n",
" 0.0 \n",
" 0.050 \n",
" 2006-11-05 \n",
" 172 days 00:00:00.000000000 \n",
" 11.76 \n",
" 12.5 \n",
" False \n",
" False \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
" call_quote_date call_expiration call_strike \\\n",
"call_quote_date \n",
"2006-04-19 2006-04-19 2006-05-17 22.5 \n",
"2006-04-20 2006-04-20 2006-05-17 22.5 \n",
"2006-04-21 2006-04-21 2006-05-17 22.5 \n",
"2006-04-24 2006-04-24 2006-05-17 22.5 \n",
"2006-04-25 2006-04-25 2006-05-17 22.5 \n",
"\n",
" call_trade_volume call_price call_targetExpiration \\\n",
"call_quote_date \n",
"2006-04-19 0.0 0.075 2006-11-05 \n",
"2006-04-20 0.0 0.075 2006-11-05 \n",
"2006-04-21 0.0 0.075 2006-11-05 \n",
"2006-04-24 0.0 0.075 2006-11-05 \n",
"2006-04-25 0.0 0.050 2006-11-05 \n",
"\n",
" call_ExpDiff call_mid_underlying futures \\\n",
"call_quote_date \n",
"2006-04-19 172 days 00:00:00.000000000 11.32 22.5 \n",
"2006-04-20 172 days 00:00:00.000000000 11.64 12.5 \n",
"2006-04-21 172 days 00:00:00.000000000 11.59 12.5 \n",
"2006-04-24 172 days 00:00:00.000000000 11.75 12.5 \n",
"2006-04-25 172 days 00:00:00.000000000 11.76 12.5 \n",
"\n",
" call_entry call_exit \n",
"call_quote_date \n",
"2006-04-19 False False \n",
"2006-04-20 False False \n",
"2006-04-21 False False \n",
"2006-04-24 False False \n",
"2006-04-25 False False "
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"call_prices = calls.filter(\n",
" [\n",
" 'quote_date', 'expiration', 'strike', 'trade_volume', 'price',\n",
" 'targetExpiration', 'ExpDiff', 'mid_underlying', 'futures','entry', 'exit'\n",
" ],\n",
" axis=1)\n",
"call_prices.rename(\n",
" columns={\n",
" 'quote_date': 'call_quote_date',\n",
" 'expiration': 'call_expiration',\n",
" 'strike': 'call_strike',\n",
" 'trade_volume': 'call_trade_volume',\n",
" 'price': 'call_price',\n",
" 'targetExpiration': 'call_targetExpiration',\n",
" 'ExpDiff': 'call_ExpDiff',\n",
" 'mid_underlying': 'call_mid_underlying',\n",
" 'futures': 'futures',\n",
" 'entry': 'call_entry',\n",
" 'exit': 'call_exit'\n",
" },\n",
" inplace=True)\n",
"call_prices.index = call_prices.call_quote_date\n",
"call_prices.head()"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:13:44.854853Z",
"start_time": "2018-10-06T17:13:44.701263Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" \n",
" put_quote_date \n",
" put_expiration \n",
" put_strike \n",
" put_trade_volume \n",
" put_price \n",
" put_targetExpiration \n",
" put_ExpDiff \n",
" put_mid_underlying \n",
" futures \n",
" put_entry \n",
" put_exit \n",
" call_quote_date \n",
" call_expiration \n",
" call_strike \n",
" call_trade_volume \n",
" call_price \n",
" call_targetExpiration \n",
" call_ExpDiff \n",
" call_mid_underlying \n",
" futures \n",
" call_entry \n",
" call_exit \n",
" bond \n",
" \n",
" \n",
" \n",
" \n",
" 2006-02-27 \n",
" NaT \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaT \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" 58.414406 \n",
" \n",
" \n",
" 2006-02-28 \n",
" NaT \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaT \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" 58.647667 \n",
" \n",
" \n",
" 2006-03-01 \n",
" NaT \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaT \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" 58.532089 \n",
" \n",
" \n",
" 2006-03-02 \n",
" NaT \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaT \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" 58.298084 \n",
" \n",
" \n",
" 2006-03-03 \n",
" NaT \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaT \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaT \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" NaN \n",
" 58.156284 \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
" put_quote_date put_expiration put_strike put_trade_volume \\\n",
"2006-02-27 NaT NaT NaN NaN \n",
"2006-02-28 NaT NaT NaN NaN \n",
"2006-03-01 NaT NaT NaN NaN \n",
"2006-03-02 NaT NaT NaN NaN \n",
"2006-03-03 NaT NaT NaN NaN \n",
"\n",
" put_price put_targetExpiration put_ExpDiff put_mid_underlying \\\n",
"2006-02-27 NaN NaT NaN NaN \n",
"2006-02-28 NaN NaT NaN NaN \n",
"2006-03-01 NaN NaT NaN NaN \n",
"2006-03-02 NaN NaT NaN NaN \n",
"2006-03-03 NaN NaT NaN NaN \n",
"\n",
" futures put_entry put_exit call_quote_date call_expiration \\\n",
"2006-02-27 NaN NaN NaN NaT NaT \n",
"2006-02-28 NaN NaN NaN NaT NaT \n",
"2006-03-01 NaN NaN NaN NaT NaT \n",
"2006-03-02 NaN NaN NaN NaT NaT \n",
"2006-03-03 NaN NaN NaN NaT NaT \n",
"\n",
" call_strike call_trade_volume call_price call_targetExpiration \\\n",
"2006-02-27 NaN NaN NaN NaT \n",
"2006-02-28 NaN NaN NaN NaT \n",
"2006-03-01 NaN NaN NaN NaT \n",
"2006-03-02 NaN NaN NaN NaT \n",
"2006-03-03 NaN NaN NaN NaT \n",
"\n",
" call_ExpDiff call_mid_underlying futures call_entry call_exit \\\n",
"2006-02-27 NaN NaN NaN NaN NaN \n",
"2006-02-28 NaN NaN NaN NaN NaN \n",
"2006-03-01 NaN NaN NaN NaN NaN \n",
"2006-03-02 NaN NaN NaN NaN NaN \n",
"2006-03-03 NaN NaN NaN NaN NaN \n",
"\n",
" bond \n",
"2006-02-27 58.414406 \n",
"2006-02-28 58.647667 \n",
"2006-03-01 58.532089 \n",
"2006-03-02 58.298084 \n",
"2006-03-03 58.156284 "
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"combined = pd.concat([put_prices, call_prices,bond], axis=1, sort=False)\n",
"combined.to_csv('../data/Options/combined1.csv')\n",
"combined.head() #.fillna(method='ffill',inplace=True)\n"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:13:44.863829Z",
"start_time": "2018-10-06T17:13:44.855873Z"
}
},
"outputs": [],
"source": [
"combined.fillna(method='ffill',inplace=True)\n",
"#combined"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:13:44.871808Z",
"start_time": "2018-10-06T17:13:44.864847Z"
}
},
"outputs": [],
"source": [
"mask = combined['call_price'].isnull()\n",
"#print(combined.loc[mask].tail())"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:13:44.882778Z",
"start_time": "2018-10-06T17:13:44.872819Z"
}
},
"outputs": [],
"source": [
"mask = combined['put_price'].isnull()\n",
"#print(combined.loc[mask].tail())"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:13:45.053322Z",
"start_time": "2018-10-06T17:13:44.883776Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" \n",
" put_quote_date \n",
" put_expiration \n",
" put_strike \n",
" put_trade_volume \n",
" put_price \n",
" put_targetExpiration \n",
" put_ExpDiff \n",
" put_mid_underlying \n",
" futures \n",
" put_entry \n",
" put_exit \n",
" call_quote_date \n",
" call_expiration \n",
" call_strike \n",
" call_trade_volume \n",
" call_price \n",
" call_targetExpiration \n",
" call_ExpDiff \n",
" call_mid_underlying \n",
" futures \n",
" call_entry \n",
" call_exit \n",
" bond \n",
" \n",
" \n",
" \n",
" \n",
" 2018-09-17 \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 12.0 \n",
" 0.0 \n",
" 0.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 16.0 \n",
" False \n",
" False \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 24.0 \n",
" 0.0 \n",
" 1.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 16.0 \n",
" False \n",
" False \n",
" 101.540001 \n",
" \n",
" \n",
" 2018-09-18 \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 12.0 \n",
" 0.0 \n",
" 0.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 16.0 \n",
" False \n",
" False \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 24.0 \n",
" 0.0 \n",
" 1.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 16.0 \n",
" False \n",
" False \n",
" 101.120003 \n",
" \n",
" \n",
" 2018-09-19 \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 12.0 \n",
" 0.0 \n",
" 0.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 16.0 \n",
" False \n",
" False \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 24.0 \n",
" 0.0 \n",
" 1.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 16.0 \n",
" False \n",
" False \n",
" 100.949997 \n",
" \n",
" \n",
" 2018-09-20 \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 12.0 \n",
" 0.0 \n",
" 0.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 16.0 \n",
" False \n",
" False \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 24.0 \n",
" 0.0 \n",
" 1.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 16.0 \n",
" False \n",
" False \n",
" 100.959999 \n",
" \n",
" \n",
" 2018-09-21 \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 12.0 \n",
" 0.0 \n",
" 0.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 16.0 \n",
" False \n",
" False \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 24.0 \n",
" 0.0 \n",
" 1.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 16.0 \n",
" False \n",
" False \n",
" 101.000000 \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
" put_quote_date put_expiration put_strike put_trade_volume \\\n",
"2018-09-17 2018-09-05 2019-01-16 12.0 0.0 \n",
"2018-09-18 2018-09-05 2019-01-16 12.0 0.0 \n",
"2018-09-19 2018-09-05 2019-01-16 12.0 0.0 \n",
"2018-09-20 2018-09-05 2019-01-16 12.0 0.0 \n",
"2018-09-21 2018-09-05 2019-01-16 12.0 0.0 \n",
"\n",
" put_price put_targetExpiration put_ExpDiff \\\n",
"2018-09-17 0.3 2019-03-10 53 days 00:00:00.000000000 \n",
"2018-09-18 0.3 2019-03-10 53 days 00:00:00.000000000 \n",
"2018-09-19 0.3 2019-03-10 53 days 00:00:00.000000000 \n",
"2018-09-20 0.3 2019-03-10 53 days 00:00:00.000000000 \n",
"2018-09-21 0.3 2019-03-10 53 days 00:00:00.000000000 \n",
"\n",
" put_mid_underlying futures put_entry put_exit call_quote_date \\\n",
"2018-09-17 13.91 16.0 False False 2018-09-05 \n",
"2018-09-18 13.91 16.0 False False 2018-09-05 \n",
"2018-09-19 13.91 16.0 False False 2018-09-05 \n",
"2018-09-20 13.91 16.0 False False 2018-09-05 \n",
"2018-09-21 13.91 16.0 False False 2018-09-05 \n",
"\n",
" call_expiration call_strike call_trade_volume call_price \\\n",
"2018-09-17 2019-01-16 24.0 0.0 1.3 \n",
"2018-09-18 2019-01-16 24.0 0.0 1.3 \n",
"2018-09-19 2019-01-16 24.0 0.0 1.3 \n",
"2018-09-20 2019-01-16 24.0 0.0 1.3 \n",
"2018-09-21 2019-01-16 24.0 0.0 1.3 \n",
"\n",
" call_targetExpiration call_ExpDiff \\\n",
"2018-09-17 2019-03-10 53 days 00:00:00.000000000 \n",
"2018-09-18 2019-03-10 53 days 00:00:00.000000000 \n",
"2018-09-19 2019-03-10 53 days 00:00:00.000000000 \n",
"2018-09-20 2019-03-10 53 days 00:00:00.000000000 \n",
"2018-09-21 2019-03-10 53 days 00:00:00.000000000 \n",
"\n",
" call_mid_underlying futures call_entry call_exit bond \n",
"2018-09-17 13.91 16.0 False False 101.540001 \n",
"2018-09-18 13.91 16.0 False False 101.120003 \n",
"2018-09-19 13.91 16.0 False False 100.949997 \n",
"2018-09-20 13.91 16.0 False False 100.959999 \n",
"2018-09-21 13.91 16.0 False False 101.000000 "
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"combined.to_csv('../data/Options/combined2.csv')\n",
"combined.tail()"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:17:20.950373Z",
"start_time": "2018-10-06T17:17:20.809750Z"
},
"pixiedust": {
"displayParams": {}
}
},
"outputs": [],
"source": [
"#%%pixie_debugger\n",
"starting_capital = 100000.00\n",
"put_allocation = 0.14\n",
"call_allocation = 0.06\n",
"bond_allocation = 0.80\n",
"put_position = 0\n",
"put_position_value = 0\n",
"call_position = 0\n",
"call_position_value = 0\n",
"bond_position = 0\n",
"bond_position_value = 0\n",
"cash = 0.00\n",
"equity_curve = 0\n",
"new_cash = 0.00\n",
"temp_PnL = {}\n",
"\n",
"for i, row in enumerate(combined.itertuples(), 0):\n",
" if i == 0: start = False\n",
" if i == 0: equity_curve = starting_capital\n",
"\n",
" if (row.put_entry == True and row.call_entry == True and row.put_price > 0\n",
" and row.call_price > 0):\n",
" #if (combined.put_expiration[i]!=combined.put_expiration[i-1]):\n",
" start = True\n",
" if row.put_mid_underlying > 40:\n",
" put_allocation = 0.14\n",
" call_allocation = 0.00\n",
" bond_allocation = 0.86\n",
" elif row.put_mid_underlying < 8:\n",
" put_allocation = 0.00\n",
" call_allocation = 0.06\n",
" bond_allocation = 0.94\n",
" else:\n",
" put_allocation = 0.00\n",
" call_allocation = 0.00\n",
" bond_allocation = 1\n",
"\n",
" put_position = round((equity_curve * put_allocation) / row.put_price,\n",
" 0)\n",
" put_position_value = put_position * row.put_price\n",
"\n",
" call_position = round(\n",
" (equity_curve * call_allocation) / row.call_price, 0)\n",
" call_position_value = call_position * row.call_price\n",
"\n",
" bond_position = round((equity_curve * bond_allocation) / row.bond, 0)\n",
" bond_position_value = bond_position * row.bond\n",
"\n",
" cash = equity_curve - (\n",
" put_position_value + call_position_value + bond_position_value)\n",
" equity_curve = put_position_value + call_position_value + bond_position_value + cash\n",
"\n",
" temp_PnL[combined.index[i]] = [\n",
" i, row.put_quote_date, row.put_expiration, row.put_strike,\n",
" row.put_trade_volume, row.put_price, row.put_targetExpiration,\n",
" row.put_ExpDiff, row.put_mid_underlying, put_position,\n",
" put_position_value, row.put_entry, row.put_exit, row.futures,\n",
" row.call_quote_date, row.call_expiration, row.call_strike,\n",
" row.call_trade_volume, row.call_price, row.call_targetExpiration,\n",
" row.call_ExpDiff, row.call_mid_underlying, call_position,\n",
" call_position_value, row.call_entry, row.call_exit, bond_position,\n",
" bond_position_value, cash, equity_curve\n",
" ]\n",
" #print(i, row.price, 'position', position, 'position_value',\n",
" #position_value, 'cash', cash, 'equity_curve', equity_curve)\n",
" if start == True:\n",
" if row.put_entry == False and row.call_entry == False and row.put_price > 0 and row.call_price > 0:\n",
" put_position_value = put_position * row.put_price\n",
" call_position_value = call_position * row.call_price\n",
" bond_position_value = bond_position * row.bond\n",
" equity_curve = put_position_value + call_position_value + bond_position_value + cash\n",
" temp_PnL[combined.index[i]] = [\n",
" i, row.put_quote_date, row.put_expiration, row.put_strike,\n",
" row.put_trade_volume, row.put_price, row.put_targetExpiration,\n",
" row.put_ExpDiff, row.put_mid_underlying, put_position,\n",
" put_position_value, row.put_entry, row.put_exit, row.futures,\n",
" row.call_quote_date, row.call_expiration, row.call_strike,\n",
" row.call_trade_volume, row.call_price,\n",
" row.call_targetExpiration, row.call_ExpDiff,\n",
" row.call_mid_underlying, call_position, call_position_value,\n",
" row.call_entry, row.call_exit, bond_position,\n",
" bond_position_value, cash, equity_curve\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:17:21.368255Z",
"start_time": "2018-10-06T17:17:20.951371Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" \n",
" i \n",
" put_quote_date \n",
" put_expiration \n",
" put_strike \n",
" put_trade_volume \n",
" put_price \n",
" put_targetExpiration \n",
" put_ExpDiff \n",
" put_mid_underlying \n",
" put_position \n",
" put_position_value \n",
" put_entry \n",
" put_exit \n",
" futures \n",
" call_quote_date \n",
" call_expiration \n",
" call_strike \n",
" call_trade_volume \n",
" call_price \n",
" call_targetExpiration \n",
" call_ExpDiff \n",
" call_mid_underlying \n",
" call_position \n",
" call_position_value \n",
" call_entry \n",
" call_exit \n",
" bond_position \n",
" bond_position_value \n",
" cash \n",
" equity_curve \n",
" \n",
" \n",
" Date \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" 2018-09-17 \n",
" 3161 \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 12.0 \n",
" 0.0 \n",
" 0.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 362530.0 \n",
" 108759.0 \n",
" False \n",
" False \n",
" 16.0 \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 24.0 \n",
" 0.0 \n",
" 1.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 0.0 \n",
" 0.0 \n",
" False \n",
" False \n",
" 9459.0 \n",
" 960466.869459 \n",
" -135979.288587 \n",
" 933246.580872 \n",
" \n",
" \n",
" 2018-09-18 \n",
" 3162 \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 12.0 \n",
" 0.0 \n",
" 0.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 362530.0 \n",
" 108759.0 \n",
" False \n",
" False \n",
" 16.0 \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 24.0 \n",
" 0.0 \n",
" 1.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 0.0 \n",
" 0.0 \n",
" False \n",
" False \n",
" 9459.0 \n",
" 956494.108377 \n",
" -135979.288587 \n",
" 929273.819790 \n",
" \n",
" \n",
" 2018-09-19 \n",
" 3163 \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 12.0 \n",
" 0.0 \n",
" 0.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 362530.0 \n",
" 108759.0 \n",
" False \n",
" False \n",
" 16.0 \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 24.0 \n",
" 0.0 \n",
" 1.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 0.0 \n",
" 0.0 \n",
" False \n",
" False \n",
" 9459.0 \n",
" 954886.021623 \n",
" -135979.288587 \n",
" 927665.733036 \n",
" \n",
" \n",
" 2018-09-20 \n",
" 3164 \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 12.0 \n",
" 0.0 \n",
" 0.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 362530.0 \n",
" 108759.0 \n",
" False \n",
" False \n",
" 16.0 \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 24.0 \n",
" 0.0 \n",
" 1.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 0.0 \n",
" 0.0 \n",
" False \n",
" False \n",
" 9459.0 \n",
" 954980.630541 \n",
" -135979.288587 \n",
" 927760.341954 \n",
" \n",
" \n",
" 2018-09-21 \n",
" 3165 \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 12.0 \n",
" 0.0 \n",
" 0.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 362530.0 \n",
" 108759.0 \n",
" False \n",
" False \n",
" 16.0 \n",
" 2018-09-05 \n",
" 2019-01-16 \n",
" 24.0 \n",
" 0.0 \n",
" 1.3 \n",
" 2019-03-10 \n",
" 53 days 00:00:00.000000000 \n",
" 13.91 \n",
" 0.0 \n",
" 0.0 \n",
" False \n",
" False \n",
" 9459.0 \n",
" 955359.000000 \n",
" -135979.288587 \n",
" 928138.711413 \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
" i put_quote_date put_expiration put_strike put_trade_volume \\\n",
"Date \n",
"2018-09-17 3161 2018-09-05 2019-01-16 12.0 0.0 \n",
"2018-09-18 3162 2018-09-05 2019-01-16 12.0 0.0 \n",
"2018-09-19 3163 2018-09-05 2019-01-16 12.0 0.0 \n",
"2018-09-20 3164 2018-09-05 2019-01-16 12.0 0.0 \n",
"2018-09-21 3165 2018-09-05 2019-01-16 12.0 0.0 \n",
"\n",
" put_price put_targetExpiration put_ExpDiff \\\n",
"Date \n",
"2018-09-17 0.3 2019-03-10 53 days 00:00:00.000000000 \n",
"2018-09-18 0.3 2019-03-10 53 days 00:00:00.000000000 \n",
"2018-09-19 0.3 2019-03-10 53 days 00:00:00.000000000 \n",
"2018-09-20 0.3 2019-03-10 53 days 00:00:00.000000000 \n",
"2018-09-21 0.3 2019-03-10 53 days 00:00:00.000000000 \n",
"\n",
" put_mid_underlying put_position put_position_value put_entry \\\n",
"Date \n",
"2018-09-17 13.91 362530.0 108759.0 False \n",
"2018-09-18 13.91 362530.0 108759.0 False \n",
"2018-09-19 13.91 362530.0 108759.0 False \n",
"2018-09-20 13.91 362530.0 108759.0 False \n",
"2018-09-21 13.91 362530.0 108759.0 False \n",
"\n",
" put_exit futures call_quote_date call_expiration call_strike \\\n",
"Date \n",
"2018-09-17 False 16.0 2018-09-05 2019-01-16 24.0 \n",
"2018-09-18 False 16.0 2018-09-05 2019-01-16 24.0 \n",
"2018-09-19 False 16.0 2018-09-05 2019-01-16 24.0 \n",
"2018-09-20 False 16.0 2018-09-05 2019-01-16 24.0 \n",
"2018-09-21 False 16.0 2018-09-05 2019-01-16 24.0 \n",
"\n",
" call_trade_volume call_price call_targetExpiration \\\n",
"Date \n",
"2018-09-17 0.0 1.3 2019-03-10 \n",
"2018-09-18 0.0 1.3 2019-03-10 \n",
"2018-09-19 0.0 1.3 2019-03-10 \n",
"2018-09-20 0.0 1.3 2019-03-10 \n",
"2018-09-21 0.0 1.3 2019-03-10 \n",
"\n",
" call_ExpDiff call_mid_underlying call_position \\\n",
"Date \n",
"2018-09-17 53 days 00:00:00.000000000 13.91 0.0 \n",
"2018-09-18 53 days 00:00:00.000000000 13.91 0.0 \n",
"2018-09-19 53 days 00:00:00.000000000 13.91 0.0 \n",
"2018-09-20 53 days 00:00:00.000000000 13.91 0.0 \n",
"2018-09-21 53 days 00:00:00.000000000 13.91 0.0 \n",
"\n",
" call_position_value call_entry call_exit bond_position \\\n",
"Date \n",
"2018-09-17 0.0 False False 9459.0 \n",
"2018-09-18 0.0 False False 9459.0 \n",
"2018-09-19 0.0 False False 9459.0 \n",
"2018-09-20 0.0 False False 9459.0 \n",
"2018-09-21 0.0 False False 9459.0 \n",
"\n",
" bond_position_value cash equity_curve \n",
"Date \n",
"2018-09-17 960466.869459 -135979.288587 933246.580872 \n",
"2018-09-18 956494.108377 -135979.288587 929273.819790 \n",
"2018-09-19 954886.021623 -135979.288587 927665.733036 \n",
"2018-09-20 954980.630541 -135979.288587 927760.341954 \n",
"2018-09-21 955359.000000 -135979.288587 928138.711413 "
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"PL = pd.DataFrame(temp_PnL).T.fillna(method='ffill')\n",
"PL.index.name = 'Date'\n",
"PL.columns = [\n",
" 'i', 'put_quote_date', 'put_expiration', 'put_strike', 'put_trade_volume',\n",
" 'put_price', 'put_targetExpiration', 'put_ExpDiff', 'put_mid_underlying',\n",
" 'put_position', 'put_position_value', 'put_entry', 'put_exit','futures',\n",
" 'call_quote_date', 'call_expiration', 'call_strike', 'call_trade_volume',\n",
" 'call_price', 'call_targetExpiration', 'call_ExpDiff',\n",
" 'call_mid_underlying', 'call_position', 'call_position_value',\n",
" 'call_entry', 'call_exit', 'bond_position', 'bond_position_value', 'cash',\n",
" 'equity_curve'\n",
"]\n",
"\n",
"PL.to_csv('../data/Options/vxxPnL.csv')\n",
"PL.tail()"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:17:23.313090Z",
"start_time": "2018-10-06T17:17:23.309025Z"
}
},
"outputs": [],
"source": [
"#because the is_list_like is moved to pandas.api.types\n",
"pd.core.common.is_list_like = pd.api.types.is_list_like\n",
"import ffn"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:17:23.330979Z",
"start_time": "2018-10-06T17:17:23.315010Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" \n",
" equity_curve \n",
" \n",
" \n",
" Date \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" 2006-05-17 \n",
" 100000.0 \n",
" \n",
" \n",
" 2006-05-18 \n",
" 100000.0 \n",
" \n",
" \n",
" 2006-05-19 \n",
" 100000.0 \n",
" \n",
" \n",
" 2006-05-22 \n",
" 100000.0 \n",
" \n",
" \n",
" 2006-05-23 \n",
" 100000.0 \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
" equity_curve\n",
"Date \n",
"2006-05-17 100000.0\n",
"2006-05-18 100000.0\n",
"2006-05-19 100000.0\n",
"2006-05-22 100000.0\n",
"2006-05-23 100000.0"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"option_series=PL[['equity_curve']].copy()\n",
"option_series.head()\n",
"#new = old[['A', 'C', 'D']].copy()"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:17:23.346925Z",
"start_time": "2018-10-06T17:17:23.332962Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" \n",
" Options \n",
" \n",
" \n",
" Date \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" 2006-05-17 \n",
" 100000.0 \n",
" \n",
" \n",
" 2006-05-18 \n",
" 100000.0 \n",
" \n",
" \n",
" 2006-05-19 \n",
" 100000.0 \n",
" \n",
" \n",
" 2006-05-22 \n",
" 100000.0 \n",
" \n",
" \n",
" 2006-05-23 \n",
" 100000.0 \n",
" \n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Options\n",
"Date \n",
"2006-05-17 100000.0\n",
"2006-05-18 100000.0\n",
"2006-05-19 100000.0\n",
"2006-05-22 100000.0\n",
"2006-05-23 100000.0"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"option_series.rename(columns={'equity_curve': 'Options'}, inplace=True)\n",
"option_series.head()"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:17:23.421724Z",
"start_time": "2018-10-06T17:17:23.351911Z"
},
"scrolled": false
},
"outputs": [
{
"data": {
"application/javascript": [
"/* Put everything inside the global mpl namespace */\n",
"window.mpl = {};\n",
"\n",
"\n",
"mpl.get_websocket_type = function() {\n",
" if (typeof(WebSocket) !== 'undefined') {\n",
" return WebSocket;\n",
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
" return MozWebSocket;\n",
" } else {\n",
" alert('Your browser does not have WebSocket support.' +\n",
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
" 'Firefox 4 and 5 are also supported but you ' +\n",
" 'have to enable WebSockets in about:config.');\n",
" };\n",
"}\n",
"\n",
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
" this.id = figure_id;\n",
"\n",
" this.ws = websocket;\n",
"\n",
" this.supports_binary = (this.ws.binaryType != undefined);\n",
"\n",
" if (!this.supports_binary) {\n",
" var warnings = document.getElementById(\"mpl-warnings\");\n",
" if (warnings) {\n",
" warnings.style.display = 'block';\n",
" warnings.textContent = (\n",
" \"This browser does not support binary websocket messages. \" +\n",
" \"Performance may be slow.\");\n",
" }\n",
" }\n",
"\n",
" this.imageObj = new Image();\n",
"\n",
" this.context = undefined;\n",
" this.message = undefined;\n",
" this.canvas = undefined;\n",
" this.rubberband_canvas = undefined;\n",
" this.rubberband_context = undefined;\n",
" this.format_dropdown = undefined;\n",
"\n",
" this.image_mode = 'full';\n",
"\n",
" this.root = $('
');\n",
" this._root_extra_style(this.root)\n",
" this.root.attr('style', 'display: inline-block');\n",
"\n",
" $(parent_element).append(this.root);\n",
"\n",
" this._init_header(this);\n",
" this._init_canvas(this);\n",
" this._init_toolbar(this);\n",
"\n",
" var fig = this;\n",
"\n",
" this.waiting = false;\n",
"\n",
" this.ws.onopen = function () {\n",
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
" fig.send_message(\"send_image_mode\", {});\n",
" if (mpl.ratio != 1) {\n",
" fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n",
" }\n",
" fig.send_message(\"refresh\", {});\n",
" }\n",
"\n",
" this.imageObj.onload = function() {\n",
" if (fig.image_mode == 'full') {\n",
" // Full images could contain transparency (where diff images\n",
" // almost always do), so we need to clear the canvas so that\n",
" // there is no ghosting.\n",
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
" }\n",
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
" };\n",
"\n",
" this.imageObj.onunload = function() {\n",
" fig.ws.close();\n",
" }\n",
"\n",
" this.ws.onmessage = this._make_on_message_function(this);\n",
"\n",
" this.ondownload = ondownload;\n",
"}\n",
"\n",
"mpl.figure.prototype._init_header = function() {\n",
" var titlebar = $(\n",
" '
');\n",
" var titletext = $(\n",
" '
');\n",
" titlebar.append(titletext)\n",
" this.root.append(titlebar);\n",
" this.header = titletext[0];\n",
"}\n",
"\n",
"\n",
"\n",
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
"\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
"\n",
"}\n",
"\n",
"mpl.figure.prototype._init_canvas = function() {\n",
" var fig = this;\n",
"\n",
" var canvas_div = $('
');\n",
"\n",
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
"\n",
" function canvas_keyboard_event(event) {\n",
" return fig.key_event(event, event['data']);\n",
" }\n",
"\n",
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
" this.canvas_div = canvas_div\n",
" this._canvas_extra_style(canvas_div)\n",
" this.root.append(canvas_div);\n",
"\n",
" var canvas = $(' ');\n",
" canvas.addClass('mpl-canvas');\n",
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
"\n",
" this.canvas = canvas[0];\n",
" this.context = canvas[0].getContext(\"2d\");\n",
"\n",
" var backingStore = this.context.backingStorePixelRatio ||\n",
"\tthis.context.webkitBackingStorePixelRatio ||\n",
"\tthis.context.mozBackingStorePixelRatio ||\n",
"\tthis.context.msBackingStorePixelRatio ||\n",
"\tthis.context.oBackingStorePixelRatio ||\n",
"\tthis.context.backingStorePixelRatio || 1;\n",
"\n",
" mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n",
"\n",
" var rubberband = $(' ');\n",
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
"\n",
" var pass_mouse_events = true;\n",
"\n",
" canvas_div.resizable({\n",
" start: function(event, ui) {\n",
" pass_mouse_events = false;\n",
" },\n",
" resize: function(event, ui) {\n",
" fig.request_resize(ui.size.width, ui.size.height);\n",
" },\n",
" stop: function(event, ui) {\n",
" pass_mouse_events = true;\n",
" fig.request_resize(ui.size.width, ui.size.height);\n",
" },\n",
" });\n",
"\n",
" function mouse_event_fn(event) {\n",
" if (pass_mouse_events)\n",
" return fig.mouse_event(event, event['data']);\n",
" }\n",
"\n",
" rubberband.mousedown('button_press', mouse_event_fn);\n",
" rubberband.mouseup('button_release', mouse_event_fn);\n",
" // Throttle sequential mouse events to 1 every 20ms.\n",
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
"\n",
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
"\n",
" canvas_div.on(\"wheel\", function (event) {\n",
" event = event.originalEvent;\n",
" event['data'] = 'scroll'\n",
" if (event.deltaY < 0) {\n",
" event.step = 1;\n",
" } else {\n",
" event.step = -1;\n",
" }\n",
" mouse_event_fn(event);\n",
" });\n",
"\n",
" canvas_div.append(canvas);\n",
" canvas_div.append(rubberband);\n",
"\n",
" this.rubberband = rubberband;\n",
" this.rubberband_canvas = rubberband[0];\n",
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
" this.rubberband_context.strokeStyle = \"#000000\";\n",
"\n",
" this._resize_canvas = function(width, height) {\n",
" // Keep the size of the canvas, canvas container, and rubber band\n",
" // canvas in synch.\n",
" canvas_div.css('width', width)\n",
" canvas_div.css('height', height)\n",
"\n",
" canvas.attr('width', width * mpl.ratio);\n",
" canvas.attr('height', height * mpl.ratio);\n",
" canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n",
"\n",
" rubberband.attr('width', width);\n",
" rubberband.attr('height', height);\n",
" }\n",
"\n",
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
" // upon first draw.\n",
" this._resize_canvas(600, 600);\n",
"\n",
" // Disable right mouse context menu.\n",
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
" return false;\n",
" });\n",
"\n",
" function set_focus () {\n",
" canvas.focus();\n",
" canvas_div.focus();\n",
" }\n",
"\n",
" window.setTimeout(set_focus, 100);\n",
"}\n",
"\n",
"mpl.figure.prototype._init_toolbar = function() {\n",
" var fig = this;\n",
"\n",
" var nav_element = $('
')\n",
" nav_element.attr('style', 'width: 100%');\n",
" this.root.append(nav_element);\n",
"\n",
" // Define a callback function for later on.\n",
" function toolbar_event(event) {\n",
" return fig.toolbar_button_onclick(event['data']);\n",
" }\n",
" function toolbar_mouse_event(event) {\n",
" return fig.toolbar_button_onmouseover(event['data']);\n",
" }\n",
"\n",
" for(var toolbar_ind in mpl.toolbar_items) {\n",
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
"\n",
" if (!name) {\n",
" // put a spacer in here.\n",
" continue;\n",
" }\n",
" var button = $(' ');\n",
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
" 'ui-button-icon-only');\n",
" button.attr('role', 'button');\n",
" button.attr('aria-disabled', 'false');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
"\n",
" var icon_img = $(' ');\n",
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
" icon_img.addClass(image);\n",
" icon_img.addClass('ui-corner-all');\n",
"\n",
" var tooltip_span = $(' ');\n",
" tooltip_span.addClass('ui-button-text');\n",
" tooltip_span.html(tooltip);\n",
"\n",
" button.append(icon_img);\n",
" button.append(tooltip_span);\n",
"\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" var fmt_picker_span = $(' ');\n",
"\n",
" var fmt_picker = $(' ');\n",
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
" fmt_picker_span.append(fmt_picker);\n",
" nav_element.append(fmt_picker_span);\n",
" this.format_dropdown = fmt_picker[0];\n",
"\n",
" for (var ind in mpl.extensions) {\n",
" var fmt = mpl.extensions[ind];\n",
" var option = $(\n",
" ' ', {selected: fmt === mpl.default_extension}).html(fmt);\n",
" fmt_picker.append(option)\n",
" }\n",
"\n",
" // Add hover states to the ui-buttons\n",
" $( \".ui-button\" ).hover(\n",
" function() { $(this).addClass(\"ui-state-hover\");},\n",
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
" );\n",
"\n",
" var status_bar = $('');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"}\n",
"\n",
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
" // which will in turn request a refresh of the image.\n",
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
"}\n",
"\n",
"mpl.figure.prototype.send_message = function(type, properties) {\n",
" properties['type'] = type;\n",
" properties['figure_id'] = this.id;\n",
" this.ws.send(JSON.stringify(properties));\n",
"}\n",
"\n",
"mpl.figure.prototype.send_draw_message = function() {\n",
" if (!this.waiting) {\n",
" this.waiting = true;\n",
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
" }\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" var format_dropdown = fig.format_dropdown;\n",
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
" fig.ondownload(fig, format);\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
" var size = msg['size'];\n",
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
" fig._resize_canvas(size[0], size[1]);\n",
" fig.send_message(\"refresh\", {});\n",
" };\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
" var x0 = msg['x0'] / mpl.ratio;\n",
" var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
" var x1 = msg['x1'] / mpl.ratio;\n",
" var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
" x0 = Math.floor(x0) + 0.5;\n",
" y0 = Math.floor(y0) + 0.5;\n",
" x1 = Math.floor(x1) + 0.5;\n",
" y1 = Math.floor(y1) + 0.5;\n",
" var min_x = Math.min(x0, x1);\n",
" var min_y = Math.min(y0, y1);\n",
" var width = Math.abs(x1 - x0);\n",
" var height = Math.abs(y1 - y0);\n",
"\n",
" fig.rubberband_context.clearRect(\n",
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
"\n",
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
" // Updates the figure title.\n",
" fig.header.textContent = msg['label'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
" var cursor = msg['cursor'];\n",
" switch(cursor)\n",
" {\n",
" case 0:\n",
" cursor = 'pointer';\n",
" break;\n",
" case 1:\n",
" cursor = 'default';\n",
" break;\n",
" case 2:\n",
" cursor = 'crosshair';\n",
" break;\n",
" case 3:\n",
" cursor = 'move';\n",
" break;\n",
" }\n",
" fig.rubberband_canvas.style.cursor = cursor;\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
" fig.message.textContent = msg['message'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
" // Request the server to send over a new figure.\n",
" fig.send_draw_message();\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
" fig.image_mode = msg['mode'];\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Called whenever the canvas gets updated.\n",
" this.send_message(\"ack\", {});\n",
"}\n",
"\n",
"// A function to construct a web socket function for onmessage handling.\n",
"// Called in the figure constructor.\n",
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
" return function socket_on_message(evt) {\n",
" if (evt.data instanceof Blob) {\n",
" /* FIXME: We get \"Resource interpreted as Image but\n",
" * transferred with MIME type text/plain:\" errors on\n",
" * Chrome. But how to set the MIME type? It doesn't seem\n",
" * to be part of the websocket stream */\n",
" evt.data.type = \"image/png\";\n",
"\n",
" /* Free the memory for the previous frames */\n",
" if (fig.imageObj.src) {\n",
" (window.URL || window.webkitURL).revokeObjectURL(\n",
" fig.imageObj.src);\n",
" }\n",
"\n",
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
" evt.data);\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
" fig.imageObj.src = evt.data;\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
"\n",
" var msg = JSON.parse(evt.data);\n",
" var msg_type = msg['type'];\n",
"\n",
" // Call the \"handle_{type}\" callback, which takes\n",
" // the figure and JSON message as its only arguments.\n",
" try {\n",
" var callback = fig[\"handle_\" + msg_type];\n",
" } catch (e) {\n",
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
" return;\n",
" }\n",
"\n",
" if (callback) {\n",
" try {\n",
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
" callback(fig, msg);\n",
" } catch (e) {\n",
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
" }\n",
" }\n",
" };\n",
"}\n",
"\n",
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
"mpl.findpos = function(e) {\n",
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
" var targ;\n",
" if (!e)\n",
" e = window.event;\n",
" if (e.target)\n",
" targ = e.target;\n",
" else if (e.srcElement)\n",
" targ = e.srcElement;\n",
" if (targ.nodeType == 3) // defeat Safari bug\n",
" targ = targ.parentNode;\n",
"\n",
" // jQuery normalizes the pageX and pageY\n",
" // pageX,Y are the mouse positions relative to the document\n",
" // offset() returns the position of the element relative to the document\n",
" var x = e.pageX - $(targ).offset().left;\n",
" var y = e.pageY - $(targ).offset().top;\n",
"\n",
" return {\"x\": x, \"y\": y};\n",
"};\n",
"\n",
"/*\n",
" * return a copy of an object with only non-object keys\n",
" * we need this to avoid circular references\n",
" * http://stackoverflow.com/a/24161582/3208463\n",
" */\n",
"function simpleKeys (original) {\n",
" return Object.keys(original).reduce(function (obj, key) {\n",
" if (typeof original[key] !== 'object')\n",
" obj[key] = original[key]\n",
" return obj;\n",
" }, {});\n",
"}\n",
"\n",
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
" var canvas_pos = mpl.findpos(event)\n",
"\n",
" if (name === 'button_press')\n",
" {\n",
" this.canvas.focus();\n",
" this.canvas_div.focus();\n",
" }\n",
"\n",
" var x = canvas_pos.x * mpl.ratio;\n",
" var y = canvas_pos.y * mpl.ratio;\n",
"\n",
" this.send_message(name, {x: x, y: y, button: event.button,\n",
" step: event.step,\n",
" guiEvent: simpleKeys(event)});\n",
"\n",
" /* This prevents the web browser from automatically changing to\n",
" * the text insertion cursor when the button is pressed. We want\n",
" * to control all of the cursor setting manually through the\n",
" * 'cursor' event from matplotlib */\n",
" event.preventDefault();\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" // Handle any extra behaviour associated with a key event\n",
"}\n",
"\n",
"mpl.figure.prototype.key_event = function(event, name) {\n",
"\n",
" // Prevent repeat events\n",
" if (name == 'key_press')\n",
" {\n",
" if (event.which === this._key)\n",
" return;\n",
" else\n",
" this._key = event.which;\n",
" }\n",
" if (name == 'key_release')\n",
" this._key = null;\n",
"\n",
" var value = '';\n",
" if (event.ctrlKey && event.which != 17)\n",
" value += \"ctrl+\";\n",
" if (event.altKey && event.which != 18)\n",
" value += \"alt+\";\n",
" if (event.shiftKey && event.which != 16)\n",
" value += \"shift+\";\n",
"\n",
" value += 'k';\n",
" value += event.which.toString();\n",
"\n",
" this._key_event_extra(event, name);\n",
"\n",
" this.send_message(name, {key: value,\n",
" guiEvent: simpleKeys(event)});\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
" if (name == 'download') {\n",
" this.handle_save(this, null);\n",
" } else {\n",
" this.send_message(\"toolbar_button\", {name: name});\n",
" }\n",
"};\n",
"\n",
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
" this.message.textContent = tooltip;\n",
"};\n",
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
"\n",
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
"\n",
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
" // object with the appropriate methods. Currently this is a non binary\n",
" // socket, so there is still some room for performance tuning.\n",
" var ws = {};\n",
"\n",
" ws.close = function() {\n",
" comm.close()\n",
" };\n",
" ws.send = function(m) {\n",
" //console.log('sending', m);\n",
" comm.send(m);\n",
" };\n",
" // Register the callback with on_msg.\n",
" comm.on_msg(function(msg) {\n",
" //console.log('receiving', msg['content']['data'], msg);\n",
" // Pass the mpl event to the overridden (by mpl) onmessage function.\n",
" ws.onmessage(msg['content']['data'])\n",
" });\n",
" return ws;\n",
"}\n",
"\n",
"mpl.mpl_figure_comm = function(comm, msg) {\n",
" // This is the function which gets called when the mpl process\n",
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
"\n",
" var id = msg.content.data.id;\n",
" // Get hold of the div created by the display call when the Comm\n",
" // socket was opened in Python.\n",
" var element = $(\"#\" + id);\n",
" var ws_proxy = comm_websocket_adapter(comm)\n",
"\n",
" function ondownload(figure, format) {\n",
" window.open(figure.imageObj.src);\n",
" }\n",
"\n",
" var fig = new mpl.figure(id, ws_proxy,\n",
" ondownload,\n",
" element.get(0));\n",
"\n",
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
" // web socket which is closed, not our websocket->open comm proxy.\n",
" ws_proxy.onopen();\n",
"\n",
" fig.parent_element = element.get(0);\n",
" fig.cell_info = mpl.find_output_cell(\"
\");\n",
" if (!fig.cell_info) {\n",
" console.error(\"Failed to find cell for figure\", id, fig);\n",
" return;\n",
" }\n",
"\n",
" var output_index = fig.cell_info[2]\n",
" var cell = fig.cell_info[0];\n",
"\n",
"};\n",
"\n",
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
" var width = fig.canvas.width/mpl.ratio\n",
" fig.root.unbind('remove')\n",
"\n",
" // Update the output cell to use the data from the current canvas.\n",
" fig.push_to_output();\n",
" var dataURL = fig.canvas.toDataURL();\n",
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
" // the notebook keyboard shortcuts fail.\n",
" IPython.keyboard_manager.enable()\n",
" $(fig.parent_element).html(' ');\n",
" fig.close_ws(fig, msg);\n",
"}\n",
"\n",
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
" fig.send_message('closing', msg);\n",
" // fig.ws.close()\n",
"}\n",
"\n",
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
" // Turn the data on the canvas into data in the output cell.\n",
" var width = this.canvas.width/mpl.ratio\n",
" var dataURL = this.canvas.toDataURL();\n",
" this.cell_info[1]['text/html'] = ' ';\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Tell IPython that the notebook contents must change.\n",
" IPython.notebook.set_dirty(true);\n",
" this.send_message(\"ack\", {});\n",
" var fig = this;\n",
" // Wait a second, then push the new image to the DOM so\n",
" // that it is saved nicely (might be nice to debounce this).\n",
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
"}\n",
"\n",
"mpl.figure.prototype._init_toolbar = function() {\n",
" var fig = this;\n",
"\n",
" var nav_element = $('
')\n",
" nav_element.attr('style', 'width: 100%');\n",
" this.root.append(nav_element);\n",
"\n",
" // Define a callback function for later on.\n",
" function toolbar_event(event) {\n",
" return fig.toolbar_button_onclick(event['data']);\n",
" }\n",
" function toolbar_mouse_event(event) {\n",
" return fig.toolbar_button_onmouseover(event['data']);\n",
" }\n",
"\n",
" for(var toolbar_ind in mpl.toolbar_items){\n",
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
"\n",
" if (!name) { continue; };\n",
"\n",
" var button = $(' ');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" // Add the status bar.\n",
" var status_bar = $(' ');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"\n",
" // Add the close button to the window.\n",
" var buttongrp = $('
');\n",
" var button = $(' ');\n",
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
" buttongrp.append(button);\n",
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
" titlebar.prepend(buttongrp);\n",
"}\n",
"\n",
"mpl.figure.prototype._root_extra_style = function(el){\n",
" var fig = this\n",
" el.on(\"remove\", function(){\n",
"\tfig.close_ws(fig, {});\n",
" });\n",
"}\n",
"\n",
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
" // this is important to make the div 'focusable\n",
" el.attr('tabindex', 0)\n",
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
" // off when our div gets focus\n",
"\n",
" // location in version 3\n",
" if (IPython.notebook.keyboard_manager) {\n",
" IPython.notebook.keyboard_manager.register_events(el);\n",
" }\n",
" else {\n",
" // location in version 2\n",
" IPython.keyboard_manager.register_events(el);\n",
" }\n",
"\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" var manager = IPython.notebook.keyboard_manager;\n",
" if (!manager)\n",
" manager = IPython.keyboard_manager;\n",
"\n",
" // Check for shift+enter\n",
" if (event.shiftKey && event.which == 13) {\n",
" this.canvas_div.blur();\n",
" event.shiftKey = false;\n",
" // Send a \"J\" for go to next cell\n",
" event.which = 74;\n",
" event.keyCode = 74;\n",
" manager.command_mode();\n",
" manager.handle_keydown(event);\n",
" }\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" fig.ondownload(fig, null);\n",
"}\n",
"\n",
"\n",
"mpl.find_output_cell = function(html_output) {\n",
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
" // IPython event is triggered only after the cells have been serialised, which for\n",
" // our purposes (turning an active figure into a static one), is too late.\n",
" var cells = IPython.notebook.get_cells();\n",
" var ncells = cells.length;\n",
" for (var i=0; i= 3 moved mimebundle to data attribute of output\n",
" data = data.data;\n",
" }\n",
" if (data['text/html'] == html_output) {\n",
" return [cell, data, j];\n",
" }\n",
" }\n",
" }\n",
" }\n",
"}\n",
"\n",
"// Register the function which deals with the matplotlib target/channel.\n",
"// The kernel may be null if the page has been refreshed.\n",
"if (IPython.notebook.kernel != null) {\n",
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
"}\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#option_series=option_series[(option_series.index< '2009-01-01') & (option_series.index> '2008-09-01')]\n",
"#option_series=option_series[(option_series.index>'2016-01-01')]\n",
"ax = option_series.plot(logy=True)"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:17:23.467631Z",
"start_time": "2018-10-06T17:17:23.426712Z"
}
},
"outputs": [
{
"data": {
"application/javascript": [
"/* Put everything inside the global mpl namespace */\n",
"window.mpl = {};\n",
"\n",
"\n",
"mpl.get_websocket_type = function() {\n",
" if (typeof(WebSocket) !== 'undefined') {\n",
" return WebSocket;\n",
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
" return MozWebSocket;\n",
" } else {\n",
" alert('Your browser does not have WebSocket support.' +\n",
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
" 'Firefox 4 and 5 are also supported but you ' +\n",
" 'have to enable WebSockets in about:config.');\n",
" };\n",
"}\n",
"\n",
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
" this.id = figure_id;\n",
"\n",
" this.ws = websocket;\n",
"\n",
" this.supports_binary = (this.ws.binaryType != undefined);\n",
"\n",
" if (!this.supports_binary) {\n",
" var warnings = document.getElementById(\"mpl-warnings\");\n",
" if (warnings) {\n",
" warnings.style.display = 'block';\n",
" warnings.textContent = (\n",
" \"This browser does not support binary websocket messages. \" +\n",
" \"Performance may be slow.\");\n",
" }\n",
" }\n",
"\n",
" this.imageObj = new Image();\n",
"\n",
" this.context = undefined;\n",
" this.message = undefined;\n",
" this.canvas = undefined;\n",
" this.rubberband_canvas = undefined;\n",
" this.rubberband_context = undefined;\n",
" this.format_dropdown = undefined;\n",
"\n",
" this.image_mode = 'full';\n",
"\n",
" this.root = $('
');\n",
" this._root_extra_style(this.root)\n",
" this.root.attr('style', 'display: inline-block');\n",
"\n",
" $(parent_element).append(this.root);\n",
"\n",
" this._init_header(this);\n",
" this._init_canvas(this);\n",
" this._init_toolbar(this);\n",
"\n",
" var fig = this;\n",
"\n",
" this.waiting = false;\n",
"\n",
" this.ws.onopen = function () {\n",
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
" fig.send_message(\"send_image_mode\", {});\n",
" if (mpl.ratio != 1) {\n",
" fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n",
" }\n",
" fig.send_message(\"refresh\", {});\n",
" }\n",
"\n",
" this.imageObj.onload = function() {\n",
" if (fig.image_mode == 'full') {\n",
" // Full images could contain transparency (where diff images\n",
" // almost always do), so we need to clear the canvas so that\n",
" // there is no ghosting.\n",
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
" }\n",
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
" };\n",
"\n",
" this.imageObj.onunload = function() {\n",
" fig.ws.close();\n",
" }\n",
"\n",
" this.ws.onmessage = this._make_on_message_function(this);\n",
"\n",
" this.ondownload = ondownload;\n",
"}\n",
"\n",
"mpl.figure.prototype._init_header = function() {\n",
" var titlebar = $(\n",
" '
');\n",
" var titletext = $(\n",
" '
');\n",
" titlebar.append(titletext)\n",
" this.root.append(titlebar);\n",
" this.header = titletext[0];\n",
"}\n",
"\n",
"\n",
"\n",
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
"\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
"\n",
"}\n",
"\n",
"mpl.figure.prototype._init_canvas = function() {\n",
" var fig = this;\n",
"\n",
" var canvas_div = $('
');\n",
"\n",
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
"\n",
" function canvas_keyboard_event(event) {\n",
" return fig.key_event(event, event['data']);\n",
" }\n",
"\n",
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
" this.canvas_div = canvas_div\n",
" this._canvas_extra_style(canvas_div)\n",
" this.root.append(canvas_div);\n",
"\n",
" var canvas = $(' ');\n",
" canvas.addClass('mpl-canvas');\n",
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
"\n",
" this.canvas = canvas[0];\n",
" this.context = canvas[0].getContext(\"2d\");\n",
"\n",
" var backingStore = this.context.backingStorePixelRatio ||\n",
"\tthis.context.webkitBackingStorePixelRatio ||\n",
"\tthis.context.mozBackingStorePixelRatio ||\n",
"\tthis.context.msBackingStorePixelRatio ||\n",
"\tthis.context.oBackingStorePixelRatio ||\n",
"\tthis.context.backingStorePixelRatio || 1;\n",
"\n",
" mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n",
"\n",
" var rubberband = $(' ');\n",
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
"\n",
" var pass_mouse_events = true;\n",
"\n",
" canvas_div.resizable({\n",
" start: function(event, ui) {\n",
" pass_mouse_events = false;\n",
" },\n",
" resize: function(event, ui) {\n",
" fig.request_resize(ui.size.width, ui.size.height);\n",
" },\n",
" stop: function(event, ui) {\n",
" pass_mouse_events = true;\n",
" fig.request_resize(ui.size.width, ui.size.height);\n",
" },\n",
" });\n",
"\n",
" function mouse_event_fn(event) {\n",
" if (pass_mouse_events)\n",
" return fig.mouse_event(event, event['data']);\n",
" }\n",
"\n",
" rubberband.mousedown('button_press', mouse_event_fn);\n",
" rubberband.mouseup('button_release', mouse_event_fn);\n",
" // Throttle sequential mouse events to 1 every 20ms.\n",
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
"\n",
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
"\n",
" canvas_div.on(\"wheel\", function (event) {\n",
" event = event.originalEvent;\n",
" event['data'] = 'scroll'\n",
" if (event.deltaY < 0) {\n",
" event.step = 1;\n",
" } else {\n",
" event.step = -1;\n",
" }\n",
" mouse_event_fn(event);\n",
" });\n",
"\n",
" canvas_div.append(canvas);\n",
" canvas_div.append(rubberband);\n",
"\n",
" this.rubberband = rubberband;\n",
" this.rubberband_canvas = rubberband[0];\n",
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
" this.rubberband_context.strokeStyle = \"#000000\";\n",
"\n",
" this._resize_canvas = function(width, height) {\n",
" // Keep the size of the canvas, canvas container, and rubber band\n",
" // canvas in synch.\n",
" canvas_div.css('width', width)\n",
" canvas_div.css('height', height)\n",
"\n",
" canvas.attr('width', width * mpl.ratio);\n",
" canvas.attr('height', height * mpl.ratio);\n",
" canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n",
"\n",
" rubberband.attr('width', width);\n",
" rubberband.attr('height', height);\n",
" }\n",
"\n",
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
" // upon first draw.\n",
" this._resize_canvas(600, 600);\n",
"\n",
" // Disable right mouse context menu.\n",
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
" return false;\n",
" });\n",
"\n",
" function set_focus () {\n",
" canvas.focus();\n",
" canvas_div.focus();\n",
" }\n",
"\n",
" window.setTimeout(set_focus, 100);\n",
"}\n",
"\n",
"mpl.figure.prototype._init_toolbar = function() {\n",
" var fig = this;\n",
"\n",
" var nav_element = $('
')\n",
" nav_element.attr('style', 'width: 100%');\n",
" this.root.append(nav_element);\n",
"\n",
" // Define a callback function for later on.\n",
" function toolbar_event(event) {\n",
" return fig.toolbar_button_onclick(event['data']);\n",
" }\n",
" function toolbar_mouse_event(event) {\n",
" return fig.toolbar_button_onmouseover(event['data']);\n",
" }\n",
"\n",
" for(var toolbar_ind in mpl.toolbar_items) {\n",
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
"\n",
" if (!name) {\n",
" // put a spacer in here.\n",
" continue;\n",
" }\n",
" var button = $(' ');\n",
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
" 'ui-button-icon-only');\n",
" button.attr('role', 'button');\n",
" button.attr('aria-disabled', 'false');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
"\n",
" var icon_img = $(' ');\n",
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
" icon_img.addClass(image);\n",
" icon_img.addClass('ui-corner-all');\n",
"\n",
" var tooltip_span = $(' ');\n",
" tooltip_span.addClass('ui-button-text');\n",
" tooltip_span.html(tooltip);\n",
"\n",
" button.append(icon_img);\n",
" button.append(tooltip_span);\n",
"\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" var fmt_picker_span = $(' ');\n",
"\n",
" var fmt_picker = $(' ');\n",
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
" fmt_picker_span.append(fmt_picker);\n",
" nav_element.append(fmt_picker_span);\n",
" this.format_dropdown = fmt_picker[0];\n",
"\n",
" for (var ind in mpl.extensions) {\n",
" var fmt = mpl.extensions[ind];\n",
" var option = $(\n",
" ' ', {selected: fmt === mpl.default_extension}).html(fmt);\n",
" fmt_picker.append(option)\n",
" }\n",
"\n",
" // Add hover states to the ui-buttons\n",
" $( \".ui-button\" ).hover(\n",
" function() { $(this).addClass(\"ui-state-hover\");},\n",
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
" );\n",
"\n",
" var status_bar = $('');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"}\n",
"\n",
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
" // which will in turn request a refresh of the image.\n",
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
"}\n",
"\n",
"mpl.figure.prototype.send_message = function(type, properties) {\n",
" properties['type'] = type;\n",
" properties['figure_id'] = this.id;\n",
" this.ws.send(JSON.stringify(properties));\n",
"}\n",
"\n",
"mpl.figure.prototype.send_draw_message = function() {\n",
" if (!this.waiting) {\n",
" this.waiting = true;\n",
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
" }\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" var format_dropdown = fig.format_dropdown;\n",
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
" fig.ondownload(fig, format);\n",
"}\n",
"\n",
"\n",
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
" var size = msg['size'];\n",
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
" fig._resize_canvas(size[0], size[1]);\n",
" fig.send_message(\"refresh\", {});\n",
" };\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
" var x0 = msg['x0'] / mpl.ratio;\n",
" var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
" var x1 = msg['x1'] / mpl.ratio;\n",
" var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
" x0 = Math.floor(x0) + 0.5;\n",
" y0 = Math.floor(y0) + 0.5;\n",
" x1 = Math.floor(x1) + 0.5;\n",
" y1 = Math.floor(y1) + 0.5;\n",
" var min_x = Math.min(x0, x1);\n",
" var min_y = Math.min(y0, y1);\n",
" var width = Math.abs(x1 - x0);\n",
" var height = Math.abs(y1 - y0);\n",
"\n",
" fig.rubberband_context.clearRect(\n",
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
"\n",
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
" // Updates the figure title.\n",
" fig.header.textContent = msg['label'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
" var cursor = msg['cursor'];\n",
" switch(cursor)\n",
" {\n",
" case 0:\n",
" cursor = 'pointer';\n",
" break;\n",
" case 1:\n",
" cursor = 'default';\n",
" break;\n",
" case 2:\n",
" cursor = 'crosshair';\n",
" break;\n",
" case 3:\n",
" cursor = 'move';\n",
" break;\n",
" }\n",
" fig.rubberband_canvas.style.cursor = cursor;\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
" fig.message.textContent = msg['message'];\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
" // Request the server to send over a new figure.\n",
" fig.send_draw_message();\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
" fig.image_mode = msg['mode'];\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Called whenever the canvas gets updated.\n",
" this.send_message(\"ack\", {});\n",
"}\n",
"\n",
"// A function to construct a web socket function for onmessage handling.\n",
"// Called in the figure constructor.\n",
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
" return function socket_on_message(evt) {\n",
" if (evt.data instanceof Blob) {\n",
" /* FIXME: We get \"Resource interpreted as Image but\n",
" * transferred with MIME type text/plain:\" errors on\n",
" * Chrome. But how to set the MIME type? It doesn't seem\n",
" * to be part of the websocket stream */\n",
" evt.data.type = \"image/png\";\n",
"\n",
" /* Free the memory for the previous frames */\n",
" if (fig.imageObj.src) {\n",
" (window.URL || window.webkitURL).revokeObjectURL(\n",
" fig.imageObj.src);\n",
" }\n",
"\n",
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
" evt.data);\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
" fig.imageObj.src = evt.data;\n",
" fig.updated_canvas_event();\n",
" fig.waiting = false;\n",
" return;\n",
" }\n",
"\n",
" var msg = JSON.parse(evt.data);\n",
" var msg_type = msg['type'];\n",
"\n",
" // Call the \"handle_{type}\" callback, which takes\n",
" // the figure and JSON message as its only arguments.\n",
" try {\n",
" var callback = fig[\"handle_\" + msg_type];\n",
" } catch (e) {\n",
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
" return;\n",
" }\n",
"\n",
" if (callback) {\n",
" try {\n",
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
" callback(fig, msg);\n",
" } catch (e) {\n",
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
" }\n",
" }\n",
" };\n",
"}\n",
"\n",
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
"mpl.findpos = function(e) {\n",
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
" var targ;\n",
" if (!e)\n",
" e = window.event;\n",
" if (e.target)\n",
" targ = e.target;\n",
" else if (e.srcElement)\n",
" targ = e.srcElement;\n",
" if (targ.nodeType == 3) // defeat Safari bug\n",
" targ = targ.parentNode;\n",
"\n",
" // jQuery normalizes the pageX and pageY\n",
" // pageX,Y are the mouse positions relative to the document\n",
" // offset() returns the position of the element relative to the document\n",
" var x = e.pageX - $(targ).offset().left;\n",
" var y = e.pageY - $(targ).offset().top;\n",
"\n",
" return {\"x\": x, \"y\": y};\n",
"};\n",
"\n",
"/*\n",
" * return a copy of an object with only non-object keys\n",
" * we need this to avoid circular references\n",
" * http://stackoverflow.com/a/24161582/3208463\n",
" */\n",
"function simpleKeys (original) {\n",
" return Object.keys(original).reduce(function (obj, key) {\n",
" if (typeof original[key] !== 'object')\n",
" obj[key] = original[key]\n",
" return obj;\n",
" }, {});\n",
"}\n",
"\n",
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
" var canvas_pos = mpl.findpos(event)\n",
"\n",
" if (name === 'button_press')\n",
" {\n",
" this.canvas.focus();\n",
" this.canvas_div.focus();\n",
" }\n",
"\n",
" var x = canvas_pos.x * mpl.ratio;\n",
" var y = canvas_pos.y * mpl.ratio;\n",
"\n",
" this.send_message(name, {x: x, y: y, button: event.button,\n",
" step: event.step,\n",
" guiEvent: simpleKeys(event)});\n",
"\n",
" /* This prevents the web browser from automatically changing to\n",
" * the text insertion cursor when the button is pressed. We want\n",
" * to control all of the cursor setting manually through the\n",
" * 'cursor' event from matplotlib */\n",
" event.preventDefault();\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" // Handle any extra behaviour associated with a key event\n",
"}\n",
"\n",
"mpl.figure.prototype.key_event = function(event, name) {\n",
"\n",
" // Prevent repeat events\n",
" if (name == 'key_press')\n",
" {\n",
" if (event.which === this._key)\n",
" return;\n",
" else\n",
" this._key = event.which;\n",
" }\n",
" if (name == 'key_release')\n",
" this._key = null;\n",
"\n",
" var value = '';\n",
" if (event.ctrlKey && event.which != 17)\n",
" value += \"ctrl+\";\n",
" if (event.altKey && event.which != 18)\n",
" value += \"alt+\";\n",
" if (event.shiftKey && event.which != 16)\n",
" value += \"shift+\";\n",
"\n",
" value += 'k';\n",
" value += event.which.toString();\n",
"\n",
" this._key_event_extra(event, name);\n",
"\n",
" this.send_message(name, {key: value,\n",
" guiEvent: simpleKeys(event)});\n",
" return false;\n",
"}\n",
"\n",
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
" if (name == 'download') {\n",
" this.handle_save(this, null);\n",
" } else {\n",
" this.send_message(\"toolbar_button\", {name: name});\n",
" }\n",
"};\n",
"\n",
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
" this.message.textContent = tooltip;\n",
"};\n",
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
"\n",
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
"\n",
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
" // object with the appropriate methods. Currently this is a non binary\n",
" // socket, so there is still some room for performance tuning.\n",
" var ws = {};\n",
"\n",
" ws.close = function() {\n",
" comm.close()\n",
" };\n",
" ws.send = function(m) {\n",
" //console.log('sending', m);\n",
" comm.send(m);\n",
" };\n",
" // Register the callback with on_msg.\n",
" comm.on_msg(function(msg) {\n",
" //console.log('receiving', msg['content']['data'], msg);\n",
" // Pass the mpl event to the overridden (by mpl) onmessage function.\n",
" ws.onmessage(msg['content']['data'])\n",
" });\n",
" return ws;\n",
"}\n",
"\n",
"mpl.mpl_figure_comm = function(comm, msg) {\n",
" // This is the function which gets called when the mpl process\n",
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
"\n",
" var id = msg.content.data.id;\n",
" // Get hold of the div created by the display call when the Comm\n",
" // socket was opened in Python.\n",
" var element = $(\"#\" + id);\n",
" var ws_proxy = comm_websocket_adapter(comm)\n",
"\n",
" function ondownload(figure, format) {\n",
" window.open(figure.imageObj.src);\n",
" }\n",
"\n",
" var fig = new mpl.figure(id, ws_proxy,\n",
" ondownload,\n",
" element.get(0));\n",
"\n",
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
" // web socket which is closed, not our websocket->open comm proxy.\n",
" ws_proxy.onopen();\n",
"\n",
" fig.parent_element = element.get(0);\n",
" fig.cell_info = mpl.find_output_cell(\"
\");\n",
" if (!fig.cell_info) {\n",
" console.error(\"Failed to find cell for figure\", id, fig);\n",
" return;\n",
" }\n",
"\n",
" var output_index = fig.cell_info[2]\n",
" var cell = fig.cell_info[0];\n",
"\n",
"};\n",
"\n",
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
" var width = fig.canvas.width/mpl.ratio\n",
" fig.root.unbind('remove')\n",
"\n",
" // Update the output cell to use the data from the current canvas.\n",
" fig.push_to_output();\n",
" var dataURL = fig.canvas.toDataURL();\n",
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
" // the notebook keyboard shortcuts fail.\n",
" IPython.keyboard_manager.enable()\n",
" $(fig.parent_element).html(' ');\n",
" fig.close_ws(fig, msg);\n",
"}\n",
"\n",
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
" fig.send_message('closing', msg);\n",
" // fig.ws.close()\n",
"}\n",
"\n",
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
" // Turn the data on the canvas into data in the output cell.\n",
" var width = this.canvas.width/mpl.ratio\n",
" var dataURL = this.canvas.toDataURL();\n",
" this.cell_info[1]['text/html'] = ' ';\n",
"}\n",
"\n",
"mpl.figure.prototype.updated_canvas_event = function() {\n",
" // Tell IPython that the notebook contents must change.\n",
" IPython.notebook.set_dirty(true);\n",
" this.send_message(\"ack\", {});\n",
" var fig = this;\n",
" // Wait a second, then push the new image to the DOM so\n",
" // that it is saved nicely (might be nice to debounce this).\n",
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
"}\n",
"\n",
"mpl.figure.prototype._init_toolbar = function() {\n",
" var fig = this;\n",
"\n",
" var nav_element = $('
')\n",
" nav_element.attr('style', 'width: 100%');\n",
" this.root.append(nav_element);\n",
"\n",
" // Define a callback function for later on.\n",
" function toolbar_event(event) {\n",
" return fig.toolbar_button_onclick(event['data']);\n",
" }\n",
" function toolbar_mouse_event(event) {\n",
" return fig.toolbar_button_onmouseover(event['data']);\n",
" }\n",
"\n",
" for(var toolbar_ind in mpl.toolbar_items){\n",
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
"\n",
" if (!name) { continue; };\n",
"\n",
" var button = $(' ');\n",
" button.click(method_name, toolbar_event);\n",
" button.mouseover(tooltip, toolbar_mouse_event);\n",
" nav_element.append(button);\n",
" }\n",
"\n",
" // Add the status bar.\n",
" var status_bar = $(' ');\n",
" nav_element.append(status_bar);\n",
" this.message = status_bar[0];\n",
"\n",
" // Add the close button to the window.\n",
" var buttongrp = $('
');\n",
" var button = $(' ');\n",
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
" buttongrp.append(button);\n",
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
" titlebar.prepend(buttongrp);\n",
"}\n",
"\n",
"mpl.figure.prototype._root_extra_style = function(el){\n",
" var fig = this\n",
" el.on(\"remove\", function(){\n",
"\tfig.close_ws(fig, {});\n",
" });\n",
"}\n",
"\n",
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
" // this is important to make the div 'focusable\n",
" el.attr('tabindex', 0)\n",
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
" // off when our div gets focus\n",
"\n",
" // location in version 3\n",
" if (IPython.notebook.keyboard_manager) {\n",
" IPython.notebook.keyboard_manager.register_events(el);\n",
" }\n",
" else {\n",
" // location in version 2\n",
" IPython.keyboard_manager.register_events(el);\n",
" }\n",
"\n",
"}\n",
"\n",
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
" var manager = IPython.notebook.keyboard_manager;\n",
" if (!manager)\n",
" manager = IPython.keyboard_manager;\n",
"\n",
" // Check for shift+enter\n",
" if (event.shiftKey && event.which == 13) {\n",
" this.canvas_div.blur();\n",
" event.shiftKey = false;\n",
" // Send a \"J\" for go to next cell\n",
" event.which = 74;\n",
" event.keyCode = 74;\n",
" manager.command_mode();\n",
" manager.handle_keydown(event);\n",
" }\n",
"}\n",
"\n",
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
" fig.ondownload(fig, null);\n",
"}\n",
"\n",
"\n",
"mpl.find_output_cell = function(html_output) {\n",
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
" // IPython event is triggered only after the cells have been serialised, which for\n",
" // our purposes (turning an active figure into a static one), is too late.\n",
" var cells = IPython.notebook.get_cells();\n",
" var ncells = cells.length;\n",
" for (var i=0; i= 3 moved mimebundle to data attribute of output\n",
" data = data.data;\n",
" }\n",
" if (data['text/html'] == html_output) {\n",
" return [cell, data, j];\n",
" }\n",
" }\n",
" }\n",
" }\n",
"}\n",
"\n",
"// Register the function which deals with the matplotlib target/channel.\n",
"// The kernel may be null if the page has been refreshed.\n",
"if (IPython.notebook.kernel != null) {\n",
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
"}\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"returns = option_series.to_returns().dropna()\n",
"ax = returns.hist()"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {
"ExecuteTime": {
"end_time": "2018-10-06T17:17:23.572359Z",
"start_time": "2018-10-06T17:17:23.469598Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Stat Options\n",
"------------------- ----------\n",
"Start 2006-05-17\n",
"End 2018-09-21\n",
"Risk-free rate 0.00%\n",
"\n",
"Total Return 828.14%\n",
"Daily Sharpe 0.83\n",
"Daily Sortino 1.50\n",
"CAGR 19.77%\n",
"Max Drawdown -27.39%\n",
"Calmar Ratio 0.72\n",
"\n",
"MTD -2.35%\n",
"3m -6.48%\n",
"6m -1.22%\n",
"YTD -8.70%\n",
"1Y 3.25%\n",
"3Y (ann.) 14.66%\n",
"5Y (ann.) 13.57%\n",
"10Y (ann.) 25.61%\n",
"Since Incep. (ann.) 19.77%\n",
"\n",
"Daily Sharpe 0.83\n",
"Daily Sortino 1.50\n",
"Daily Mean (ann.) 21.64%\n",
"Daily Vol (ann.) 26.02%\n",
"Daily Skew 2.04\n",
"Daily Kurt 56.50\n",
"Best Day 29.25%\n",
"Worst Day -23.18%\n",
"\n",
"Monthly Sharpe 0.91\n",
"Monthly Sortino 2.91\n",
"Monthly Mean (ann.) 21.87%\n",
"Monthly Vol (ann.) 24.12%\n",
"Monthly Skew 1.87\n",
"Monthly Kurt 5.21\n",
"Best Month 34.48%\n",
"Worst Month -12.28%\n",
"\n",
"Yearly Sharpe 0.81\n",
"Yearly Sortino 6.27\n",
"Yearly Mean 21.94%\n",
"Yearly Vol 26.92%\n",
"Yearly Skew 0.74\n",
"Yearly Kurt 0.28\n",
"Best Year 79.93%\n",
"Worst Year -8.70%\n",
"\n",
"Avg. Drawdown -4.29%\n",
"Avg. Drawdown Days 41.94\n",
"Avg. Up Month 6.08%\n",
"Avg. Down Month -3.06%\n",
"Win Year % 75.00%\n",
"Win 12m % 78.99%\n"
]
}
],
"source": [
"stats = option_series.calc_stats()\n",
"stats.display()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.5"
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"position": {
"height": "416px",
"left": "1163px",
"right": "20px",
"top": "127px",
"width": "356px"
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}