Skip to content

Commit

Permalink
Move Bar FromPyObject impl
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Apr 18, 2024
1 parent 5a311db commit 26db548
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 50 deletions.
50 changes: 0 additions & 50 deletions nautilus_core/model/src/data/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ use std::{

use indexmap::IndexMap;
use nautilus_core::{nanos::UnixNanos, serialization::Serializable};
#[cfg(feature = "python")]
use pyo3::{PyAny, PyResult};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::{
Expand Down Expand Up @@ -281,54 +279,6 @@ impl Bar {
metadata.insert("ts_init".to_string(), "UInt64".to_string());
metadata
}

/// Create a new [`Bar`] extracted from the given [`PyAny`].
#[cfg(feature = "python")]
pub fn from_pyobject(obj: &PyAny) -> PyResult<Self> {
use nautilus_core::python::to_pyvalue_err;

let bar_type_obj: &PyAny = obj.getattr("bar_type")?.extract()?;
let bar_type_str = bar_type_obj.call_method0("__str__")?.extract()?;
let bar_type = BarType::from_str(bar_type_str)
.map_err(to_pyvalue_err)
.unwrap();

let open_py: &PyAny = obj.getattr("open")?;
let price_prec: u8 = open_py.getattr("precision")?.extract()?;
let open_raw: i64 = open_py.getattr("raw")?.extract()?;
let open = Price::from_raw(open_raw, price_prec).map_err(to_pyvalue_err)?;

let high_py: &PyAny = obj.getattr("high")?;
let high_raw: i64 = high_py.getattr("raw")?.extract()?;
let high = Price::from_raw(high_raw, price_prec).map_err(to_pyvalue_err)?;

let low_py: &PyAny = obj.getattr("low")?;
let low_raw: i64 = low_py.getattr("raw")?.extract()?;
let low = Price::from_raw(low_raw, price_prec).map_err(to_pyvalue_err)?;

let close_py: &PyAny = obj.getattr("close")?;
let close_raw: i64 = close_py.getattr("raw")?.extract()?;
let close = Price::from_raw(close_raw, price_prec).map_err(to_pyvalue_err)?;

let volume_py: &PyAny = obj.getattr("volume")?;
let volume_raw: u64 = volume_py.getattr("raw")?.extract()?;
let volume_prec: u8 = volume_py.getattr("precision")?.extract()?;
let volume = Quantity::from_raw(volume_raw, volume_prec).map_err(to_pyvalue_err)?;

let ts_event: u64 = obj.getattr("ts_event")?.extract()?;
let ts_init: u64 = obj.getattr("ts_init")?.extract()?;

Ok(Self::new(
bar_type,
open,
high,
low,
close,
volume,
ts_event.into(),
ts_init.into(),
))
}
}

impl Serializable for Bar {}
Expand Down
46 changes: 46 additions & 0 deletions nautilus_core/model/src/python/data/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,52 @@ impl BarType {
}
}

impl Bar {
pub fn from_pyobject(obj: &PyAny) -> PyResult<Self> {
let bar_type_obj: &PyAny = obj.getattr("bar_type")?.extract()?;
let bar_type_str = bar_type_obj.call_method0("__str__")?.extract()?;
let bar_type = BarType::from_str(bar_type_str)
.map_err(to_pyvalue_err)
.unwrap();

let open_py: &PyAny = obj.getattr("open")?;
let price_prec: u8 = open_py.getattr("precision")?.extract()?;
let open_raw: i64 = open_py.getattr("raw")?.extract()?;
let open = Price::from_raw(open_raw, price_prec).map_err(to_pyvalue_err)?;

let high_py: &PyAny = obj.getattr("high")?;
let high_raw: i64 = high_py.getattr("raw")?.extract()?;
let high = Price::from_raw(high_raw, price_prec).map_err(to_pyvalue_err)?;

let low_py: &PyAny = obj.getattr("low")?;
let low_raw: i64 = low_py.getattr("raw")?.extract()?;
let low = Price::from_raw(low_raw, price_prec).map_err(to_pyvalue_err)?;

let close_py: &PyAny = obj.getattr("close")?;
let close_raw: i64 = close_py.getattr("raw")?.extract()?;
let close = Price::from_raw(close_raw, price_prec).map_err(to_pyvalue_err)?;

let volume_py: &PyAny = obj.getattr("volume")?;
let volume_raw: u64 = volume_py.getattr("raw")?.extract()?;
let volume_prec: u8 = volume_py.getattr("precision")?.extract()?;
let volume = Quantity::from_raw(volume_raw, volume_prec).map_err(to_pyvalue_err)?;

let ts_event: u64 = obj.getattr("ts_event")?.extract()?;
let ts_init: u64 = obj.getattr("ts_init")?.extract()?;

Ok(Self::new(
bar_type,
open,
high,
low,
close,
volume,
ts_event.into(),
ts_init.into(),
))
}
}

#[pymethods]
#[allow(clippy::too_many_arguments)]
impl Bar {
Expand Down

0 comments on commit 26db548

Please sign in to comment.