Skip to content

Commit

Permalink
Merge pull request #47 from cvxgrp/43-position-tracking-bug-in-simula…
Browse files Browse the repository at this point in the history
…tion

updated short cost
  • Loading branch information
kasperjo authored Feb 29, 2024
2 parents 7c4d2a7 + e0144b5 commit 69dd0b5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 155 deletions.
167 changes: 15 additions & 152 deletions experiments/moving_midpoint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 32,
"metadata": {},
"outputs": [
{
Expand All @@ -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"
]
}
Expand All @@ -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<?, ?it/s]"
]
}
],
"source": [
"stat_arb_metrics = metrics(portfolios_after_cost, results)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
},
{
"name": "stdout",
"name": "stderr",
"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.03\n",
"75th percentile: 2.07\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.12\n",
"Median drawdown: 0.09\n",
"75th percentile drawdown: 0.15\n",
"25th percentile drawdown: 0.05\n"
"100%|██████████| 712/712 [00:28<00:00, 24.56it/s]\n"
]
}
],
"source": [
"avg_lens = np.mean(lens)\n",
"median_len = np.median(lens)\n",
"n_unique_stat_arbs = n_stat_arbs\n",
"\n",
"means = stat_arb_metrics.means\n",
"stdevs = stat_arb_metrics.stdevs\n",
"sharpes = stat_arb_metrics.sharpes\n",
"profits = stat_arb_metrics.profits\n",
"min_navs = stat_arb_metrics.min_navs\n",
"min_cum_prof = stat_arb_metrics.min_cum_prof\n",
"drawdowns = stat_arb_metrics.drawdowns\n",
"\n",
"print(f\"Average number of assets per SA: {avg_lens:.1f}\")\n",
"print(f\"Median number of assets per SA: {median_len:.1f}\")\n",
"print(f\"Number of unique SAs: {n_unique_stat_arbs}\")\n",
"print(f\"Mean profit: {profits.mean():.2f}\")\n",
"print(f\"Median profit: {profits.median():.2f}\")\n",
"print(f\"Std profit: {profits.std():.2f}\")\n",
"print(f\"75th percentile: {profits.quantile(0.75):.2f}\")\n",
"print(f\"25th percentile: {profits.quantile(0.25):.2f}\")\n",
"print(f\"Fraction profitable: {(profits > 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": [
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -494,7 +357,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -512,7 +375,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 19,
"metadata": {},
"outputs": [
{
Expand Down
14 changes: 11 additions & 3 deletions experiments/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 69dd0b5

Please sign in to comment.