Skip to content

Commit

Permalink
Add CryptoPerpetual instrument for Rust (#1274)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipmacek authored Oct 14, 2023
1 parent 4be7b57 commit 7968b48
Show file tree
Hide file tree
Showing 14 changed files with 437 additions and 86 deletions.
20 changes: 19 additions & 1 deletion nautilus_core/core/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
// limitations under the License.
// -------------------------------------------------------------------------------------------------

use serde::{Deserialize, Serialize};
use pyo3::{prelude::*, types::PyDict, Py, PyErr, Python};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::python::to_pyvalue_err;

/// Represents types which are serializable for JSON and `MsgPack` specifications.
pub trait Serializable: Serialize + for<'de> Deserialize<'de> {
Expand All @@ -37,3 +40,18 @@ pub trait Serializable: Serialize + for<'de> Deserialize<'de> {
rmp_serde::to_vec_named(self)
}
}

#[cfg(feature = "python")]
pub fn from_dict_pyo3<T>(py: Python<'_>, values: Py<PyDict>) -> Result<T, PyErr>
where
T: DeserializeOwned,
{
// Extract to JSON string
let json_str: String = PyModule::import(py, "json")?
.call_method("dumps", (values,), None)?
.extract()?;

// Deserialize to object
let instance = serde_json::from_slice(&json_str.into_bytes()).map_err(to_pyvalue_err)?;
Ok(instance)
}
15 changes: 6 additions & 9 deletions nautilus_core/model/src/data/bar_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ use std::{
hash::{Hash, Hasher},
};

use nautilus_core::{python::to_pyvalue_err, serialization::Serializable, time::UnixNanos};
use nautilus_core::{
python::to_pyvalue_err,
serialization::{from_dict_pyo3, Serializable},
time::UnixNanos,
};
use pyo3::{prelude::*, pyclass::CompareOp, types::PyDict};

use super::bar::{Bar, BarSpecification, BarType};
Expand Down Expand Up @@ -215,14 +219,7 @@ impl Bar {
#[staticmethod]
#[pyo3(name = "from_dict")]
fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
// Extract to JSON string
let json_str: String = PyModule::import(py, "json")?
.call_method("dumps", (values,), None)?
.extract()?;

// Deserialize to object
let instance = serde_json::from_slice(&json_str.into_bytes()).map_err(to_pyvalue_err)?;
Ok(instance)
from_dict_pyo3(py, values)
}

#[staticmethod]
Expand Down
15 changes: 6 additions & 9 deletions nautilus_core/model/src/data/delta_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ use std::{
hash::{Hash, Hasher},
};

use nautilus_core::{python::to_pyvalue_err, serialization::Serializable, time::UnixNanos};
use nautilus_core::{
python::to_pyvalue_err,
serialization::{from_dict_pyo3, Serializable},
time::UnixNanos,
};
use pyo3::{prelude::*, pyclass::CompareOp, types::PyDict};

use super::{delta::OrderBookDelta, order::BookOrder};
Expand Down Expand Up @@ -126,14 +130,7 @@ impl OrderBookDelta {
#[staticmethod]
#[pyo3(name = "from_dict")]
fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
// Extract to JSON string
let json_str: String = PyModule::import(py, "json")?
.call_method("dumps", (values,), None)?
.extract()?;

// Deserialize to object
let instance = serde_json::from_slice(&json_str.into_bytes()).map_err(to_pyvalue_err)?;
Ok(instance)
from_dict_pyo3(py, values)
}

#[staticmethod]
Expand Down
14 changes: 5 additions & 9 deletions nautilus_core/model/src/data/order_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ use std::{
hash::{Hash, Hasher},
};

use nautilus_core::{python::to_pyvalue_err, serialization::Serializable};
use nautilus_core::{
python::to_pyvalue_err,
serialization::{from_dict_pyo3, Serializable},
};
use pyo3::{prelude::*, pyclass::CompareOp, types::PyDict};

use super::order::{BookOrder, OrderId};
Expand Down Expand Up @@ -109,14 +112,7 @@ impl BookOrder {
#[staticmethod]
#[pyo3(name = "from_dict")]
pub fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
// Extract to JSON string
let json_str: String = PyModule::import(py, "json")?
.call_method("dumps", (values,), None)?
.extract()?;

// Deserialize to object
let instance = serde_json::from_slice(&json_str.into_bytes()).map_err(to_pyvalue_err)?;
Ok(instance)
from_dict_pyo3(py, values)
}

#[staticmethod]
Expand Down
15 changes: 6 additions & 9 deletions nautilus_core/model/src/data/quote_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ use std::{
str::FromStr,
};

use nautilus_core::{python::to_pyvalue_err, serialization::Serializable, time::UnixNanos};
use nautilus_core::{
python::to_pyvalue_err,
serialization::{from_dict_pyo3, Serializable},
time::UnixNanos,
};
use pyo3::{
prelude::*,
pyclass::CompareOp,
Expand Down Expand Up @@ -249,14 +253,7 @@ impl QuoteTick {
#[staticmethod]
#[pyo3(name = "from_dict")]
fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
// Extract to JSON string
let json_str: String = PyModule::import(py, "json")?
.call_method("dumps", (values,), None)?
.extract()?;

// Deserialize to object
let instance = serde_json::from_slice(&json_str.into_bytes()).map_err(to_pyvalue_err)?;
Ok(instance)
from_dict_pyo3(py, values)
}

#[staticmethod]
Expand Down
15 changes: 6 additions & 9 deletions nautilus_core/model/src/data/ticker_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ use std::{
hash::{Hash, Hasher},
};

use nautilus_core::{python::to_pyvalue_err, serialization::Serializable, time::UnixNanos};
use nautilus_core::{
python::to_pyvalue_err,
serialization::{from_dict_pyo3, Serializable},
time::UnixNanos,
};
use pyo3::{prelude::*, pyclass::CompareOp, types::PyDict};

use super::ticker::Ticker;
Expand Down Expand Up @@ -90,14 +94,7 @@ impl Ticker {
#[staticmethod]
#[pyo3(name = "from_dict")]
pub fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
// Extract to JSON string
let json_str: String = PyModule::import(py, "json")?
.call_method("dumps", (values,), None)?
.extract()?;

// Deserialize to object
let instance = serde_json::from_slice(&json_str.into_bytes()).map_err(to_pyvalue_err)?;
Ok(instance)
from_dict_pyo3(py, values)
}

#[staticmethod]
Expand Down
15 changes: 6 additions & 9 deletions nautilus_core/model/src/data/trade_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ use std::{
str::FromStr,
};

use nautilus_core::{python::to_pyvalue_err, serialization::Serializable, time::UnixNanos};
use nautilus_core::{
python::to_pyvalue_err,
serialization::{from_dict_pyo3, Serializable},
time::UnixNanos,
};
use pyo3::{
prelude::*,
pyclass::CompareOp,
Expand Down Expand Up @@ -201,14 +205,7 @@ impl TradeTick {
#[staticmethod]
#[pyo3(name = "from_dict")]
fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
// Extract to JSON string
let json_str: String = PyModule::import(py, "json")?
.call_method("dumps", (values,), None)?
.extract()?;

// Deserialize to object
let instance = serde_json::from_slice(&json_str.into_bytes()).map_err(to_pyvalue_err)?;
Ok(instance)
from_dict_pyo3(py, values)
}

#[staticmethod]
Expand Down
Loading

0 comments on commit 7968b48

Please sign in to comment.