Skip to content

Commit

Permalink
Add transaction costs to derivatives
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoHuebner committed Mar 22, 2024
1 parent 6cebb4f commit 16ec9e3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
16 changes: 15 additions & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def performance_cumprod(returns: pd.Series) -> float:
def simplified_lev_factor(
daily_returns: pd.Series,
expense_ratio: float,
rel_transact_costs: float,
leverage: float = 1.0,
percent: float = 100.0,
):
Expand All @@ -104,16 +105,24 @@ def simplified_lev_factor(
:param daily_returns: pd.Series, daily returns of the underlying asset
:param expense_ratio: float, expense ratio of the factor (in percent)
:param rel_transact_costs: float, cost ratio assumed for each buy and sell transaction (in percent)
:param leverage: float | pd.Series, leverage of the factor
:param percent: float, percentage factor used for expense ratio conversion
:return: pd.Series, daily returns of the factor with leverage
"""
return daily_returns * leverage + gmean(-expense_ratio / percent)
daily_returns = daily_returns * leverage + gmean(-expense_ratio / percent)

# simplify: Assume the costs consist of only volume depend costs, neglecting fixed costs
daily_returns.iloc[0] -= rel_transact_costs / percent
daily_returns.iloc[-1] -= rel_transact_costs / percent

return daily_returns


def simplified_knockout(
price: pd.Series,
expense_ratio: float,
rel_transact_costs: float,
initial_leverage: float,
percent: float = 100,
) -> pd.Series:
Expand All @@ -124,6 +133,7 @@ def simplified_knockout(
:param price: pd.Series, price of the underlying asset
:param expense_ratio: float, expense ratio of the knockout product (in percent)
:param rel_transact_costs: float, cost ratio assumed for each buy and sell transaction (in percent)
:param initial_leverage: float, initial leverage factor of the knockout product
:param percent: float, percentage factor used for expense ratio conversion
:return: pd.Series, daily returns of the knockout product
Expand All @@ -145,6 +155,10 @@ def simplified_knockout(
else:
pass

# simplify: Assume the costs consist of only volume depend costs, neglecting fixed costs
pct_change.iloc[0] -= rel_transact_costs / percent
pct_change.iloc[-1] -= rel_transact_costs / percent

return pct_change.fillna(0)


Expand Down
23 changes: 20 additions & 3 deletions volatility_decay_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ def update_derivatives_performance_plot(
risk_free_rate_ticker: float,
leverage: float,
expenses: float,
rel_transact_costs: float,
time_window: int,
) -> go.Figure:
# create the plotly figure
Expand Down Expand Up @@ -393,13 +394,20 @@ def update_derivatives_performance_plot(
]
returns_lev = [
performance_cumprod(
simplified_lev_factor(pct_change.iloc[date : date + 15], expenses, leverage)
simplified_lev_factor(
pct_change.iloc[date : date + 15],
expenses,
rel_transact_costs,
leverage,
)
)
for date in dates_iloc
]
returns_ko = [
performance_cumprod(
simplified_knockout(price.iloc[date : date + 15], expenses, leverage)
simplified_knockout(
price.iloc[date : date + 15], expenses, rel_transact_costs, leverage
)
)
for date in dates_iloc
]
Expand Down Expand Up @@ -743,7 +751,15 @@ def update_derivatives_performance_plot(
)
# Slider for the expenses of the derivatives
derivative_expenses = st.slider(
"Yearly Expense Ratio of the Derivatives",
"Yearly Expense Ratio of the Derivatives [%]",
min_value=0.0,
max_value=5.0,
value=3.0,
step=0.25,
)
# Slider for the transaction costs of the derivatives
rel_transact_costs = st.slider(
"Transaction Costs for Buying and Selling Separately [%]",
min_value=0.0,
max_value=5.0,
value=3.0,
Expand All @@ -764,6 +780,7 @@ def update_derivatives_performance_plot(
risk_free_rate_ticker,
derivative_leverage,
derivative_expenses,
rel_transact_costs,
look_back_window,
),
use_container_width=True,
Expand Down

0 comments on commit 16ec9e3

Please sign in to comment.