{ "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", " \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", "
DateAdj Close
02002-07-3048.837349
12002-07-3149.285275
22002-08-0149.488335
32002-08-0249.870586
42002-08-0550.121433
\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", " \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", "
bond
Date
2006-02-2758.414406
2006-02-2858.647667
2006-03-0158.532089
2006-03-0258.298084
2006-03-0358.156284
\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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Dateiquote_dateexpirationstriketrade_volumepricenewMonthtargetExpirationExpDiffmid_underlyingfuturesstrikeTargetstrikeLocationStrikeDiffentryexit
02006-04-197142006-04-192006-05-1722.50.09.902006-04-192006-11-05172 days 00:00:00.00000000011.3222.511.3271411.18FalseFalse
12006-04-207212006-04-202006-05-1722.50.012.552006-04-192006-11-05172 days 00:00:00.00000000011.6412.511.3271411.18FalseFalse
22006-04-217292006-04-212006-05-1722.50.012.352006-04-192006-11-05172 days 00:00:00.00000000011.5912.511.3271411.18FalseFalse
32006-04-247682006-04-242006-05-1722.520.012.452006-04-192006-11-05172 days 00:00:00.00000000011.7512.511.3271411.18FalseFalse
42006-04-258072006-04-252006-05-1722.50.09.902006-04-192006-11-05172 days 00:00:00.00000000011.7612.511.3271411.18FalseFalse
\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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Dateiquote_dateexpirationstriketrade_volumepricenewMonthtargetExpirationExpDiffmid_underlyingfuturesstrikeTargetstrikeLocationStrikeDiffentryexit
02006-04-197142006-04-192006-05-1722.50.09.902006-04-192006-11-05172 days 00:00:00.00000000011.3222.511.3271411.18FalseFalse
12006-04-207212006-04-202006-05-1722.50.012.552006-04-192006-11-05172 days 00:00:00.00000000011.6412.511.3271411.18FalseFalse
22006-04-217292006-04-212006-05-1722.50.012.352006-04-192006-11-05172 days 00:00:00.00000000011.5912.511.3271411.18FalseFalse
32006-04-247682006-04-242006-05-1722.520.012.452006-04-192006-11-05172 days 00:00:00.00000000011.7512.511.3271411.18FalseFalse
42006-04-258072006-04-252006-05-1722.50.09.902006-04-192006-11-05172 days 00:00:00.00000000011.7612.511.3271411.18FalseFalse
\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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Dateiquote_dateexpirationstriketrade_volumepricenewMonthtargetExpirationExpDiffmid_underlyingfuturesstrikeTargetstrikeLocationStrikeDiffentryexit
02006-04-197142006-04-192006-05-1722.50.00.0752006-04-192006-11-05172 days 00:00:00.00000000011.3222.522.647140.14FalseFalse
12006-04-207212006-04-202006-05-1722.50.00.0752006-04-192006-11-05172 days 00:00:00.00000000011.6412.522.647140.14FalseFalse
22006-04-217292006-04-212006-05-1722.50.00.0752006-04-192006-11-05172 days 00:00:00.00000000011.5912.522.647140.14FalseFalse
32006-04-247682006-04-242006-05-1722.50.00.0752006-04-192006-11-05172 days 00:00:00.00000000011.7512.522.647140.14FalseFalse
42006-04-258072006-04-252006-05-1722.50.00.0502006-04-192006-11-05172 days 00:00:00.00000000011.7612.522.647140.14FalseFalse
\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", " \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", " \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", " \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", "
put_quote_dateput_expirationput_strikeput_trade_volumeput_priceput_targetExpirationput_ExpDiffput_mid_underlyingfuturesput_entryput_exit
put_quote_date
2006-04-192006-04-192006-05-1722.50.09.902006-11-05172 days 00:00:00.00000000011.3222.5FalseFalse
2006-04-202006-04-202006-05-1722.50.012.552006-11-05172 days 00:00:00.00000000011.6412.5FalseFalse
2006-04-212006-04-212006-05-1722.50.012.352006-11-05172 days 00:00:00.00000000011.5912.5FalseFalse
2006-04-242006-04-242006-05-1722.520.012.452006-11-05172 days 00:00:00.00000000011.7512.5FalseFalse
2006-04-252006-04-252006-05-1722.50.09.902006-11-05172 days 00:00:00.00000000011.7612.5FalseFalse
\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", " \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", " \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", " \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", "
call_quote_datecall_expirationcall_strikecall_trade_volumecall_pricecall_targetExpirationcall_ExpDiffcall_mid_underlyingfuturescall_entrycall_exit
call_quote_date
2006-04-192006-04-192006-05-1722.50.00.0752006-11-05172 days 00:00:00.00000000011.3222.5FalseFalse
2006-04-202006-04-202006-05-1722.50.00.0752006-11-05172 days 00:00:00.00000000011.6412.5FalseFalse
2006-04-212006-04-212006-05-1722.50.00.0752006-11-05172 days 00:00:00.00000000011.5912.5FalseFalse
2006-04-242006-04-242006-05-1722.50.00.0752006-11-05172 days 00:00:00.00000000011.7512.5FalseFalse
2006-04-252006-04-252006-05-1722.50.00.0502006-11-05172 days 00:00:00.00000000011.7612.5FalseFalse
\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", " \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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
put_quote_dateput_expirationput_strikeput_trade_volumeput_priceput_targetExpirationput_ExpDiffput_mid_underlyingfuturesput_entryput_exitcall_quote_datecall_expirationcall_strikecall_trade_volumecall_pricecall_targetExpirationcall_ExpDiffcall_mid_underlyingfuturescall_entrycall_exitbond
2006-02-27NaTNaTNaNNaNNaNNaTNaNNaNNaNNaNNaNNaTNaTNaNNaNNaNNaTNaNNaNNaNNaNNaN58.414406
2006-02-28NaTNaTNaNNaNNaNNaTNaNNaNNaNNaNNaNNaTNaTNaNNaNNaNNaTNaNNaNNaNNaNNaN58.647667
2006-03-01NaTNaTNaNNaNNaNNaTNaNNaNNaNNaNNaNNaTNaTNaNNaNNaNNaTNaNNaNNaNNaNNaN58.532089
2006-03-02NaTNaTNaNNaNNaNNaTNaNNaNNaNNaNNaNNaTNaTNaNNaNNaNNaTNaNNaNNaNNaNNaN58.298084
2006-03-03NaTNaTNaNNaNNaNNaTNaNNaNNaNNaNNaNNaTNaTNaNNaNNaNNaTNaNNaNNaNNaNNaN58.156284
\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", " \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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
put_quote_dateput_expirationput_strikeput_trade_volumeput_priceput_targetExpirationput_ExpDiffput_mid_underlyingfuturesput_entryput_exitcall_quote_datecall_expirationcall_strikecall_trade_volumecall_pricecall_targetExpirationcall_ExpDiffcall_mid_underlyingfuturescall_entrycall_exitbond
2018-09-172018-09-052019-01-1612.00.00.32019-03-1053 days 00:00:00.00000000013.9116.0FalseFalse2018-09-052019-01-1624.00.01.32019-03-1053 days 00:00:00.00000000013.9116.0FalseFalse101.540001
2018-09-182018-09-052019-01-1612.00.00.32019-03-1053 days 00:00:00.00000000013.9116.0FalseFalse2018-09-052019-01-1624.00.01.32019-03-1053 days 00:00:00.00000000013.9116.0FalseFalse101.120003
2018-09-192018-09-052019-01-1612.00.00.32019-03-1053 days 00:00:00.00000000013.9116.0FalseFalse2018-09-052019-01-1624.00.01.32019-03-1053 days 00:00:00.00000000013.9116.0FalseFalse100.949997
2018-09-202018-09-052019-01-1612.00.00.32019-03-1053 days 00:00:00.00000000013.9116.0FalseFalse2018-09-052019-01-1624.00.01.32019-03-1053 days 00:00:00.00000000013.9116.0FalseFalse100.959999
2018-09-212018-09-052019-01-1612.00.00.32019-03-1053 days 00:00:00.00000000013.9116.0FalseFalse2018-09-052019-01-1624.00.01.32019-03-1053 days 00:00:00.00000000013.9116.0FalseFalse101.000000
\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", " \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", " \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", " \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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
iput_quote_dateput_expirationput_strikeput_trade_volumeput_priceput_targetExpirationput_ExpDiffput_mid_underlyingput_positionput_position_valueput_entryput_exitfuturescall_quote_datecall_expirationcall_strikecall_trade_volumecall_pricecall_targetExpirationcall_ExpDiffcall_mid_underlyingcall_positioncall_position_valuecall_entrycall_exitbond_positionbond_position_valuecashequity_curve
Date
2018-09-1731612018-09-052019-01-1612.00.00.32019-03-1053 days 00:00:00.00000000013.91362530.0108759.0FalseFalse16.02018-09-052019-01-1624.00.01.32019-03-1053 days 00:00:00.00000000013.910.00.0FalseFalse9459.0960466.869459-135979.288587933246.580872
2018-09-1831622018-09-052019-01-1612.00.00.32019-03-1053 days 00:00:00.00000000013.91362530.0108759.0FalseFalse16.02018-09-052019-01-1624.00.01.32019-03-1053 days 00:00:00.00000000013.910.00.0FalseFalse9459.0956494.108377-135979.288587929273.819790
2018-09-1931632018-09-052019-01-1612.00.00.32019-03-1053 days 00:00:00.00000000013.91362530.0108759.0FalseFalse16.02018-09-052019-01-1624.00.01.32019-03-1053 days 00:00:00.00000000013.910.00.0FalseFalse9459.0954886.021623-135979.288587927665.733036
2018-09-2031642018-09-052019-01-1612.00.00.32019-03-1053 days 00:00:00.00000000013.91362530.0108759.0FalseFalse16.02018-09-052019-01-1624.00.01.32019-03-1053 days 00:00:00.00000000013.910.00.0FalseFalse9459.0954980.630541-135979.288587927760.341954
2018-09-2131652018-09-052019-01-1612.00.00.32019-03-1053 days 00:00:00.00000000013.91362530.0108759.0FalseFalse16.02018-09-052019-01-1624.00.01.32019-03-1053 days 00:00:00.00000000013.910.00.0FalseFalse9459.0955359.000000-135979.288587928138.711413
\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", " \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", "
equity_curve
Date
2006-05-17100000.0
2006-05-18100000.0
2006-05-19100000.0
2006-05-22100000.0
2006-05-23100000.0
\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", " \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", "
Options
Date
2006-05-17100000.0
2006-05-18100000.0
2006-05-19100000.0
2006-05-22100000.0
2006-05-23100000.0
\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.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 }