Skip to main content

nautilus_kraken/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 Kraken configuration.
17
18use nautilus_core::python::to_pyvalue_err;
19use nautilus_model::{
20    enums::AccountType,
21    identifiers::{AccountId, TraderId},
22};
23use pyo3::prelude::*;
24
25use crate::{
26    common::enums::{KrakenEnvironment, KrakenProductType},
27    config::{KrakenDataClientConfig, KrakenExecClientConfig},
28};
29
30#[pymethods]
31#[pyo3_stub_gen::derive::gen_stub_pymethods]
32impl KrakenDataClientConfig {
33    /// Configuration for the Kraken data client.
34    #[new]
35    #[pyo3(signature = (
36        product_type = None,
37        environment = None,
38        api_key = None,
39        api_secret = None,
40        base_url = None,
41        ws_public_url = None,
42        ws_private_url = None,
43        ws_l3_url = None,
44        validate_l3_checksum = None,
45        proxy_url = None,
46        timeout_secs = None,
47        heartbeat_interval_secs = None,
48        ws_idle_timeout_ms = None,
49        max_requests_per_second = None,
50    ))]
51    #[expect(clippy::too_many_arguments)]
52    fn py_new(
53        product_type: Option<KrakenProductType>,
54        environment: Option<KrakenEnvironment>,
55        api_key: Option<String>,
56        api_secret: Option<String>,
57        base_url: Option<String>,
58        ws_public_url: Option<String>,
59        ws_private_url: Option<String>,
60        ws_l3_url: Option<String>,
61        validate_l3_checksum: Option<bool>,
62        proxy_url: Option<String>,
63        timeout_secs: Option<u64>,
64        heartbeat_interval_secs: Option<u64>,
65        ws_idle_timeout_ms: Option<u64>,
66        max_requests_per_second: Option<u32>,
67    ) -> Self {
68        let defaults = Self::default();
69        Self {
70            api_key,
71            api_secret,
72            product_type: product_type.unwrap_or(defaults.product_type),
73            environment: environment.unwrap_or(defaults.environment),
74            base_url,
75            ws_public_url,
76            ws_private_url,
77            ws_l3_url,
78            validate_l3_checksum: validate_l3_checksum.unwrap_or(defaults.validate_l3_checksum),
79            proxy_url,
80            timeout_secs: timeout_secs.unwrap_or(defaults.timeout_secs),
81            heartbeat_interval_secs: heartbeat_interval_secs
82                .unwrap_or(defaults.heartbeat_interval_secs),
83            ws_idle_timeout_ms: ws_idle_timeout_ms.unwrap_or(defaults.ws_idle_timeout_ms),
84            max_requests_per_second,
85            transport_backend: defaults.transport_backend,
86        }
87    }
88
89    fn __repr__(&self) -> String {
90        format!("{self:?}")
91    }
92}
93
94#[pymethods]
95#[pyo3_stub_gen::derive::gen_stub_pymethods]
96impl KrakenExecClientConfig {
97    /// Configuration for the Kraken execution client.
98    #[new]
99    #[pyo3(signature = (
100        trader_id,
101        account_id,
102        api_key,
103        api_secret,
104        product_type = None,
105        environment = None,
106        base_url = None,
107        ws_url = None,
108        proxy_url = None,
109        timeout_secs = None,
110        heartbeat_interval_secs = None,
111        max_requests_per_second = None,
112        spot_account_type = None,
113        default_leverage = None,
114        use_spot_position_reports = None,
115        spot_positions_quote_currency = None,
116        margin_balance_asset = None,
117        use_ws_trade = None,
118        ws_request_timeout_secs = None,
119    ))]
120    #[expect(clippy::too_many_arguments)]
121    fn py_new(
122        trader_id: TraderId,
123        account_id: AccountId,
124        api_key: String,
125        api_secret: String,
126        product_type: Option<KrakenProductType>,
127        environment: Option<KrakenEnvironment>,
128        base_url: Option<String>,
129        ws_url: Option<String>,
130        proxy_url: Option<String>,
131        timeout_secs: Option<u64>,
132        heartbeat_interval_secs: Option<u64>,
133        max_requests_per_second: Option<u32>,
134        spot_account_type: Option<AccountType>,
135        default_leverage: Option<u16>,
136        use_spot_position_reports: Option<bool>,
137        spot_positions_quote_currency: Option<String>,
138        margin_balance_asset: Option<String>,
139        use_ws_trade: Option<bool>,
140        ws_request_timeout_secs: Option<u64>,
141    ) -> PyResult<Self> {
142        let defaults = Self::default();
143        let spot_account_type = spot_account_type.unwrap_or(defaults.spot_account_type);
144        if default_leverage.is_some() && spot_account_type == AccountType::Cash {
145            return Err(to_pyvalue_err(
146                "default_leverage requires spot_account_type=Margin",
147            ));
148        }
149        Ok(Self {
150            trader_id,
151            account_id,
152            api_key,
153            api_secret,
154            product_type: product_type.unwrap_or(defaults.product_type),
155            environment: environment.unwrap_or(defaults.environment),
156            base_url,
157            ws_url,
158            proxy_url,
159            timeout_secs: timeout_secs.unwrap_or(defaults.timeout_secs),
160            heartbeat_interval_secs: heartbeat_interval_secs
161                .unwrap_or(defaults.heartbeat_interval_secs),
162            max_requests_per_second,
163            transport_backend: defaults.transport_backend,
164            spot_account_type,
165            default_leverage,
166            use_spot_position_reports: use_spot_position_reports
167                .unwrap_or(defaults.use_spot_position_reports),
168            spot_positions_quote_currency: spot_positions_quote_currency
169                .unwrap_or(defaults.spot_positions_quote_currency),
170            margin_balance_asset,
171            use_ws_trade: use_ws_trade.unwrap_or(defaults.use_ws_trade),
172            ws_request_timeout_secs: ws_request_timeout_secs
173                .unwrap_or(defaults.ws_request_timeout_secs),
174        })
175    }
176
177    fn __repr__(&self) -> String {
178        format!("{self:?}")
179    }
180}