nautilus_trading/python/sessions.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3// https://poseitrader.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]
28impl ForexSession {
29 #[new]
30 fn py_new(py: Python<'_>, value: &Bound<'_, PyAny>) -> PyResult<Self> {
31 let t = Self::type_object(py);
32 Self::py_from_str(&t, value)
33 }
34
35 const fn __hash__(&self) -> isize {
36 *self as isize
37 }
38
39 fn __repr__(&self) -> String {
40 format!(
41 "<{}.{}: '{}'>",
42 stringify!(PositionSide),
43 self.name(),
44 self.value(),
45 )
46 }
47
48 fn __str__(&self) -> String {
49 self.to_string()
50 }
51
52 #[getter]
53 #[must_use]
54 pub fn name(&self) -> String {
55 self.to_string()
56 }
57
58 #[getter]
59 #[must_use]
60 pub const fn value(&self) -> u8 {
61 *self as u8
62 }
63
64 #[classmethod]
65 fn variants(_: &Bound<'_, PyType>, py: Python<'_>) -> EnumIterator {
66 EnumIterator::new::<Self>(py)
67 }
68
69 #[classmethod]
70 #[pyo3(name = "from_str")]
71 fn py_from_str(_: &Bound<'_, PyType>, data: &Bound<'_, PyAny>) -> PyResult<Self> {
72 let data_str: &str = data.extract()?;
73 let tokenized = data_str.to_uppercase();
74 Self::from_str(&tokenized).map_err(to_pyvalue_err)
75 }
76
77 #[classattr]
78 #[pyo3(name = "SYDNEY")]
79 const fn py_no_position_side() -> Self {
80 Self::Sydney
81 }
82
83 #[classattr]
84 #[pyo3(name = "TOKYO")]
85 const fn py_flat() -> Self {
86 Self::Tokyo
87 }
88
89 #[classattr]
90 #[pyo3(name = "LONDON")]
91 const fn py_long() -> Self {
92 Self::London
93 }
94
95 #[classattr]
96 #[pyo3(name = "NEW_YORK")]
97 const fn py_short() -> Self {
98 Self::NewYork
99 }
100}
101
102/// Converts a UTC timestamp to the local time for the given Forex session.
103///
104/// The `time_now` must be timezone-aware with its tzinfo set to a built-in `datetime.timezone`
105/// (e.g. `datetime.timezone.utc`). Third-party tzinfo objects (like those from `pytz`) are not supported.
106///
107/// # Errors
108///
109/// Returns a `PyErr` if an error occurs during session conversion or value conversion to Python.
110#[pyfunction]
111#[pyo3(name = "fx_local_from_utc")]
112pub fn py_fx_local_from_utc(session: ForexSession, time_now: DateTime<Utc>) -> PyResult<String> {
113 Ok(fx_local_from_utc(session, time_now).to_rfc3339())
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///
121/// # Errors
122///
123/// Returns a `PyErr` if an error occurs during session conversion or value conversion to Python.
124#[pyfunction]
125#[pyo3(name = "fx_next_start")]
126pub fn py_fx_next_start(session: ForexSession, time_now: DateTime<Utc>) -> PyResult<DateTime<Utc>> {
127 Ok(fx_next_start(session, time_now))
128}
129
130/// Returns the next session end time in UTC.
131///
132/// The `time_now` must be timezone-aware with its tzinfo set to a built-in `datetime.timezone`
133/// (e.g. `datetime.timezone.utc`). Third-party tzinfo objects (like those from `pytz`) are not supported.
134///
135/// # Errors
136///
137/// Returns a `PyErr` if an error occurs during session conversion or value conversion to Python.
138#[pyfunction]
139#[pyo3(name = "fx_next_end")]
140pub fn py_fx_next_end(session: ForexSession, time_now: DateTime<Utc>) -> PyResult<DateTime<Utc>> {
141 Ok(fx_next_end(session, time_now))
142}
143
144/// Returns the previous session start time in UTC.
145///
146/// The `time_now` must be timezone-aware with its tzinfo set to a built-in `datetime.timezone`
147/// (e.g. `datetime.timezone.utc`). Third-party tzinfo objects (like those from `pytz`) are not supported.
148///
149/// # Errors
150///
151/// Returns a `PyErr` if an error occurs during session conversion or value conversion to Python.
152#[pyfunction]
153#[pyo3(name = "fx_prev_start")]
154pub fn py_fx_prev_start(session: ForexSession, time_now: DateTime<Utc>) -> PyResult<DateTime<Utc>> {
155 Ok(fx_prev_start(session, time_now))
156}
157
158/// Returns the previous session end time in UTC.
159///
160/// The `time_now` must be timezone-aware with its tzinfo set to a built-in `datetime.timezone`
161/// (e.g. `datetime.timezone.utc`). Third-party tzinfo objects (like those from `pytz`) are not supported.
162///
163/// # Errors
164///
165/// Returns a `PyErr` if an error occurs during session conversion or value conversion to Python.
166#[pyfunction]
167#[pyo3(name = "fx_prev_end")]
168pub fn py_fx_prev_end(session: ForexSession, time_now: DateTime<Utc>) -> PyResult<DateTime<Utc>> {
169 Ok(fx_prev_end(session, time_now))
170}