1use std::{cell::RefCell, rc::Rc, sync::LazyLock};
19
20use bytes::Bytes;
21use nautilus_core::python::to_pyvalue_err;
22#[cfg(feature = "defi")]
23use nautilus_model::defi::{Pool, PoolProfiler};
24use nautilus_model::{
25 data::{
26 Bar, BarType, FundingRateUpdate, InstrumentClose, InstrumentStatus, QuoteTick, TradeTick,
27 YieldCurve,
28 prices::{IndexPriceUpdate, MarkPriceUpdate},
29 },
30 enums::{AggregationSource, OmsType, OrderSide, PositionSide, PriceType},
31 identifiers::{
32 AccountId, ActorId, ClientId, ClientOrderId, ExecAlgorithmId, InstrumentId, OrderListId,
33 PositionId, StrategyId, Venue, VenueOrderId,
34 },
35 instruments::SyntheticInstrument,
36 orderbook::{OrderBook, own::OwnOrderBook},
37 orders::OrderList,
38 position::Position,
39 python::{
40 account::account_any_to_pyobject,
41 instruments::{instrument_any_to_pyobject, pyobject_to_instrument_any},
42 orders::{order_any_to_pyobject, pyobject_to_order_any},
43 },
44 types::{Currency, Money, Price, Quantity},
45};
46use pyo3::prelude::*;
47use rust_decimal::prelude::ToPrimitive;
48
49use crate::{
50 cache::{Cache, CacheConfig, database::CacheDatabaseFactory},
51 enums::SerializationEncoding,
52 python::{config_error_to_pyvalue_err, factory::FactoryRegistry},
53};
54
55pub type CacheDatabaseFactoryRegistry = FactoryRegistry<dyn CacheDatabaseFactory>;
57
58static GLOBAL_CACHE_DATABASE_FACTORY_REGISTRY: LazyLock<CacheDatabaseFactoryRegistry> =
59 LazyLock::new(|| CacheDatabaseFactoryRegistry::new("cache database factory"));
60
61#[must_use]
63pub fn get_global_cache_database_factory_registry() -> &'static CacheDatabaseFactoryRegistry {
64 &GLOBAL_CACHE_DATABASE_FACTORY_REGISTRY
65}
66
67#[allow(non_camel_case_types)]
72#[pyo3::pyclass(
73 module = "nautilus_trader.common",
74 name = "Cache",
75 unsendable,
76 from_py_object
77)]
78#[pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.common")]
79#[derive(Debug, Clone)]
80pub struct PyCache(Rc<RefCell<Cache>>);
81
82impl PyCache {
83 #[must_use]
85 pub fn from_rc(rc: Rc<RefCell<Cache>>) -> Self {
86 Self(rc)
87 }
88
89 #[must_use]
91 pub fn cache_rc(&self) -> Rc<RefCell<Cache>> {
92 self.0.clone()
93 }
94}
95
96#[pymethods]
97#[pyo3_stub_gen::derive::gen_stub_pymethods]
98impl PyCache {
99 #[new]
100 #[pyo3(signature = (config=None))]
101 fn py_new(config: Option<CacheConfig>) -> PyResult<Self> {
102 let cache = Cache::try_new(config, None).map_err(config_error_to_pyvalue_err)?;
103 Ok(Self(Rc::new(RefCell::new(cache))))
104 }
105
106 #[pyo3(name = "reset")]
107 fn py_reset(&mut self) {
108 self.0.borrow_mut().reset();
109 }
110
111 #[pyo3(name = "dispose")]
112 fn py_dispose(&mut self) {
113 self.0.borrow_mut().dispose();
114 }
115
116 #[pyo3(name = "purge_closed_orders", signature = (ts_now, buffer_secs=0, purge_from_database=false))]
117 fn py_purge_closed_orders(
118 &mut self,
119 ts_now: u64,
120 buffer_secs: u64,
121 purge_from_database: bool,
122 ) -> PyResult<()> {
123 self.0
124 .borrow_mut()
125 .purge_closed_orders(ts_now.into(), buffer_secs, purge_from_database)
126 .map_err(to_pyvalue_err)
127 }
128
129 #[pyo3(name = "purge_closed_positions", signature = (ts_now, buffer_secs=0, purge_from_database=false))]
130 fn py_purge_closed_positions(
131 &mut self,
132 ts_now: u64,
133 buffer_secs: u64,
134 purge_from_database: bool,
135 ) -> PyResult<()> {
136 self.0
137 .borrow_mut()
138 .purge_closed_positions(ts_now.into(), buffer_secs, purge_from_database)
139 .map_err(to_pyvalue_err)
140 }
141
142 #[pyo3(name = "delete_actor")]
143 fn py_delete_actor(&self, actor_id: ActorId) -> PyResult<()> {
144 self.0
145 .borrow()
146 .delete_actor(&actor_id)
147 .map_err(to_pyvalue_err)
148 }
149
150 #[pyo3(name = "delete_strategy")]
151 fn py_delete_strategy(&self, strategy_id: StrategyId) -> PyResult<()> {
152 self.0
153 .borrow()
154 .delete_strategy(&strategy_id)
155 .map_err(to_pyvalue_err)
156 }
157
158 #[pyo3(name = "heartbeat")]
159 fn py_heartbeat(&self, timestamp: u64) -> PyResult<()> {
160 self.0
161 .borrow()
162 .heartbeat(timestamp.into())
163 .map_err(to_pyvalue_err)
164 }
165
166 #[pyo3(name = "load_instrument")]
167 fn py_load_instrument(
168 &mut self,
169 py: Python<'_>,
170 instrument_id: InstrumentId,
171 ) -> PyResult<Option<Py<PyAny>>> {
172 let instrument = self
173 .0
174 .borrow_mut()
175 .load_instrument(&instrument_id)
176 .map_err(to_pyvalue_err)?;
177 instrument
178 .map(|instrument| instrument_any_to_pyobject(py, instrument))
179 .transpose()
180 }
181
182 #[pyo3(name = "load_synthetic")]
183 fn py_load_synthetic(
184 &mut self,
185 instrument_id: InstrumentId,
186 ) -> PyResult<Option<SyntheticInstrument>> {
187 self.0
188 .borrow_mut()
189 .load_synthetic(&instrument_id)
190 .map_err(to_pyvalue_err)
191 }
192
193 #[pyo3(name = "purge_order", signature = (client_order_id, purge_from_database=false))]
194 fn py_purge_order(
195 &mut self,
196 client_order_id: ClientOrderId,
197 purge_from_database: bool,
198 ) -> PyResult<()> {
199 self.0
200 .borrow_mut()
201 .purge_order(client_order_id, purge_from_database)
202 .map_err(to_pyvalue_err)
203 }
204
205 #[pyo3(name = "purge_position", signature = (position_id, purge_from_database=false))]
206 fn py_purge_position(
207 &mut self,
208 position_id: PositionId,
209 purge_from_database: bool,
210 ) -> PyResult<()> {
211 self.0
212 .borrow_mut()
213 .purge_position(position_id, purge_from_database)
214 .map_err(to_pyvalue_err)
215 }
216
217 #[pyo3(name = "purge_instrument")]
218 fn py_purge_instrument(&mut self, instrument_id: InstrumentId) {
219 self.0.borrow_mut().purge_instrument(instrument_id);
220 }
221
222 #[pyo3(name = "purge_account_events", signature = (ts_now, lookback_secs=0, purge_from_database=false))]
223 fn py_purge_account_events(
224 &mut self,
225 ts_now: u64,
226 lookback_secs: u64,
227 purge_from_database: bool,
228 ) -> PyResult<()> {
229 self.0
230 .borrow_mut()
231 .purge_account_events(ts_now.into(), lookback_secs, purge_from_database)
232 .map_err(to_pyvalue_err)
233 }
234
235 #[pyo3(name = "get")]
236 fn py_get(&self, key: &str) -> PyResult<Option<Vec<u8>>> {
237 match self.0.borrow().get(key).map_err(to_pyvalue_err)? {
238 Some(bytes) => Ok(Some(bytes.to_vec())),
239 None => Ok(None),
240 }
241 }
242
243 #[pyo3(name = "add")]
244 fn py_add_general(&mut self, key: &str, value: Vec<u8>) -> PyResult<()> {
245 self.0
246 .borrow_mut()
247 .add(key, Bytes::from(value))
248 .map_err(to_pyvalue_err)
249 }
250
251 #[pyo3(name = "add_instrument_close")]
253 fn py_add_instrument_close(&mut self, close: InstrumentClose) -> PyResult<()> {
254 self.0
255 .borrow_mut()
256 .add_instrument_close(close)
257 .map_err(to_pyvalue_err)
258 }
259
260 #[pyo3(name = "add_yield_curve")]
261 fn py_add_yield_curve(&mut self, yield_curve: YieldCurve) -> PyResult<()> {
262 self.0
263 .borrow_mut()
264 .add_yield_curve(yield_curve)
265 .map_err(to_pyvalue_err)
266 }
267
268 #[pyo3(name = "quote", signature = (instrument_id, index=0))]
273 fn py_quote(&self, instrument_id: InstrumentId, index: usize) -> Option<QuoteTick> {
274 self.0
275 .borrow()
276 .quote_at_index(&instrument_id, index)
277 .copied()
278 }
279
280 #[pyo3(name = "trade", signature = (instrument_id, index=0))]
285 fn py_trade(&self, instrument_id: InstrumentId, index: usize) -> Option<TradeTick> {
286 self.0
287 .borrow()
288 .trade_at_index(&instrument_id, index)
289 .copied()
290 }
291
292 #[pyo3(name = "bar", signature = (bar_type, index=0))]
297 fn py_bar(&self, bar_type: BarType, index: usize) -> Option<Bar> {
298 self.0.borrow().bar_at_index(&bar_type, index).copied()
299 }
300
301 #[pyo3(name = "quotes")]
306 fn py_quotes(&self, instrument_id: InstrumentId) -> Option<Vec<QuoteTick>> {
307 self.0.borrow().quotes(&instrument_id)
308 }
309
310 #[pyo3(name = "trades")]
315 fn py_trades(&self, instrument_id: InstrumentId) -> Option<Vec<TradeTick>> {
316 self.0.borrow().trades(&instrument_id)
317 }
318
319 #[pyo3(name = "bars")]
323 fn py_bars(&self, bar_type: BarType) -> Option<Vec<Bar>> {
324 self.0.borrow().bars(&bar_type)
325 }
326
327 #[pyo3(name = "bar_types", signature = (aggregation_source, instrument_id=None, price_type=None))]
328 fn py_bar_types(
329 &self,
330 aggregation_source: AggregationSource,
331 instrument_id: Option<InstrumentId>,
332 price_type: Option<PriceType>,
333 ) -> Vec<BarType> {
334 self.0
335 .borrow()
336 .bar_types(
337 instrument_id.as_ref(),
338 price_type.as_ref(),
339 aggregation_source,
340 )
341 .into_iter()
342 .copied()
343 .collect()
344 }
345
346 #[pyo3(name = "mark_price", signature = (instrument_id, index=0))]
351 fn py_mark_price(&self, instrument_id: InstrumentId, index: usize) -> Option<MarkPriceUpdate> {
352 self.0
353 .borrow()
354 .mark_price_at_index(&instrument_id, index)
355 .copied()
356 }
357
358 #[pyo3(name = "mark_prices")]
363 fn py_mark_prices(&self, instrument_id: InstrumentId) -> Option<Vec<MarkPriceUpdate>> {
364 self.0.borrow().mark_prices(&instrument_id)
365 }
366
367 #[pyo3(name = "index_price", signature = (instrument_id, index=0))]
372 fn py_index_price(
373 &self,
374 instrument_id: InstrumentId,
375 index: usize,
376 ) -> Option<IndexPriceUpdate> {
377 self.0
378 .borrow()
379 .index_price_at_index(&instrument_id, index)
380 .copied()
381 }
382
383 #[pyo3(name = "index_prices")]
388 fn py_index_prices(&self, instrument_id: InstrumentId) -> Option<Vec<IndexPriceUpdate>> {
389 self.0.borrow().index_prices(&instrument_id)
390 }
391
392 #[pyo3(name = "funding_rate", signature = (instrument_id, index=0))]
397 fn py_funding_rate(
398 &self,
399 instrument_id: InstrumentId,
400 index: usize,
401 ) -> Option<FundingRateUpdate> {
402 self.0
403 .borrow()
404 .funding_rate_at_index(&instrument_id, index)
405 .copied()
406 }
407
408 #[pyo3(name = "funding_rates")]
413 fn py_funding_rates(&self, instrument_id: InstrumentId) -> Option<Vec<FundingRateUpdate>> {
414 self.0.borrow().funding_rates(&instrument_id)
415 }
416
417 #[pyo3(name = "instrument_status")]
419 fn py_instrument_status(&self, instrument_id: InstrumentId) -> Option<InstrumentStatus> {
420 self.0.borrow().instrument_status(&instrument_id).copied()
421 }
422
423 #[pyo3(name = "instrument_statuses")]
428 fn py_instrument_statuses(&self, instrument_id: InstrumentId) -> Option<Vec<InstrumentStatus>> {
429 self.0.borrow().instrument_statuses(&instrument_id)
430 }
431
432 #[pyo3(name = "instrument_close")]
436 fn py_instrument_close(&self, instrument_id: InstrumentId) -> Option<InstrumentClose> {
437 self.0.borrow().instrument_close(&instrument_id).copied()
438 }
439
440 #[pyo3(name = "price")]
441 fn py_price(&self, instrument_id: InstrumentId, price_type: PriceType) -> Option<Price> {
442 self.0.borrow().price(&instrument_id, price_type)
443 }
444
445 #[pyo3(name = "order_book")]
449 fn py_order_book(&self, instrument_id: InstrumentId) -> Option<OrderBook> {
450 self.0.borrow().order_book(&instrument_id).cloned()
451 }
452
453 #[pyo3(name = "top_of_book")]
465 fn py_top_of_book(
466 &self,
467 instrument_id: InstrumentId,
468 ) -> Option<(Price, Quantity, Price, Quantity)> {
469 let cache = self.0.borrow();
470 let book = cache.order_book(&instrument_id)?;
471 Some((
472 book.best_bid_price()?,
473 book.best_bid_size()?,
474 book.best_ask_price()?,
475 book.best_ask_size()?,
476 ))
477 }
478
479 #[pyo3(name = "has_order_book")]
480 fn py_has_order_book(&self, instrument_id: InstrumentId) -> bool {
481 self.0.borrow().has_order_book(&instrument_id)
482 }
483
484 #[pyo3(name = "book_update_count")]
485 fn py_book_update_count(&self, instrument_id: InstrumentId) -> usize {
486 self.0.borrow().book_update_count(&instrument_id)
487 }
488
489 #[pyo3(name = "has_quote_ticks")]
490 fn py_has_quote_ticks(&self, instrument_id: InstrumentId) -> bool {
491 self.0.borrow().has_quote_ticks(&instrument_id)
492 }
493
494 #[pyo3(name = "has_trade_ticks")]
495 fn py_has_trade_ticks(&self, instrument_id: InstrumentId) -> bool {
496 self.0.borrow().has_trade_ticks(&instrument_id)
497 }
498
499 #[pyo3(name = "has_mark_prices")]
500 fn py_has_mark_prices(&self, instrument_id: InstrumentId) -> bool {
501 self.0.borrow().has_mark_prices(&instrument_id)
502 }
503
504 #[pyo3(name = "has_index_prices")]
505 fn py_has_index_prices(&self, instrument_id: InstrumentId) -> bool {
506 self.0.borrow().has_index_prices(&instrument_id)
507 }
508
509 #[pyo3(name = "has_funding_rates")]
510 fn py_has_funding_rates(&self, instrument_id: InstrumentId) -> bool {
511 self.0.borrow().has_funding_rates(&instrument_id)
512 }
513
514 #[pyo3(name = "has_instrument_statuses")]
515 fn py_has_instrument_statuses(&self, instrument_id: InstrumentId) -> bool {
516 self.0.borrow().has_instrument_statuses(&instrument_id)
517 }
518
519 #[pyo3(name = "has_instrument_close")]
520 fn py_has_instrument_close(&self, instrument_id: InstrumentId) -> bool {
521 self.0.borrow().has_instrument_close(&instrument_id)
522 }
523
524 #[pyo3(name = "has_bars")]
525 fn py_has_bars(&self, bar_type: BarType) -> bool {
526 self.0.borrow().has_bars(&bar_type)
527 }
528
529 #[pyo3(name = "quote_count")]
530 fn py_quote_count(&self, instrument_id: InstrumentId) -> usize {
531 self.0.borrow().quote_count(&instrument_id)
532 }
533
534 #[pyo3(name = "trade_count")]
535 fn py_trade_count(&self, instrument_id: InstrumentId) -> usize {
536 self.0.borrow().trade_count(&instrument_id)
537 }
538
539 #[pyo3(name = "mark_price_count")]
540 fn py_mark_price_count(&self, instrument_id: InstrumentId) -> usize {
541 self.0.borrow().mark_price_count(&instrument_id)
542 }
543
544 #[pyo3(name = "index_price_count")]
545 fn py_index_price_count(&self, instrument_id: InstrumentId) -> usize {
546 self.0.borrow().index_price_count(&instrument_id)
547 }
548
549 #[pyo3(name = "funding_rate_count")]
550 fn py_funding_rate_count(&self, instrument_id: InstrumentId) -> usize {
551 self.0.borrow().funding_rate_count(&instrument_id)
552 }
553
554 #[pyo3(name = "instrument_status_count")]
555 fn py_instrument_status_count(&self, instrument_id: InstrumentId) -> usize {
556 self.0.borrow().instrument_status_count(&instrument_id)
557 }
558
559 #[pyo3(name = "bar_count")]
560 fn py_bar_count(&self, bar_type: BarType) -> usize {
561 self.0.borrow().bar_count(&bar_type)
562 }
563
564 #[pyo3(name = "get_xrate")]
565 fn py_get_xrate(
566 &self,
567 venue: Venue,
568 from_currency: Currency,
569 to_currency: Currency,
570 price_type: PriceType,
571 ) -> Option<f64> {
572 self.0
573 .borrow()
574 .get_xrate(venue, from_currency, to_currency, price_type)
575 .and_then(|rate| rate.to_f64())
576 }
577
578 #[pyo3(name = "get_mark_xrate")]
579 fn py_get_mark_xrate(&self, from_currency: Currency, to_currency: Currency) -> Option<f64> {
580 self.0.borrow().get_mark_xrate(from_currency, to_currency)
581 }
582
583 #[pyo3(name = "own_order_book")]
584 fn py_own_order_book(&self, instrument_id: InstrumentId) -> Option<OwnOrderBook> {
585 self.0.borrow().own_order_book(&instrument_id).cloned()
586 }
587
588 #[pyo3(name = "instrument")]
589 fn py_instrument(
590 &self,
591 py: Python,
592 instrument_id: InstrumentId,
593 ) -> PyResult<Option<Py<PyAny>>> {
594 let cache = self.0.borrow();
595 match cache.instrument(&instrument_id) {
596 Some(instrument) => Ok(Some(instrument_any_to_pyobject(py, instrument.clone())?)),
597 None => Ok(None),
598 }
599 }
600
601 #[pyo3(name = "instrument_ids", signature = (venue=None))]
602 fn py_instrument_ids(&self, venue: Option<Venue>) -> Vec<InstrumentId> {
603 self.0
604 .borrow()
605 .instrument_ids(venue.as_ref())
606 .into_iter()
607 .copied()
608 .collect()
609 }
610
611 #[pyo3(name = "instruments", signature = (venue=None))]
612 fn py_instruments(&self, py: Python, venue: Option<Venue>) -> PyResult<Vec<Py<PyAny>>> {
613 let cache = self.0.borrow();
614 let mut py_instruments = Vec::new();
615
616 match venue {
617 Some(venue) => {
618 for instrument in cache.instruments(&venue, None) {
619 py_instruments.push(instrument_any_to_pyobject(py, (*instrument).clone())?);
620 }
621 }
622 None => {
623 for instrument_id in cache.instrument_ids(None) {
624 if let Some(instrument) = cache.instrument(instrument_id) {
625 py_instruments.push(instrument_any_to_pyobject(py, instrument.clone())?);
626 }
627 }
628 }
629 }
630 Ok(py_instruments)
631 }
632
633 #[pyo3(name = "synthetic")]
634 fn py_synthetic(&self, instrument_id: InstrumentId) -> Option<SyntheticInstrument> {
635 self.0.borrow().synthetic(&instrument_id).cloned()
636 }
637
638 #[pyo3(name = "synthetic_ids")]
639 fn py_synthetic_ids(&self) -> Vec<InstrumentId> {
640 self.0
641 .borrow()
642 .synthetic_ids()
643 .into_iter()
644 .copied()
645 .collect()
646 }
647
648 #[pyo3(name = "account")]
649 fn py_account(&self, py: Python, account_id: AccountId) -> PyResult<Option<Py<PyAny>>> {
650 let cache = self.0.borrow();
651 match cache.account(&account_id) {
652 Some(account) => Ok(Some(account_any_to_pyobject(py, account.clone())?)),
653 None => Ok(None),
654 }
655 }
656
657 #[pyo3(name = "account_for_venue")]
658 fn py_account_for_venue(&self, py: Python, venue: Venue) -> PyResult<Option<Py<PyAny>>> {
659 let cache = self.0.borrow();
660 match cache.account_for_venue(&venue) {
661 Some(account) => Ok(Some(account_any_to_pyobject(py, account.clone())?)),
662 None => Ok(None),
663 }
664 }
665
666 #[pyo3(name = "account_id")]
667 fn py_account_id(&self, venue: Venue) -> Option<AccountId> {
668 self.0.borrow().account_id(&venue).copied()
669 }
670
671 #[pyo3(name = "client_order_ids", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None))]
672 fn py_client_order_ids(
673 &self,
674 venue: Option<Venue>,
675 instrument_id: Option<InstrumentId>,
676 strategy_id: Option<StrategyId>,
677 account_id: Option<AccountId>,
678 ) -> Vec<ClientOrderId> {
679 self.0
680 .borrow()
681 .client_order_ids(
682 venue.as_ref(),
683 instrument_id.as_ref(),
684 strategy_id.as_ref(),
685 account_id.as_ref(),
686 )
687 .into_iter()
688 .collect()
689 }
690
691 #[pyo3(name = "client_order_ids_open", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None))]
692 fn py_client_order_ids_open(
693 &self,
694 venue: Option<Venue>,
695 instrument_id: Option<InstrumentId>,
696 strategy_id: Option<StrategyId>,
697 account_id: Option<AccountId>,
698 ) -> Vec<ClientOrderId> {
699 self.0
700 .borrow()
701 .client_order_ids_open(
702 venue.as_ref(),
703 instrument_id.as_ref(),
704 strategy_id.as_ref(),
705 account_id.as_ref(),
706 )
707 .into_iter()
708 .collect()
709 }
710
711 #[pyo3(name = "client_order_ids_closed", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None))]
712 fn py_client_order_ids_closed(
713 &self,
714 venue: Option<Venue>,
715 instrument_id: Option<InstrumentId>,
716 strategy_id: Option<StrategyId>,
717 account_id: Option<AccountId>,
718 ) -> Vec<ClientOrderId> {
719 self.0
720 .borrow()
721 .client_order_ids_closed(
722 venue.as_ref(),
723 instrument_id.as_ref(),
724 strategy_id.as_ref(),
725 account_id.as_ref(),
726 )
727 .into_iter()
728 .collect()
729 }
730
731 #[pyo3(name = "client_order_ids_emulated", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None))]
732 fn py_client_order_ids_emulated(
733 &self,
734 venue: Option<Venue>,
735 instrument_id: Option<InstrumentId>,
736 strategy_id: Option<StrategyId>,
737 account_id: Option<AccountId>,
738 ) -> Vec<ClientOrderId> {
739 self.0
740 .borrow()
741 .client_order_ids_emulated(
742 venue.as_ref(),
743 instrument_id.as_ref(),
744 strategy_id.as_ref(),
745 account_id.as_ref(),
746 )
747 .into_iter()
748 .collect()
749 }
750
751 #[pyo3(name = "client_order_ids_inflight", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None))]
752 fn py_client_order_ids_inflight(
753 &self,
754 venue: Option<Venue>,
755 instrument_id: Option<InstrumentId>,
756 strategy_id: Option<StrategyId>,
757 account_id: Option<AccountId>,
758 ) -> Vec<ClientOrderId> {
759 self.0
760 .borrow()
761 .client_order_ids_inflight(
762 venue.as_ref(),
763 instrument_id.as_ref(),
764 strategy_id.as_ref(),
765 account_id.as_ref(),
766 )
767 .into_iter()
768 .collect()
769 }
770
771 #[pyo3(name = "position_ids", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None))]
772 fn py_position_ids(
773 &self,
774 venue: Option<Venue>,
775 instrument_id: Option<InstrumentId>,
776 strategy_id: Option<StrategyId>,
777 account_id: Option<AccountId>,
778 ) -> Vec<PositionId> {
779 self.0
780 .borrow()
781 .position_ids(
782 venue.as_ref(),
783 instrument_id.as_ref(),
784 strategy_id.as_ref(),
785 account_id.as_ref(),
786 )
787 .into_iter()
788 .collect()
789 }
790
791 #[pyo3(name = "position_open_ids", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None))]
792 fn py_position_open_ids(
793 &self,
794 venue: Option<Venue>,
795 instrument_id: Option<InstrumentId>,
796 strategy_id: Option<StrategyId>,
797 account_id: Option<AccountId>,
798 ) -> Vec<PositionId> {
799 self.0
800 .borrow()
801 .position_open_ids(
802 venue.as_ref(),
803 instrument_id.as_ref(),
804 strategy_id.as_ref(),
805 account_id.as_ref(),
806 )
807 .into_iter()
808 .collect()
809 }
810
811 #[pyo3(name = "position_closed_ids", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None))]
812 fn py_position_closed_ids(
813 &self,
814 venue: Option<Venue>,
815 instrument_id: Option<InstrumentId>,
816 strategy_id: Option<StrategyId>,
817 account_id: Option<AccountId>,
818 ) -> Vec<PositionId> {
819 self.0
820 .borrow()
821 .position_closed_ids(
822 venue.as_ref(),
823 instrument_id.as_ref(),
824 strategy_id.as_ref(),
825 account_id.as_ref(),
826 )
827 .into_iter()
828 .collect()
829 }
830
831 #[pyo3(name = "strategy_ids")]
832 fn py_strategy_ids(&self) -> Vec<StrategyId> {
833 self.0.borrow().strategy_ids().into_iter().collect()
834 }
835
836 #[pyo3(name = "exec_algorithm_ids")]
837 fn py_exec_algorithm_ids(&self) -> Vec<ExecAlgorithmId> {
838 self.0.borrow().exec_algorithm_ids().into_iter().collect()
839 }
840
841 #[pyo3(name = "order")]
842 fn py_order(&self, py: Python, client_order_id: ClientOrderId) -> PyResult<Option<Py<PyAny>>> {
843 let cache = self.0.borrow();
844 match cache.order(&client_order_id) {
845 Some(order) => Ok(Some(order_any_to_pyobject(py, order.clone())?)),
846 None => Ok(None),
847 }
848 }
849
850 #[pyo3(name = "client_order_id")]
851 fn py_client_order_id(&self, venue_order_id: VenueOrderId) -> Option<ClientOrderId> {
852 self.0.borrow().client_order_id(&venue_order_id).copied()
853 }
854
855 #[pyo3(name = "venue_order_id")]
856 fn py_venue_order_id(&self, client_order_id: ClientOrderId) -> Option<VenueOrderId> {
857 self.0.borrow().venue_order_id(&client_order_id).copied()
858 }
859
860 #[pyo3(name = "client_id")]
861 fn py_client_id(&self, client_order_id: ClientOrderId) -> Option<ClientId> {
862 self.0.borrow().client_id(&client_order_id).copied()
863 }
864
865 #[pyo3(name = "orders", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
866 fn py_orders(
867 &self,
868 py: Python,
869 venue: Option<Venue>,
870 instrument_id: Option<InstrumentId>,
871 strategy_id: Option<StrategyId>,
872 account_id: Option<AccountId>,
873 side: Option<OrderSide>,
874 ) -> PyResult<Vec<Py<PyAny>>> {
875 let cache = self.0.borrow();
876 cache
877 .orders(
878 venue.as_ref(),
879 instrument_id.as_ref(),
880 strategy_id.as_ref(),
881 account_id.as_ref(),
882 side,
883 )
884 .into_iter()
885 .map(|o| order_any_to_pyobject(py, o.clone()))
886 .collect()
887 }
888
889 #[pyo3(name = "orders_open", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
890 fn py_orders_open(
891 &self,
892 py: Python,
893 venue: Option<Venue>,
894 instrument_id: Option<InstrumentId>,
895 strategy_id: Option<StrategyId>,
896 account_id: Option<AccountId>,
897 side: Option<OrderSide>,
898 ) -> PyResult<Vec<Py<PyAny>>> {
899 let cache = self.0.borrow();
900 cache
901 .orders_open(
902 venue.as_ref(),
903 instrument_id.as_ref(),
904 strategy_id.as_ref(),
905 account_id.as_ref(),
906 side,
907 )
908 .into_iter()
909 .map(|o| order_any_to_pyobject(py, o.clone()))
910 .collect()
911 }
912
913 #[pyo3(name = "orders_closed", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
914 fn py_orders_closed(
915 &self,
916 py: Python,
917 venue: Option<Venue>,
918 instrument_id: Option<InstrumentId>,
919 strategy_id: Option<StrategyId>,
920 account_id: Option<AccountId>,
921 side: Option<OrderSide>,
922 ) -> PyResult<Vec<Py<PyAny>>> {
923 let cache = self.0.borrow();
924 cache
925 .orders_closed(
926 venue.as_ref(),
927 instrument_id.as_ref(),
928 strategy_id.as_ref(),
929 account_id.as_ref(),
930 side,
931 )
932 .into_iter()
933 .map(|o| order_any_to_pyobject(py, o.clone()))
934 .collect()
935 }
936
937 #[pyo3(name = "orders_emulated", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
938 fn py_orders_emulated(
939 &self,
940 py: Python,
941 venue: Option<Venue>,
942 instrument_id: Option<InstrumentId>,
943 strategy_id: Option<StrategyId>,
944 account_id: Option<AccountId>,
945 side: Option<OrderSide>,
946 ) -> PyResult<Vec<Py<PyAny>>> {
947 let cache = self.0.borrow();
948 cache
949 .orders_emulated(
950 venue.as_ref(),
951 instrument_id.as_ref(),
952 strategy_id.as_ref(),
953 account_id.as_ref(),
954 side,
955 )
956 .into_iter()
957 .map(|o| order_any_to_pyobject(py, o.clone()))
958 .collect()
959 }
960
961 #[pyo3(name = "orders_inflight", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
962 fn py_orders_inflight(
963 &self,
964 py: Python,
965 venue: Option<Venue>,
966 instrument_id: Option<InstrumentId>,
967 strategy_id: Option<StrategyId>,
968 account_id: Option<AccountId>,
969 side: Option<OrderSide>,
970 ) -> PyResult<Vec<Py<PyAny>>> {
971 let cache = self.0.borrow();
972 cache
973 .orders_inflight(
974 venue.as_ref(),
975 instrument_id.as_ref(),
976 strategy_id.as_ref(),
977 account_id.as_ref(),
978 side,
979 )
980 .into_iter()
981 .map(|o| order_any_to_pyobject(py, o.clone()))
982 .collect()
983 }
984
985 #[pyo3(name = "orders_for_position")]
986 fn py_orders_for_position(
987 &self,
988 py: Python,
989 position_id: PositionId,
990 ) -> PyResult<Vec<Py<PyAny>>> {
991 let cache = self.0.borrow();
992 cache
993 .orders_for_position(&position_id)
994 .into_iter()
995 .map(|o| order_any_to_pyobject(py, o.clone()))
996 .collect()
997 }
998
999 #[pyo3(name = "order_exists")]
1000 fn py_order_exists(&self, client_order_id: ClientOrderId) -> bool {
1001 self.0.borrow().order_exists(&client_order_id)
1002 }
1003
1004 #[pyo3(name = "is_order_open")]
1005 fn py_is_order_open(&self, client_order_id: ClientOrderId) -> bool {
1006 self.0.borrow().is_order_open(&client_order_id)
1007 }
1008
1009 #[pyo3(name = "is_order_closed")]
1010 fn py_is_order_closed(&self, client_order_id: ClientOrderId) -> bool {
1011 self.0.borrow().is_order_closed(&client_order_id)
1012 }
1013
1014 #[pyo3(name = "is_order_emulated")]
1015 fn py_is_order_emulated(&self, client_order_id: ClientOrderId) -> bool {
1016 self.0.borrow().is_order_emulated(&client_order_id)
1017 }
1018
1019 #[pyo3(name = "is_order_inflight")]
1020 fn py_is_order_inflight(&self, client_order_id: ClientOrderId) -> bool {
1021 self.0.borrow().is_order_inflight(&client_order_id)
1022 }
1023
1024 #[pyo3(name = "is_order_pending_cancel_local")]
1025 fn py_is_order_pending_cancel_local(&self, client_order_id: ClientOrderId) -> bool {
1026 self.0
1027 .borrow()
1028 .is_order_pending_cancel_local(&client_order_id)
1029 }
1030
1031 #[pyo3(name = "orders_open_count", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
1032 fn py_orders_open_count(
1033 &self,
1034 venue: Option<Venue>,
1035 instrument_id: Option<InstrumentId>,
1036 strategy_id: Option<StrategyId>,
1037 account_id: Option<AccountId>,
1038 side: Option<OrderSide>,
1039 ) -> usize {
1040 self.0.borrow().orders_open_count(
1041 venue.as_ref(),
1042 instrument_id.as_ref(),
1043 strategy_id.as_ref(),
1044 account_id.as_ref(),
1045 side,
1046 )
1047 }
1048
1049 #[pyo3(name = "orders_closed_count", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
1050 fn py_orders_closed_count(
1051 &self,
1052 venue: Option<Venue>,
1053 instrument_id: Option<InstrumentId>,
1054 strategy_id: Option<StrategyId>,
1055 account_id: Option<AccountId>,
1056 side: Option<OrderSide>,
1057 ) -> usize {
1058 self.0.borrow().orders_closed_count(
1059 venue.as_ref(),
1060 instrument_id.as_ref(),
1061 strategy_id.as_ref(),
1062 account_id.as_ref(),
1063 side,
1064 )
1065 }
1066
1067 #[pyo3(name = "orders_emulated_count", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
1068 fn py_orders_emulated_count(
1069 &self,
1070 venue: Option<Venue>,
1071 instrument_id: Option<InstrumentId>,
1072 strategy_id: Option<StrategyId>,
1073 account_id: Option<AccountId>,
1074 side: Option<OrderSide>,
1075 ) -> usize {
1076 self.0.borrow().orders_emulated_count(
1077 venue.as_ref(),
1078 instrument_id.as_ref(),
1079 strategy_id.as_ref(),
1080 account_id.as_ref(),
1081 side,
1082 )
1083 }
1084
1085 #[pyo3(name = "orders_inflight_count", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
1086 fn py_orders_inflight_count(
1087 &self,
1088 venue: Option<Venue>,
1089 instrument_id: Option<InstrumentId>,
1090 strategy_id: Option<StrategyId>,
1091 account_id: Option<AccountId>,
1092 side: Option<OrderSide>,
1093 ) -> usize {
1094 self.0.borrow().orders_inflight_count(
1095 venue.as_ref(),
1096 instrument_id.as_ref(),
1097 strategy_id.as_ref(),
1098 account_id.as_ref(),
1099 side,
1100 )
1101 }
1102
1103 #[pyo3(name = "orders_total_count", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
1104 fn py_orders_total_count(
1105 &self,
1106 venue: Option<Venue>,
1107 instrument_id: Option<InstrumentId>,
1108 strategy_id: Option<StrategyId>,
1109 account_id: Option<AccountId>,
1110 side: Option<OrderSide>,
1111 ) -> usize {
1112 self.0.borrow().orders_total_count(
1113 venue.as_ref(),
1114 instrument_id.as_ref(),
1115 strategy_id.as_ref(),
1116 account_id.as_ref(),
1117 side,
1118 )
1119 }
1120
1121 #[pyo3(name = "order_list")]
1122 fn py_order_list(&self, order_list_id: OrderListId) -> Option<OrderList> {
1123 self.0.borrow().order_list(&order_list_id).cloned()
1124 }
1125
1126 #[pyo3(name = "order_lists", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None))]
1127 fn py_order_lists(
1128 &self,
1129 venue: Option<Venue>,
1130 instrument_id: Option<InstrumentId>,
1131 strategy_id: Option<StrategyId>,
1132 account_id: Option<AccountId>,
1133 ) -> Vec<OrderList> {
1134 let cache = self.0.borrow();
1135 cache
1136 .order_lists(
1137 venue.as_ref(),
1138 instrument_id.as_ref(),
1139 strategy_id.as_ref(),
1140 account_id.as_ref(),
1141 )
1142 .into_iter()
1143 .cloned()
1144 .collect()
1145 }
1146
1147 #[pyo3(name = "order_list_exists")]
1148 fn py_order_list_exists(&self, order_list_id: OrderListId) -> bool {
1149 self.0.borrow().order_list_exists(&order_list_id)
1150 }
1151
1152 #[pyo3(name = "orders_for_exec_algorithm", signature = (exec_algorithm_id, venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
1153 #[expect(clippy::too_many_arguments)]
1154 fn py_orders_for_exec_algorithm(
1155 &self,
1156 py: Python,
1157 exec_algorithm_id: ExecAlgorithmId,
1158 venue: Option<Venue>,
1159 instrument_id: Option<InstrumentId>,
1160 strategy_id: Option<StrategyId>,
1161 account_id: Option<AccountId>,
1162 side: Option<OrderSide>,
1163 ) -> PyResult<Vec<Py<PyAny>>> {
1164 let cache = self.0.borrow();
1165 cache
1166 .orders_for_exec_algorithm(
1167 &exec_algorithm_id,
1168 venue.as_ref(),
1169 instrument_id.as_ref(),
1170 strategy_id.as_ref(),
1171 account_id.as_ref(),
1172 side,
1173 )
1174 .into_iter()
1175 .map(|o| order_any_to_pyobject(py, o.clone()))
1176 .collect()
1177 }
1178
1179 #[pyo3(name = "orders_for_exec_spawn")]
1180 fn py_orders_for_exec_spawn(
1181 &self,
1182 py: Python,
1183 exec_spawn_id: ClientOrderId,
1184 ) -> PyResult<Vec<Py<PyAny>>> {
1185 let cache = self.0.borrow();
1186 cache
1187 .orders_for_exec_spawn(&exec_spawn_id)
1188 .into_iter()
1189 .map(|o| order_any_to_pyobject(py, o.clone()))
1190 .collect()
1191 }
1192
1193 #[pyo3(name = "exec_spawn_total_quantity")]
1194 fn py_exec_spawn_total_quantity(
1195 &self,
1196 exec_spawn_id: ClientOrderId,
1197 active_only: bool,
1198 ) -> Option<Quantity> {
1199 self.0
1200 .borrow()
1201 .exec_spawn_total_quantity(&exec_spawn_id, active_only)
1202 }
1203
1204 #[pyo3(name = "exec_spawn_total_filled_qty")]
1205 fn py_exec_spawn_total_filled_qty(
1206 &self,
1207 exec_spawn_id: ClientOrderId,
1208 active_only: bool,
1209 ) -> Option<Quantity> {
1210 self.0
1211 .borrow()
1212 .exec_spawn_total_filled_qty(&exec_spawn_id, active_only)
1213 }
1214
1215 #[pyo3(name = "exec_spawn_total_leaves_qty")]
1216 fn py_exec_spawn_total_leaves_qty(
1217 &self,
1218 exec_spawn_id: ClientOrderId,
1219 active_only: bool,
1220 ) -> Option<Quantity> {
1221 self.0
1222 .borrow()
1223 .exec_spawn_total_leaves_qty(&exec_spawn_id, active_only)
1224 }
1225
1226 #[pyo3(name = "position")]
1227 fn py_position(&self, py: Python, position_id: PositionId) -> PyResult<Option<Py<PyAny>>> {
1228 let cache = self.0.borrow();
1229 match cache.position(&position_id) {
1230 Some(position) => Ok(Some(position.clone().into_pyobject(py)?.into())),
1231 None => Ok(None),
1232 }
1233 }
1234
1235 #[pyo3(name = "position_for_order")]
1236 fn py_position_for_order(
1237 &self,
1238 py: Python,
1239 client_order_id: ClientOrderId,
1240 ) -> PyResult<Option<Py<PyAny>>> {
1241 let cache = self.0.borrow();
1242 match cache.position_for_order(&client_order_id) {
1243 Some(position) => Ok(Some(position.clone().into_pyobject(py)?.into())),
1244 None => Ok(None),
1245 }
1246 }
1247
1248 #[pyo3(name = "position_id")]
1249 fn py_position_id(&self, client_order_id: ClientOrderId) -> Option<PositionId> {
1250 self.0.borrow().position_id(&client_order_id).copied()
1251 }
1252
1253 #[pyo3(name = "positions", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
1254 fn py_positions(
1255 &self,
1256 py: Python,
1257 venue: Option<Venue>,
1258 instrument_id: Option<InstrumentId>,
1259 strategy_id: Option<StrategyId>,
1260 account_id: Option<AccountId>,
1261 side: Option<PositionSide>,
1262 ) -> PyResult<Vec<Py<PyAny>>> {
1263 let cache = self.0.borrow();
1264 cache
1265 .positions(
1266 venue.as_ref(),
1267 instrument_id.as_ref(),
1268 strategy_id.as_ref(),
1269 account_id.as_ref(),
1270 side,
1271 )
1272 .into_iter()
1273 .map(|p| Ok(p.clone().into_pyobject(py)?.into()))
1274 .collect()
1275 }
1276
1277 #[pyo3(name = "positions_open", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
1278 fn py_positions_open(
1279 &self,
1280 py: Python,
1281 venue: Option<Venue>,
1282 instrument_id: Option<InstrumentId>,
1283 strategy_id: Option<StrategyId>,
1284 account_id: Option<AccountId>,
1285 side: Option<PositionSide>,
1286 ) -> PyResult<Vec<Py<PyAny>>> {
1287 let cache = self.0.borrow();
1288 cache
1289 .positions_open(
1290 venue.as_ref(),
1291 instrument_id.as_ref(),
1292 strategy_id.as_ref(),
1293 account_id.as_ref(),
1294 side,
1295 )
1296 .into_iter()
1297 .map(|p| Ok(p.clone().into_pyobject(py)?.into()))
1298 .collect()
1299 }
1300
1301 #[pyo3(name = "positions_closed", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
1302 fn py_positions_closed(
1303 &self,
1304 py: Python,
1305 venue: Option<Venue>,
1306 instrument_id: Option<InstrumentId>,
1307 strategy_id: Option<StrategyId>,
1308 account_id: Option<AccountId>,
1309 side: Option<PositionSide>,
1310 ) -> PyResult<Vec<Py<PyAny>>> {
1311 let cache = self.0.borrow();
1312 cache
1313 .positions_closed(
1314 venue.as_ref(),
1315 instrument_id.as_ref(),
1316 strategy_id.as_ref(),
1317 account_id.as_ref(),
1318 side,
1319 )
1320 .into_iter()
1321 .map(|p| Ok(p.clone().into_pyobject(py)?.into()))
1322 .collect()
1323 }
1324
1325 #[pyo3(name = "position_exists")]
1326 fn py_position_exists(&self, position_id: PositionId) -> bool {
1327 self.0.borrow().position_exists(&position_id)
1328 }
1329
1330 #[pyo3(name = "is_position_open")]
1331 fn py_is_position_open(&self, position_id: PositionId) -> bool {
1332 self.0.borrow().is_position_open(&position_id)
1333 }
1334
1335 #[pyo3(name = "is_position_closed")]
1336 fn py_is_position_closed(&self, position_id: PositionId) -> bool {
1337 self.0.borrow().is_position_closed(&position_id)
1338 }
1339
1340 #[pyo3(name = "positions_open_count", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
1341 fn py_positions_open_count(
1342 &self,
1343 venue: Option<Venue>,
1344 instrument_id: Option<InstrumentId>,
1345 strategy_id: Option<StrategyId>,
1346 account_id: Option<AccountId>,
1347 side: Option<PositionSide>,
1348 ) -> usize {
1349 self.0.borrow().positions_open_count(
1350 venue.as_ref(),
1351 instrument_id.as_ref(),
1352 strategy_id.as_ref(),
1353 account_id.as_ref(),
1354 side,
1355 )
1356 }
1357
1358 #[pyo3(name = "positions_closed_count", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
1359 fn py_positions_closed_count(
1360 &self,
1361 venue: Option<Venue>,
1362 instrument_id: Option<InstrumentId>,
1363 strategy_id: Option<StrategyId>,
1364 account_id: Option<AccountId>,
1365 side: Option<PositionSide>,
1366 ) -> usize {
1367 self.0.borrow().positions_closed_count(
1368 venue.as_ref(),
1369 instrument_id.as_ref(),
1370 strategy_id.as_ref(),
1371 account_id.as_ref(),
1372 side,
1373 )
1374 }
1375
1376 #[pyo3(name = "positions_total_count", signature = (venue=None, instrument_id=None, strategy_id=None, account_id=None, side=None))]
1377 fn py_positions_total_count(
1378 &self,
1379 venue: Option<Venue>,
1380 instrument_id: Option<InstrumentId>,
1381 strategy_id: Option<StrategyId>,
1382 account_id: Option<AccountId>,
1383 side: Option<PositionSide>,
1384 ) -> usize {
1385 self.0.borrow().positions_total_count(
1386 venue.as_ref(),
1387 instrument_id.as_ref(),
1388 strategy_id.as_ref(),
1389 account_id.as_ref(),
1390 side,
1391 )
1392 }
1393
1394 #[pyo3(name = "strategy_id_for_order")]
1395 fn py_strategy_id_for_order(&self, client_order_id: ClientOrderId) -> Option<StrategyId> {
1396 self.0
1397 .borrow()
1398 .strategy_id_for_order(&client_order_id)
1399 .copied()
1400 }
1401
1402 #[pyo3(name = "strategy_id_for_position")]
1403 fn py_strategy_id_for_position(&self, position_id: PositionId) -> Option<StrategyId> {
1404 self.0
1405 .borrow()
1406 .strategy_id_for_position(&position_id)
1407 .copied()
1408 }
1409
1410 #[pyo3(name = "position_snapshot_bytes")]
1411 fn py_position_snapshot_bytes(&self, position_id: PositionId) -> Option<Vec<Vec<u8>>> {
1412 self.0.borrow().position_snapshot_bytes(&position_id)
1413 }
1414
1415 #[pyo3(name = "snapshot_position")]
1416 #[expect(clippy::needless_pass_by_value)]
1417 fn py_snapshot_position(&self, py: Python, position: Py<PyAny>) -> PyResult<()> {
1418 let position_obj = position.extract::<Position>(py)?;
1419 self.0
1420 .borrow_mut()
1421 .snapshot_position(&position_obj)
1422 .map_err(to_pyvalue_err)
1423 }
1424
1425 #[pyo3(name = "position_snapshots", signature = (position_id=None, account_id=None))]
1426 fn py_position_snapshots(
1427 &self,
1428 py: Python,
1429 position_id: Option<PositionId>,
1430 account_id: Option<AccountId>,
1431 ) -> PyResult<Vec<Py<PyAny>>> {
1432 let cache = self.0.borrow();
1433 cache
1434 .position_snapshots(position_id.as_ref(), account_id.as_ref())
1435 .into_iter()
1436 .map(|p| Ok(p.into_pyobject(py)?.into()))
1437 .collect()
1438 }
1439}
1440
1441#[cfg(test)]
1442mod tests {
1443 use nautilus_core::UnixNanos;
1444 use nautilus_model::{
1445 data::{BookOrder, stubs::stub_instrument_close},
1446 enums::{BookType, InstrumentCloseType},
1447 };
1448 use pyo3::exceptions::PyValueError;
1449 use rstest::rstest;
1450
1451 use super::*;
1452
1453 fn book_order(side: OrderSide, price: &str, size: &str, id: u64) -> BookOrder {
1454 BookOrder::new(side, Price::from(price), Quantity::from(size), id)
1455 }
1456
1457 #[rstest]
1458 fn test_top_of_book_missing() {
1459 let cache = PyCache::from_rc(Rc::new(RefCell::new(Cache::default())));
1460
1461 assert_eq!(
1462 cache.py_top_of_book(InstrumentId::from("AUD/USD.SIM")),
1463 None
1464 );
1465 }
1466
1467 #[rstest]
1468 #[case::empty(None)]
1469 #[case::bid_only(Some(OrderSide::Buy))]
1470 #[case::ask_only(Some(OrderSide::Sell))]
1471 fn test_top_of_book_incomplete(#[case] side: Option<OrderSide>) {
1472 let instrument_id = InstrumentId::from("AUD/USD.SIM");
1473 let mut book = OrderBook::new(instrument_id, BookType::L2_MBP);
1474 if let Some(side) = side {
1475 book.add(book_order(side, "0.70000", "10", 1), 0, 1, 1.into());
1476 }
1477 let mut cache = Cache::default();
1478 cache.add_order_book(book).unwrap();
1479 let cache = PyCache::from_rc(Rc::new(RefCell::new(cache)));
1480
1481 assert_eq!(cache.py_top_of_book(instrument_id), None);
1482 }
1483
1484 #[rstest]
1485 fn test_top_of_book_python_values_and_resident_updates() {
1486 let instrument_id = InstrumentId::from("AUD/USD.SIM");
1487 let mut book = OrderBook::new(instrument_id, BookType::L2_MBP);
1488 book.add(
1489 book_order(OrderSide::Buy, "0.70000", "10", 1),
1490 0,
1491 1,
1492 1.into(),
1493 );
1494 book.add(
1495 book_order(OrderSide::Sell, "0.70010", "20", 2),
1496 0,
1497 2,
1498 2.into(),
1499 );
1500 let mut cache = Cache::default();
1501 cache.add_order_book(book).unwrap();
1502 let cache = Rc::new(RefCell::new(cache));
1503
1504 Python::initialize();
1505 Python::attach(|py| {
1506 let py_cache = Py::new(py, PyCache::from_rc(cache.clone())).unwrap();
1507 let original = py_cache
1508 .call_method1(py, "top_of_book", (instrument_id,))
1509 .unwrap();
1510 let expected = (
1511 Price::from("0.70000"),
1512 Quantity::from("10"),
1513 Price::from("0.70010"),
1514 Quantity::from("20"),
1515 );
1516 assert_eq!(
1517 original
1518 .extract::<(Price, Quantity, Price, Quantity)>(py)
1519 .unwrap(),
1520 expected
1521 );
1522
1523 {
1524 let mut cache = cache.borrow_mut();
1525 let book = cache.order_book_mut(&instrument_id).unwrap();
1526 book.update(
1527 book_order(OrderSide::Buy, "0.70000", "15", 1),
1528 0,
1529 3,
1530 3.into(),
1531 );
1532 book.add(
1533 book_order(OrderSide::Sell, "0.70005", "25", 3),
1534 0,
1535 4,
1536 4.into(),
1537 );
1538 }
1539 let updated = py_cache
1540 .call_method1(py, "top_of_book", (instrument_id,))
1541 .unwrap()
1542 .extract::<(Price, Quantity, Price, Quantity)>(py)
1543 .unwrap();
1544 assert_eq!(
1545 updated,
1546 (
1547 Price::from("0.70000"),
1548 Quantity::from("15"),
1549 Price::from("0.70005"),
1550 Quantity::from("25"),
1551 )
1552 );
1553 assert_eq!(
1554 original
1555 .extract::<(Price, Quantity, Price, Quantity)>(py)
1556 .unwrap(),
1557 expected
1558 );
1559
1560 cache
1561 .borrow_mut()
1562 .order_book_mut(&instrument_id)
1563 .unwrap()
1564 .clear_asks(5, 5.into());
1565 assert!(
1566 py_cache
1567 .call_method1(py, "top_of_book", (instrument_id,))
1568 .unwrap()
1569 .is_none(py)
1570 );
1571 });
1572 }
1573
1574 #[rstest]
1575 fn test_top_of_book_l3_returns_first_order_sizes() {
1576 let instrument_id = InstrumentId::from("AUD/USD.SIM");
1577 let mut book = OrderBook::new(instrument_id, BookType::L3_MBO);
1578 let first_bid = book_order(OrderSide::Buy, "0.70000", "10", 1);
1579 let first_ask = book_order(OrderSide::Sell, "0.70010", "20", 3);
1580 book.add(first_bid, 0, 1, 1.into());
1581 book.add(
1582 book_order(OrderSide::Buy, "0.70000", "30", 2),
1583 0,
1584 2,
1585 2.into(),
1586 );
1587 book.add(first_ask, 0, 3, 3.into());
1588 book.add(
1589 book_order(OrderSide::Sell, "0.70010", "40", 4),
1590 0,
1591 4,
1592 4.into(),
1593 );
1594 let mut cache = Cache::default();
1595 cache.add_order_book(book).unwrap();
1596 let cache = PyCache::from_rc(Rc::new(RefCell::new(cache)));
1597
1598 assert_eq!(
1599 cache.py_top_of_book(instrument_id),
1600 Some((
1601 Price::from("0.70000"),
1602 Quantity::from("10"),
1603 Price::from("0.70010"),
1604 Quantity::from("20"),
1605 ))
1606 );
1607
1608 {
1609 let mut inner = cache.0.borrow_mut();
1610 let book = inner.order_book_mut(&instrument_id).unwrap();
1611 book.delete(first_bid, 0, 5, 5.into());
1612 book.delete(first_ask, 0, 6, 6.into());
1613 }
1614 assert_eq!(
1615 cache.py_top_of_book(instrument_id),
1616 Some((
1617 Price::from("0.70000"),
1618 Quantity::from("30"),
1619 Price::from("0.70010"),
1620 Quantity::from("40"),
1621 ))
1622 );
1623 }
1624
1625 fn create_order_list() -> OrderList {
1626 OrderList::new(
1627 OrderListId::from("OL-001"),
1628 InstrumentId::from("AUD/USD.SIM"),
1629 StrategyId::from("S-001"),
1630 vec![ClientOrderId::from("O-001")],
1631 UnixNanos::from(42_u64),
1632 )
1633 }
1634
1635 #[rstest]
1636 fn test_order_list_queries_preserve_concrete_type() {
1637 let order_list = create_order_list();
1638 let order_list_id = order_list.id;
1639 let cache = Rc::new(RefCell::new(Cache::default()));
1640 cache
1641 .borrow_mut()
1642 .add_order_list(order_list.clone())
1643 .unwrap();
1644 let py_cache = PyCache::from_rc(cache);
1645
1646 assert_eq!(
1647 py_cache.py_order_list(order_list_id),
1648 Some(order_list.clone()),
1649 );
1650 assert_eq!(
1651 py_cache.py_order_lists(None, None, None, None),
1652 vec![order_list],
1653 );
1654 }
1655
1656 #[rstest]
1657 fn test_add_instrument_close_replaces_existing() {
1658 let first = stub_instrument_close();
1659 let replacement = InstrumentClose::new(
1660 first.instrument_id,
1661 Price::from("0.00000"),
1662 InstrumentCloseType::EndOfSession,
1663 UnixNanos::from(3_u64),
1664 UnixNanos::from(4_u64),
1665 );
1666 let mut py_cache = PyCache::from_rc(Rc::new(RefCell::new(Cache::default())));
1667
1668 py_cache.py_add_instrument_close(first).unwrap();
1669 py_cache.py_add_instrument_close(replacement).unwrap();
1670
1671 assert_eq!(
1672 py_cache.py_instrument_close(first.instrument_id),
1673 Some(replacement)
1674 );
1675 }
1676
1677 #[rstest]
1678 fn test_py_cache_constructor_returns_value_error_for_invalid_config() {
1679 Python::initialize();
1680 let config = CacheConfig {
1681 tick_capacity: 0,
1682 ..Default::default()
1683 };
1684
1685 let err = PyCache::py_new(Some(config)).expect_err("invalid capacity must be rejected");
1686
1687 Python::attach(|py| assert!(err.is_instance_of::<PyValueError>(py)));
1688 }
1689
1690 #[rstest]
1691 fn test_native_cache_binding_returns_value_error_for_invalid_config() {
1692 Python::initialize();
1693 let config = CacheConfig {
1694 bar_capacity: 0,
1695 ..Default::default()
1696 };
1697
1698 let err = Cache::py_new(Some(config)).expect_err("invalid capacity must be rejected");
1699
1700 Python::attach(|py| assert!(err.is_instance_of::<PyValueError>(py)));
1701 }
1702}
1703
1704#[cfg(feature = "defi")]
1705#[pymethods]
1706#[pyo3_stub_gen::derive::gen_stub_pymethods]
1707impl PyCache {
1708 #[pyo3(name = "pool")]
1709 fn py_pool(&self, instrument_id: InstrumentId) -> Option<Pool> {
1710 self.0
1711 .try_borrow()
1712 .ok()
1713 .and_then(|cache| cache.pool(&instrument_id).cloned())
1714 }
1715
1716 #[pyo3(name = "pool_profiler")]
1717 fn py_pool_profiler(&self, instrument_id: InstrumentId) -> Option<PoolProfiler> {
1718 self.0
1719 .try_borrow()
1720 .ok()
1721 .and_then(|cache| cache.pool_profiler(&instrument_id).cloned())
1722 }
1723}
1724
1725#[pymethods]
1726#[pyo3_stub_gen::derive::gen_stub_pymethods]
1727impl CacheConfig {
1728 #[new]
1730 #[expect(clippy::too_many_arguments)]
1731 #[pyo3(signature = (
1732 encoding=None,
1733 timestamps_as_iso8601=None,
1734 buffer_interval_ms=None,
1735 bulk_read_batch_size=None,
1736 use_trader_prefix=None,
1737 use_instance_id=None,
1738 flush_on_start=None,
1739 drop_instruments_on_reset=None,
1740 tick_capacity=None,
1741 bar_capacity=None,
1742 save_market_data=None,
1743 persist_account_events=None,
1744 ))]
1745 fn py_new(
1746 encoding: Option<SerializationEncoding>,
1747 timestamps_as_iso8601: Option<bool>,
1748 buffer_interval_ms: Option<usize>,
1749 bulk_read_batch_size: Option<usize>,
1750 use_trader_prefix: Option<bool>,
1751 use_instance_id: Option<bool>,
1752 flush_on_start: Option<bool>,
1753 drop_instruments_on_reset: Option<bool>,
1754 tick_capacity: Option<usize>,
1755 bar_capacity: Option<usize>,
1756 save_market_data: Option<bool>,
1757 persist_account_events: Option<bool>,
1758 ) -> PyResult<Self> {
1759 let config = Self {
1760 encoding: encoding.unwrap_or_default(),
1761 timestamps_as_iso8601: timestamps_as_iso8601.unwrap_or(false),
1762 buffer_interval_ms,
1763 bulk_read_batch_size,
1764 use_trader_prefix: use_trader_prefix.unwrap_or(true),
1765 use_instance_id: use_instance_id.unwrap_or(false),
1766 flush_on_start: flush_on_start.unwrap_or(false),
1767 drop_instruments_on_reset: drop_instruments_on_reset.unwrap_or(true),
1768 tick_capacity: tick_capacity.unwrap_or(10_000),
1769 bar_capacity: bar_capacity.unwrap_or(10_000),
1770 persist_account_events: persist_account_events.unwrap_or(true),
1771 save_market_data: save_market_data.unwrap_or(false),
1772 };
1773 config.validate().map_err(config_error_to_pyvalue_err)?;
1774 Ok(config)
1775 }
1776
1777 fn __str__(&self) -> String {
1778 format!("{self:?}")
1779 }
1780
1781 fn __repr__(&self) -> String {
1782 format!("{self:?}")
1783 }
1784
1785 #[getter]
1786 fn encoding(&self) -> SerializationEncoding {
1787 self.encoding
1788 }
1789
1790 #[getter]
1791 fn timestamps_as_iso8601(&self) -> bool {
1792 self.timestamps_as_iso8601
1793 }
1794
1795 #[getter]
1796 fn buffer_interval_ms(&self) -> Option<usize> {
1797 self.buffer_interval_ms
1798 }
1799
1800 #[getter]
1801 fn bulk_read_batch_size(&self) -> Option<usize> {
1802 self.bulk_read_batch_size
1803 }
1804
1805 #[getter]
1806 fn use_trader_prefix(&self) -> bool {
1807 self.use_trader_prefix
1808 }
1809
1810 #[getter]
1811 fn use_instance_id(&self) -> bool {
1812 self.use_instance_id
1813 }
1814
1815 #[getter]
1816 fn flush_on_start(&self) -> bool {
1817 self.flush_on_start
1818 }
1819
1820 #[getter]
1821 fn drop_instruments_on_reset(&self) -> bool {
1822 self.drop_instruments_on_reset
1823 }
1824
1825 #[getter]
1826 fn tick_capacity(&self) -> usize {
1827 self.tick_capacity
1828 }
1829
1830 #[getter]
1831 fn bar_capacity(&self) -> usize {
1832 self.bar_capacity
1833 }
1834
1835 #[getter]
1836 fn persist_account_events(&self) -> bool {
1837 self.persist_account_events
1838 }
1839
1840 #[getter]
1841 fn save_market_data(&self) -> bool {
1842 self.save_market_data
1843 }
1844}
1845
1846#[pymethods]
1847impl Cache {
1848 #[new]
1850 fn py_new(config: Option<CacheConfig>) -> PyResult<Self> {
1851 Self::try_new(config, None).map_err(config_error_to_pyvalue_err)
1852 }
1853
1854 fn __repr__(&self) -> String {
1855 format!("{self:?}")
1856 }
1857
1858 #[pyo3(name = "reset")]
1866 fn py_reset(&mut self) {
1867 self.reset();
1868 }
1869
1870 #[pyo3(name = "dispose")]
1874 fn py_dispose(&mut self) {
1875 self.dispose();
1876 }
1877
1878 #[pyo3(name = "purge_closed_orders", signature = (ts_now, buffer_secs=0, purge_from_database=false))]
1892 fn py_purge_closed_orders(
1893 &mut self,
1894 ts_now: u64,
1895 buffer_secs: u64,
1896 purge_from_database: bool,
1897 ) -> PyResult<()> {
1898 self.purge_closed_orders(ts_now.into(), buffer_secs, purge_from_database)
1899 .map_err(to_pyvalue_err)
1900 }
1901
1902 #[pyo3(name = "purge_closed_positions", signature = (ts_now, buffer_secs=0, purge_from_database=false))]
1912 fn py_purge_closed_positions(
1913 &mut self,
1914 ts_now: u64,
1915 buffer_secs: u64,
1916 purge_from_database: bool,
1917 ) -> PyResult<()> {
1918 self.purge_closed_positions(ts_now.into(), buffer_secs, purge_from_database)
1919 .map_err(to_pyvalue_err)
1920 }
1921
1922 #[pyo3(name = "delete_actor")]
1930 fn py_delete_actor(&self, actor_id: ActorId) -> PyResult<()> {
1931 self.delete_actor(&actor_id).map_err(to_pyvalue_err)
1932 }
1933
1934 #[pyo3(name = "delete_strategy")]
1942 fn py_delete_strategy(&self, strategy_id: StrategyId) -> PyResult<()> {
1943 self.delete_strategy(&strategy_id).map_err(to_pyvalue_err)
1944 }
1945
1946 #[pyo3(name = "heartbeat")]
1954 fn py_heartbeat(&self, timestamp: u64) -> PyResult<()> {
1955 self.heartbeat(timestamp.into()).map_err(to_pyvalue_err)
1956 }
1957
1958 #[pyo3(name = "load_instrument")]
1964 fn py_load_instrument(
1965 &mut self,
1966 py: Python<'_>,
1967 instrument_id: InstrumentId,
1968 ) -> PyResult<Option<Py<PyAny>>> {
1969 let instrument = self
1970 .load_instrument(&instrument_id)
1971 .map_err(to_pyvalue_err)?;
1972 instrument
1973 .map(|instrument| instrument_any_to_pyobject(py, instrument))
1974 .transpose()
1975 }
1976
1977 #[pyo3(name = "load_synthetic")]
1983 fn py_load_synthetic(
1984 &mut self,
1985 instrument_id: InstrumentId,
1986 ) -> PyResult<Option<SyntheticInstrument>> {
1987 self.load_synthetic(&instrument_id).map_err(to_pyvalue_err)
1988 }
1989
1990 #[pyo3(name = "purge_order", signature = (client_order_id, purge_from_database=false))]
2002 fn py_purge_order(
2003 &mut self,
2004 client_order_id: ClientOrderId,
2005 purge_from_database: bool,
2006 ) -> PyResult<()> {
2007 self.purge_order(client_order_id, purge_from_database)
2008 .map_err(to_pyvalue_err)
2009 }
2010
2011 #[pyo3(name = "purge_position", signature = (position_id, purge_from_database=false))]
2023 fn py_purge_position(
2024 &mut self,
2025 position_id: PositionId,
2026 purge_from_database: bool,
2027 ) -> PyResult<()> {
2028 self.purge_position(position_id, purge_from_database)
2029 .map_err(to_pyvalue_err)
2030 }
2031
2032 #[pyo3(name = "purge_instrument")]
2037 fn py_purge_instrument(&mut self, instrument_id: InstrumentId) {
2038 self.purge_instrument(instrument_id);
2039 }
2040
2041 #[pyo3(name = "purge_account_events", signature = (ts_now, lookback_secs=0, purge_from_database=false))]
2055 fn py_purge_account_events(
2056 &mut self,
2057 ts_now: u64,
2058 lookback_secs: u64,
2059 purge_from_database: bool,
2060 ) -> PyResult<()> {
2061 self.purge_account_events(ts_now.into(), lookback_secs, purge_from_database)
2062 .map_err(to_pyvalue_err)
2063 }
2064
2065 #[pyo3(name = "add_currency")]
2074 fn py_add_currency(&mut self, currency: Currency) -> PyResult<()> {
2075 self.add_currency(currency).map_err(to_pyvalue_err)
2076 }
2077
2078 #[pyo3(name = "add_instrument")]
2087 fn py_add_instrument(&mut self, py: Python, instrument: Py<PyAny>) -> PyResult<()> {
2088 let instrument_any = pyobject_to_instrument_any(py, instrument)?;
2089 self.add_instrument(instrument_any).map_err(to_pyvalue_err)
2090 }
2091
2092 #[pyo3(name = "instrument")]
2094 fn py_instrument(
2095 &self,
2096 py: Python,
2097 instrument_id: InstrumentId,
2098 ) -> PyResult<Option<Py<PyAny>>> {
2099 match self.instrument(&instrument_id) {
2100 Some(instrument) => Ok(Some(instrument_any_to_pyobject(py, instrument.clone())?)),
2101 None => Ok(None),
2102 }
2103 }
2104
2105 #[pyo3(name = "instrument_ids")]
2107 fn py_instrument_ids(&self, venue: Option<Venue>) -> Vec<InstrumentId> {
2108 self.instrument_ids(venue.as_ref())
2109 .into_iter()
2110 .copied()
2111 .collect()
2112 }
2113
2114 #[pyo3(name = "instruments")]
2116 fn py_instruments(&self, py: Python, venue: Option<Venue>) -> PyResult<Vec<Py<PyAny>>> {
2117 let mut py_instruments = Vec::new();
2118
2119 if let Some(venue) = venue {
2120 let instruments = self.instruments(&venue, None);
2121 for instrument in instruments {
2122 py_instruments.push(instrument_any_to_pyobject(py, (*instrument).clone())?);
2123 }
2124 } else {
2125 let instrument_ids = self.instrument_ids(None);
2126 for instrument_id in instrument_ids {
2127 if let Some(instrument) = self.instrument(instrument_id) {
2128 py_instruments.push(instrument_any_to_pyobject(py, instrument.clone())?);
2129 }
2130 }
2131 }
2132
2133 Ok(py_instruments)
2134 }
2135
2136 #[pyo3(name = "add_order")]
2151 fn py_add_order(
2152 &mut self,
2153 py: Python,
2154 order: Py<PyAny>,
2155 position_id: Option<PositionId>,
2156 client_id: Option<ClientId>,
2157 replace_existing: Option<bool>,
2158 ) -> PyResult<()> {
2159 let order_any = pyobject_to_order_any(py, order)?;
2160 self.add_order(
2161 order_any,
2162 position_id,
2163 client_id,
2164 replace_existing.unwrap_or(false),
2165 )
2166 .map_err(to_pyvalue_err)
2167 }
2168
2169 #[pyo3(name = "order")]
2173 fn py_order(&self, py: Python, client_order_id: ClientOrderId) -> PyResult<Option<Py<PyAny>>> {
2174 match self.order(&client_order_id) {
2175 Some(order) => Ok(Some(order_any_to_pyobject(py, order.clone())?)),
2176 None => Ok(None),
2177 }
2178 }
2179
2180 #[pyo3(name = "order_exists")]
2182 fn py_order_exists(&self, client_order_id: ClientOrderId) -> bool {
2183 self.order_exists(&client_order_id)
2184 }
2185
2186 #[pyo3(name = "is_order_open")]
2188 fn py_is_order_open(&self, client_order_id: ClientOrderId) -> bool {
2189 self.is_order_open(&client_order_id)
2190 }
2191
2192 #[pyo3(name = "is_order_closed")]
2194 fn py_is_order_closed(&self, client_order_id: ClientOrderId) -> bool {
2195 self.is_order_closed(&client_order_id)
2196 }
2197
2198 #[pyo3(name = "is_order_active_local")]
2203 fn py_is_order_active_local(&self, client_order_id: ClientOrderId) -> bool {
2204 self.is_order_active_local(&client_order_id)
2205 }
2206
2207 #[pyo3(name = "orders_active_local")]
2211 fn py_orders_active_local(
2212 &self,
2213 py: Python,
2214 venue: Option<Venue>,
2215 instrument_id: Option<InstrumentId>,
2216 strategy_id: Option<StrategyId>,
2217 account_id: Option<AccountId>,
2218 side: Option<OrderSide>,
2219 ) -> PyResult<Vec<Py<PyAny>>> {
2220 self.orders_active_local(
2221 venue.as_ref(),
2222 instrument_id.as_ref(),
2223 strategy_id.as_ref(),
2224 account_id.as_ref(),
2225 side,
2226 )
2227 .into_iter()
2228 .map(|order| order_any_to_pyobject(py, order.clone()))
2229 .collect()
2230 }
2231
2232 #[pyo3(name = "orders_active_local_count")]
2237 fn py_orders_active_local_count(
2238 &self,
2239 venue: Option<Venue>,
2240 instrument_id: Option<InstrumentId>,
2241 strategy_id: Option<StrategyId>,
2242 account_id: Option<AccountId>,
2243 side: Option<OrderSide>,
2244 ) -> usize {
2245 self.orders_active_local_count(
2246 venue.as_ref(),
2247 instrument_id.as_ref(),
2248 strategy_id.as_ref(),
2249 account_id.as_ref(),
2250 side,
2251 )
2252 }
2253
2254 #[pyo3(name = "orders_open_count")]
2256 fn py_orders_open_count(
2257 &self,
2258 venue: Option<Venue>,
2259 instrument_id: Option<InstrumentId>,
2260 strategy_id: Option<StrategyId>,
2261 account_id: Option<AccountId>,
2262 side: Option<OrderSide>,
2263 ) -> usize {
2264 self.orders_open_count(
2265 venue.as_ref(),
2266 instrument_id.as_ref(),
2267 strategy_id.as_ref(),
2268 account_id.as_ref(),
2269 side,
2270 )
2271 }
2272
2273 #[pyo3(name = "orders_closed_count")]
2275 fn py_orders_closed_count(
2276 &self,
2277 venue: Option<Venue>,
2278 instrument_id: Option<InstrumentId>,
2279 strategy_id: Option<StrategyId>,
2280 account_id: Option<AccountId>,
2281 side: Option<OrderSide>,
2282 ) -> usize {
2283 self.orders_closed_count(
2284 venue.as_ref(),
2285 instrument_id.as_ref(),
2286 strategy_id.as_ref(),
2287 account_id.as_ref(),
2288 side,
2289 )
2290 }
2291
2292 #[pyo3(name = "orders_total_count")]
2294 fn py_orders_total_count(
2295 &self,
2296 venue: Option<Venue>,
2297 instrument_id: Option<InstrumentId>,
2298 strategy_id: Option<StrategyId>,
2299 account_id: Option<AccountId>,
2300 side: Option<OrderSide>,
2301 ) -> usize {
2302 self.orders_total_count(
2303 venue.as_ref(),
2304 instrument_id.as_ref(),
2305 strategy_id.as_ref(),
2306 account_id.as_ref(),
2307 side,
2308 )
2309 }
2310
2311 #[pyo3(name = "add_position")]
2323 #[expect(clippy::needless_pass_by_value)]
2324 fn py_add_position(
2325 &mut self,
2326 py: Python,
2327 position: Py<PyAny>,
2328 oms_type: OmsType,
2329 ) -> PyResult<()> {
2330 let position_obj = position.extract::<Position>(py)?;
2331 self.add_position(&position_obj, oms_type)
2332 .map_err(to_pyvalue_err)
2333 }
2334
2335 #[pyo3(name = "snapshot_position")]
2350 #[expect(clippy::needless_pass_by_value)]
2351 fn py_snapshot_position(&mut self, py: Python, position: Py<PyAny>) -> PyResult<()> {
2352 let position_obj = position.extract::<Position>(py)?;
2353 self.snapshot_position(&position_obj)
2354 .map_err(to_pyvalue_err)
2355 }
2356
2357 #[pyo3(name = "position")]
2361 fn py_position(&self, py: Python, position_id: PositionId) -> PyResult<Option<Py<PyAny>>> {
2362 match self.position(&position_id) {
2363 Some(position) => Ok(Some(position.clone().into_pyobject(py)?.into())),
2364 None => Ok(None),
2365 }
2366 }
2367
2368 #[pyo3(name = "position_exists")]
2370 fn py_position_exists(&self, position_id: PositionId) -> bool {
2371 self.position_exists(&position_id)
2372 }
2373
2374 #[pyo3(name = "is_position_open")]
2376 fn py_is_position_open(&self, position_id: PositionId) -> bool {
2377 self.is_position_open(&position_id)
2378 }
2379
2380 #[pyo3(name = "is_position_closed")]
2382 fn py_is_position_closed(&self, position_id: PositionId) -> bool {
2383 self.is_position_closed(&position_id)
2384 }
2385
2386 #[pyo3(name = "positions_open_count")]
2388 fn py_positions_open_count(
2389 &self,
2390 venue: Option<Venue>,
2391 instrument_id: Option<InstrumentId>,
2392 strategy_id: Option<StrategyId>,
2393 account_id: Option<AccountId>,
2394 side: Option<PositionSide>,
2395 ) -> usize {
2396 self.positions_open_count(
2397 venue.as_ref(),
2398 instrument_id.as_ref(),
2399 strategy_id.as_ref(),
2400 account_id.as_ref(),
2401 side,
2402 )
2403 }
2404
2405 #[pyo3(name = "positions_closed_count")]
2407 fn py_positions_closed_count(
2408 &self,
2409 venue: Option<Venue>,
2410 instrument_id: Option<InstrumentId>,
2411 strategy_id: Option<StrategyId>,
2412 account_id: Option<AccountId>,
2413 side: Option<PositionSide>,
2414 ) -> usize {
2415 self.positions_closed_count(
2416 venue.as_ref(),
2417 instrument_id.as_ref(),
2418 strategy_id.as_ref(),
2419 account_id.as_ref(),
2420 side,
2421 )
2422 }
2423
2424 #[pyo3(name = "positions_total_count")]
2426 fn py_positions_total_count(
2427 &self,
2428 venue: Option<Venue>,
2429 instrument_id: Option<InstrumentId>,
2430 strategy_id: Option<StrategyId>,
2431 account_id: Option<AccountId>,
2432 side: Option<PositionSide>,
2433 ) -> usize {
2434 self.positions_total_count(
2435 venue.as_ref(),
2436 instrument_id.as_ref(),
2437 strategy_id.as_ref(),
2438 account_id.as_ref(),
2439 side,
2440 )
2441 }
2442
2443 #[pyo3(name = "add_quote")]
2451 fn py_add_quote(&mut self, quote: QuoteTick) -> PyResult<()> {
2452 self.add_quote(quote).map_err(to_pyvalue_err)
2453 }
2454
2455 #[pyo3(name = "add_trade")]
2463 fn py_add_trade(&mut self, trade: TradeTick) -> PyResult<()> {
2464 self.add_trade(trade).map_err(to_pyvalue_err)
2465 }
2466
2467 #[pyo3(name = "add_bar")]
2478 fn py_add_bar(&mut self, bar: Bar) -> PyResult<()> {
2479 self.add_bar(bar).map_err(to_pyvalue_err)
2480 }
2481
2482 #[pyo3(name = "quote")]
2484 fn py_quote(&self, instrument_id: InstrumentId) -> Option<QuoteTick> {
2485 self.quote(&instrument_id).copied()
2486 }
2487
2488 #[pyo3(name = "trade")]
2490 fn py_trade(&self, instrument_id: InstrumentId) -> Option<TradeTick> {
2491 self.trade(&instrument_id).copied()
2492 }
2493
2494 #[pyo3(name = "bar")]
2496 fn py_bar(&self, bar_type: BarType) -> Option<Bar> {
2497 self.bar(&bar_type).copied()
2498 }
2499
2500 #[pyo3(name = "quotes")]
2505 fn py_quotes(&self, instrument_id: InstrumentId) -> Option<Vec<QuoteTick>> {
2506 self.quotes(&instrument_id)
2507 }
2508
2509 #[pyo3(name = "trades")]
2514 fn py_trades(&self, instrument_id: InstrumentId) -> Option<Vec<TradeTick>> {
2515 self.trades(&instrument_id)
2516 }
2517
2518 #[pyo3(name = "bars")]
2522 fn py_bars(&self, bar_type: BarType) -> Option<Vec<Bar>> {
2523 self.bars(&bar_type)
2524 }
2525
2526 #[pyo3(name = "has_quote_ticks")]
2528 fn py_has_quote_ticks(&self, instrument_id: InstrumentId) -> bool {
2529 self.has_quote_ticks(&instrument_id)
2530 }
2531
2532 #[pyo3(name = "has_trade_ticks")]
2534 fn py_has_trade_ticks(&self, instrument_id: InstrumentId) -> bool {
2535 self.has_trade_ticks(&instrument_id)
2536 }
2537
2538 #[pyo3(name = "has_mark_prices")]
2540 fn py_has_mark_prices(&self, instrument_id: InstrumentId) -> bool {
2541 self.has_mark_prices(&instrument_id)
2542 }
2543
2544 #[pyo3(name = "has_index_prices")]
2546 fn py_has_index_prices(&self, instrument_id: InstrumentId) -> bool {
2547 self.has_index_prices(&instrument_id)
2548 }
2549
2550 #[pyo3(name = "has_funding_rates")]
2552 fn py_has_funding_rates(&self, instrument_id: InstrumentId) -> bool {
2553 self.has_funding_rates(&instrument_id)
2554 }
2555
2556 #[pyo3(name = "has_instrument_statuses")]
2558 fn py_has_instrument_statuses(&self, instrument_id: InstrumentId) -> bool {
2559 self.has_instrument_statuses(&instrument_id)
2560 }
2561
2562 #[pyo3(name = "has_instrument_close")]
2564 fn py_has_instrument_close(&self, instrument_id: InstrumentId) -> bool {
2565 self.has_instrument_close(&instrument_id)
2566 }
2567
2568 #[pyo3(name = "has_bars")]
2570 fn py_has_bars(&self, bar_type: BarType) -> bool {
2571 self.has_bars(&bar_type)
2572 }
2573
2574 #[pyo3(name = "quote_count")]
2576 fn py_quote_count(&self, instrument_id: InstrumentId) -> usize {
2577 self.quote_count(&instrument_id)
2578 }
2579
2580 #[pyo3(name = "trade_count")]
2582 fn py_trade_count(&self, instrument_id: InstrumentId) -> usize {
2583 self.trade_count(&instrument_id)
2584 }
2585
2586 #[pyo3(name = "mark_price_count")]
2588 fn py_mark_price_count(&self, instrument_id: InstrumentId) -> usize {
2589 self.mark_price_count(&instrument_id)
2590 }
2591
2592 #[pyo3(name = "index_price_count")]
2594 fn py_index_price_count(&self, instrument_id: InstrumentId) -> usize {
2595 self.index_price_count(&instrument_id)
2596 }
2597
2598 #[pyo3(name = "funding_rate_count")]
2600 fn py_funding_rate_count(&self, instrument_id: InstrumentId) -> usize {
2601 self.funding_rate_count(&instrument_id)
2602 }
2603
2604 #[pyo3(name = "instrument_status_count")]
2606 fn py_instrument_status_count(&self, instrument_id: InstrumentId) -> usize {
2607 self.instrument_status_count(&instrument_id)
2608 }
2609
2610 #[pyo3(name = "bar_count")]
2612 fn py_bar_count(&self, bar_type: BarType) -> usize {
2613 self.bar_count(&bar_type)
2614 }
2615
2616 #[pyo3(name = "mark_price")]
2618 fn py_mark_price(&self, instrument_id: InstrumentId) -> Option<MarkPriceUpdate> {
2619 self.mark_price(&instrument_id).copied()
2620 }
2621
2622 #[pyo3(name = "mark_prices")]
2627 fn py_mark_prices(&self, instrument_id: InstrumentId) -> Option<Vec<MarkPriceUpdate>> {
2628 self.mark_prices(&instrument_id)
2629 }
2630
2631 #[pyo3(name = "index_price")]
2633 fn py_index_price(&self, instrument_id: InstrumentId) -> Option<IndexPriceUpdate> {
2634 self.index_price(&instrument_id).copied()
2635 }
2636
2637 #[pyo3(name = "index_prices")]
2642 fn py_index_prices(&self, instrument_id: InstrumentId) -> Option<Vec<IndexPriceUpdate>> {
2643 self.index_prices(&instrument_id)
2644 }
2645
2646 #[pyo3(name = "funding_rate")]
2648 fn py_funding_rate(&self, instrument_id: InstrumentId) -> Option<FundingRateUpdate> {
2649 self.funding_rate(&instrument_id).copied()
2650 }
2651
2652 #[pyo3(name = "funding_rates")]
2657 fn py_funding_rates(&self, instrument_id: InstrumentId) -> Option<Vec<FundingRateUpdate>> {
2658 self.funding_rates(&instrument_id)
2659 }
2660
2661 #[pyo3(name = "instrument_status")]
2663 fn py_instrument_status(&self, instrument_id: InstrumentId) -> Option<InstrumentStatus> {
2664 self.instrument_status(&instrument_id).copied()
2665 }
2666
2667 #[pyo3(name = "instrument_statuses")]
2672 fn py_instrument_statuses(&self, instrument_id: InstrumentId) -> Option<Vec<InstrumentStatus>> {
2673 self.instrument_statuses(&instrument_id)
2674 }
2675
2676 #[pyo3(name = "instrument_close")]
2680 fn py_instrument_close(&self, instrument_id: InstrumentId) -> Option<InstrumentClose> {
2681 self.instrument_close(&instrument_id).copied()
2682 }
2683
2684 #[pyo3(name = "order_book")]
2688 fn py_order_book(&self, instrument_id: InstrumentId) -> Option<OrderBook> {
2689 self.order_book(&instrument_id).cloned()
2690 }
2691
2692 #[pyo3(name = "has_order_book")]
2694 fn py_has_order_book(&self, instrument_id: InstrumentId) -> bool {
2695 self.has_order_book(&instrument_id)
2696 }
2697
2698 #[pyo3(name = "book_update_count")]
2700 fn py_book_update_count(&self, instrument_id: InstrumentId) -> usize {
2701 self.book_update_count(&instrument_id)
2702 }
2703
2704 #[pyo3(name = "synthetic")]
2706 fn py_synthetic(&self, instrument_id: InstrumentId) -> Option<SyntheticInstrument> {
2707 self.synthetic(&instrument_id).cloned()
2708 }
2709
2710 #[pyo3(name = "synthetic_ids")]
2712 fn py_synthetic_ids(&self) -> Vec<InstrumentId> {
2713 self.synthetic_ids().into_iter().copied().collect()
2714 }
2715
2716 #[pyo3(name = "client_order_ids")]
2718 fn py_client_order_ids(
2719 &self,
2720 venue: Option<Venue>,
2721 instrument_id: Option<InstrumentId>,
2722 strategy_id: Option<StrategyId>,
2723 account_id: Option<AccountId>,
2724 ) -> Vec<ClientOrderId> {
2725 self.client_order_ids(
2726 venue.as_ref(),
2727 instrument_id.as_ref(),
2728 strategy_id.as_ref(),
2729 account_id.as_ref(),
2730 )
2731 .into_iter()
2732 .collect()
2733 }
2734
2735 #[pyo3(name = "client_order_ids_open")]
2737 fn py_client_order_ids_open(
2738 &self,
2739 venue: Option<Venue>,
2740 instrument_id: Option<InstrumentId>,
2741 strategy_id: Option<StrategyId>,
2742 account_id: Option<AccountId>,
2743 ) -> Vec<ClientOrderId> {
2744 self.client_order_ids_open(
2745 venue.as_ref(),
2746 instrument_id.as_ref(),
2747 strategy_id.as_ref(),
2748 account_id.as_ref(),
2749 )
2750 .into_iter()
2751 .collect()
2752 }
2753
2754 #[pyo3(name = "client_order_ids_closed")]
2756 fn py_client_order_ids_closed(
2757 &self,
2758 venue: Option<Venue>,
2759 instrument_id: Option<InstrumentId>,
2760 strategy_id: Option<StrategyId>,
2761 account_id: Option<AccountId>,
2762 ) -> Vec<ClientOrderId> {
2763 self.client_order_ids_closed(
2764 venue.as_ref(),
2765 instrument_id.as_ref(),
2766 strategy_id.as_ref(),
2767 account_id.as_ref(),
2768 )
2769 .into_iter()
2770 .collect()
2771 }
2772
2773 #[pyo3(name = "client_order_ids_emulated")]
2775 fn py_client_order_ids_emulated(
2776 &self,
2777 venue: Option<Venue>,
2778 instrument_id: Option<InstrumentId>,
2779 strategy_id: Option<StrategyId>,
2780 account_id: Option<AccountId>,
2781 ) -> Vec<ClientOrderId> {
2782 self.client_order_ids_emulated(
2783 venue.as_ref(),
2784 instrument_id.as_ref(),
2785 strategy_id.as_ref(),
2786 account_id.as_ref(),
2787 )
2788 .into_iter()
2789 .collect()
2790 }
2791
2792 #[pyo3(name = "client_order_ids_inflight")]
2794 fn py_client_order_ids_inflight(
2795 &self,
2796 venue: Option<Venue>,
2797 instrument_id: Option<InstrumentId>,
2798 strategy_id: Option<StrategyId>,
2799 account_id: Option<AccountId>,
2800 ) -> Vec<ClientOrderId> {
2801 self.client_order_ids_inflight(
2802 venue.as_ref(),
2803 instrument_id.as_ref(),
2804 strategy_id.as_ref(),
2805 account_id.as_ref(),
2806 )
2807 .into_iter()
2808 .collect()
2809 }
2810
2811 #[pyo3(name = "position_ids")]
2813 fn py_position_ids(
2814 &self,
2815 venue: Option<Venue>,
2816 instrument_id: Option<InstrumentId>,
2817 strategy_id: Option<StrategyId>,
2818 account_id: Option<AccountId>,
2819 ) -> Vec<PositionId> {
2820 self.position_ids(
2821 venue.as_ref(),
2822 instrument_id.as_ref(),
2823 strategy_id.as_ref(),
2824 account_id.as_ref(),
2825 )
2826 .into_iter()
2827 .collect()
2828 }
2829
2830 #[pyo3(name = "position_open_ids")]
2832 fn py_position_open_ids(
2833 &self,
2834 venue: Option<Venue>,
2835 instrument_id: Option<InstrumentId>,
2836 strategy_id: Option<StrategyId>,
2837 account_id: Option<AccountId>,
2838 ) -> Vec<PositionId> {
2839 self.position_open_ids(
2840 venue.as_ref(),
2841 instrument_id.as_ref(),
2842 strategy_id.as_ref(),
2843 account_id.as_ref(),
2844 )
2845 .into_iter()
2846 .collect()
2847 }
2848
2849 #[pyo3(name = "position_closed_ids")]
2851 fn py_position_closed_ids(
2852 &self,
2853 venue: Option<Venue>,
2854 instrument_id: Option<InstrumentId>,
2855 strategy_id: Option<StrategyId>,
2856 account_id: Option<AccountId>,
2857 ) -> Vec<PositionId> {
2858 self.position_closed_ids(
2859 venue.as_ref(),
2860 instrument_id.as_ref(),
2861 strategy_id.as_ref(),
2862 account_id.as_ref(),
2863 )
2864 .into_iter()
2865 .collect()
2866 }
2867
2868 #[pyo3(name = "strategy_ids")]
2870 fn py_strategy_ids(&self) -> Vec<StrategyId> {
2871 self.strategy_ids().into_iter().collect()
2872 }
2873
2874 #[pyo3(name = "exec_algorithm_ids")]
2876 fn py_exec_algorithm_ids(&self) -> Vec<ExecAlgorithmId> {
2877 self.exec_algorithm_ids().into_iter().collect()
2878 }
2879
2880 #[pyo3(name = "client_order_id")]
2882 fn py_client_order_id(&self, venue_order_id: VenueOrderId) -> Option<ClientOrderId> {
2883 self.client_order_id(&venue_order_id).copied()
2884 }
2885
2886 #[pyo3(name = "venue_order_id")]
2888 fn py_venue_order_id(&self, client_order_id: ClientOrderId) -> Option<VenueOrderId> {
2889 self.venue_order_id(&client_order_id).copied()
2890 }
2891
2892 #[pyo3(name = "client_id")]
2894 fn py_client_id(&self, client_order_id: ClientOrderId) -> Option<ClientId> {
2895 self.client_id(&client_order_id).copied()
2896 }
2897
2898 #[pyo3(name = "orders")]
2902 fn py_orders(
2903 &self,
2904 py: Python,
2905 venue: Option<Venue>,
2906 instrument_id: Option<InstrumentId>,
2907 strategy_id: Option<StrategyId>,
2908 account_id: Option<AccountId>,
2909 side: Option<OrderSide>,
2910 ) -> PyResult<Vec<Py<PyAny>>> {
2911 self.orders(
2912 venue.as_ref(),
2913 instrument_id.as_ref(),
2914 strategy_id.as_ref(),
2915 account_id.as_ref(),
2916 side,
2917 )
2918 .into_iter()
2919 .map(|o| order_any_to_pyobject(py, o.clone()))
2920 .collect()
2921 }
2922
2923 #[pyo3(name = "orders_open")]
2927 fn py_orders_open(
2928 &self,
2929 py: Python,
2930 venue: Option<Venue>,
2931 instrument_id: Option<InstrumentId>,
2932 strategy_id: Option<StrategyId>,
2933 account_id: Option<AccountId>,
2934 side: Option<OrderSide>,
2935 ) -> PyResult<Vec<Py<PyAny>>> {
2936 self.orders_open(
2937 venue.as_ref(),
2938 instrument_id.as_ref(),
2939 strategy_id.as_ref(),
2940 account_id.as_ref(),
2941 side,
2942 )
2943 .into_iter()
2944 .map(|o| order_any_to_pyobject(py, o.clone()))
2945 .collect()
2946 }
2947
2948 #[pyo3(name = "orders_closed")]
2952 fn py_orders_closed(
2953 &self,
2954 py: Python,
2955 venue: Option<Venue>,
2956 instrument_id: Option<InstrumentId>,
2957 strategy_id: Option<StrategyId>,
2958 account_id: Option<AccountId>,
2959 side: Option<OrderSide>,
2960 ) -> PyResult<Vec<Py<PyAny>>> {
2961 self.orders_closed(
2962 venue.as_ref(),
2963 instrument_id.as_ref(),
2964 strategy_id.as_ref(),
2965 account_id.as_ref(),
2966 side,
2967 )
2968 .into_iter()
2969 .map(|o| order_any_to_pyobject(py, o.clone()))
2970 .collect()
2971 }
2972
2973 #[pyo3(name = "orders_emulated")]
2977 fn py_orders_emulated(
2978 &self,
2979 py: Python,
2980 venue: Option<Venue>,
2981 instrument_id: Option<InstrumentId>,
2982 strategy_id: Option<StrategyId>,
2983 account_id: Option<AccountId>,
2984 side: Option<OrderSide>,
2985 ) -> PyResult<Vec<Py<PyAny>>> {
2986 self.orders_emulated(
2987 venue.as_ref(),
2988 instrument_id.as_ref(),
2989 strategy_id.as_ref(),
2990 account_id.as_ref(),
2991 side,
2992 )
2993 .into_iter()
2994 .map(|o| order_any_to_pyobject(py, o.clone()))
2995 .collect()
2996 }
2997
2998 #[pyo3(name = "orders_inflight")]
3002 fn py_orders_inflight(
3003 &self,
3004 py: Python,
3005 venue: Option<Venue>,
3006 instrument_id: Option<InstrumentId>,
3007 strategy_id: Option<StrategyId>,
3008 account_id: Option<AccountId>,
3009 side: Option<OrderSide>,
3010 ) -> PyResult<Vec<Py<PyAny>>> {
3011 self.orders_inflight(
3012 venue.as_ref(),
3013 instrument_id.as_ref(),
3014 strategy_id.as_ref(),
3015 account_id.as_ref(),
3016 side,
3017 )
3018 .into_iter()
3019 .map(|o| order_any_to_pyobject(py, o.clone()))
3020 .collect()
3021 }
3022
3023 #[pyo3(name = "orders_for_position")]
3025 fn py_orders_for_position(
3026 &self,
3027 py: Python,
3028 position_id: PositionId,
3029 ) -> PyResult<Vec<Py<PyAny>>> {
3030 self.orders_for_position(&position_id)
3031 .into_iter()
3032 .map(|o| order_any_to_pyobject(py, o.clone()))
3033 .collect()
3034 }
3035
3036 #[pyo3(name = "is_order_emulated")]
3038 fn py_is_order_emulated(&self, client_order_id: ClientOrderId) -> bool {
3039 self.is_order_emulated(&client_order_id)
3040 }
3041
3042 #[pyo3(name = "is_order_inflight")]
3044 fn py_is_order_inflight(&self, client_order_id: ClientOrderId) -> bool {
3045 self.is_order_inflight(&client_order_id)
3046 }
3047
3048 #[pyo3(name = "is_order_pending_cancel_local")]
3050 fn py_is_order_pending_cancel_local(&self, client_order_id: ClientOrderId) -> bool {
3051 self.is_order_pending_cancel_local(&client_order_id)
3052 }
3053
3054 #[pyo3(name = "orders_emulated_count")]
3056 fn py_orders_emulated_count(
3057 &self,
3058 venue: Option<Venue>,
3059 instrument_id: Option<InstrumentId>,
3060 strategy_id: Option<StrategyId>,
3061 account_id: Option<AccountId>,
3062 side: Option<OrderSide>,
3063 ) -> usize {
3064 self.orders_emulated_count(
3065 venue.as_ref(),
3066 instrument_id.as_ref(),
3067 strategy_id.as_ref(),
3068 account_id.as_ref(),
3069 side,
3070 )
3071 }
3072
3073 #[pyo3(name = "orders_inflight_count")]
3075 fn py_orders_inflight_count(
3076 &self,
3077 venue: Option<Venue>,
3078 instrument_id: Option<InstrumentId>,
3079 strategy_id: Option<StrategyId>,
3080 account_id: Option<AccountId>,
3081 side: Option<OrderSide>,
3082 ) -> usize {
3083 self.orders_inflight_count(
3084 venue.as_ref(),
3085 instrument_id.as_ref(),
3086 strategy_id.as_ref(),
3087 account_id.as_ref(),
3088 side,
3089 )
3090 }
3091
3092 #[pyo3(name = "order_list")]
3094 fn py_order_list(&self, order_list_id: OrderListId) -> Option<OrderList> {
3095 self.order_list(&order_list_id).cloned()
3096 }
3097
3098 #[pyo3(name = "order_lists")]
3100 fn py_order_lists(
3101 &self,
3102 venue: Option<Venue>,
3103 instrument_id: Option<InstrumentId>,
3104 strategy_id: Option<StrategyId>,
3105 account_id: Option<AccountId>,
3106 ) -> Vec<OrderList> {
3107 self.order_lists(
3108 venue.as_ref(),
3109 instrument_id.as_ref(),
3110 strategy_id.as_ref(),
3111 account_id.as_ref(),
3112 )
3113 .into_iter()
3114 .cloned()
3115 .collect()
3116 }
3117
3118 #[pyo3(name = "order_list_exists")]
3120 fn py_order_list_exists(&self, order_list_id: OrderListId) -> bool {
3121 self.order_list_exists(&order_list_id)
3122 }
3123
3124 #[pyo3(name = "orders_for_exec_algorithm")]
3127 #[expect(clippy::too_many_arguments)]
3128 fn py_orders_for_exec_algorithm(
3129 &self,
3130 py: Python,
3131 exec_algorithm_id: ExecAlgorithmId,
3132 venue: Option<Venue>,
3133 instrument_id: Option<InstrumentId>,
3134 strategy_id: Option<StrategyId>,
3135 account_id: Option<AccountId>,
3136 side: Option<OrderSide>,
3137 ) -> PyResult<Vec<Py<PyAny>>> {
3138 self.orders_for_exec_algorithm(
3139 &exec_algorithm_id,
3140 venue.as_ref(),
3141 instrument_id.as_ref(),
3142 strategy_id.as_ref(),
3143 account_id.as_ref(),
3144 side,
3145 )
3146 .into_iter()
3147 .map(|o| order_any_to_pyobject(py, o.clone()))
3148 .collect()
3149 }
3150
3151 #[pyo3(name = "orders_for_exec_spawn")]
3153 fn py_orders_for_exec_spawn(
3154 &self,
3155 py: Python,
3156 exec_spawn_id: ClientOrderId,
3157 ) -> PyResult<Vec<Py<PyAny>>> {
3158 self.orders_for_exec_spawn(&exec_spawn_id)
3159 .into_iter()
3160 .map(|o| order_any_to_pyobject(py, o.clone()))
3161 .collect()
3162 }
3163
3164 #[pyo3(name = "exec_spawn_total_quantity")]
3166 fn py_exec_spawn_total_quantity(
3167 &self,
3168 exec_spawn_id: ClientOrderId,
3169 active_only: bool,
3170 ) -> Option<Quantity> {
3171 self.exec_spawn_total_quantity(&exec_spawn_id, active_only)
3172 }
3173
3174 #[pyo3(name = "exec_spawn_total_filled_qty")]
3176 fn py_exec_spawn_total_filled_qty(
3177 &self,
3178 exec_spawn_id: ClientOrderId,
3179 active_only: bool,
3180 ) -> Option<Quantity> {
3181 self.exec_spawn_total_filled_qty(&exec_spawn_id, active_only)
3182 }
3183
3184 #[pyo3(name = "exec_spawn_total_leaves_qty")]
3186 fn py_exec_spawn_total_leaves_qty(
3187 &self,
3188 exec_spawn_id: ClientOrderId,
3189 active_only: bool,
3190 ) -> Option<Quantity> {
3191 self.exec_spawn_total_leaves_qty(&exec_spawn_id, active_only)
3192 }
3193
3194 #[pyo3(name = "position_for_order")]
3198 fn py_position_for_order(
3199 &self,
3200 py: Python,
3201 client_order_id: ClientOrderId,
3202 ) -> PyResult<Option<Py<PyAny>>> {
3203 match self.position_for_order(&client_order_id) {
3204 Some(position) => Ok(Some(position.clone().into_pyobject(py)?.into())),
3205 None => Ok(None),
3206 }
3207 }
3208
3209 #[pyo3(name = "position_id")]
3211 fn py_position_id(&self, client_order_id: ClientOrderId) -> Option<PositionId> {
3212 self.position_id(&client_order_id).copied()
3213 }
3214
3215 #[pyo3(name = "positions")]
3219 fn py_positions(
3220 &self,
3221 py: Python,
3222 venue: Option<Venue>,
3223 instrument_id: Option<InstrumentId>,
3224 strategy_id: Option<StrategyId>,
3225 account_id: Option<AccountId>,
3226 side: Option<PositionSide>,
3227 ) -> PyResult<Vec<Py<PyAny>>> {
3228 self.positions(
3229 venue.as_ref(),
3230 instrument_id.as_ref(),
3231 strategy_id.as_ref(),
3232 account_id.as_ref(),
3233 side,
3234 )
3235 .into_iter()
3236 .map(|p| Ok(p.clone().into_pyobject(py)?.into()))
3237 .collect()
3238 }
3239
3240 #[pyo3(name = "positions_open")]
3244 fn py_positions_open(
3245 &self,
3246 py: Python,
3247 venue: Option<Venue>,
3248 instrument_id: Option<InstrumentId>,
3249 strategy_id: Option<StrategyId>,
3250 account_id: Option<AccountId>,
3251 side: Option<PositionSide>,
3252 ) -> PyResult<Vec<Py<PyAny>>> {
3253 self.positions_open(
3254 venue.as_ref(),
3255 instrument_id.as_ref(),
3256 strategy_id.as_ref(),
3257 account_id.as_ref(),
3258 side,
3259 )
3260 .into_iter()
3261 .map(|p| Ok(p.clone().into_pyobject(py)?.into()))
3262 .collect()
3263 }
3264
3265 #[pyo3(name = "positions_closed")]
3269 fn py_positions_closed(
3270 &self,
3271 py: Python,
3272 venue: Option<Venue>,
3273 instrument_id: Option<InstrumentId>,
3274 strategy_id: Option<StrategyId>,
3275 account_id: Option<AccountId>,
3276 side: Option<PositionSide>,
3277 ) -> PyResult<Vec<Py<PyAny>>> {
3278 self.positions_closed(
3279 venue.as_ref(),
3280 instrument_id.as_ref(),
3281 strategy_id.as_ref(),
3282 account_id.as_ref(),
3283 side,
3284 )
3285 .into_iter()
3286 .map(|p| Ok(p.clone().into_pyobject(py)?.into()))
3287 .collect()
3288 }
3289
3290 #[pyo3(name = "strategy_id_for_order")]
3292 fn py_strategy_id_for_order(&self, client_order_id: ClientOrderId) -> Option<StrategyId> {
3293 self.strategy_id_for_order(&client_order_id).copied()
3294 }
3295
3296 #[pyo3(name = "strategy_id_for_position")]
3298 fn py_strategy_id_for_position(&self, position_id: PositionId) -> Option<StrategyId> {
3299 self.strategy_id_for_position(&position_id).copied()
3300 }
3301
3302 #[pyo3(name = "position_snapshot_bytes")]
3307 fn py_position_snapshot_bytes(&self, position_id: PositionId) -> Option<Vec<Vec<u8>>> {
3308 self.position_snapshot_bytes(&position_id)
3309 }
3310
3311 #[pyo3(name = "position_snapshots", signature = (position_id=None, account_id=None))]
3316 fn py_position_snapshots(
3317 &self,
3318 py: Python,
3319 position_id: Option<PositionId>,
3320 account_id: Option<AccountId>,
3321 ) -> PyResult<Vec<Py<PyAny>>> {
3322 self.position_snapshots(position_id.as_ref(), account_id.as_ref())
3323 .into_iter()
3324 .map(|p| Ok(p.into_pyobject(py)?.into()))
3325 .collect()
3326 }
3327
3328 #[pyo3(name = "account")]
3332 fn py_account(&self, py: Python, account_id: AccountId) -> PyResult<Option<Py<PyAny>>> {
3333 match self.account(&account_id) {
3334 Some(account) => Ok(Some(account_any_to_pyobject(py, account.clone())?)),
3335 None => Ok(None),
3336 }
3337 }
3338
3339 #[pyo3(name = "account_for_venue")]
3344 fn py_account_for_venue(&self, py: Python, venue: Venue) -> PyResult<Option<Py<PyAny>>> {
3345 match self.account_for_venue(&venue) {
3346 Some(account) => Ok(Some(account_any_to_pyobject(py, account.clone())?)),
3347 None => Ok(None),
3348 }
3349 }
3350
3351 #[pyo3(name = "account_id")]
3355 fn py_account_id(&self, venue: Venue) -> Option<AccountId> {
3356 self.account_id(&venue).copied()
3357 }
3358
3359 #[pyo3(name = "get")]
3365 fn py_get(&self, key: &str) -> PyResult<Option<Vec<u8>>> {
3366 match self.get(key).map_err(to_pyvalue_err)? {
3367 Some(bytes) => Ok(Some(bytes.to_vec())),
3368 None => Ok(None),
3369 }
3370 }
3371
3372 #[pyo3(name = "add")]
3374 fn py_add_general(&mut self, key: &str, value: Vec<u8>) -> PyResult<()> {
3375 self.add(key, Bytes::from(value)).map_err(to_pyvalue_err)
3376 }
3377
3378 #[pyo3(name = "price")]
3386 fn py_price(&self, instrument_id: InstrumentId, price_type: PriceType) -> Option<Price> {
3387 self.price(&instrument_id, price_type)
3388 }
3389
3390 #[pyo3(name = "get_xrate")]
3392 fn py_get_xrate(
3393 &self,
3394 venue: Venue,
3395 from_currency: Currency,
3396 to_currency: Currency,
3397 price_type: PriceType,
3398 ) -> Option<f64> {
3399 self.get_xrate(venue, from_currency, to_currency, price_type)
3400 .and_then(|rate| rate.to_f64())
3401 }
3402
3403 #[pyo3(name = "get_mark_xrate")]
3405 fn py_get_mark_xrate(&self, from_currency: Currency, to_currency: Currency) -> Option<f64> {
3406 self.get_mark_xrate(from_currency, to_currency)
3407 }
3408
3409 #[pyo3(name = "set_mark_xrate")]
3411 fn py_set_mark_xrate(&mut self, from_currency: Currency, to_currency: Currency, xrate: f64) {
3412 self.set_mark_xrate(from_currency, to_currency, xrate);
3413 }
3414
3415 #[pyo3(name = "clear_mark_xrate")]
3421 fn py_clear_mark_xrate(&mut self, from_currency: Currency, to_currency: Currency) {
3422 self.clear_mark_xrate(from_currency, to_currency);
3423 }
3424
3425 #[pyo3(name = "clear_mark_xrates")]
3427 fn py_clear_mark_xrates(&mut self) {
3428 self.clear_mark_xrates();
3429 }
3430
3431 #[pyo3(name = "calculate_unrealized_pnl")]
3433 #[expect(clippy::needless_pass_by_value)]
3434 fn py_calculate_unrealized_pnl(
3435 &self,
3436 py: Python,
3437 position: Py<PyAny>,
3438 ) -> PyResult<Option<Money>> {
3439 let position = position.extract::<Position>(py)?;
3440 Ok(self.calculate_unrealized_pnl(&position))
3441 }
3442
3443 #[pyo3(name = "own_order_book")]
3445 fn py_own_order_book(&self, instrument_id: InstrumentId) -> Option<OwnOrderBook> {
3446 self.own_order_book(&instrument_id).cloned()
3447 }
3448
3449 #[pyo3(name = "update_own_order_book")]
3457 fn py_update_own_order_book(&mut self, py: Python, order: Py<PyAny>) -> PyResult<()> {
3458 let order_any = pyobject_to_order_any(py, order)?;
3459 self.update_own_order_book(&order_any);
3460 Ok(())
3461 }
3462
3463 #[pyo3(name = "force_remove_from_own_order_book")]
3469 fn py_force_remove_from_own_order_book(&mut self, client_order_id: ClientOrderId) {
3470 self.force_remove_from_own_order_book(&client_order_id);
3471 }
3472
3473 #[pyo3(name = "audit_own_order_books")]
3478 fn py_audit_own_order_books(&mut self) {
3479 self.audit_own_order_books();
3480 }
3481}
3482
3483#[cfg(feature = "defi")]
3484#[pymethods]
3485impl Cache {
3486 #[pyo3(name = "add_pool")]
3490 fn py_add_pool(&mut self, pool: Pool) -> PyResult<()> {
3491 self.add_pool(pool).map_err(to_pyvalue_err)
3492 }
3493
3494 #[pyo3(name = "pool")]
3496 fn py_pool(&self, instrument_id: InstrumentId) -> Option<Pool> {
3497 self.pool(&instrument_id).cloned()
3498 }
3499
3500 #[pyo3(name = "pool_ids")]
3502 fn py_pool_ids(&self, venue: Option<Venue>) -> Vec<InstrumentId> {
3503 self.pool_ids(venue.as_ref())
3504 }
3505
3506 #[pyo3(name = "pools")]
3508 fn py_pools(&self, venue: Option<Venue>) -> Vec<Pool> {
3509 self.pools(venue.as_ref()).into_iter().cloned().collect()
3510 }
3511
3512 #[pyo3(name = "add_pool_profiler")]
3516 fn py_add_pool_profiler(&mut self, pool_profiler: PoolProfiler) -> PyResult<()> {
3517 self.add_pool_profiler(pool_profiler)
3518 .map_err(to_pyvalue_err)
3519 }
3520
3521 #[pyo3(name = "pool_profiler")]
3523 fn py_pool_profiler(&self, instrument_id: InstrumentId) -> Option<PoolProfiler> {
3524 self.pool_profiler(&instrument_id).cloned()
3525 }
3526
3527 #[pyo3(name = "pool_profiler_ids")]
3529 fn py_pool_profiler_ids(&self, venue: Option<Venue>) -> Vec<InstrumentId> {
3530 self.pool_profiler_ids(venue.as_ref())
3531 }
3532
3533 #[pyo3(name = "pool_profilers")]
3535 fn py_pool_profilers(&self, venue: Option<Venue>) -> Vec<PoolProfiler> {
3536 self.pool_profilers(venue.as_ref())
3537 .into_iter()
3538 .cloned()
3539 .collect()
3540 }
3541}