Skip to main content

nautilus_kraken/python/
mod.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 from `pyo3`.
17
18#![expect(
19    clippy::missing_errors_doc,
20    reason = "errors documented on underlying Rust methods"
21)]
22
23use nautilus_common::factories::{ClientConfig, DataClientFactory, ExecutionClientFactory};
24use nautilus_core::python::{to_pyruntime_err, to_pyvalue_err};
25use nautilus_system::get_global_pyo3_registry;
26use pyo3::prelude::*;
27
28use crate::{
29    common::enums::{KrakenEnvironment, KrakenProductType},
30    config::{KrakenDataClientConfig, KrakenExecClientConfig},
31    factories::{KrakenDataClientFactory, KrakenExecutionClientFactory},
32    http::{KrakenFuturesHttpClient, KrakenSpotHttpClient},
33    websocket::{
34        futures::client::KrakenFuturesWebSocketClient, spot_v2::client::KrakenSpotWebSocketClient,
35    },
36};
37
38pub mod config;
39pub mod enums;
40pub mod factories;
41pub mod http_futures;
42pub mod http_spot;
43pub mod websocket_futures;
44pub mod websocket_spot;
45
46/// Determines the product type from a Kraken symbol.
47///
48/// Futures symbols have the following prefixes:
49/// - `PI_` - Perpetual Inverse futures (e.g., `PI_XBTUSD`)
50/// - `PF_` - Perpetual Fixed-margin futures (e.g., `PF_XBTUSD`)
51/// - `FI_` - Fixed maturity Inverse futures (e.g., `FI_XBTUSD_230929`)
52/// - `FF_` - Flex futures
53///
54/// All other symbols are considered spot.
55#[pyfunction]
56#[pyo3_stub_gen::derive::gen_stub_pyfunction(module = "nautilus_trader.kraken")]
57#[pyo3(name = "kraken_product_type_from_symbol")]
58fn py_kraken_product_type_from_symbol(symbol: &str) -> KrakenProductType {
59    crate::common::enums::product_type_from_symbol(symbol)
60}
61
62#[expect(clippy::needless_pass_by_value)]
63fn extract_kraken_data_factory(
64    py: Python<'_>,
65    factory: Py<PyAny>,
66) -> PyResult<Box<dyn DataClientFactory>> {
67    match factory.extract::<KrakenDataClientFactory>(py) {
68        Ok(f) => Ok(Box::new(f)),
69        Err(e) => Err(to_pyvalue_err(format!(
70            "Failed to extract KrakenDataClientFactory: {e}"
71        ))),
72    }
73}
74
75#[expect(clippy::needless_pass_by_value)]
76fn extract_kraken_exec_factory(
77    py: Python<'_>,
78    factory: Py<PyAny>,
79) -> PyResult<Box<dyn ExecutionClientFactory>> {
80    match factory.extract::<KrakenExecutionClientFactory>(py) {
81        Ok(f) => Ok(Box::new(f)),
82        Err(e) => Err(to_pyvalue_err(format!(
83            "Failed to extract KrakenExecutionClientFactory: {e}"
84        ))),
85    }
86}
87
88#[expect(clippy::needless_pass_by_value)]
89fn extract_kraken_data_config(
90    py: Python<'_>,
91    config: Py<PyAny>,
92) -> PyResult<Box<dyn ClientConfig>> {
93    match config.extract::<KrakenDataClientConfig>(py) {
94        Ok(c) => Ok(Box::new(c)),
95        Err(e) => Err(to_pyvalue_err(format!(
96            "Failed to extract KrakenDataClientConfig: {e}"
97        ))),
98    }
99}
100
101#[expect(clippy::needless_pass_by_value)]
102fn extract_kraken_exec_config(
103    py: Python<'_>,
104    config: Py<PyAny>,
105) -> PyResult<Box<dyn ClientConfig>> {
106    match config.extract::<KrakenExecClientConfig>(py) {
107        Ok(c) => Ok(Box::new(c)),
108        Err(e) => Err(to_pyvalue_err(format!(
109            "Failed to extract KrakenExecClientConfig: {e}"
110        ))),
111    }
112}
113
114#[pymodule]
115pub fn kraken(m: &Bound<'_, PyModule>) -> PyResult<()> {
116    m.add_class::<KrakenEnvironment>()?;
117    m.add_class::<KrakenProductType>()?;
118    m.add_class::<KrakenSpotHttpClient>()?;
119    m.add_class::<KrakenFuturesHttpClient>()?;
120    m.add_class::<KrakenSpotWebSocketClient>()?;
121    m.add_class::<KrakenFuturesWebSocketClient>()?;
122    m.add_class::<KrakenDataClientConfig>()?;
123    m.add_class::<KrakenExecClientConfig>()?;
124    m.add_class::<KrakenDataClientFactory>()?;
125    m.add_class::<KrakenExecutionClientFactory>()?;
126    m.add_function(wrap_pyfunction!(py_kraken_product_type_from_symbol, m)?)?;
127
128    let registry = get_global_pyo3_registry();
129
130    if let Err(e) =
131        registry.register_factory_extractor("KRAKEN".to_string(), extract_kraken_data_factory)
132    {
133        return Err(to_pyruntime_err(format!(
134            "Failed to register Kraken data factory extractor: {e}"
135        )));
136    }
137
138    if let Err(e) =
139        registry.register_exec_factory_extractor("KRAKEN".to_string(), extract_kraken_exec_factory)
140    {
141        return Err(to_pyruntime_err(format!(
142            "Failed to register Kraken exec factory extractor: {e}"
143        )));
144    }
145
146    if let Err(e) = registry.register_config_extractor(
147        "KrakenDataClientConfig".to_string(),
148        extract_kraken_data_config,
149    ) {
150        return Err(to_pyruntime_err(format!(
151            "Failed to register Kraken data config extractor: {e}"
152        )));
153    }
154
155    if let Err(e) = registry.register_config_extractor(
156        "KrakenExecClientConfig".to_string(),
157        extract_kraken_exec_config,
158    ) {
159        return Err(to_pyruntime_err(format!(
160            "Failed to register Kraken exec config extractor: {e}"
161        )));
162    }
163
164    Ok(())
165}