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