File size: 3,437 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
QuantLib Pro SDK — Options Resource
"""
from typing import Any, Dict, Optional
from quantlib_api.resources.base import BaseResource


class OptionsResource(BaseResource):
    """Options pricing and Greeks calculation."""

    PREFIX = "/api/v1/options"

    def price(
        self,
        spot: float,
        strike: float,
        expiry_days: int,
        risk_free_rate: float = 0.045,
        volatility: float = 0.25,
        option_type: str = "call",
        model: str = "black_scholes",
    ) -> Dict[str, Any]:
        """
        Price an option using specified model.

        Parameters
        ----------
        spot : float
            Current underlying price
        strike : float
            Option strike price
        expiry_days : int
            Days to expiration
        risk_free_rate : float
            Risk-free interest rate
        volatility : float
            Implied or historical volatility
        option_type : str
            "call" or "put"
        model : str
            "black_scholes", "binomial", "monte_carlo"

        Returns
        -------
        dict
            Option price and Greeks
        """
        return self._http.post(
            self._url("/price"),
            json={
                "spot": spot,
                "strike": strike,
                "expiry_days": expiry_days,
                "risk_free_rate": risk_free_rate,
                "volatility": volatility,
                "option_type": option_type,
                "model": model,
            },
        )

    def greeks(
        self,
        spot: float,
        strike: float,
        expiry_days: int,
        risk_free_rate: float = 0.045,
        volatility: float = 0.25,
        option_type: str = "call",
    ) -> Dict[str, Any]:
        """Calculate all option Greeks (Delta, Gamma, Theta, Vega, Rho)."""
        return self._http.post(
            self._url("/greeks"),
            json={
                "spot": spot,
                "strike": strike,
                "expiry_days": expiry_days,
                "risk_free_rate": risk_free_rate,
                "volatility": volatility,
                "option_type": option_type,
            },
        )

    def monte_carlo(
        self,
        spot: float,
        strike: float,
        expiry_days: int,
        volatility: float = 0.25,
        simulations: int = 10000,
        option_type: str = "call",
    ) -> Dict[str, Any]:
        """Price option using Monte Carlo simulation."""
        return self._http.post(
            self._url("/monte-carlo"),
            json={
                "spot": spot,
                "strike": strike,
                "expiry_days": expiry_days,
                "volatility": volatility,
                "simulations": simulations,
                "option_type": option_type,
            },
        )

    def implied_volatility(
        self,
        spot: float,
        strike: float,
        expiry_days: int,
        market_price: float,
        option_type: str = "call",
    ) -> Dict[str, Any]:
        """Calculate implied volatility from market price."""
        return self._http.post(
            self._url("/implied-volatility"),
            json={
                "spot": spot,
                "strike": strike,
                "expiry_days": expiry_days,
                "market_price": market_price,
                "option_type": option_type,
            },
        )