nautilus_trading/python/
sessions.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 std::str::FromStr;
17
18use chrono::{DateTime, Utc};
19use chrono_tz::Tz;
20use nautilus_core::python::to_pyvalue_err;
21use nautilus_model::python::common::EnumIterator;
22use pyo3::{PyTypeInfo, prelude::*, types::PyType};
23
24use crate::sessions::{
25    ForexSession, fx_local_from_utc, fx_next_end, fx_next_start, fx_prev_end, fx_prev_start,
26};
27
28#[pymethods]
29impl ForexSession {
30    #[new]
31    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
32        let t = Self::type_object(py);
33        Self::py_from_str(&t, value)
34    }
35
36    const fn __hash__(&self) -> isize {
37        *self as isize
38    }
39
40    fn __repr__(&self) -> String {
41        format!(
42            "<{}.{}: '{}'>",
43            stringify!(PositionSide),
44            self.name(),
45            self.value(),
46        )
47    }
48
49    fn __str__(&self) -> String {
50        self.to_string()
51    }
52
53    #[getter]
54    #[must_use]
55    pub fn name(&self) -> String {
56        self.to_string()
57    }
58
59    #[getter]
60    #[must_use]
61    pub const fn value(&self) -> u8 {
62        *self as u8
63    }
64
65    #[classmethod]
66    fn variants(_: &Bound<'_, PyType>, py: Python<'_>) -> EnumIterator {
67        EnumIterator::new::<Self>(py)
68    }
69
70    #[classmethod]
71    #[pyo3(name = "from_str")]
72    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
73        let data_str: &str = data.extract()?;
74        let tokenized = data_str.to_uppercase();
75        Self::from_str(&tokenized).map_err(to_pyvalue_err)
76    }
77
78    #[classattr]
79    #[pyo3(name = "SYDNEY")]
80    const fn py_no_position_side() -> Self {
81        Self::Sydney
82    }
83
84    #[classattr]
85    #[pyo3(name = "TOKYO")]
86    const fn py_flat() -> Self {
87        Self::Tokyo
88    }
89
90    #[classattr]
91    #[pyo3(name = "LONDON")]
92    const fn py_long() -> Self {
93        Self::London
94    }
95
96    #[classattr]
97    #[pyo3(name = "NEW_YORK")]
98    const fn py_short() -> Self {
99        Self::NewYork
100    }
101}
102
103/// Converts a UTC timestamp to the local time for the given Forex session.
104///
105/// The `time_now` must be timezone-aware with its tzinfo set to a built-in `datetime.timezone`
106/// (e.g. `datetime.timezone.utc`). Third-party tzinfo objects (like those from `pytz`) are not supported.
107#[pyfunction]
108#[pyo3(name = "fx_local_from_utc")]
109pub fn py_fx_local_from_utc(
110    session: ForexSession,
111    time_now: DateTime<Utc>,
112) -> PyResult<DateTime<Tz>> {
113    Ok(fx_local_from_utc(session, time_now))
114}
115
116/// Returns the next session start time in UTC.
117///
118/// The `time_now` must be timezone-aware with its tzinfo set to a built-in `datetime.timezone`
119/// (e.g. `datetime.timezone.utc`). Third-party tzinfo objects (like those from `pytz`) are not supported.
120#[pyfunction]
121#[pyo3(name = "fx_next_start")]
122pub fn py_fx_next_start(session: ForexSession, time_now: DateTime<Utc>) -> PyResult<DateTime<Utc>> {
123    Ok(fx_next_start(session, time_now))
124}
125
126/// Returns the next session end time in UTC.
127///
128/// The `time_now` must be timezone-aware with its tzinfo set to a built-in `datetime.timezone`
129/// (e.g. `datetime.timezone.utc`). Third-party tzinfo objects (like those from `pytz`) are not supported.
130#[pyfunction]
131#[pyo3(name = "fx_next_end")]
132pub fn py_fx_next_end(session: ForexSession, time_now: DateTime<Utc>) -> PyResult<DateTime<Utc>> {
133    Ok(fx_next_end(session, time_now))
134}
135
136/// Returns the previous session start time in UTC.
137///
138/// The `time_now` must be timezone-aware with its tzinfo set to a built-in `datetime.timezone`
139/// (e.g. `datetime.timezone.utc`). Third-party tzinfo objects (like those from `pytz`) are not supported.
140#[pyfunction]
141#[pyo3(name = "fx_prev_start")]
142pub fn py_fx_prev_start(session: ForexSession, time_now: DateTime<Utc>) -> PyResult<DateTime<Utc>> {
143    Ok(fx_prev_start(session, time_now))
144}
145
146/// Returns the previous session end time in UTC.
147///
148/// The `time_now` must be timezone-aware with its tzinfo set to a built-in `datetime.timezone`
149/// (e.g. `datetime.timezone.utc`). Third-party tzinfo objects (like those from `pytz`) are not supported.
150#[pyfunction]
151#[pyo3(name = "fx_prev_end")]
152pub fn py_fx_prev_end(session: ForexSession, time_now: DateTime<Utc>) -> PyResult<DateTime<Utc>> {
153    Ok(fx_prev_end(session, time_now))
154}