Skip to main content

nautilus_trading/python/
sessions.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 std::str::FromStr;
17
18use chrono::{DateTime, Utc};
19use nautilus_core::python::to_pyvalue_err;
20use nautilus_model::python::common::EnumIterator;
21use pyo3::{PyTypeInfo, prelude::*, types::PyType};
22
23use crate::sessions::{
24    ForexSession, fx_local_from_utc, fx_next_end, fx_next_start, fx_prev_end, fx_prev_start,
25};
26
27#[pymethods]
28#[pyo3_stub_gen::derive::gen_stub_pymethods]
29impl ForexSession {
30    /// Represents a major Forex market session based on trading hours.
31    #[new]
32    fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
33        let t = Self::type_object(py);
34        Self::py_from_str(&t, value)
35    }
36
37    const fn __hash__(&self) -> isize {
38        *self as isize
39    }
40
41    fn __repr__(&self) -> String {
42        format!(
43            "<{}.{}: '{}'>",
44            stringify!(PositionSide),
45            self.name(),
46            self.value(),
47        )
48    }
49
50    fn __str__(&self) -> String {
51        self.to_string()
52    }
53
54    #[getter]
55    #[must_use]
56    pub fn name(&self) -> String {
57        self.to_string()
58    }
59
60    #[getter]
61    #[must_use]
62    pub const fn value(&self) -> u8 {
63        *self as u8
64    }
65
66    #[classmethod]
67    fn variants(_: &Bound<'_, PyType>, py: Python<'_>) -> EnumIterator {
68        EnumIterator::new::<Self>(py)
69    }
70
71    #[classmethod]
72    #[pyo3(name = "from_str")]
73    fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
74        let data_str: &str = data.extract()?;
75        let tokenized = data_str.to_uppercase();
76        Self::from_str(&tokenized).map_err(to_pyvalue_err)
77    }
78}
79
80/// Converts a UTC timestamp to the local time for the given Forex session.
81#[pyfunction]
82#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.trading")]
83#[pyo3(name = "fx_local_from_utc")]
84pub fn py_fx_local_from_utc(session: ForexSession, time_now: DateTime<Utc>) -> PyResult<String> {
85    Ok(fx_local_from_utc(session, time_now).to_rfc3339())
86}
87
88/// Returns the next session start time in UTC.
89#[pyfunction]
90#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.trading")]
91#[pyo3(name = "fx_next_start")]
92pub fn py_fx_next_start(session: ForexSession, time_now: DateTime<Utc>) -> PyResult<DateTime<Utc>> {
93    Ok(fx_next_start(session, time_now))
94}
95
96/// Returns the next session end time in UTC.
97#[pyfunction]
98#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.trading")]
99#[pyo3(name = "fx_next_end")]
100pub fn py_fx_next_end(session: ForexSession, time_now: DateTime<Utc>) -> PyResult<DateTime<Utc>> {
101    Ok(fx_next_end(session, time_now))
102}
103
104/// Returns the previous session start time in UTC.
105#[pyfunction]
106#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.trading")]
107#[pyo3(name = "fx_prev_start")]
108pub fn py_fx_prev_start(session: ForexSession, time_now: DateTime<Utc>) -> PyResult<DateTime<Utc>> {
109    Ok(fx_prev_start(session, time_now))
110}
111
112/// Returns the previous session end time in UTC.
113#[pyfunction]
114#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.trading")]
115#[pyo3(name = "fx_prev_end")]
116pub fn py_fx_prev_end(session: ForexSession, time_now: DateTime<Utc>) -> PyResult<DateTime<Utc>> {
117    Ok(fx_prev_end(session, time_now))
118}