nautilus-coinbase 0.56.0

Coinbase Advanced Trade integration adapter for the Nautilus trading engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Factory functions for creating Coinbase clients and components.

use std::{any::Any, cell::RefCell, rc::Rc};

use nautilus_common::{
    cache::Cache,
    clients::{DataClient, ExecutionClient},
    clock::Clock,
    factories::{ClientConfig, DataClientFactory, ExecutionClientFactory},
};
use nautilus_live::ExecutionClientCore;
use nautilus_model::{
    enums::{AccountType, OmsType},
    identifiers::{AccountId, ClientId, TraderId},
};

use crate::{
    common::consts::COINBASE_VENUE,
    config::{CoinbaseDataClientConfig, CoinbaseExecClientConfig},
    data::CoinbaseDataClient,
    execution::CoinbaseExecutionClient,
};

impl ClientConfig for CoinbaseDataClientConfig {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl ClientConfig for CoinbaseExecClientConfig {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

/// Factory for creating Coinbase data clients.
#[derive(Debug, Clone)]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.coinbase", from_py_object)
)]
#[cfg_attr(
    feature = "python",
    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.coinbase")
)]
pub struct CoinbaseDataClientFactory;

impl CoinbaseDataClientFactory {
    /// Creates a new [`CoinbaseDataClientFactory`] instance.
    #[must_use]
    pub const fn new() -> Self {
        Self
    }
}

impl Default for CoinbaseDataClientFactory {
    fn default() -> Self {
        Self::new()
    }
}

impl DataClientFactory for CoinbaseDataClientFactory {
    fn create(
        &self,
        name: &str,
        config: &dyn ClientConfig,
        _cache: Rc<RefCell<Cache>>,
        _clock: Rc<RefCell<dyn Clock>>,
    ) -> anyhow::Result<Box<dyn DataClient>> {
        let coinbase_config = config
            .as_any()
            .downcast_ref::<CoinbaseDataClientConfig>()
            .ok_or_else(|| {
                anyhow::anyhow!(
                    "Invalid config type for CoinbaseDataClientFactory. Expected CoinbaseDataClientConfig, was {config:?}",
                )
            })?
            .clone();

        let client_id = ClientId::from(name);
        let client = CoinbaseDataClient::new(client_id, coinbase_config)?;
        Ok(Box::new(client))
    }

    fn name(&self) -> &'static str {
        "COINBASE"
    }

    fn config_type(&self) -> &'static str {
        "CoinbaseDataClientConfig"
    }
}

/// Factory for creating Coinbase execution clients.
///
/// Dispatches the spot vs derivatives (CFM) scope from the config's
/// [`AccountType`]: `Cash` bootstraps spot products and uses the
/// `/accounts` endpoint; `Margin` bootstraps perpetual and dated futures,
/// subscribes to the `futures_balance_summary` WebSocket channel, and
/// produces position reports from the CFM endpoints. Other account types
/// are rejected. Hedge mode is not exposed by the venue, so OMS is always
/// `Netting`.
#[derive(Debug, Clone)]
#[cfg_attr(
    feature = "python",
    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.coinbase", from_py_object)
)]
#[cfg_attr(
    feature = "python",
    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.coinbase")
)]
pub struct CoinbaseExecutionClientFactory {
    trader_id: TraderId,
    account_id: AccountId,
}

impl CoinbaseExecutionClientFactory {
    /// Creates a new [`CoinbaseExecutionClientFactory`] instance.
    #[must_use]
    pub const fn new(trader_id: TraderId, account_id: AccountId) -> Self {
        Self {
            trader_id,
            account_id,
        }
    }
}

impl ExecutionClientFactory for CoinbaseExecutionClientFactory {
    fn create(
        &self,
        name: &str,
        config: &dyn ClientConfig,
        cache: Rc<RefCell<Cache>>,
    ) -> anyhow::Result<Box<dyn ExecutionClient>> {
        let coinbase_config = config
            .as_any()
            .downcast_ref::<CoinbaseExecClientConfig>()
            .ok_or_else(|| {
                anyhow::anyhow!(
                    "Invalid config type for CoinbaseExecutionClientFactory. Expected CoinbaseExecClientConfig, was {config:?}",
                )
            })?
            .clone();

        let account_type = coinbase_config.account_type;
        if !matches!(account_type, AccountType::Cash | AccountType::Margin) {
            anyhow::bail!(
                "Unsupported account_type {account_type:?} for Coinbase; expected Cash (spot) or Margin (CFM derivatives)"
            );
        }

        let core = ExecutionClientCore::new(
            self.trader_id,
            ClientId::from(name),
            *COINBASE_VENUE,
            OmsType::Netting,
            self.account_id,
            account_type,
            None,
            cache,
        );

        let client = CoinbaseExecutionClient::new(core, coinbase_config)?;

        Ok(Box::new(client))
    }

    fn name(&self) -> &'static str {
        "COINBASE"
    }

    fn config_type(&self) -> &'static str {
        "CoinbaseExecClientConfig"
    }
}

#[cfg(test)]
mod tests {
    use std::{cell::RefCell, rc::Rc};

    use nautilus_common::{
        cache::Cache,
        clock::TestClock,
        factories::{ClientConfig, DataClientFactory},
        live::runner::set_data_event_sender,
        messages::DataEvent,
    };
    use rstest::rstest;

    use super::*;

    fn setup_test_env() {
        let (sender, _receiver) = tokio::sync::mpsc::unbounded_channel::<DataEvent>();
        set_data_event_sender(sender);
    }

    #[rstest]
    fn test_coinbase_data_client_factory_creation() {
        let factory = CoinbaseDataClientFactory::new();
        assert_eq!(factory.name(), "COINBASE");
        assert_eq!(factory.config_type(), "CoinbaseDataClientConfig");
    }

    #[rstest]
    fn test_coinbase_exec_client_config_implements_client_config() {
        let config = CoinbaseExecClientConfig::default();
        let boxed_config: Box<dyn ClientConfig> = Box::new(config);
        let downcasted = boxed_config
            .as_any()
            .downcast_ref::<CoinbaseExecClientConfig>();
        assert!(downcasted.is_some());
    }

    #[rstest]
    fn test_coinbase_data_client_config_implements_client_config() {
        let config = CoinbaseDataClientConfig::default();
        let boxed_config: Box<dyn ClientConfig> = Box::new(config);
        let downcasted = boxed_config
            .as_any()
            .downcast_ref::<CoinbaseDataClientConfig>();
        assert!(downcasted.is_some());
    }

    #[rstest]
    fn test_coinbase_data_client_factory_creates_client() {
        setup_test_env();

        let factory = CoinbaseDataClientFactory::new();
        let config = CoinbaseDataClientConfig::default();
        let cache = Rc::new(RefCell::new(Cache::default()));
        let clock = Rc::new(RefCell::new(TestClock::new()));

        let result = factory.create("COINBASE-TEST", &config, cache, clock);
        assert!(result.is_ok());

        let client = result.unwrap();
        assert_eq!(client.client_id(), ClientId::from("COINBASE-TEST"));
    }

    #[rstest]
    fn test_coinbase_data_client_factory_rejects_wrong_config_type() {
        #[derive(Debug)]
        struct WrongConfig;

        impl ClientConfig for WrongConfig {
            fn as_any(&self) -> &dyn std::any::Any {
                self
            }
        }

        let factory = CoinbaseDataClientFactory::new();
        let cache = Rc::new(RefCell::new(Cache::default()));
        let clock = Rc::new(RefCell::new(TestClock::new()));

        let result = factory.create("COINBASE-TEST", &WrongConfig, cache, clock);
        let err = match result {
            Ok(_) => panic!("wrong config type should be rejected"),
            Err(e) => e,
        };
        let msg = err.to_string();
        assert!(
            msg.contains("CoinbaseDataClientFactory"),
            "error should name the factory, was: {msg}"
        );
        assert!(
            msg.contains("CoinbaseDataClientConfig"),
            "error should name the expected config type, was: {msg}"
        );
    }

    fn make_test_exec_config() -> CoinbaseExecClientConfig {
        CoinbaseExecClientConfig {
            api_key: Some("organizations/test-org/apiKeys/test-key".to_string()),
            api_secret: Some("test-pem-placeholder".to_string()),
            ..CoinbaseExecClientConfig::default()
        }
    }

    fn setup_exec_test_env() {
        use nautilus_common::{live::runner::replace_exec_event_sender, messages::ExecutionEvent};
        let (sender, _receiver) = tokio::sync::mpsc::unbounded_channel::<ExecutionEvent>();
        replace_exec_event_sender(sender);
    }

    #[rstest]
    fn test_coinbase_execution_client_factory_creation() {
        let factory = CoinbaseExecutionClientFactory::new(
            TraderId::from("TRADER-001"),
            AccountId::from("COINBASE-001"),
        );
        assert_eq!(factory.name(), "COINBASE");
        assert_eq!(factory.config_type(), "CoinbaseExecClientConfig");
    }

    #[rstest]
    fn test_coinbase_execution_client_factory_creates_cash_client() {
        setup_exec_test_env();

        let factory = CoinbaseExecutionClientFactory::new(
            TraderId::from("TRADER-001"),
            AccountId::from("COINBASE-001"),
        );
        let config = make_test_exec_config();
        let cache = Rc::new(RefCell::new(Cache::default()));

        let client = factory
            .create("COINBASE-TEST", &config, cache)
            .expect("factory should create exec client with valid config");

        assert_eq!(client.client_id(), ClientId::from("COINBASE-TEST"));
        assert_eq!(client.account_id(), AccountId::from("COINBASE-001"));
        assert_eq!(client.venue(), *COINBASE_VENUE);
        assert_eq!(client.oms_type(), OmsType::Netting);
    }

    #[rstest]
    fn test_coinbase_execution_client_factory_creates_margin_client() {
        setup_exec_test_env();

        let factory = CoinbaseExecutionClientFactory::new(
            TraderId::from("TRADER-001"),
            AccountId::from("COINBASE-001"),
        );
        let config = CoinbaseExecClientConfig {
            account_type: AccountType::Margin,
            ..make_test_exec_config()
        };
        let cache = Rc::new(RefCell::new(Cache::default()));

        let client = factory
            .create("COINBASE-DERIV", &config, cache)
            .expect("factory should create margin exec client when configured for derivatives");

        assert_eq!(client.client_id(), ClientId::from("COINBASE-DERIV"));
        assert_eq!(client.account_id(), AccountId::from("COINBASE-001"));
        assert_eq!(client.venue(), *COINBASE_VENUE);
        assert_eq!(client.oms_type(), OmsType::Netting);
    }

    #[rstest]
    fn test_coinbase_execution_client_factory_rejects_unsupported_account_type() {
        setup_exec_test_env();

        let factory = CoinbaseExecutionClientFactory::new(
            TraderId::from("TRADER-001"),
            AccountId::from("COINBASE-001"),
        );
        let config = CoinbaseExecClientConfig {
            account_type: AccountType::Betting,
            ..make_test_exec_config()
        };
        let cache = Rc::new(RefCell::new(Cache::default()));

        let err = factory
            .create("COINBASE-TEST", &config, cache)
            .err()
            .expect("unsupported account type must be rejected");
        let msg = err.to_string();
        assert!(
            msg.contains("Unsupported account_type"),
            "error should mention unsupported account type, was: {msg}"
        );
    }

    #[rstest]
    fn test_coinbase_execution_client_factory_rejects_wrong_config_type() {
        setup_exec_test_env();

        let factory = CoinbaseExecutionClientFactory::new(
            TraderId::from("TRADER-001"),
            AccountId::from("COINBASE-001"),
        );
        let wrong_config = CoinbaseDataClientConfig::default();
        let cache = Rc::new(RefCell::new(Cache::default()));

        let result = factory.create("COINBASE-TEST", &wrong_config, cache);
        let err = match result {
            Ok(_) => panic!("wrong config type should be rejected"),
            Err(e) => e,
        };
        let msg = err.to_string();
        assert!(
            msg.contains("CoinbaseExecutionClientFactory"),
            "error should name the factory, was: {msg}"
        );
        assert!(
            msg.contains("CoinbaseExecClientConfig"),
            "error should name the expected config type, was: {msg}"
        );
    }
}