diff --git a/docs/examples/_utils.py b/docs/examples/_utils.py index 66d00a4..e8698b7 100644 --- a/docs/examples/_utils.py +++ b/docs/examples/_utils.py @@ -5,6 +5,7 @@ from pydantic import BaseModel EXAMPLE_DIR = Path(__file__).parent +FIXTURES = EXAMPLE_DIR / "fixtures" OUT_DIR = EXAMPLE_DIR / "output" ASSET_DIR = EXAMPLE_DIR.parent / "assets" / "examples" diff --git a/quantflow_tests/fixtures/deribit_futures.json b/docs/examples/fixtures/deribit_futures.json similarity index 100% rename from quantflow_tests/fixtures/deribit_futures.json rename to docs/examples/fixtures/deribit_futures.json diff --git a/quantflow_tests/fixtures/deribit_instruments.json b/docs/examples/fixtures/deribit_instruments.json similarity index 100% rename from quantflow_tests/fixtures/deribit_instruments.json rename to docs/examples/fixtures/deribit_instruments.json diff --git a/quantflow_tests/fixtures/deribit_options.json b/docs/examples/fixtures/deribit_options.json similarity index 100% rename from quantflow_tests/fixtures/deribit_options.json rename to docs/examples/fixtures/deribit_options.json diff --git a/docs/examples/volsurface.json b/docs/examples/fixtures/volsurface_btc.json similarity index 100% rename from docs/examples/volsurface.json rename to docs/examples/fixtures/volsurface_btc.json diff --git a/quantflow_tests/fixtures/volsurface.json b/docs/examples/fixtures/volsurface_eth.json similarity index 100% rename from quantflow_tests/fixtures/volsurface.json rename to docs/examples/fixtures/volsurface_eth.json diff --git a/quantflow_tests/fixtures/yahoo_spx.json.gz b/docs/examples/fixtures/yahoo_spx.json.gz similarity index 100% rename from quantflow_tests/fixtures/yahoo_spx.json.gz rename to docs/examples/fixtures/yahoo_spx.json.gz diff --git a/docs/examples/pricing_method_comparison.py b/docs/examples/pricing_method_comparison.py index 1d5ce5a..a8b8df3 100644 --- a/docs/examples/pricing_method_comparison.py +++ b/docs/examples/pricing_method_comparison.py @@ -86,7 +86,7 @@ def _iv_error( def run_ttm(self) -> None: for ttm in self.ttms: ms = self.model.marginal(ttm) - max_log_strike = self.max_moneyness * np.sqrt(ttm) + max_log_strike = self.max_moneyness * ms.std_validated() log_strikes = ms.option_support( self.ref_n + 1, max_log_strike=max_log_strike ) diff --git a/docs/examples/spx_vol_surface.py b/docs/examples/spx_vol_surface.py index 170708d..6d0ef70 100644 --- a/docs/examples/spx_vol_surface.py +++ b/docs/examples/spx_vol_surface.py @@ -1,22 +1,14 @@ import gzip import json -from pathlib import Path -from docs.examples._utils import assets_path, print_model +from docs.examples._utils import FIXTURES, assets_path, print_model from quantflow.data.yahoo import Yahoo from quantflow.options.calibration import BNS2Calibration from quantflow.options.calibration.base import ResidualKind from quantflow.options.pricer import OptionPricer, OptionPricingMethod from quantflow.sp.bns import BNS, BNS2 -FIXTURE = ( - Path(__file__).resolve().parents[2] - / "quantflow_tests" - / "fixtures" - / "yahoo_spx.json.gz" -) - -chain = json.loads(gzip.decompress(FIXTURE.read_bytes())) +chain = json.loads(gzip.decompress((FIXTURES / "yahoo_spx.json.gz").read_bytes())) loader = Yahoo.loader_from_chain(chain, exclude_volume=1) surface = loader.surface() surface.bs() diff --git a/docs/examples/vol_surface_bns2_calibration.py b/docs/examples/vol_surface_bns2_calibration.py index c3fd19b..e17f20a 100644 --- a/docs/examples/vol_surface_bns2_calibration.py +++ b/docs/examples/vol_surface_bns2_calibration.py @@ -1,6 +1,6 @@ import json -from docs.examples._utils import assets_path, print_model +from docs.examples._utils import FIXTURES, assets_path, print_model from quantflow.options.calibration import BNS2Calibration from quantflow.options.calibration.base import ResidualKind from quantflow.options.pricer import OptionPricer, OptionPricingMethod @@ -8,7 +8,7 @@ from quantflow.sp.bns import BNS, BNS2 # Load a saved volatility surface snapshot and build the surface -with open("docs/examples/volsurface.json") as fp: +with open(FIXTURES / "volsurface_btc.json") as fp: surface: VolSurface = surface_from_inputs(VolSurfaceInputs(**json.load(fp))) surface.bs() diff --git a/docs/examples/vol_surface_bns_calibration.py b/docs/examples/vol_surface_bns_calibration.py index 43ab482..5106161 100644 --- a/docs/examples/vol_surface_bns_calibration.py +++ b/docs/examples/vol_surface_bns_calibration.py @@ -1,13 +1,13 @@ import json -from docs.examples._utils import assets_path, print_model +from docs.examples._utils import FIXTURES, assets_path, print_model from quantflow.options.calibration import BNSCalibration from quantflow.options.pricer import OptionPricer, OptionPricingMethod from quantflow.options.surface import VolSurface, VolSurfaceInputs, surface_from_inputs from quantflow.sp.bns import BNS # Load a saved volatility surface snapshot and build the surface -with open("docs/examples/volsurface.json") as fp: +with open(FIXTURES / "volsurface_btc.json") as fp: surface: VolSurface = surface_from_inputs(VolSurfaceInputs(**json.load(fp))) surface.bs() diff --git a/docs/examples/vol_surface_heston_calibration.py b/docs/examples/vol_surface_heston_calibration.py index c56570f..8b74e15 100644 --- a/docs/examples/vol_surface_heston_calibration.py +++ b/docs/examples/vol_surface_heston_calibration.py @@ -1,13 +1,13 @@ import json -from docs.examples._utils import assets_path, print_model +from docs.examples._utils import FIXTURES, assets_path, print_model from quantflow.options.calibration import HestonCalibration from quantflow.options.pricer import OptionPricer, OptionPricingMethod from quantflow.options.surface import VolSurfaceInputs, surface_from_inputs from quantflow.sp.heston import Heston # Load a saved volatility surface snapshot and build the surface -with open("docs/examples/volsurface.json") as fp: +with open(FIXTURES / "volsurface_btc.json") as fp: surface = surface_from_inputs(VolSurfaceInputs(**json.load(fp))) surface.bs() diff --git a/docs/examples/vol_surface_hestonj_calibration.py b/docs/examples/vol_surface_hestonj_calibration.py index fba69a8..0ce5cd7 100644 --- a/docs/examples/vol_surface_hestonj_calibration.py +++ b/docs/examples/vol_surface_hestonj_calibration.py @@ -1,6 +1,6 @@ import json -from docs.examples._utils import assets_path, print_model +from docs.examples._utils import FIXTURES, assets_path, print_model from quantflow.options.calibration import HestonJCalibration from quantflow.options.calibration.base import ResidualKind from quantflow.options.pricer import OptionPricer @@ -10,7 +10,7 @@ from quantflow.utils.marginal import OptionPricingMethod # Load a saved volatility surface snapshot and build the surface -with open("docs/examples/volsurface.json") as fp: +with open(FIXTURES / "volsurface_btc.json") as fp: surface: VolSurface = surface_from_inputs(VolSurfaceInputs(**json.load(fp))) surface.bs() diff --git a/docs/examples/vol_surface_inputs.py b/docs/examples/vol_surface_inputs.py index d8af85d..becaf23 100644 --- a/docs/examples/vol_surface_inputs.py +++ b/docs/examples/vol_surface_inputs.py @@ -2,11 +2,12 @@ import pandas as pd +from docs.examples._utils import FIXTURES from quantflow.options.inputs import OptionInput from quantflow.options.surface import VolSurface, VolSurfaceInputs, surface_from_inputs # Load a saved volatility surface snapshot from JSON -with open("docs/examples/volsurface.json") as fp: +with open(FIXTURES / "volsurface_btc.json") as fp: surface_inputs = VolSurfaceInputs(**json.load(fp)) # Build the VolSurface from the inputs and calculate implied volatilities diff --git a/quantflow_tests/conftest.py b/quantflow_tests/conftest.py index 730a392..7a64582 100644 --- a/quantflow_tests/conftest.py +++ b/quantflow_tests/conftest.py @@ -9,5 +9,5 @@ @pytest.fixture def vol_surface(): - inputs = load_fixture_dict("volsurface.json") + inputs = load_fixture_dict("volsurface_eth.json") return surface_from_inputs(VolSurfaceInputs(**inputs)) diff --git a/quantflow_tests/test_data_deribit.py b/quantflow_tests/test_data_deribit.py index 30b6fe5..01c8a99 100644 --- a/quantflow_tests/test_data_deribit.py +++ b/quantflow_tests/test_data_deribit.py @@ -2,9 +2,7 @@ from __future__ import annotations -import json from datetime import date, datetime -from pathlib import Path from typing import Any, AsyncIterator from unittest.mock import AsyncMock, patch @@ -12,12 +10,7 @@ from quantflow.data.deribit import Deribit from quantflow.utils.dates import as_utc - -FIXTURES = Path(__file__).parent / "fixtures" - - -def load_fixture(name: str) -> list[dict]: - return json.loads((FIXTURES / name).read_text()) +from quantflow_tests.utils import load_fixture @pytest.fixture diff --git a/quantflow_tests/test_data_yahoo.py b/quantflow_tests/test_data_yahoo.py index 1c66772..d3ee18a 100644 --- a/quantflow_tests/test_data_yahoo.py +++ b/quantflow_tests/test_data_yahoo.py @@ -11,13 +11,12 @@ import pytest from quantflow.data.yahoo import Yahoo - -FIXTURES = Path(__file__).parent / "fixtures" +from quantflow_tests.utils import load_fixture_dict @pytest.fixture def spx_chain() -> dict: - return json.loads(gzip.decompress((FIXTURES / "yahoo_spx.json.gz").read_bytes())) + return load_fixture_dict("yahoo_spx.json.gz") @pytest.fixture diff --git a/quantflow_tests/test_surface_methods.py b/quantflow_tests/test_surface_methods.py index 966dd5d..ce90919 100644 --- a/quantflow_tests/test_surface_methods.py +++ b/quantflow_tests/test_surface_methods.py @@ -10,7 +10,7 @@ @pytest.fixture def vol_surface() -> VolSurface: - inputs = load_fixture_dict("volsurface.json") + inputs = load_fixture_dict("volsurface_eth.json") return surface_from_inputs(VolSurfaceInputs(**inputs)) diff --git a/quantflow_tests/utils.py b/quantflow_tests/utils.py index a012563..f000380 100644 --- a/quantflow_tests/utils.py +++ b/quantflow_tests/utils.py @@ -1,11 +1,12 @@ +import gzip import json -from pathlib import Path from typing import cast import numpy as np import pytest from aiohttp.client_exceptions import ClientError +from docs.examples._utils import FIXTURES from quantflow.sp.base import StochasticProcess1D from quantflow.utils.marginal import Marginal1D from quantflow.utils.plot import check_plotly @@ -16,14 +17,14 @@ except ImportError: has_plotly = False -FIXTURES = Path(__file__).parent / "fixtures" - def load_fixture(name: str) -> list[dict]: return json.loads((FIXTURES / name).read_text()) def load_fixture_dict(name: str) -> dict: + if name.endswith(".gz"): + return json.loads(gzip.decompress((FIXTURES / name).read_bytes())) return json.loads((FIXTURES / name).read_text())