nautilus_model/python/data/
greeks.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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    black_scholes_greeks, imply_vol, imply_vol_and_greeks, BlackScholesGreeksResult,
20    ImplyVolAndGreeksResult,
21};
22
23#[pymethods]
24impl ImplyVolAndGreeksResult {
25    /// Creates a new [`ImplyVolAndGreeksResult`] instance.
26    #[new]
27    fn py_new(vol: f64, price: f64, delta: f64, gamma: f64, theta: f64, vega: f64) -> Self {
28        Self {
29            vol,
30            price,
31            delta,
32            gamma,
33            theta,
34            vega,
35        }
36    }
37
38    #[getter]
39    #[pyo3(name = "vol")]
40    fn py_vol(&self) -> f64 {
41        self.vol
42    }
43
44    #[getter]
45    #[pyo3(name = "price")]
46    fn py_price(&self) -> f64 {
47        self.price
48    }
49
50    #[getter]
51    #[pyo3(name = "delta")]
52    fn py_delta(&self) -> f64 {
53        self.delta
54    }
55
56    #[getter]
57    #[pyo3(name = "gamma")]
58    fn py_gamma(&self) -> f64 {
59        self.gamma
60    }
61
62    #[getter]
63    #[pyo3(name = "vega")]
64    fn py_vega(&self) -> f64 {
65        self.vega
66    }
67
68    #[getter]
69    #[pyo3(name = "theta")]
70    fn py_theta(&self) -> f64 {
71        self.theta
72    }
73
74    fn __repr__(&self) -> String {
75        format!("{self:?}")
76    }
77}
78
79#[pymethods]
80impl BlackScholesGreeksResult {
81    /// Creates a new [`BlackScholesGreeksResult`] instance.
82    #[new]
83    fn py_new(price: f64, delta: f64, gamma: f64, theta: f64, vega: f64) -> Self {
84        Self {
85            price,
86            delta,
87            gamma,
88            theta,
89            vega,
90        }
91    }
92
93    #[getter]
94    #[pyo3(name = "price")]
95    fn py_price(&self) -> f64 {
96        self.price
97    }
98
99    #[getter]
100    #[pyo3(name = "delta")]
101    fn py_delta(&self) -> f64 {
102        self.delta
103    }
104
105    #[getter]
106    #[pyo3(name = "gamma")]
107    fn py_gamma(&self) -> f64 {
108        self.gamma
109    }
110
111    #[getter]
112    #[pyo3(name = "vega")]
113    fn py_vega(&self) -> f64 {
114        self.vega
115    }
116
117    #[getter]
118    #[pyo3(name = "theta")]
119    fn py_theta(&self) -> f64 {
120        self.theta
121    }
122
123    fn __repr__(&self) -> String {
124        format!("{self:?}")
125    }
126}
127
128#[pyfunction]
129#[pyo3(name = "black_scholes_greeks")]
130#[allow(clippy::too_many_arguments)]
131pub fn py_black_scholes_greeks(
132    s: f64,
133    r: f64,
134    b: f64,
135    sigma: f64,
136    is_call: bool,
137    k: f64,
138    t: f64,
139    multiplier: f64,
140) -> PyResult<BlackScholesGreeksResult> {
141    let result = black_scholes_greeks(s, r, b, sigma, is_call, k, t, multiplier);
142    Ok(result)
143}
144
145#[pyfunction]
146#[pyo3(name = "imply_vol")]
147pub fn py_imply_vol(
148    s: f64,
149    r: f64,
150    b: f64,
151    is_call: bool,
152    k: f64,
153    t: f64,
154    price: f64,
155) -> PyResult<f64> {
156    let vol = imply_vol(s, r, b, is_call, k, t, price);
157    Ok(vol)
158}
159
160#[pyfunction]
161#[pyo3(name = "imply_vol_and_greeks")]
162#[allow(clippy::too_many_arguments)]
163pub fn py_imply_vol_and_greeks(
164    s: f64,
165    r: f64,
166    b: f64,
167    is_call: bool,
168    k: f64,
169    t: f64,
170    price: f64,
171    multiplier: f64,
172) -> PyResult<ImplyVolAndGreeksResult> {
173    let result = imply_vol_and_greeks(s, r, b, is_call, k, t, price, multiplier);
174    Ok(result)
175}