Skip to main content

nautilus_betfair/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 Betfair configuration.
17
18use nautilus_core::{python::to_pyvalue_err, string::secret::SecretString};
19use nautilus_live::book::DEFAULT_BOOK_SNAPSHOT_TIMEOUT_SECS;
20use nautilus_model::identifiers::AccountId;
21use pyo3::prelude::*;
22use rust_decimal::Decimal;
23
24use crate::config::{BetfairDataClientConfig, BetfairExecutionClientConfig};
25
26fn stringify_ids(values: Option<Vec<u64>>) -> Option<Vec<String>> {
27    values.map(|values| values.into_iter().map(|value| value.to_string()).collect())
28}
29
30#[pymethods]
31#[pyo3_stub_gen::derive::gen_stub_pymethods]
32impl BetfairDataClientConfig {
33    /// Configuration for the Betfair live data client.
34    #[new]
35    #[pyo3(signature = (
36        account_currency = None,
37        username = None,
38        password = None,
39        app_key = None,
40        proxy_url = None,
41        request_rate_per_second = 5,
42        default_min_notional = None,
43        event_type_ids = None,
44        event_type_names = None,
45        event_ids = None,
46        country_codes = None,
47        market_types = None,
48        market_ids = None,
49        min_market_start_time = None,
50        max_market_start_time = None,
51        stream_host = None,
52        stream_port = None,
53        stream_heartbeat_secs = None,
54        stream_heartbeat_timeout_secs = None,
55        stream_reconnect_delay_initial_ms = 2_000,
56        stream_reconnect_delay_max_ms = 30_000,
57        stream_use_tls = true,
58        stream_conflate_ms = None,
59        subscription_delay_secs = None,
60        subscribe_race_data = false,
61        subscribe_cricket_data = false,
62        book_snapshot_timeout_secs = None,
63        certs_dir = None,
64    ))]
65    #[expect(clippy::too_many_arguments)]
66    fn py_new(
67        account_currency: Option<String>,
68        username: Option<String>,
69        password: Option<String>,
70        app_key: Option<String>,
71        proxy_url: Option<String>,
72        request_rate_per_second: u32,
73        default_min_notional: Option<Decimal>,
74        event_type_ids: Option<Vec<u64>>,
75        event_type_names: Option<Vec<String>>,
76        event_ids: Option<Vec<u64>>,
77        country_codes: Option<Vec<String>>,
78        market_types: Option<Vec<String>>,
79        market_ids: Option<Vec<String>>,
80        min_market_start_time: Option<String>,
81        max_market_start_time: Option<String>,
82        stream_host: Option<String>,
83        stream_port: Option<u16>,
84        stream_heartbeat_secs: Option<u64>,
85        stream_heartbeat_timeout_secs: Option<u64>,
86        stream_reconnect_delay_initial_ms: u64,
87        stream_reconnect_delay_max_ms: u64,
88        stream_use_tls: bool,
89        stream_conflate_ms: Option<u64>,
90        subscription_delay_secs: Option<u64>,
91        subscribe_race_data: bool,
92        subscribe_cricket_data: bool,
93        book_snapshot_timeout_secs: Option<u64>,
94        certs_dir: Option<String>,
95    ) -> PyResult<Self> {
96        let config = Self {
97            account_currency: account_currency.unwrap_or_else(|| "GBP".to_string()),
98            username: username.map(SecretString::from),
99            password: password.map(SecretString::from),
100            app_key: app_key.map(SecretString::from),
101            proxy_url: proxy_url.map(SecretString::from),
102            request_rate_per_second,
103            default_min_notional,
104            event_type_ids: stringify_ids(event_type_ids),
105            event_type_names,
106            event_ids: stringify_ids(event_ids),
107            country_codes,
108            market_types,
109            market_ids,
110            min_market_start_time,
111            max_market_start_time,
112            stream_host,
113            stream_port,
114            stream_heartbeat_secs,
115            stream_heartbeat_timeout_secs,
116            stream_reconnect_delay_initial_ms,
117            stream_reconnect_delay_max_ms,
118            stream_use_tls,
119            stream_conflate_ms,
120            subscription_delay_secs: subscription_delay_secs.unwrap_or(3),
121            subscribe_race_data,
122            subscribe_cricket_data,
123            book_snapshot_timeout_secs: book_snapshot_timeout_secs
124                .unwrap_or(DEFAULT_BOOK_SNAPSHOT_TIMEOUT_SECS),
125            certs_dir,
126        };
127
128        config.validate().map_err(to_pyvalue_err)?;
129        Ok(config)
130    }
131
132    /// Returns the username.
133    #[getter]
134    fn username(&self) -> Option<&str> {
135        self.username.as_ref().map(SecretString::expose_secret)
136    }
137
138    #[getter]
139    const fn has_proxy_url(&self) -> bool {
140        self.proxy_url.is_some()
141    }
142
143    fn __repr__(&self) -> String {
144        stringify!(BetfairDataClientConfig).to_string()
145    }
146}
147
148#[pymethods]
149#[pyo3_stub_gen::derive::gen_stub_pymethods]
150impl BetfairExecutionClientConfig {
151    /// Configuration for the Betfair live execution client.
152    #[new]
153    #[pyo3(signature = (
154        account_id = None,
155        account_currency = None,
156        username = None,
157        password = None,
158        app_key = None,
159        proxy_url = None,
160        request_rate_per_second = 5,
161        order_request_rate_per_second = 20,
162        stream_host = None,
163        stream_port = None,
164        stream_heartbeat_secs = None,
165        stream_heartbeat_timeout_secs = None,
166        stream_reconnect_delay_initial_ms = 2_000,
167        stream_reconnect_delay_max_ms = 30_000,
168        stream_use_tls = true,
169        stream_market_ids_filter = None,
170        ignore_external_orders = false,
171        calculate_account_state = true,
172        request_account_state_secs = 300,
173        reconcile_market_ids_only = false,
174        reconcile_market_ids = None,
175        use_market_version = false,
176        stream_gap_recovery_lookback_mins = 10,
177        certs_dir = None,
178    ))]
179    #[expect(clippy::too_many_arguments)]
180    fn py_new(
181        account_id: Option<AccountId>,
182        account_currency: Option<String>,
183        username: Option<String>,
184        password: Option<String>,
185        app_key: Option<String>,
186        proxy_url: Option<String>,
187        request_rate_per_second: u32,
188        order_request_rate_per_second: u32,
189        stream_host: Option<String>,
190        stream_port: Option<u16>,
191        stream_heartbeat_secs: Option<u64>,
192        stream_heartbeat_timeout_secs: Option<u64>,
193        stream_reconnect_delay_initial_ms: u64,
194        stream_reconnect_delay_max_ms: u64,
195        stream_use_tls: bool,
196        stream_market_ids_filter: Option<Vec<String>>,
197        ignore_external_orders: bool,
198        calculate_account_state: bool,
199        request_account_state_secs: u64,
200        reconcile_market_ids_only: bool,
201        reconcile_market_ids: Option<Vec<String>>,
202        use_market_version: bool,
203        stream_gap_recovery_lookback_mins: u64,
204        certs_dir: Option<String>,
205    ) -> PyResult<Self> {
206        let config = Self {
207            account_id: account_id.unwrap_or_else(|| AccountId::from("BETFAIR-001")),
208            account_currency: account_currency.unwrap_or_else(|| "GBP".to_string()),
209            username: username.map(SecretString::from),
210            password: password.map(SecretString::from),
211            app_key: app_key.map(SecretString::from),
212            proxy_url: proxy_url.map(SecretString::from),
213            request_rate_per_second,
214            order_request_rate_per_second,
215            stream_host,
216            stream_port,
217            stream_heartbeat_secs,
218            stream_heartbeat_timeout_secs,
219            stream_reconnect_delay_initial_ms,
220            stream_reconnect_delay_max_ms,
221            stream_use_tls,
222            stream_market_ids_filter,
223            ignore_external_orders,
224            calculate_account_state,
225            request_account_state_secs,
226            reconcile_market_ids_only,
227            reconcile_market_ids,
228            use_market_version,
229            stream_gap_recovery_lookback_mins,
230            certs_dir,
231        };
232
233        config.validate().map_err(to_pyvalue_err)?;
234        Ok(config)
235    }
236
237    /// Returns the username.
238    #[getter]
239    fn username(&self) -> Option<&str> {
240        self.username.as_ref().map(SecretString::expose_secret)
241    }
242
243    #[getter]
244    const fn has_proxy_url(&self) -> bool {
245        self.proxy_url.is_some()
246    }
247
248    fn __repr__(&self) -> String {
249        stringify!(BetfairExecutionClientConfig).to_string()
250    }
251}