File size: 2,309 Bytes
72fc0ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
QuantLib Pro SDK — Volatility Resource
"""
from typing import Any, Dict, List
from quantlib_api.resources.base import BaseResource


class VolatilityResource(BaseResource):
    """Volatility surface construction and analysis."""

    PREFIX = "/api/v1/volatility"

    def surface(
        self,
        ticker: str,
        expiries: List[int] = None,
        moneyness_range: List[float] = None,
    ) -> Dict[str, Any]:
        """
        Construct implied volatility surface.

        Parameters
        ----------
        ticker : str
            Underlying ticker
        expiries : list of int
            Days to expiration (e.g., [30, 60, 90, 180, 365])
        moneyness_range : list of float
            Moneyness levels (e.g., [0.8, 0.9, 1.0, 1.1, 1.2])

        Returns
        -------
        dict
            IV surface grid and metadata
        """
        return self._http.post(
            self._url("/surface"),
            json={
                "ticker": ticker,
                "expiries": expiries or [30, 60, 90, 180, 365],
                "moneyness_range": moneyness_range or [0.8, 0.9, 0.95, 1.0, 1.05, 1.1, 1.2],
            },
        )

    def term_structure(
        self,
        ticker: str,
        expiries: List[int] = None,
    ) -> Dict[str, Any]:
        """Get ATM volatility term structure."""
        return self._http.post(
            self._url("/term-structure"),
            json={
                "ticker": ticker,
                "expiries": expiries or [7, 14, 30, 60, 90, 180, 365],
            },
        )

    def smile(
        self,
        ticker: str,
        expiry_days: int = 30,
    ) -> Dict[str, Any]:
        """Get volatility smile for a specific expiry."""
        return self._http.post(
            self._url("/smile"),
            json={"ticker": ticker, "expiry_days": expiry_days},
        )

    def garch(
        self,
        ticker: str,
        lookback_days: int = 252,
        forecast_days: int = 20,
    ) -> Dict[str, Any]:
        """Fit GARCH model and forecast volatility."""
        return self._http.post(
            self._url("/garch"),
            json={
                "ticker": ticker,
                "lookback_days": lookback_days,
                "forecast_days": forecast_days,
            },
        )