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 #[getter]
57 fn itm_prob(&self) -> f64 {
58 self.itm_prob
59 }
60}
61
62/// Computes Black-Scholes greeks for given parameters using the fast compute_greeks implementation.
63///
64/// # Errors
65///
66/// Returns a `PyErr` if the greeks calculation fails.
67#[pyfunction]
68#[pyo3(name = "black_scholes_greeks")]
69#[allow(clippy::too_many_arguments)]
70pub fn py_black_scholes_greeks(
71 s: f64,
72 r: f64,
73 b: f64,
74 vol: f64,
75 is_call: bool,
76 k: f64,
77 t: f64,
78) -> PyResult<BlackScholesGreeksResult> {
79 Ok(black_scholes_greeks(s, r, b, vol, is_call, k, t))
80}
81
82/// Computes the implied volatility for an option given its parameters and market price.
83///
84/// # Errors
85///
86/// Returns a `PyErr` if implied volatility calculation fails.
87#[pyfunction]
88#[pyo3(name = "imply_vol")]
89pub fn py_imply_vol(
90 s: f64,
91 r: f64,
92 b: f64,
93 is_call: bool,
94 k: f64,
95 t: f64,
96 price: f64,
97) -> PyResult<f64> {
98 let vol = imply_vol(s, r, b, is_call, k, t, price);
99 Ok(vol)
100}
101
102/// Computes implied volatility and option greeks for given parameters and market price.
103/// This function uses compute_greeks after implying volatility.
104///
105/// # Errors
106///
107/// Returns a `PyErr` if calculation fails.
108#[pyfunction]
109#[pyo3(name = "imply_vol_and_greeks")]
110#[allow(clippy::too_many_arguments)]
111pub fn py_imply_vol_and_greeks(
112 s: f64,
113 r: f64,
114 b: f64,
115 is_call: bool,
116 k: f64,
117 t: f64,
118 price: f64,
119) -> PyResult<BlackScholesGreeksResult> {
120 Ok(imply_vol_and_greeks(s, r, b, is_call, k, t, price))
121}
122
123/// Refines implied volatility using an initial guess and computes greeks.
124/// This function uses compute_iv_and_greeks which performs a Halley iteration
125/// to refine the volatility estimate from an initial guess.
126///
127/// # Errors
128///
129/// Returns a `PyErr` if calculation fails.
130#[pyfunction]
131#[pyo3(name = "refine_vol_and_greeks")]
132#[allow(clippy::too_many_arguments)]
133pub fn py_refine_vol_and_greeks(
134 s: f64,
135 r: f64,
136 b: f64,
137 is_call: bool,
138 k: f64,
139 t: f64,
140 target_price: f64,
141 initial_vol: f64,
142) -> PyResult<BlackScholesGreeksResult> {
143 Ok(refine_vol_and_greeks(
144 s,
145 r,
146 b,
147 is_call,
148 k,
149 t,
150 target_price,
151 initial_vol,
152 ))
153}