diff --git a/experiments/moving_midpoint.ipynb b/experiments/moving_midpoint.ipynb index cd24b4c..b3d4aa3 100644 --- a/experiments/moving_midpoint.ipynb +++ b/experiments/moving_midpoint.ipynb @@ -120,7 +120,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 32, "metadata": {}, "outputs": [ { @@ -132,6 +132,9 @@ "bust_sort: 1\n", "\n", "Portfolio exited early at 2020-03-16 00:00:00\n", + "bust_sort: 1\n", + "\n", + "Portfolio exited early at 2021-10-12 00:00:00\n", "bust_sort: 1\n" ] } @@ -158,128 +161,31 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "100%|██████████| 712/712 [00:28<00:00, 25.12it/s]\n" + " 0%| | 0/712 [00:00 0).mean():.2f}\")\n", - "print(f\"Average minimum cumulative profit: {min_cum_prof.mean():.2f}\")\n", - "\n", - "print(\"\\nMean return\")\n", - "print(f\"Mean return: {means.mean():.2f}\")\n", - "print(f\"Median return: {means.median():.2f}\")\n", - "# print(f\"Std return: {means.std():.2f}\")\n", - "print(f\"75th percentile return: {means.quantile(0.75):.2f}\")\n", - "print(f\"25th percentile return: {means.quantile(0.25):.2f}\")\n", - "print(f\"Fraction positive: {(means > 0).mean():.2f}\")\n", - "\n", - "\n", - "print(\"\\nRisk\")\n", - "print(f\"Mean risk: {stdevs.mean():.2f}\")\n", - "print(f\"Median risk: {stdevs.median():.2f}\")\n", - "# print(f\"Std risk: {stdevs.std():.2f}\")\n", - "print(f\"75th percentile risk: {stdevs.quantile(0.75):.2f}\")\n", - "print(f\"25th percentile risk: {stdevs.quantile(0.25):.2f}\")\n", - "\n", - "print(\"\\nSharpe\")\n", - "print(f\"Average Sharpe: {sharpes.mean():.2f}\")\n", - "print(f\"Median Sharpe: {sharpes.median():.2f}\")\n", - "# print(f\"Std Sharpe: {sharpes.std():.2f}\")\n", - "print(f\"75th percentile Sharpe: {sharpes.quantile(0.75):.2f}\")\n", - "print(f\"25th percentile Sharpe: {sharpes.quantile(0.25):.2f}\")\n", - "print(f\"Fraction positive: {(sharpes > 0).mean():.2f}\")\n", - "\n", - "print(\"\\nDrawdown\")\n", - "print(f\"Mean drawdown: {drawdowns.mean():.2f}\")\n", - "print(f\"Median drawdown: {drawdowns.median():.2f}\")\n", - "# print(f\"Std drawdown: {drawdowns.std():.2f}\")\n", - "print(f\"75th percentile drawdown: {drawdowns.quantile(0.75):.2f}\")\n", - "print(f\"25th percentile drawdown: {drawdowns.quantile(0.25):.2f}\")\n" + "stat_arb_metrics = metrics(portfolios_after_cost, results)" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 34, "metadata": {}, "outputs": [ { @@ -383,52 +289,9 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Average number of assets per SA: 5.3\n", - "Median number of assets per SA: 5.0\n", - "Number of unique SAs: 712\n", - "Mean profit: 1.10\n", - "Median profit: 0.80\n", - "Std profit: 3.01\n", - "75th percentile: 2.05\n", - "25th percentile: -0.24\n", - "Fraction profitable: 0.70\n", - "Average minimum cumulative profit: -1.76\n", - "\n", - "Mean return\n", - "Mean return: 0.15\n", - "Median return: 0.12\n", - "75th percentile return: 0.24\n", - "25th percentile return: 0.03\n", - "Fraction positive: 0.81\n", - "\n", - "Risk\n", - "Mean risk: 0.20\n", - "Median risk: 0.15\n", - "75th percentile risk: 0.25\n", - "25th percentile risk: 0.09\n", - "\n", - "Sharpe\n", - "Average Sharpe: 0.84\n", - "Median Sharpe: 0.88\n", - "75th percentile Sharpe: 1.52\n", - "25th percentile Sharpe: 0.21\n", - "Fraction positive: 0.81\n", - "\n", - "Drawdown\n", - "Mean drawdown: 0.11\n", - "Median drawdown: 0.09\n", - "75th percentile drawdown: 0.15\n", - "25th percentile drawdown: 0.05\n" - ] - } - ], + "outputs": [], "source": [ "avg_lens = np.mean(lens)\n", "median_len = np.median(lens)\n", @@ -494,7 +357,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -512,7 +375,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 19, "metadata": {}, "outputs": [ { diff --git a/experiments/utils.py b/experiments/utils.py index 0994d16..ca96df7 100644 --- a/experiments/utils.py +++ b/experiments/utils.py @@ -127,7 +127,7 @@ def metrics(portfolios_after_cost, results): profit -= portfolio.trading_costs.sum(axis=1) ### Shorting cost - short_rate = 3 * 0.01**2 + short_rate = 0.5 / 100 / 252 # half a percent per year short_cost = ( portfolio.stocks[portfolio.stocks < 0].abs() * portfolio.prices ).sum(axis=1) * short_rate @@ -179,7 +179,15 @@ def simulate(res, portfolio, trading_cost_model, lev_fraction): initial_cash=lev_fraction * lev0, ) - navs = portfolio_temp.nav + ### Shorting cost + short_rate = 0.5 / 100 / 252 # half a percent per year + short_cost = (portfolio.stocks[portfolio.stocks < 0].abs() * portfolio.prices).sum( + axis=1 + ) * short_rate + + navs = portfolio_temp.nav - short_cost.cumsum() + + # navs = portfolio_temp.nav positions = portfolio_temp.stocks absolute_notionals = positions.abs() * portfolio_temp.prices long_positions = absolute_notionals[positions > 0].sum(axis=1) @@ -629,7 +637,7 @@ def plot_stat_arb( profit -= portfolio.trading_costs.sum(axis=1) ### Shorting cost - short_rate = 3 * 0.01**2 + short_rate = 0.5 / 100 / 252 # half a percent per year short_cost = (portfolio.stocks[portfolio.stocks < 0].abs() * portfolio.prices).sum( axis=1 ) * short_rate