nautilus_model/python/data/
greeks.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use pyo3::prelude::*;
17
18use crate::data::greeks::{
19    BlackScholesGreeksResult, black_scholes_greeks, imply_vol, imply_vol_and_greeks,
20    refine_vol_and_greeks,
21};
22
23#[cfg(feature = "python")]
24#[pymethods]
25impl BlackScholesGreeksResult {
26    #[getter]
27    fn price(&self) -> f64 {
28        self.price
29    }
30
31    #[getter]
32    fn vol(&self) -> f64 {
33        self.vol
34    }
35
36    #[getter]
37    fn delta(&self) -> f64 {
38        self.delta
39    }
40
41    #[getter]
42    fn gamma(&self) -> f64 {
43        self.gamma
44    }
45
46    #[getter]
47    fn vega(&self) -> f64 {
48        self.vega
49    }
50
51    #[getter]
52    fn theta(&self) -> f64 {
53        self.theta
54    }
55}
56
57/// Computes Black-Scholes greeks for given parameters using the fast compute_greeks implementation.
58///
59/// # Errors
60///
61/// Returns a `PyErr` if the greeks calculation fails.
62#[pyfunction]
63#[pyo3(name = "black_scholes_greeks")]
64#[allow(clippy::too_many_arguments)]
65pub fn py_black_scholes_greeks(
66    s: f64,
67    r: f64,
68    b: f64,
69    vol: f64,
70    is_call: bool,
71    k: f64,
72    t: f64,
73    multiplier: f64,
74) -> PyResult<BlackScholesGreeksResult> {
75    Ok(black_scholes_greeks(
76        s, r, b, vol, is_call, k, t, multiplier,
77    ))
78}
79
80/// Computes the implied volatility for an option given its parameters and market price.
81///
82/// # Errors
83///
84/// Returns a `PyErr` if implied volatility calculation fails.
85#[pyfunction]
86#[pyo3(name = "imply_vol")]
87pub fn py_imply_vol(
88    s: f64,
89    r: f64,
90    b: f64,
91    is_call: bool,
92    k: f64,
93    t: f64,
94    price: f64,
95) -> PyResult<f64> {
96    let vol = imply_vol(s, r, b, is_call, k, t, price);
97    Ok(vol)
98}
99
100/// Computes implied volatility and option greeks for given parameters and market price.
101/// This function uses compute_greeks after implying volatility.
102///
103/// # Errors
104///
105/// Returns a `PyErr` if calculation fails.
106#[pyfunction]
107#[pyo3(name = "imply_vol_and_greeks")]
108#[allow(clippy::too_many_arguments)]
109pub fn py_imply_vol_and_greeks(
110    s: f64,
111    r: f64,
112    b: f64,
113    is_call: bool,
114    k: f64,
115    t: f64,
116    price: f64,
117    multiplier: f64,
118) -> PyResult<BlackScholesGreeksResult> {
119    Ok(imply_vol_and_greeks(
120        s, r, b, is_call, k, t, price, multiplier,
121    ))
122}
123
124/// Refines implied volatility using an initial guess and computes greeks.
125/// This function uses compute_iv_and_greeks which performs a Halley iteration
126/// to refine the volatility estimate from an initial guess.
127///
128/// # Errors
129///
130/// Returns a `PyErr` if calculation fails.
131#[pyfunction]
132#[pyo3(name = "refine_vol_and_greeks")]
133#[allow(clippy::too_many_arguments)]
134pub fn py_refine_vol_and_greeks(
135    s: f64,
136    r: f64,
137    b: f64,
138    is_call: bool,
139    k: f64,
140    t: f64,
141    target_price: f64,
142    initial_vol: f64,
143    multiplier: f64,
144) -> PyResult<BlackScholesGreeksResult> {
145    Ok(refine_vol_and_greeks(
146        s,
147        r,
148        b,
149        is_call,
150        k,
151        t,
152        target_price,
153        initial_vol,
154        multiplier,
155    ))
156}