Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish missing pyo3 Rust Orders #1593

Merged
merged 4 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions nautilus_core/core/src/nanos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::{
cmp::Ordering,
fmt::Display,
ops::{Add, AddAssign, Deref, MulAssign, Sub, SubAssign},
str::FromStr,
};

use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -76,6 +77,20 @@ impl From<u64> for UnixNanos {
}
}

impl From<&str> for UnixNanos {
fn from(value: &str) -> Self {
UnixNanos(value.parse().unwrap())
}
}

impl FromStr for UnixNanos {
type Err = std::num::ParseIntError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
s.parse().map(UnixNanos)
}
}

// impl Into<u64> for UnixNanos {
// fn into(self) -> u64 {
// self.0
Expand Down
98 changes: 98 additions & 0 deletions nautilus_core/model/src/python/orders/limit_if_touched.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2015-2024 Nautech Systems Pty Ltd. All rights reserved.
// https://nautechsystems.io
//
// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -------------------------------------------------------------------------------------------------

use std::collections::HashMap;

use nautilus_core::uuid::UUID4;
use pyo3::prelude::*;
use ustr::Ustr;

use crate::{
enums::{ContingencyType, OrderSide, TimeInForce, TriggerType},
identifiers::{
client_order_id::ClientOrderId, exec_algorithm_id::ExecAlgorithmId,
instrument_id::InstrumentId, order_list_id::OrderListId, strategy_id::StrategyId,
trader_id::TraderId,
},
orders::{base::str_hashmap_to_ustr, limit_if_touched::LimitIfTouchedOrder},
types::{price::Price, quantity::Quantity},
};

#[pymethods]
impl LimitIfTouchedOrder {
#[new]
#[allow(clippy::too_many_arguments)]
fn py_new(
trader_id: TraderId,
strategy_id: StrategyId,
instrument_id: InstrumentId,
client_order_id: ClientOrderId,
order_side: OrderSide,
quantity: Quantity,
price: Price,
trigger_price: Price,
trigger_type: TriggerType,
time_in_force: TimeInForce,
post_only: bool,
reduce_only: bool,
quote_quantity: bool,
init_id: UUID4,
ts_init: u64,
expire_time: Option<u64>,
display_qty: Option<Quantity>,
emulation_trigger: Option<TriggerType>,
trigger_instrument_id: Option<InstrumentId>,
contingency_type: Option<ContingencyType>,
order_list_id: Option<OrderListId>,
linked_order_ids: Option<Vec<ClientOrderId>>,
parent_order_id: Option<ClientOrderId>,
exec_algorithm_id: Option<ExecAlgorithmId>,
exec_algorithm_params: Option<HashMap<String, String>>,
exec_spawn_id: Option<ClientOrderId>,
tags: Option<String>,
) -> PyResult<Self> {
let exec_algorithm_params = exec_algorithm_params.map(str_hashmap_to_ustr);
Ok(Self::new(
trader_id,
strategy_id,
instrument_id,
client_order_id,
order_side,
quantity,
price,
trigger_price,
trigger_type,
time_in_force,
expire_time.map(|x| x.into()),
post_only,
reduce_only,
quote_quantity,
display_qty,
emulation_trigger,
trigger_instrument_id,
contingency_type,
order_list_id,
linked_order_ids,
parent_order_id,
exec_algorithm_id,
exec_algorithm_params,
exec_spawn_id,
tags.map(|s| Ustr::from(&s)),
init_id,
ts_init.into(),
)
.unwrap())
}
}
3 changes: 2 additions & 1 deletion nautilus_core/model/src/python/orders/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl MarketOrder {
exec_spawn_id: Option<ClientOrderId>,
tags: Option<String>,
) -> PyResult<Self> {
let exec_algorithm_params = exec_algorithm_params.map(str_hashmap_to_ustr);
Self::new(
trader_id,
strategy_id,
Expand All @@ -81,7 +82,7 @@ impl MarketOrder {
linked_order_ids,
parent_order_id,
exec_algorithm_id,
exec_algorithm_params.map(str_hashmap_to_ustr),
exec_algorithm_params,
exec_spawn_id,
tags.map(|s| Ustr::from(&s)),
)
Expand Down
94 changes: 94 additions & 0 deletions nautilus_core/model/src/python/orders/market_if_touched.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2015-2024 Nautech Systems Pty Ltd. All rights reserved.
// https://nautechsystems.io
//
// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -------------------------------------------------------------------------------------------------

use std::collections::HashMap;

use nautilus_core::uuid::UUID4;
use pyo3::prelude::*;
use ustr::Ustr;

use crate::{
enums::{ContingencyType, OrderSide, TimeInForce, TriggerType},
identifiers::{
client_order_id::ClientOrderId, exec_algorithm_id::ExecAlgorithmId,
instrument_id::InstrumentId, order_list_id::OrderListId, strategy_id::StrategyId,
trader_id::TraderId,
},
orders::{base::str_hashmap_to_ustr, market_if_touched::MarketIfTouchedOrder},
types::{price::Price, quantity::Quantity},
};

#[pymethods]
impl MarketIfTouchedOrder {
#[new]
#[allow(clippy::too_many_arguments)]
fn py_new(
trader_id: TraderId,
strategy_id: StrategyId,
instrument_id: InstrumentId,
client_order_id: ClientOrderId,
order_side: OrderSide,
quantity: Quantity,
trigger_price: Price,
trigger_type: TriggerType,
time_in_force: TimeInForce,
reduce_only: bool,
quote_quantity: bool,
init_id: UUID4,
ts_init: u64,
expire_time: Option<u64>,
display_qty: Option<Quantity>,
emulation_trigger: Option<TriggerType>,
trigger_instrument_id: Option<InstrumentId>,
contingency_type: Option<ContingencyType>,
order_list_id: Option<OrderListId>,
linked_order_ids: Option<Vec<ClientOrderId>>,
parent_order_id: Option<ClientOrderId>,
exec_algorithm_id: Option<ExecAlgorithmId>,
exec_algorithm_params: Option<HashMap<String, String>>,
exec_spawn_id: Option<ClientOrderId>,
tags: Option<String>,
) -> PyResult<Self> {
let exec_algorithm_params = exec_algorithm_params.map(str_hashmap_to_ustr);
Ok(Self::new(
trader_id,
strategy_id,
instrument_id,
client_order_id,
order_side,
quantity,
trigger_price,
trigger_type,
time_in_force,
expire_time.map(|x| x.into()),
reduce_only,
quote_quantity,
display_qty,
emulation_trigger,
trigger_instrument_id,
contingency_type,
order_list_id,
linked_order_ids,
parent_order_id,
exec_algorithm_id,
exec_algorithm_params,
exec_spawn_id,
tags.map(|s| Ustr::from(&s)),
init_id,
ts_init.into(),
)
.unwrap())
}
}
88 changes: 88 additions & 0 deletions nautilus_core/model/src/python/orders/market_to_limit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2015-2024 Nautech Systems Pty Ltd. All rights reserved.
// https://nautechsystems.io
//
// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -------------------------------------------------------------------------------------------------

use std::collections::HashMap;

use nautilus_core::uuid::UUID4;
use pyo3::prelude::*;
use ustr::Ustr;

use crate::{
enums::{ContingencyType, OrderSide, TimeInForce},
identifiers::{
client_order_id::ClientOrderId, exec_algorithm_id::ExecAlgorithmId,
instrument_id::InstrumentId, order_list_id::OrderListId, strategy_id::StrategyId,
trader_id::TraderId,
},
orders::{base::str_hashmap_to_ustr, market_to_limit::MarketToLimitOrder},
types::quantity::Quantity,
};

#[pymethods]
impl MarketToLimitOrder {
#[new]
#[allow(clippy::too_many_arguments)]
fn py_new(
trader_id: TraderId,
strategy_id: StrategyId,
instrument_id: InstrumentId,
client_order_id: ClientOrderId,
order_side: OrderSide,
quantity: Quantity,
time_in_force: TimeInForce,
post_only: bool,
reduce_only: bool,
quote_quantity: bool,
init_id: UUID4,
ts_init: u64,
expire_time: Option<u64>,
display_qty: Option<Quantity>,
contingency_type: Option<ContingencyType>,
order_list_id: Option<OrderListId>,
linked_order_ids: Option<Vec<ClientOrderId>>,
parent_order_id: Option<ClientOrderId>,
exec_algorithm_id: Option<ExecAlgorithmId>,
exec_algorithm_params: Option<HashMap<String, String>>,
exec_spawn_id: Option<ClientOrderId>,
tags: Option<String>,
) -> PyResult<Self> {
let exec_algorithm_params = exec_algorithm_params.map(str_hashmap_to_ustr);
Ok(Self::new(
trader_id,
strategy_id,
instrument_id,
client_order_id,
order_side,
quantity,
time_in_force,
expire_time.map(|x| x.into()),
post_only,
reduce_only,
quote_quantity,
display_qty,
contingency_type,
order_list_id,
linked_order_ids,
parent_order_id,
exec_algorithm_id,
exec_algorithm_params,
exec_spawn_id,
tags.map(|s| Ustr::from(&s)),
init_id,
ts_init.into(),
)
.unwrap())
}
}
6 changes: 6 additions & 0 deletions nautilus_core/model/src/python/orders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@
// -------------------------------------------------------------------------------------------------

pub mod limit;
pub mod limit_if_touched;
pub mod market;
pub mod market_if_touched;
pub mod market_to_limit;
pub mod stop_limit;
pub mod stop_market;
pub mod trailing_stop_limit;
pub mod trailing_stop_market;
2 changes: 1 addition & 1 deletion nautilus_core/model/src/python/orders/stop_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl StopLimitOrder {
trigger_price,
trigger_type,
time_in_force,
expire_time.map(UnixNanos::from),
expire_time.map(|x| x.into()),
post_only,
reduce_only,
quote_quantity,
Expand Down
Loading