Skip to main content

nautilus_model/python/data/
forward.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 nautilus_core::{UnixNanos, python::to_pyvalue_err};
17use pyo3::prelude::*;
18use rust_decimal::Decimal;
19
20use crate::{data::forward::ForwardPrice, identifiers::InstrumentId};
21
22#[pymethods]
23#[pyo3_stub_gen::derive::gen_stub_pymethods]
24impl ForwardPrice {
25    /// Represents a forward/underlying price for a derivatives instrument.
26    ///
27    /// This is a general derivatives concept used for ATM determination in option chains
28    /// and other forward-price dependent calculations.
29    #[new]
30    #[pyo3(signature = (instrument_id, forward_price, underlying_index=None, ts_event=0, ts_init=0))]
31    fn py_new(
32        instrument_id: InstrumentId,
33        forward_price: &str,
34        underlying_index: Option<String>,
35        ts_event: u64,
36        ts_init: u64,
37    ) -> PyResult<Self> {
38        let price = forward_price.parse::<Decimal>().map_err(to_pyvalue_err)?;
39        Ok(Self {
40            instrument_id,
41            forward_price: price,
42            underlying_index,
43            ts_event: UnixNanos::from(ts_event),
44            ts_init: UnixNanos::from(ts_init),
45        })
46    }
47
48    #[getter]
49    #[pyo3(name = "instrument_id")]
50    fn py_instrument_id(&self) -> InstrumentId {
51        self.instrument_id
52    }
53
54    #[getter]
55    #[pyo3(name = "forward_price")]
56    fn py_forward_price(&self) -> String {
57        self.forward_price.to_string()
58    }
59
60    #[getter]
61    #[pyo3(name = "underlying_index")]
62    fn py_underlying_index(&self) -> Option<String> {
63        self.underlying_index.clone()
64    }
65
66    #[getter]
67    #[pyo3(name = "ts_event")]
68    fn py_ts_event(&self) -> u64 {
69        self.ts_event.as_u64()
70    }
71
72    #[getter]
73    #[pyo3(name = "ts_init")]
74    fn py_ts_init(&self) -> u64 {
75        self.ts_init.as_u64()
76    }
77
78    fn __repr__(&self) -> String {
79        format!(
80            "ForwardPrice({}, price={}, index={:?})",
81            self.instrument_id, self.forward_price, self.underlying_index
82        )
83    }
84
85    fn __str__(&self) -> String {
86        format!(
87            "ForwardPrice({}, {})",
88            self.instrument_id, self.forward_price
89        )
90    }
91}