Skip to main content

nautilus_sandbox/python/
config.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Python bindings for sandbox configuration.
17
18use nautilus_common::python::config_error_to_pyvalue_err;
19use nautilus_execution::{
20    matching_engine::config::OrderPriorityResetPolicy,
21    models::{fee::FeeModelAny, fill::FillModelAny, latency::LatencyModelAny},
22    python::{
23        fee::{fee_model_any_to_pyobject, pyobject_to_fee_model_any},
24        fill::{fill_model_any_to_pyobject, pyobject_to_fill_model_any},
25        latency::{latency_model_any_to_pyobject, pyobject_to_latency_model_any},
26    },
27};
28use nautilus_model::{
29    enums::{AccountType, BookType, OmsType},
30    identifiers::{AccountId, Venue},
31    python::account::margin_model::{margin_model_any_to_pyobject, pyobject_to_margin_model_any},
32    types::{Currency, Money},
33};
34use pyo3::{Py, PyAny, Python, prelude::*};
35use rust_decimal::Decimal;
36
37use crate::config::SandboxExecutionClientConfig;
38
39#[pymethods]
40#[pyo3_stub_gen::derive::gen_stub_pymethods]
41impl SandboxExecutionClientConfig {
42    /// Configuration for `SandboxExecutionClient` instances.
43    #[new]
44    #[pyo3(signature = (
45        venue,
46        starting_balances,
47        account_id = None,
48        base_currency = None,
49        oms_type = None,
50        account_type = None,
51        default_leverage = None,
52        book_type = None,
53        fill_model = None,
54        fee_model = None,
55        margin_model = None,
56        latency_model = None,
57        frozen_account = false,
58        bar_execution = true,
59        trade_execution = true,
60        reject_stop_orders = true,
61        support_gtd_orders = true,
62        support_contingent_orders = true,
63        use_position_ids = true,
64        use_random_ids = false,
65        use_reduce_only = true,
66        queue_position = false,
67        liquidity_consumption = false,
68        bar_adaptive_high_low_ordering = false,
69        use_market_order_acks = false,
70        oto_full_trigger = false,
71        price_protection_points = None,
72        order_priority_reset_policy = OrderPriorityResetPolicy::PriceChange,
73    ))]
74    #[expect(clippy::too_many_arguments)]
75    fn py_new(
76        venue: Venue,
77        starting_balances: Vec<Money>,
78        account_id: Option<AccountId>,
79        base_currency: Option<Currency>,
80        oms_type: Option<OmsType>,
81        account_type: Option<AccountType>,
82        default_leverage: Option<Decimal>,
83        book_type: Option<BookType>,
84        fill_model: Option<Py<PyAny>>,
85        fee_model: Option<Py<PyAny>>,
86        margin_model: Option<Py<PyAny>>,
87        latency_model: Option<Py<PyAny>>,
88        frozen_account: bool,
89        bar_execution: bool,
90        trade_execution: bool,
91        reject_stop_orders: bool,
92        support_gtd_orders: bool,
93        support_contingent_orders: bool,
94        use_position_ids: bool,
95        use_random_ids: bool,
96        use_reduce_only: bool,
97        queue_position: bool,
98        liquidity_consumption: bool,
99        bar_adaptive_high_low_ordering: bool,
100        use_market_order_acks: bool,
101        oto_full_trigger: bool,
102        price_protection_points: Option<u32>,
103        order_priority_reset_policy: OrderPriorityResetPolicy,
104    ) -> PyResult<Self> {
105        // Generate the default account ID from the venue
106        let account_id =
107            account_id.unwrap_or_else(|| AccountId::from(format!("{venue}-SANDBOX-001").as_str()));
108        let fill_model: Option<FillModelAny> = fill_model
109            .map(|obj| Python::attach(|py| pyobject_to_fill_model_any(obj.bind(py))))
110            .transpose()?;
111
112        let fee_model: Option<FeeModelAny> = fee_model
113            .map(|obj| Python::attach(|py| pyobject_to_fee_model_any(obj.bind(py))))
114            .transpose()?;
115
116        let margin_model = margin_model
117            .map(|obj| Python::attach(|py| pyobject_to_margin_model_any(obj.bind(py))))
118            .transpose()?;
119
120        let latency_model: Option<LatencyModelAny> = latency_model
121            .map(|obj| Python::attach(|py| pyobject_to_latency_model_any(obj.bind(py))))
122            .transpose()?;
123
124        let config = Self {
125            account_id,
126            venue,
127            starting_balances,
128            base_currency,
129            oms_type: oms_type.unwrap_or(OmsType::Netting),
130            account_type: account_type.unwrap_or(AccountType::Margin),
131            default_leverage: default_leverage.unwrap_or(Decimal::ONE),
132            leverages: ahash::AHashMap::new(),
133            book_type: book_type.unwrap_or(BookType::L1_MBP),
134            fill_model,
135            fee_model,
136            margin_model,
137            latency_model,
138            frozen_account,
139            bar_execution,
140            trade_execution,
141            reject_stop_orders,
142            support_gtd_orders,
143            support_contingent_orders,
144            use_position_ids,
145            use_random_ids,
146            use_reduce_only,
147            queue_position,
148            liquidity_consumption,
149            bar_adaptive_high_low_ordering,
150            use_market_order_acks,
151            oto_full_trigger,
152            price_protection_points: price_protection_points.unwrap_or(0),
153            order_priority_reset_policy,
154        };
155
156        config.validate().map_err(config_error_to_pyvalue_err)?;
157        Ok(config)
158    }
159
160    #[getter]
161    fn account_id(&self) -> AccountId {
162        self.account_id
163    }
164
165    #[getter]
166    fn venue(&self) -> Venue {
167        self.venue
168    }
169
170    #[getter]
171    fn starting_balances(&self) -> Vec<Money> {
172        self.starting_balances.clone()
173    }
174
175    #[getter]
176    fn base_currency(&self) -> Option<Currency> {
177        self.base_currency
178    }
179
180    #[getter]
181    fn oms_type(&self) -> OmsType {
182        self.oms_type
183    }
184
185    #[getter]
186    fn account_type(&self) -> AccountType {
187        self.account_type
188    }
189
190    #[getter]
191    fn default_leverage(&self) -> Decimal {
192        self.default_leverage
193    }
194
195    #[getter]
196    fn book_type(&self) -> BookType {
197        self.book_type
198    }
199
200    #[getter]
201    fn fill_model(&self, py: Python<'_>) -> PyResult<Option<Py<PyAny>>> {
202        self.fill_model
203            .as_ref()
204            .map(|model| fill_model_any_to_pyobject(py, model))
205            .transpose()
206    }
207
208    #[getter]
209    fn fee_model(&self, py: Python<'_>) -> PyResult<Option<Py<PyAny>>> {
210        self.fee_model
211            .as_ref()
212            .map(|model| fee_model_any_to_pyobject(py, model))
213            .transpose()
214    }
215
216    #[getter]
217    fn margin_model(&self, py: Python<'_>) -> PyResult<Option<Py<PyAny>>> {
218        self.margin_model
219            .as_ref()
220            .map(|model| margin_model_any_to_pyobject(py, model))
221            .transpose()
222    }
223
224    #[getter]
225    fn latency_model(&self, py: Python<'_>) -> PyResult<Option<Py<PyAny>>> {
226        self.latency_model
227            .as_ref()
228            .map(|model| latency_model_any_to_pyobject(py, model))
229            .transpose()
230    }
231
232    #[getter]
233    fn frozen_account(&self) -> bool {
234        self.frozen_account
235    }
236
237    #[getter]
238    fn bar_execution(&self) -> bool {
239        self.bar_execution
240    }
241
242    #[getter]
243    fn trade_execution(&self) -> bool {
244        self.trade_execution
245    }
246
247    #[getter]
248    fn reject_stop_orders(&self) -> bool {
249        self.reject_stop_orders
250    }
251
252    #[getter]
253    fn support_gtd_orders(&self) -> bool {
254        self.support_gtd_orders
255    }
256
257    #[getter]
258    fn support_contingent_orders(&self) -> bool {
259        self.support_contingent_orders
260    }
261
262    #[getter]
263    fn use_position_ids(&self) -> bool {
264        self.use_position_ids
265    }
266
267    #[getter]
268    fn use_random_ids(&self) -> bool {
269        self.use_random_ids
270    }
271
272    #[getter]
273    fn use_reduce_only(&self) -> bool {
274        self.use_reduce_only
275    }
276
277    #[getter]
278    fn order_priority_reset_policy(&self) -> OrderPriorityResetPolicy {
279        self.order_priority_reset_policy
280    }
281
282    #[getter]
283    fn queue_position(&self) -> bool {
284        self.queue_position
285    }
286
287    #[getter]
288    fn liquidity_consumption(&self) -> bool {
289        self.liquidity_consumption
290    }
291
292    #[getter]
293    fn bar_adaptive_high_low_ordering(&self) -> bool {
294        self.bar_adaptive_high_low_ordering
295    }
296
297    #[getter]
298    fn use_market_order_acks(&self) -> bool {
299        self.use_market_order_acks
300    }
301
302    #[getter]
303    fn oto_full_trigger(&self) -> bool {
304        self.oto_full_trigger
305    }
306
307    #[getter]
308    fn price_protection_points(&self) -> u32 {
309        self.price_protection_points
310    }
311}