Updated example notebooks

This commit is contained in:
Javier Rodríguez Chatruc
2020-03-25 10:54:25 -03:00
parent 7586609a08
commit 1bda04d30b
10 changed files with 2630 additions and 6838 deletions
-549
View File
@@ -1,549 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"\n",
"BACKTESTER_DIR = os.path.realpath(os.path.join(os.getcwd(), '..', '..'))\n",
"DATA_DIR = os.path.join(BACKTESTER_DIR, 'data')\n",
"OPTIONS_DATA = os.path.join(DATA_DIR, 'options_data_clean_v2.h5')\n",
"STOCKS_DATA = os.path.join(DATA_DIR, 'ivy_5assets.csv')\n",
"\n",
"sys.path.append(BACKTESTER_DIR) # Add backtester base dir to $PYTHONPATH"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/jamoroso/.local/share/virtualenvs/backtester_options-33KCFJeg/lib/python3.7/site-packages/pandas_datareader/compat/__init__.py:7: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.\n",
" from pandas.util.testing import assert_frame_equal\n",
"/Users/jamoroso/.local/share/virtualenvs/backtester_options-33KCFJeg/lib/python3.7/site-packages/pyfolio/pos.py:27: UserWarning: Module \"zipline.assets\" not found; mutltipliers will not be applied to position notionals.\n",
" 'Module \"zipline.assets\" not found; mutltipliers will not be applied' +\n"
]
}
],
"source": [
"import pyfolio as pf\n",
"\n",
"from backtester import Backtest, Type, Direction, Stock\n",
"from backtester.strategy import Strategy, StrategyLeg\n",
"from backtester.datahandler import HistoricalOptionsData, TiingoData\n",
"\n",
"# Cleaned up data\n",
"options_data = HistoricalOptionsData(\n",
" OPTIONS_DATA,\n",
" key=\"/SPX\",\n",
" where='quotedate >= \"2012-01-01\" & quotedate <= \"2014-01-01\"')\n",
"options_schema = options_data.schema"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Strategy(legs=[StrategyLeg(name=leg_1, type=Type.PUT, direction=Direction.BUY, entry_filter=Filter(query='((type == 'put') & (ask > 0)) & ((underlying == 'SPX') & (dte >= 60))'), exit_filter=Filter(query='(type == 'put') & (dte <= 30)')), StrategyLeg(name=leg_2, type=Type.CALL, direction=Direction.BUY, entry_filter=Filter(query='((type == 'call') & (ask > 0)) & ((underlying == 'SPX') & (dte >= 60))'), exit_filter=Filter(query='(type == 'call') & (dte <= 30)'))], exit_thresholds=(inf, inf))"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"put_otm = Strategy(options_schema)\n",
"\n",
"leg_1 = StrategyLeg(\"leg_1\", options_schema, option_type=Type.PUT, direction=Direction.BUY)\n",
"leg_1.entry_filter = (options_schema.underlying == \"SPX\") & (options_schema.dte >= 60)\n",
"leg_1.exit_filter = (options_schema.dte <= 30)\n",
"\n",
"leg_2 = StrategyLeg(\"leg_2\", options_schema, option_type=Type.CALL, direction=Direction.BUY)\n",
"leg_2.entry_filter = (options_schema.underlying == \"SPX\") & (options_schema.dte >= 60)\n",
"leg_2.exit_filter = (options_schema.dte <= 30)\n",
"put_otm.add_legs([leg_1, leg_2])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"asset_data = TiingoData(STOCKS_DATA)\n",
"asset_data._data = asset_data.query('date >= \"2012-01-01\" & date <= \"2014-01-01\"')\n",
"\n",
"VTI = Stock(\"VTI\", 0.2)\n",
"VEU = Stock(\"VEU\", 0.2)\n",
"BND = Stock(\"BND\", 0.2)\n",
"VNQ = Stock(\"VNQ\", 0.2)\n",
"DBC = Stock(\"DBC\", 0.2)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"0% [██████████████████████████████] 100% | ETA: 00:00:00\n",
"Total time elapsed: 00:00:07\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>total capital</th>\n",
" <th>cash</th>\n",
" <th>VTI</th>\n",
" <th>VEU</th>\n",
" <th>BND</th>\n",
" <th>VNQ</th>\n",
" <th>DBC</th>\n",
" <th>options qty</th>\n",
" <th>calls capital</th>\n",
" <th>puts capital</th>\n",
" <th>stocks qty</th>\n",
" <th>options capital</th>\n",
" <th>stocks capital</th>\n",
" <th>% change</th>\n",
" <th>accumulated return</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2010-01-03</th>\n",
" <td>1.000000e+06</td>\n",
" <td>1000000.000000</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>0.0</td>\n",
" <td>0.000000e+00</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2012-01-03</th>\n",
" <td>9.978700e+05</td>\n",
" <td>2233.455073</td>\n",
" <td>193990.075633</td>\n",
" <td>193976.304225</td>\n",
" <td>193986.878802</td>\n",
" <td>193966.992283</td>\n",
" <td>193976.293985</td>\n",
" <td>6.0</td>\n",
" <td>25740.0</td>\n",
" <td>0.0</td>\n",
" <td>24257.0</td>\n",
" <td>25740.0</td>\n",
" <td>9.698965e+05</td>\n",
" <td>-0.002130</td>\n",
" <td>0.997870</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2012-01-04</th>\n",
" <td>9.949656e+05</td>\n",
" <td>2233.455073</td>\n",
" <td>194079.321182</td>\n",
" <td>193451.915101</td>\n",
" <td>194080.052134</td>\n",
" <td>190650.750322</td>\n",
" <td>195450.064670</td>\n",
" <td>6.0</td>\n",
" <td>25020.0</td>\n",
" <td>0.0</td>\n",
" <td>24257.0</td>\n",
" <td>25020.0</td>\n",
" <td>9.677121e+05</td>\n",
" <td>-0.002911</td>\n",
" <td>0.994966</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2012-01-05</th>\n",
" <td>9.927494e+05</td>\n",
" <td>2233.455073</td>\n",
" <td>194942.028158</td>\n",
" <td>191259.015126</td>\n",
" <td>194010.172135</td>\n",
" <td>192441.520981</td>\n",
" <td>192783.241526</td>\n",
" <td>6.0</td>\n",
" <td>25080.0</td>\n",
" <td>0.0</td>\n",
" <td>24257.0</td>\n",
" <td>25080.0</td>\n",
" <td>9.654360e+05</td>\n",
" <td>-0.002227</td>\n",
" <td>0.992749</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2012-01-06</th>\n",
" <td>9.887005e+05</td>\n",
" <td>2233.455073</td>\n",
" <td>194495.800412</td>\n",
" <td>188827.756460</td>\n",
" <td>194126.638801</td>\n",
" <td>191811.435009</td>\n",
" <td>193625.396202</td>\n",
" <td>6.0</td>\n",
" <td>23580.0</td>\n",
" <td>0.0</td>\n",
" <td>24257.0</td>\n",
" <td>23580.0</td>\n",
" <td>9.628870e+05</td>\n",
" <td>-0.004079</td>\n",
" <td>0.988700</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2013-12-24</th>\n",
" <td>1.276243e+06</td>\n",
" <td>273.097958</td>\n",
" <td>248730.685735</td>\n",
" <td>246256.243708</td>\n",
" <td>242926.387533</td>\n",
" <td>245754.530575</td>\n",
" <td>246032.446280</td>\n",
" <td>7.0</td>\n",
" <td>46270.0</td>\n",
" <td>0.0</td>\n",
" <td>27125.0</td>\n",
" <td>46270.0</td>\n",
" <td>1.229700e+06</td>\n",
" <td>0.003401</td>\n",
" <td>1.276243</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2013-12-26</th>\n",
" <td>1.282165e+06</td>\n",
" <td>273.097958</td>\n",
" <td>249907.026341</td>\n",
" <td>247293.424036</td>\n",
" <td>242562.316514</td>\n",
" <td>245944.449069</td>\n",
" <td>246414.781084</td>\n",
" <td>7.0</td>\n",
" <td>49770.0</td>\n",
" <td>0.0</td>\n",
" <td>27125.0</td>\n",
" <td>49770.0</td>\n",
" <td>1.232122e+06</td>\n",
" <td>0.004640</td>\n",
" <td>1.282165</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2013-12-27</th>\n",
" <td>1.284267e+06</td>\n",
" <td>273.097958</td>\n",
" <td>249750.180927</td>\n",
" <td>248281.214826</td>\n",
" <td>242562.316514</td>\n",
" <td>246476.220851</td>\n",
" <td>247083.866990</td>\n",
" <td>7.0</td>\n",
" <td>49840.0</td>\n",
" <td>0.0</td>\n",
" <td>27125.0</td>\n",
" <td>49840.0</td>\n",
" <td>1.234154e+06</td>\n",
" <td>0.001639</td>\n",
" <td>1.284267</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2013-12-30</th>\n",
" <td>1.284806e+06</td>\n",
" <td>273.097958</td>\n",
" <td>249828.603634</td>\n",
" <td>249565.342851</td>\n",
" <td>242956.726785</td>\n",
" <td>246552.188248</td>\n",
" <td>245650.111476</td>\n",
" <td>7.0</td>\n",
" <td>49980.0</td>\n",
" <td>0.0</td>\n",
" <td>27125.0</td>\n",
" <td>49980.0</td>\n",
" <td>1.234553e+06</td>\n",
" <td>0.000420</td>\n",
" <td>1.284806</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2013-12-31</th>\n",
" <td>1.288826e+06</td>\n",
" <td>273.097958</td>\n",
" <td>250743.535215</td>\n",
" <td>250553.133640</td>\n",
" <td>242865.709030</td>\n",
" <td>245222.758794</td>\n",
" <td>245267.776672</td>\n",
" <td>7.0</td>\n",
" <td>53900.0</td>\n",
" <td>0.0</td>\n",
" <td>27125.0</td>\n",
" <td>53900.0</td>\n",
" <td>1.234653e+06</td>\n",
" <td>0.003129</td>\n",
" <td>1.288826</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>503 rows × 15 columns</p>\n",
"</div>"
],
"text/plain": [
" total capital cash VTI VEU \\\n",
"2010-01-03 1.000000e+06 1000000.000000 NaN NaN \n",
"2012-01-03 9.978700e+05 2233.455073 193990.075633 193976.304225 \n",
"2012-01-04 9.949656e+05 2233.455073 194079.321182 193451.915101 \n",
"2012-01-05 9.927494e+05 2233.455073 194942.028158 191259.015126 \n",
"2012-01-06 9.887005e+05 2233.455073 194495.800412 188827.756460 \n",
"... ... ... ... ... \n",
"2013-12-24 1.276243e+06 273.097958 248730.685735 246256.243708 \n",
"2013-12-26 1.282165e+06 273.097958 249907.026341 247293.424036 \n",
"2013-12-27 1.284267e+06 273.097958 249750.180927 248281.214826 \n",
"2013-12-30 1.284806e+06 273.097958 249828.603634 249565.342851 \n",
"2013-12-31 1.288826e+06 273.097958 250743.535215 250553.133640 \n",
"\n",
" BND VNQ DBC options qty \\\n",
"2010-01-03 NaN NaN NaN NaN \n",
"2012-01-03 193986.878802 193966.992283 193976.293985 6.0 \n",
"2012-01-04 194080.052134 190650.750322 195450.064670 6.0 \n",
"2012-01-05 194010.172135 192441.520981 192783.241526 6.0 \n",
"2012-01-06 194126.638801 191811.435009 193625.396202 6.0 \n",
"... ... ... ... ... \n",
"2013-12-24 242926.387533 245754.530575 246032.446280 7.0 \n",
"2013-12-26 242562.316514 245944.449069 246414.781084 7.0 \n",
"2013-12-27 242562.316514 246476.220851 247083.866990 7.0 \n",
"2013-12-30 242956.726785 246552.188248 245650.111476 7.0 \n",
"2013-12-31 242865.709030 245222.758794 245267.776672 7.0 \n",
"\n",
" calls capital puts capital stocks qty options capital \\\n",
"2010-01-03 NaN NaN NaN 0.0 \n",
"2012-01-03 25740.0 0.0 24257.0 25740.0 \n",
"2012-01-04 25020.0 0.0 24257.0 25020.0 \n",
"2012-01-05 25080.0 0.0 24257.0 25080.0 \n",
"2012-01-06 23580.0 0.0 24257.0 23580.0 \n",
"... ... ... ... ... \n",
"2013-12-24 46270.0 0.0 27125.0 46270.0 \n",
"2013-12-26 49770.0 0.0 27125.0 49770.0 \n",
"2013-12-27 49840.0 0.0 27125.0 49840.0 \n",
"2013-12-30 49980.0 0.0 27125.0 49980.0 \n",
"2013-12-31 53900.0 0.0 27125.0 53900.0 \n",
"\n",
" stocks capital % change accumulated return \n",
"2010-01-03 0.000000e+00 NaN NaN \n",
"2012-01-03 9.698965e+05 -0.002130 0.997870 \n",
"2012-01-04 9.677121e+05 -0.002911 0.994966 \n",
"2012-01-05 9.654360e+05 -0.002227 0.992749 \n",
"2012-01-06 9.628870e+05 -0.004079 0.988700 \n",
"... ... ... ... \n",
"2013-12-24 1.229700e+06 0.003401 1.276243 \n",
"2013-12-26 1.232122e+06 0.004640 1.282165 \n",
"2013-12-27 1.234154e+06 0.001639 1.284267 \n",
"2013-12-30 1.234553e+06 0.000420 1.284806 \n",
"2013-12-31 1.234653e+06 0.003129 1.288826 \n",
"\n",
"[503 rows x 15 columns]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"allocation = {'cash': 0, 'stocks': 97, 'options': 3}\n",
"\n",
"bt = Backtest(allocation=allocation)\n",
"bt.options_data = options_data\n",
"bt.options_strategy = put_otm\n",
"\n",
"bt.stocks = [VTI, VEU, BND, VNQ, DBC]\n",
"bt.stocks_data = asset_data\n",
"\n",
"bt.run(rebalance_freq=1)\n",
"bt.balance"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\"><th>Start date</th><td colspan=2>2012-01-03</td></tr>\n",
" <tr style=\"text-align: right;\"><th>End date</th><td colspan=2>2013-12-31</td></tr>\n",
" <tr style=\"text-align: right;\"><th>Total months</th><td colspan=2>23</td></tr>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Backtest</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Annual return</th>\n",
" <td>13.6%</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Cumulative returns</th>\n",
" <td>28.9%</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Annual volatility</th>\n",
" <td>14.3%</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Sharpe ratio</th>\n",
" <td>0.96</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Calmar ratio</th>\n",
" <td>1.14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Stability</th>\n",
" <td>0.83</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Max drawdown</th>\n",
" <td>-11.9%</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Omega ratio</th>\n",
" <td>1.18</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Sortino ratio</th>\n",
" <td>1.37</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Skew</th>\n",
" <td>-0.52</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Kurtosis</th>\n",
" <td>3.88</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Tail ratio</th>\n",
" <td>0.97</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Daily value at risk</th>\n",
" <td>-1.7%</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"ename": "AttributeError",
"evalue": "'numpy.int64' object has no attribute 'to_pydatetime'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-6-ea8302e3d94e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mpf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcreate_returns_tear_sheet\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreturns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mbt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbalance\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'% change'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdropna\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/.local/share/virtualenvs/backtester_options-33KCFJeg/lib/python3.7/site-packages/pyfolio/plotting.py\u001b[0m in \u001b[0;36mcall_w_context\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 50\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mset_context\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mplotting_context\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxes_style\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 52\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 53\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.local/share/virtualenvs/backtester_options-33KCFJeg/lib/python3.7/site-packages/pyfolio/tears.py\u001b[0m in \u001b[0;36mcreate_returns_tear_sheet\u001b[0;34m(returns, positions, transactions, live_start_date, cone_std, benchmark_rets, bootstrap, turnover_denom, header_rows, return_fig)\u001b[0m\n\u001b[1;32m 502\u001b[0m header_rows=header_rows)\n\u001b[1;32m 503\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 504\u001b[0;31m \u001b[0mplotting\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshow_worst_drawdown_periods\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreturns\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 505\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 506\u001b[0m \u001b[0mvertical_sections\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m11\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.local/share/virtualenvs/backtester_options-33KCFJeg/lib/python3.7/site-packages/pyfolio/plotting.py\u001b[0m in \u001b[0;36mshow_worst_drawdown_periods\u001b[0;34m(returns, top)\u001b[0m\n\u001b[1;32m 1662\u001b[0m \"\"\"\n\u001b[1;32m 1663\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1664\u001b[0;31m \u001b[0mdrawdown_df\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtimeseries\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgen_drawdown_table\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreturns\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtop\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtop\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1665\u001b[0m utils.print_table(\n\u001b[1;32m 1666\u001b[0m \u001b[0mdrawdown_df\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msort_values\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Net drawdown in %'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mascending\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.local/share/virtualenvs/backtester_options-33KCFJeg/lib/python3.7/site-packages/pyfolio/timeseries.py\u001b[0m in \u001b[0;36mgen_drawdown_table\u001b[0;34m(returns, top)\u001b[0m\n\u001b[1;32m 1006\u001b[0m df_drawdowns.loc[i, 'Peak date'] = (peak.to_pydatetime()\n\u001b[1;32m 1007\u001b[0m .strftime('%Y-%m-%d'))\n\u001b[0;32m-> 1008\u001b[0;31m df_drawdowns.loc[i, 'Valley date'] = (valley.to_pydatetime()\n\u001b[0m\u001b[1;32m 1009\u001b[0m .strftime('%Y-%m-%d'))\n\u001b[1;32m 1010\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrecovery\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfloat\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'numpy.int64' object has no attribute 'to_pydatetime'"
]
}
],
"source": [
"pf.create_returns_tear_sheet(returns=bt.balance['% change'].dropna())"
]
}
],
"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.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
+1 -1
View File
@@ -965,7 +965,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
"version": "3.7.5"
}
},
"nbformat": 4,
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long