From 0ba83f288cfd6a327aeda092885cc5bd740bf3dc Mon Sep 17 00:00:00 2001 From: Kiril1Shevtsov Date: Sun, 22 Nov 2026 20:25:11 -0500 Subject: [PATCH] Honor payer and receiver direction in variance swap valuation --- .../products/equity/equity_variance_swap.py | 9 ++-- financepy/products/fx/fx_variance_swap.py | 9 ++-- tests/unit/test_variance_swap_direction.py | 48 +++++++++++++++++++ 3 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 tests/unit/test_variance_swap_direction.py diff --git a/financepy/products/equity/equity_variance_swap.py b/financepy/products/equity/equity_variance_swap.py index c17ea5b4..e53c0740 100644 --- a/financepy/products/equity/equity_variance_swap.py +++ b/financepy/products/equity/equity_variance_swap.py @@ -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") @@ -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 diff --git a/financepy/products/fx/fx_variance_swap.py b/financepy/products/fx/fx_variance_swap.py index 4dc08902..398f9ca8 100644 --- a/financepy/products/fx/fx_variance_swap.py +++ b/financepy/products/fx/fx_variance_swap.py @@ -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") @@ -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 diff --git a/tests/unit/test_variance_swap_direction.py b/tests/unit/test_variance_swap_direction.py new file mode 100644 index 00000000..5cf0204e --- /dev/null +++ b/tests/unit/test_variance_swap_direction.py @@ -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)