Skip to content

Commit

Permalink
Standardize order dict expire_time_ns
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Apr 15, 2024
1 parent 3ae93a2 commit 07cba7c
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion nautilus_core/model/src/orders/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl MarketOrder {
) -> anyhow::Result<Self> {
check_quantity_positive(quantity)?;
if time_in_force == TimeInForce::Gtd {
anyhow::bail!("{}", "GTD not supported for Market orders");
anyhow::bail!("GTD not supported for Market orders");
}
let init_order = OrderInitialized::new(
trader_id,
Expand Down
5 changes: 4 additions & 1 deletion nautilus_core/model/src/python/orders/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,10 @@ impl LimitOrder {
dict.set_item("price", self.price.to_string())?;
dict.set_item("status", self.status.to_string())?;
dict.set_item("time_in_force", self.time_in_force.to_string())?;
dict.set_item("expire_time_ns", self.expire_time.map(|e| e.as_u64()))?;
dict.set_item(
"expire_time_ns",
self.expire_time.filter(|&t| t != 0).map(|t| t.as_u64()),
)?;
dict.set_item("is_post_only", self.is_post_only)?;
dict.set_item("is_reduce_only", self.is_reduce_only)?;
dict.set_item("is_quote_quantity", self.is_quote_quantity)?;
Expand Down
5 changes: 4 additions & 1 deletion nautilus_core/model/src/python/orders/stop_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,10 @@ impl StopLimitOrder {
dict.set_item("is_reduce_only", self.is_reduce_only)?;
dict.set_item("is_quote_quantity", self.is_quote_quantity)?;
dict.set_item("init_id", self.init_id.to_string())?;
dict.set_item("expire_time_ns", self.expire_time.map(|e| e.as_u64()))?;
dict.set_item(
"expire_time_ns",
self.expire_time.filter(|&t| t != 0).map(|t| t.as_u64()),
)?;
dict.set_item("ts_init", self.ts_init.as_u64())?;
dict.set_item("ts_last", self.ts_last.as_u64())?;
let commissions_dict = PyDict::new(py);
Expand Down
4 changes: 2 additions & 2 deletions nautilus_trader/model/orders/limit.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ cdef class LimitOrder(Order):
Condition.true(expire_time_ns == 0, "`expire_time_ns` was set when `time_in_force` not GTD.")
Condition.true(
display_qty is None or 0 <= display_qty <= quantity,
fail_msg="display_qty was negative or greater than order quantity",
fail_msg="`display_qty` was negative or greater than `quantity`",
)

# Set options
Expand Down Expand Up @@ -319,7 +319,7 @@ cdef class LimitOrder(Order):
"quantity": str(self.quantity),
"price": str(self.price),
"time_in_force": time_in_force_to_str(self.time_in_force),
"expire_time_ns": self.expire_time_ns,
"expire_time_ns": self.expire_time_ns if self.expire_time_ns > 0 else None,
"filled_qty": str(self.filled_qty),
"liquidity_side": liquidity_side_to_str(self.liquidity_side),
"avg_px": str(self.avg_px) if self.filled_qty.as_f64_c() > 0.0 else None,
Expand Down
4 changes: 2 additions & 2 deletions nautilus_trader/model/orders/limit_if_touched.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ cdef class LimitIfTouchedOrder(Order):
Condition.true(expire_time_ns == 0, "`expire_time_ns` was set when `time_in_force` not GTD.")
Condition.true(
display_qty is None or 0 <= display_qty <= quantity,
fail_msg="display_qty was negative or greater than order quantity",
fail_msg="`display_qty` was negative or greater than `quantity`",
)

# Set options
Expand Down Expand Up @@ -312,7 +312,7 @@ cdef class LimitIfTouchedOrder(Order):
"price": str(self.price),
"trigger_price": str(self.trigger_price),
"trigger_type": trigger_type_to_str(self.trigger_type),
"expire_time_ns": self.expire_time_ns,
"expire_time_ns": self.expire_time_ns if self.expire_time_ns > 0 else None,
"time_in_force": time_in_force_to_str(self.time_in_force),
"filled_qty": str(self.filled_qty),
"liquidity_side": liquidity_side_to_str(self.liquidity_side),
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/model/orders/market_if_touched.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ cdef class MarketIfTouchedOrder(Order):
"quantity": str(self.quantity),
"trigger_price": str(self.trigger_price),
"trigger_type": trigger_type_to_str(self.trigger_type),
"expire_time_ns": self.expire_time_ns,
"expire_time_ns": self.expire_time_ns if self.expire_time_ns > 0 else None,
"time_in_force": time_in_force_to_str(self.time_in_force),
"filled_qty": str(self.filled_qty),
"liquidity_side": liquidity_side_to_str(self.liquidity_side),
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/model/orders/market_to_limit.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ cdef class MarketToLimitOrder(Order):
"quantity": str(self.quantity),
"price": str(self.price),
"time_in_force": time_in_force_to_str(self.time_in_force),
"expire_time_ns": self.expire_time_ns,
"expire_time_ns": self.expire_time_ns if self.expire_time_ns > 0 else None,
"is_reduce_only": self.is_reduce_only,
"is_quote_quantity": self.is_quote_quantity,
"display_qty": str(self.display_qty) if self.display_qty is not None else None,
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/model/orders/stop_limit.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ cdef class StopLimitOrder(Order):
Condition.true(expire_time_ns == 0, "`expire_time_ns` was set when `time_in_force` not GTD.")
Condition.true(
display_qty is None or 0 <= display_qty <= quantity,
fail_msg="display_qty was negative or greater than order quantity",
fail_msg="`display_qty` was negative or greater than `quantity`",
)

# Set options
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/model/orders/stop_market.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ cdef class StopMarketOrder(Order):
"quantity": str(self.quantity),
"trigger_price": str(self.trigger_price),
"trigger_type": trigger_type_to_str(self.trigger_type),
"expire_time_ns": self.expire_time_ns,
"expire_time_ns": self.expire_time_ns if self.expire_time_ns > 0 else None,
"time_in_force": time_in_force_to_str(self.time_in_force),
"filled_qty": str(self.filled_qty),
"liquidity_side": liquidity_side_to_str(self.liquidity_side),
Expand Down
4 changes: 2 additions & 2 deletions nautilus_trader/model/orders/trailing_stop_limit.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ cdef class TrailingStopLimitOrder(Order):
Condition.true(expire_time_ns == 0, "`expire_time_ns` was set when `time_in_force` not GTD.")
Condition.true(
display_qty is None or 0 <= display_qty <= quantity,
fail_msg="`display_qty` was negative or greater than order quantity",
fail_msg="`display_qty` was negative or greater than `quantity`",
)

# Set options
Expand Down Expand Up @@ -329,7 +329,7 @@ cdef class TrailingStopLimitOrder(Order):
"limit_offset": str(self.limit_offset),
"trailing_offset": str(self.trailing_offset),
"trailing_offset_type": trailing_offset_type_to_str(self.trailing_offset_type),
"expire_time_ns": self.expire_time_ns,
"expire_time_ns": self.expire_time_ns if self.expire_time_ns > 0 else None,
"time_in_force": time_in_force_to_str(self.time_in_force),
"filled_qty": str(self.filled_qty),
"liquidity_side": liquidity_side_to_str(self.liquidity_side),
Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/model/orders/trailing_stop_market.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ cdef class TrailingStopMarketOrder(Order):
"trigger_type": trigger_type_to_str(self.trigger_type),
"trailing_offset": str(self.trailing_offset),
"trailing_offset_type": trailing_offset_type_to_str(self.trailing_offset_type),
"expire_time_ns": self.expire_time_ns,
"expire_time_ns": self.expire_time_ns if self.expire_time_ns > 0 else None,
"time_in_force": time_in_force_to_str(self.time_in_force),
"filled_qty": str(self.filled_qty),
"liquidity_side": liquidity_side_to_str(self.liquidity_side),
Expand Down
16 changes: 8 additions & 8 deletions tests/unit_tests/model/test_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ def test_limit_order_to_dict(self):
"side": "BUY",
"quantity": "100000",
"price": "1.00000",
"expire_time_ns": 0,
"expire_time_ns": None,
"time_in_force": "GTC",
"filled_qty": "0",
"liquidity_side": "NO_LIQUIDITY_SIDE",
Expand Down Expand Up @@ -603,7 +603,7 @@ def test_stop_market_order_to_dict(self):
"quantity": "100000",
"trigger_price": "1.00000",
"trigger_type": "DEFAULT",
"expire_time_ns": 0,
"expire_time_ns": None,
"time_in_force": "GTC",
"filled_qty": "0",
"liquidity_side": "NO_LIQUIDITY_SIDE",
Expand Down Expand Up @@ -860,7 +860,7 @@ def test_market_if_touched_order_to_dict(self):
"quantity": "100000",
"trigger_price": "1.00000",
"trigger_type": "DEFAULT",
"expire_time_ns": 0,
"expire_time_ns": None,
"time_in_force": "GTC",
"filled_qty": "0",
"liquidity_side": "NO_LIQUIDITY_SIDE",
Expand Down Expand Up @@ -951,7 +951,7 @@ def test_limit_if_touched_order_to_dict(self):
"price": "1.00000",
"trigger_price": "1.10010",
"trigger_type": "MARK_PRICE",
"expire_time_ns": 0,
"expire_time_ns": None,
"time_in_force": "GTC",
"filled_qty": "0",
"liquidity_side": "NO_LIQUIDITY_SIDE",
Expand Down Expand Up @@ -1070,7 +1070,7 @@ def test_trailing_stop_market_order_to_dict(self):
"trigger_type": "DEFAULT",
"trailing_offset": "0.00050",
"trailing_offset_type": "PRICE",
"expire_time_ns": 0,
"expire_time_ns": None,
"time_in_force": "GTC",
"filled_qty": "0",
"liquidity_side": "NO_LIQUIDITY_SIDE",
Expand Down Expand Up @@ -1123,7 +1123,7 @@ def test_trailing_stop_market_order_with_no_initial_trigger_to_dict(self):
"trigger_type": "DEFAULT",
"trailing_offset": "0.00050",
"trailing_offset_type": "PRICE",
"expire_time_ns": 0,
"expire_time_ns": None,
"time_in_force": "GTC",
"filled_qty": "0",
"liquidity_side": "NO_LIQUIDITY_SIDE",
Expand Down Expand Up @@ -1242,7 +1242,7 @@ def test_trailing_stop_limit_order_to_dict(self):
"limit_offset": "5",
"trailing_offset": "10",
"trailing_offset_type": "BASIS_POINTS",
"expire_time_ns": 0,
"expire_time_ns": None,
"time_in_force": "GTC",
"filled_qty": "0",
"liquidity_side": "NO_LIQUIDITY_SIDE",
Expand Down Expand Up @@ -1299,7 +1299,7 @@ def test_trailing_stop_limit_order_with_no_initial_prices_to_dict(self):
"limit_offset": "5",
"trailing_offset": "10",
"trailing_offset_type": "BASIS_POINTS",
"expire_time_ns": 0,
"expire_time_ns": None,
"time_in_force": "GTC",
"filled_qty": "0",
"liquidity_side": "NO_LIQUIDITY_SIDE",
Expand Down

0 comments on commit 07cba7c

Please sign in to comment.