Skip to content
Merged
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
9 changes: 6 additions & 3 deletions financepy/products/equity/equity_variance_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ def value(self, value_dt: Date,
realised_var: float,
fair_strike_var: float,
libor_curve: DiscountCurve):
"""Calculate the value of the variance swap based on the realised
volatility to the valuation date, the forward looking implied
volatility to the maturity date using the libor discount curve."""
"""Value the swap from realised and remaining fair variances.

Paying the strike receives the floating variance payoff; receiving
the strike reverses the net payoff."""

if value_dt < self.start_dt or value_dt > self.maturity_dt:
raise FinError("Valuation date must be within the variance swap life")
Expand All @@ -79,6 +80,8 @@ def value(self, value_dt: Date,
expected_variance += (t2 - t1) * fair_strike_var / t2

payoff = expected_variance - self.strike_variance
if not self.pay_strike_flag:
payoff = -payoff

df = libor_curve.df(self.maturity_dt)
v = payoff * self.notional * df
Expand Down
9 changes: 6 additions & 3 deletions financepy/products/fx/fx_variance_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ def __init__(
###########################################################################

def value(self, value_dt: Date, realised_var, fair_strike_var, libor_curve: DiscountCurve):
"""Calculate the value of the variance swap based on the realised
volatility to the valuation date, the forward looking implied
volatility to the maturity date using the libor discount curve."""
"""Value the swap from realised and remaining fair variances.

Paying the strike receives the floating variance payoff; receiving
the strike reverses the net payoff."""

if isinstance(value_dt, Date) is False:
raise FinError("Valuation date is not a Date")
Expand All @@ -80,6 +81,8 @@ def value(self, value_dt: Date, realised_var, fair_strike_var, libor_curve: Disc
expected_var += (t2 - t1) * fair_strike_var / t2

payoff = expected_var - self.strike_variance
if not self.pay_strike_flag:
payoff = -payoff

df = libor_curve.df(self.maturity_dt)
v = payoff * self.notional * df
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/test_variance_swap_direction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from decimal import Decimal, localcontext

import pytest

from financepy.market.curves.flat_discount_curve import FlatDiscountCurve
from financepy.products.equity.equity_variance_swap import EquityVarianceSwap
from financepy.products.fx.fx_variance_swap import FinFXVarianceSwap
from financepy.utils.date import Date


@pytest.mark.parametrize("swap_type", [EquityVarianceSwap, FinFXVarianceSwap])
@pytest.mark.parametrize("pay_strike", [None, True, False], ids=["default", "pay", "receive"])
@pytest.mark.parametrize(
"elapsed, realised, remaining",
[
(0, "0.25", "0.09"),
(91, "0.09", "0.01"),
(200, "0.08", "0.01"),
(365, "0.01", "0.25"),
(365, "0.04", "0.25"),
],
ids=["inception", "mid_below", "mid_above", "maturity", "par"],
)
def test_variance_swap_direction(swap_type, pay_strike, elapsed, realised, remaining):
"""Receiving the fixed strike reverses the full discounted net payoff."""
start = Date(1, 1, 2025)
maturity = Date(1, 1, 2026)
value_dt = start.add_days(elapsed)
curve = FlatDiscountCurve(value_dt, 0.031) # continuous ACT/365F
kwargs = {} if pay_strike is None else {"pay_strike_flag": pay_strike}
swap = swap_type(start, maturity, 0.04, notional=2_000_000.0, **kwargs)

# Exact 365-day payoff identity, written without FinancePy day count or
# discount routines. Calendar days are known from the fixture.
with localcontext() as ctx:
ctx.prec = 60
total = Decimal(365)
elapsed = Decimal(elapsed)
left = total - elapsed
df = (-Decimal("0.031") * left / total).exp()
received_var = (elapsed * Decimal(realised) + left * Decimal(remaining)) / total
signed = -1 if pay_strike is False else 1
expected = signed * df * Decimal(2_000_000) * (received_var - Decimal("0.04"))

actual = swap.value(value_dt, float(realised), float(remaining), curve)
# The library flat curve floors exact zero time at 1e-12, so its
# same-day discount can differ from one by a few dozen ULPs.
assert actual == pytest.approx(float(expected), rel=1e-12, abs=1e-10)