nautilus_trading/python/
sessions.rs1use 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#[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#[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#[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#[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#[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}