Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion financepy/products/equity/equity_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,15 @@ def value(
dividend_curve: DiscountCurve = None,
current_price: float = None,
first_fixing_rate: float = None,
current_reset_price: float = None,
):
"""Value the Equity swap on a valuation date."""
"""Value the equity swap on a valuation date.

current_price is required after the effective date. For the current
period, current_reset_price is the equity level at the reset date. The
strike supplies the first reset fixing; later fixings are required
when valuing between reset dates.
"""

check_curve_dt(value_dt, discount_curve)
check_curve_dt(value_dt, dividend_curve)
Expand All @@ -144,6 +151,7 @@ def value(
index_curve,
dividend_curve,
current_price,
current_reset_price,
)
self._fill_rate_notional_array()

Expand Down
31 changes: 23 additions & 8 deletions financepy/products/equity/equity_swap_leg.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def value(
index_curve: DiscountCurve,
dividend_curve: DiscountCurve = None,
current_price: float = None,
current_reset_price: float = None,
):
"""Value the equity leg with payments from an equity price, quantity,
an index curve and an [optional] dividend curve. Discounting is based
Expand All @@ -185,9 +186,10 @@ def value(
PV_i = Q * S_0 * df_idx(0, t_{i-1}) * df_div(0, t_{i-1}) * df(0, t_i)
* [1 / (df_idx(0, t_i) * df_div(0, t_i)) - 1]

For the current (in-progress) period where t_{i-1} < value_dt <= t_i,
the period-start equity price is known (current_price or strike), so
we value it as a single forward from value_dt to t_i.
For the current (in-progress) period, current_price is the equity level
at value_dt and current_reset_price is the fixing at the period start.
The initial strike supplies the first reset fixing. Later reset fixings
must be provided when valuing between reset dates.
"""

if discount_curve is None:
Expand All @@ -202,6 +204,9 @@ def value(
check_curve_dt(value_dt, discount_curve)
check_curve_dt(value_dt, dividend_curve)

if current_price is None and self.effective_dt < value_dt < self.maturity_dt:
raise FinError("Current price is required after the effective date")

self.current_price = current_price if current_price is not None else self.strike

# Reset all result lists
Expand Down Expand Up @@ -244,13 +249,23 @@ def value(
if start_accrued_dt <= value_dt:
# ----------------------------------------------------------------
# Current (in-progress) period:
# - Equity price at period start is already fixed = current_price
# (or strike if no current price supplied and value_dt ==
# effective_dt).
# - We only need to forward from value_dt to end_accrued_dt.
# - current_price is the equity level at value_dt.
# - current_reset_price is the observed level at period start.
# - The initial strike supplies the first reset level.
# - Period-start notional is known: S_reset * quantity.
# ----------------------------------------------------------------
period_start_notional = self.current_price * self.quantity
if current_reset_price is not None:
reset_price = current_reset_price
elif start_accrued_dt == self.effective_dt:
reset_price = self.strike
elif start_accrued_dt == value_dt:
reset_price = self.current_price
else:
raise FinError(
"Current reset price is required for an in-progress period"
)

period_start_notional = reset_price * self.quantity

df_idx_end = index_curve.df(end_accrued_dt)
df_div_end = dividend_curve.df(end_accrued_dt)
Expand Down
111 changes: 107 additions & 4 deletions tests/unit/test_equity_swap.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane

import pytest

from financepy.utils.date import Date
from financepy.utils.calendar import CalendarTypes
from financepy.utils.frequency import FrequencyTypes
Expand All @@ -13,6 +15,7 @@
from financepy.products.equity.equity_swap_leg import EquitySwapLeg

from financepy.market.curves.flat_discount_curve import FlatDiscountCurve
from financepy.utils.error import FinError

########################################################################################

Expand Down Expand Up @@ -62,10 +65,8 @@ def test_equity_swap_at_inception():
dg_type,
)

first_fixing = 0.02

value = equity_swap.value(
effective_dt, discount_curve, index_curve, dividend_curve, first_fixing
effective_dt, discount_curve, index_curve, dividend_curve
)

assert round(value, 0) == 0.0
Expand Down Expand Up @@ -161,7 +162,7 @@ def test_equity_swap_not_in_inception():
first_fixing,
)

assert round(value, 5) == -25734.69494
assert abs(value) < 1e-8


########################################################################################
Expand Down Expand Up @@ -222,3 +223,105 @@ def test_equity_swap_with_dividends():
)

assert abs(value_with_divs - value_higher_disc) < 1e-8


def test_equity_leg_in_progress_period_uses_current_and_reset_prices():
effective_dt = Date(1, 1, 2026)
value_dt = Date(1, 7, 2026)
maturity_dt = Date(1, 1, 2027)
reset_price = 100.0
current_price = 120.0
quantity = 1000.0
discount_rate = 0.05

discount_curve = FlatDiscountCurve(value_dt, discount_rate)
dividend_curve = FlatDiscountCurve(value_dt, 0.0)
leg = EquitySwapLeg(
effective_dt,
maturity_dt,
SwapTypes.RECEIVE,
FrequencyTypes.ANNUAL,
DayCountTypes.ACT_365F,
reset_price,
quantity,
)

actual = leg.value(
value_dt,
discount_curve,
discount_curve,
dividend_curve,
current_price,
)

forward_growth = (
discount_curve.df(value_dt) / discount_curve.df(maturity_dt)
)
expected = quantity * (
current_price * forward_growth - reset_price
) * discount_curve.df(maturity_dt) / discount_curve.df(value_dt)

assert abs(actual - expected) < 1e-8


def test_equity_leg_requires_fixing_for_later_in_progress_reset_period():
effective_dt = Date(1, 1, 2025)
value_dt = Date(1, 3, 2026)
maturity_dt = Date(1, 1, 2027)
curve = FlatDiscountCurve(value_dt, 0.05)
leg = EquitySwapLeg(
effective_dt,
maturity_dt,
SwapTypes.RECEIVE,
FrequencyTypes.SEMI_ANNUAL,
DayCountTypes.ACT_365F,
100.0,
1000.0,
)

with pytest.raises(FinError, match="Current price is required"):
leg.value(value_dt, curve, curve, current_reset_price=105.0)

with pytest.raises(FinError, match="Current reset price is required"):
leg.value(value_dt, curve, curve, current_price=110.0)

reset_price = 105.0
leg.value(
value_dt,
curve,
curve,
current_price=110.0,
current_reset_price=reset_price,
)
maturity_df = curve.df(leg.end_accd_dts[2])
expected = (
1000.0
* (110.0 * curve.df(value_dt) / maturity_df - reset_price)
* maturity_df
/ curve.df(value_dt)
)

assert leg.payment_pvs[2] == pytest.approx(expected)

swap = EquitySwap(
effective_dt,
maturity_dt,
SwapTypes.RECEIVE,
FrequencyTypes.SEMI_ANNUAL,
DayCountTypes.ACT_365F,
100.0,
1000.0,
rate_freq_type=FrequencyTypes.SEMI_ANNUAL,
rate_dc_type=DayCountTypes.ACT_365F,
)
swap.value(
value_dt,
curve,
curve,
FlatDiscountCurve(value_dt, 0.0),
first_fixing_rate=0.05,
current_price=110.0,
current_reset_price=reset_price,
)

assert swap.equity_leg.payment_pvs[2] == pytest.approx(expected)