Skip to main content

nautilus_sandbox/
factory.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//! Factory for creating sandbox execution clients.
17
18use std::{any::Any, cell::RefCell, rc::Rc};
19
20use nautilus_common::{
21    cache::Cache,
22    clients::ExecutionClient,
23    clock::Clock,
24    factories::{ClientConfig, SimulatedExecutionClientFactory},
25    live::clock::LiveClock,
26};
27use nautilus_execution::client::core::ExecutionClientCore;
28use nautilus_model::identifiers::{ClientId, TraderId};
29
30use crate::{config::SandboxExecutionClientConfig, execution::SandboxExecutionClient};
31
32impl ClientConfig for SandboxExecutionClientConfig {
33    fn as_any(&self) -> &dyn Any {
34        self
35    }
36}
37
38/// Factory for creating sandbox execution clients.
39#[derive(Debug, Default, Clone)]
40#[cfg_attr(
41    feature = "python",
42    pyo3::pyclass(
43        module = "nautilus_trader.adapters.sandbox",
44        unsendable,
45        from_py_object
46    )
47)]
48#[cfg_attr(
49    feature = "python",
50    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.adapters.sandbox")
51)]
52pub struct SandboxExecutionClientFactory;
53
54impl SandboxExecutionClientFactory {
55    /// Creates a new [`SandboxExecutionClientFactory`] instance.
56    #[must_use]
57    pub const fn new() -> Self {
58        Self
59    }
60}
61
62impl SimulatedExecutionClientFactory for SandboxExecutionClientFactory {
63    fn create(
64        &self,
65        trader_id: TraderId,
66        name: &str,
67        config: &dyn ClientConfig,
68        cache: Rc<RefCell<Cache>>,
69    ) -> anyhow::Result<Box<dyn ExecutionClient>> {
70        let sandbox_config = config
71            .as_any()
72            .downcast_ref::<SandboxExecutionClientConfig>()
73            .ok_or_else(|| {
74                anyhow::anyhow!(
75                    "Invalid config type for SandboxExecutionClientFactory. Expected SandboxExecutionClientConfig, was {config:?}",
76                )
77            })?
78            .clone();
79
80        let client_id = ClientId::from(name);
81        let clock: Rc<RefCell<dyn Clock>> = Rc::new(RefCell::new(LiveClock::default()));
82
83        let core = ExecutionClientCore::new(
84            trader_id,
85            client_id,
86            sandbox_config.venue,
87            sandbox_config.oms_type,
88            sandbox_config.account_id,
89            sandbox_config.account_type,
90            sandbox_config.base_currency,
91            cache.clone(),
92        );
93
94        let client = SandboxExecutionClient::new(core, sandbox_config, clock, cache);
95        Ok(Box::new(client))
96    }
97
98    fn name(&self) -> &'static str {
99        "SANDBOX"
100    }
101
102    fn config_type(&self) -> &'static str {
103        "SandboxExecutionClientConfig"
104    }
105}