nautilus_model/data/
greeks.rs

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2024 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

use implied_vol::{implied_black_volatility, norm_cdf, norm_pdf};

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
)]
pub struct BlackScholesGreeksResult {
    pub price: f64,
    pub delta: f64,
    pub gamma: f64,
    pub vega: f64,
    pub theta: f64,
}

// dS_t = S_t * (b * dt + sigma * dW_t) (stock)
// dC_t = r * C_t * dt (cash numeraire)
#[allow(clippy::too_many_arguments)]
pub fn black_scholes_greeks(
    s: f64,
    r: f64,
    b: f64,
    sigma: f64,
    is_call: bool,
    k: f64,
    t: f64,
    multiplier: f64,
) -> BlackScholesGreeksResult {
    let phi = if is_call { 1.0 } else { -1.0 };
    let scaled_vol = sigma * t.sqrt();
    let d1 = ((s / k).ln() + (b + 0.5 * sigma.powi(2)) * t) / scaled_vol;
    let d2 = d1 - scaled_vol;
    let cdf_phi_d1 = norm_cdf(phi * d1);
    let cdf_phi_d2 = norm_cdf(phi * d2);
    let dist_d1 = norm_pdf(d1);
    let df = ((b - r) * t).exp();
    let s_t = s * df;
    let k_t = k * (-r * t).exp();

    let price = multiplier * phi * (s_t * cdf_phi_d1 - k_t * cdf_phi_d2);
    let delta = multiplier * phi * df * cdf_phi_d1;
    let gamma = multiplier * df * dist_d1 / (s * scaled_vol);
    let vega = multiplier * s_t * t.sqrt() * dist_d1 * 0.01; // in absolute percent change
    let theta = multiplier
        * (s_t * (-dist_d1 * sigma / (2.0 * t.sqrt()) - phi * (b - r) * cdf_phi_d1)
            - phi * r * k_t * cdf_phi_d2)
        * 0.0027378507871321013; // 1 / 365.25 in change per calendar day

    BlackScholesGreeksResult {
        price,
        delta,
        gamma,
        vega,
        theta,
    }
}

pub fn imply_vol(s: f64, r: f64, b: f64, is_call: bool, k: f64, t: f64, price: f64) -> f64 {
    let forward = s * b.exp();
    let forward_price = price * (r * t).exp();

    implied_black_volatility(forward_price, forward, k, t, is_call)
}

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
)]
pub struct ImplyVolAndGreeksResult {
    pub vol: f64,
    pub price: f64,
    pub delta: f64,
    pub gamma: f64,
    pub vega: f64,
    pub theta: f64,
}

#[allow(clippy::too_many_arguments)]
pub fn imply_vol_and_greeks(
    s: f64,
    r: f64,
    b: f64,
    is_call: bool,
    k: f64,
    t: f64,
    price: f64,
    multiplier: f64,
) -> ImplyVolAndGreeksResult {
    let vol = imply_vol(s, r, b, is_call, k, t, price);
    let greeks = black_scholes_greeks(s, r, b, vol, is_call, k, t, multiplier);

    ImplyVolAndGreeksResult {
        vol,
        price: greeks.price,
        delta: greeks.delta,
        gamma: greeks.gamma,
        vega: greeks.vega,
        theta: greeks.theta,
    }
}

#[test]
fn test_greeks_accuracy_call() {
    let s = 100.0;
    let k = 100.1;
    let t = 1.0;
    let r = 0.01;
    let b = 0.005;
    let sigma = 0.2;
    let is_call = true;
    let eps = 1e-3;

    let greeks = black_scholes_greeks(s, r, b, sigma, is_call, k, t, 1.0);

    let price0 = |s: f64| black_scholes_greeks(s, r, b, sigma, is_call, k, t, 1.0).price;

    let delta_bnr = (price0(s + eps) - price0(s - eps)) / (2.0 * eps);
    let gamma_bnr = (price0(s + eps) + price0(s - eps) - 2.0 * price0(s)) / (eps * eps);
    let vega_bnr = (black_scholes_greeks(s, r, b, sigma + eps, is_call, k, t, 1.0).price
        - black_scholes_greeks(s, r, b, sigma - eps, is_call, k, t, 1.0).price)
        / (2.0 * eps)
        / 100.0;
    let theta_bnr = (black_scholes_greeks(s, r, b, sigma, is_call, k, t - eps, 1.0).price
        - black_scholes_greeks(s, r, b, sigma, is_call, k, t + eps, 1.0).price)
        / (2.0 * eps)
        / 365.25;

    let tolerance = 1e-5;
    assert!(
        (greeks.delta - delta_bnr).abs() < tolerance,
        "Delta difference exceeds tolerance"
    );
    assert!(
        (greeks.gamma - gamma_bnr).abs() < tolerance,
        "Gamma difference exceeds tolerance"
    );
    assert!(
        (greeks.vega - vega_bnr).abs() < tolerance,
        "Vega difference exceeds tolerance"
    );
    assert!(
        (greeks.theta - theta_bnr).abs() < tolerance,
        "Theta difference exceeds tolerance"
    );
}

#[test]
fn test_greeks_accuracy_put() {
    let s = 100.0;
    let k = 100.1;
    let t = 1.0;
    let r = 0.01;
    let b = 0.005;
    let sigma = 0.2;
    let is_call = false;
    let eps = 1e-3;

    let greeks = black_scholes_greeks(s, r, b, sigma, is_call, k, t, 1.0);

    let price0 = |s: f64| black_scholes_greeks(s, r, b, sigma, is_call, k, t, 1.0).price;

    let delta_bnr = (price0(s + eps) - price0(s - eps)) / (2.0 * eps);
    let gamma_bnr = (price0(s + eps) + price0(s - eps) - 2.0 * price0(s)) / (eps * eps);
    let vega_bnr = (black_scholes_greeks(s, r, b, sigma + eps, is_call, k, t, 1.0).price
        - black_scholes_greeks(s, r, b, sigma - eps, is_call, k, t, 1.0).price)
        / (2.0 * eps)
        / 100.0;
    let theta_bnr = (black_scholes_greeks(s, r, b, sigma, is_call, k, t - eps, 1.0).price
        - black_scholes_greeks(s, r, b, sigma, is_call, k, t + eps, 1.0).price)
        / (2.0 * eps)
        / 365.25;

    let tolerance = 1e-5;
    assert!(
        (greeks.delta - delta_bnr).abs() < tolerance,
        "Delta difference exceeds tolerance"
    );
    assert!(
        (greeks.gamma - gamma_bnr).abs() < tolerance,
        "Gamma difference exceeds tolerance"
    );
    assert!(
        (greeks.vega - vega_bnr).abs() < tolerance,
        "Vega difference exceeds tolerance"
    );
    assert!(
        (greeks.theta - theta_bnr).abs() < tolerance,
        "Theta difference exceeds tolerance"
    );
}

#[test]
fn test_imply_vol_and_greeks_accuracy_call() {
    let s = 100.0;
    let k = 100.1;
    let t = 1.0;
    let r = 0.01;
    let b = 0.005;
    let sigma = 0.2;
    let is_call = true;

    let base_greeks = black_scholes_greeks(s, r, b, sigma, is_call, k, t, 1.0);
    let price = base_greeks.price;

    let implied_result = imply_vol_and_greeks(s, r, b, is_call, k, t, price, 1.0);

    let tolerance = 1e-5;
    assert!(
        (implied_result.vol - sigma).abs() < tolerance,
        "Vol difference exceeds tolerance"
    );
    assert!(
        (implied_result.price - base_greeks.price).abs() < tolerance,
        "Price difference exceeds tolerance"
    );
    assert!(
        (implied_result.delta - base_greeks.delta).abs() < tolerance,
        "Delta difference exceeds tolerance"
    );
    assert!(
        (implied_result.gamma - base_greeks.gamma).abs() < tolerance,
        "Gamma difference exceeds tolerance"
    );
    assert!(
        (implied_result.vega - base_greeks.vega).abs() < tolerance,
        "Vega difference exceeds tolerance"
    );
    assert!(
        (implied_result.theta - base_greeks.theta).abs() < tolerance,
        "Theta difference exceeds tolerance"
    );
}

#[test]
fn test_imply_vol_and_greeks_accuracy_put() {
    let s = 100.0;
    let k = 100.1;
    let t = 1.0;
    let r = 0.01;
    let b = 0.005;
    let sigma = 0.2;
    let is_call = false;

    let base_greeks = black_scholes_greeks(s, r, b, sigma, is_call, k, t, 1.0);
    let price = base_greeks.price;

    let implied_result = imply_vol_and_greeks(s, r, b, is_call, k, t, price, 1.0);

    let tolerance = 1e-5;
    assert!(
        (implied_result.vol - sigma).abs() < tolerance,
        "Vol difference exceeds tolerance"
    );
    assert!(
        (implied_result.price - base_greeks.price).abs() < tolerance,
        "Price difference exceeds tolerance"
    );
    assert!(
        (implied_result.delta - base_greeks.delta).abs() < tolerance,
        "Delta difference exceeds tolerance"
    );
    assert!(
        (implied_result.gamma - base_greeks.gamma).abs() < tolerance,
        "Gamma difference exceeds tolerance"
    );
    assert!(
        (implied_result.vega - base_greeks.vega).abs() < tolerance,
        "Vega difference exceeds tolerance"
    );
    assert!(
        (implied_result.theta - base_greeks.theta).abs() < tolerance,
        "Theta difference exceeds tolerance"
    );
}