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
// -------------------------------------------------------------------------------------------------
//  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.
// -------------------------------------------------------------------------------------------------

//! Venue-shaped request bodies for the Coinbase Advanced Trade REST API.
//!
//! These types serialize to the exact JSON shape Coinbase expects on its
//! POST endpoints. The raw HTTP client takes one of these types per endpoint;
//! the domain HTTP client builds them from Nautilus types.

use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use ustr::Ustr;

use crate::common::{
    enums::{CoinbaseMarginType, CoinbaseOrderSide, CoinbaseStopDirection},
    parse::{
        deserialize_decimal_from_str, deserialize_optional_decimal_from_str,
        serialize_decimal_as_str, serialize_optional_decimal_as_str,
    },
};

/// Request body for `POST /api/v3/brokerage/orders` (Create Order).
///
/// # References
///
/// - <https://docs.cdp.coinbase.com/api-reference/advanced-trade-api/rest-api/orders/create-order>
#[derive(Debug, Clone, Serialize)]
pub struct CreateOrderRequest {
    pub client_order_id: String,
    pub product_id: Ustr,
    pub side: CoinbaseOrderSide,
    pub order_configuration: OrderConfiguration,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub self_trade_prevention_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub leverage: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub margin_type: Option<CoinbaseMarginType>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub retail_portfolio_id: Option<String>,
    /// Derivatives-only flag that marks the order as position-reducing only.
    ///
    /// Coinbase does not document `reduce_only` as an accepted create-order
    /// field; the venue's failure-reason enum acknowledges the concept but the
    /// order schema has no slot for it. The field is threaded through the
    /// request for API parity with other adapters and is omitted from the wire
    /// payload when `false`.
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub reduce_only: bool,
}

/// Request body for `POST /api/v3/brokerage/orders/batch_cancel` (Cancel Orders).
///
/// # References
///
/// - <https://docs.cdp.coinbase.com/api-reference/advanced-trade-api/rest-api/orders/cancel-order>
#[derive(Debug, Clone, Serialize)]
pub struct CancelOrdersRequest {
    pub order_ids: Vec<String>,
}

/// Filter parameters for `GET /api/v3/brokerage/orders/historical/batch`
/// (List Orders).
///
/// `client_order_id_filter` is a client-side filter applied during pagination
/// because Coinbase's batch endpoint does not accept a `client_order_id`
/// query parameter.
///
/// # References
///
/// - <https://docs.cdp.coinbase.com/api-reference/advanced-trade-api/rest-api/orders/list-orders>
#[derive(Debug, Clone, Default)]
pub struct OrderListQuery {
    pub product_id: Option<String>,
    pub open_only: bool,
    pub start: Option<DateTime<Utc>>,
    pub end: Option<DateTime<Utc>>,
    pub limit: Option<u32>,
    pub client_order_id_filter: Option<String>,
}

/// Filter parameters for `GET /api/v3/brokerage/orders/historical/fills`
/// (List Fills).
///
/// # References
///
/// - <https://docs.cdp.coinbase.com/api-reference/advanced-trade-api/rest-api/orders/list-fills>
#[derive(Debug, Clone, Default)]
pub struct FillListQuery {
    pub product_id: Option<String>,
    pub venue_order_id: Option<String>,
    pub start: Option<DateTime<Utc>>,
    pub end: Option<DateTime<Utc>>,
    pub limit: Option<u32>,
}

/// Request body for `POST /api/v3/brokerage/orders/edit` (Edit Order).
///
/// Coinbase restricts edits to GTC variants of LIMIT (and limited STOP_LIMIT
/// configurations). Each field is optional so callers can edit a subset.
///
/// # References
///
/// - <https://docs.cdp.coinbase.com/api-reference/advanced-trade-api/rest-api/orders/edit-order>
#[derive(Debug, Clone, Serialize)]
pub struct EditOrderRequest {
    pub order_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub price: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop_price: Option<String>,
}

/// Order configuration for different order types.
///
/// Uses `#[serde(untagged)]` because Coinbase wraps each order type in a
/// uniquely-named key (e.g. `market_market_ioc`, `limit_limit_gtc`), which
/// serde matches by attempting each variant in declaration order. Error
/// messages on deserialization failure are opaque; prefer constructing
/// variants directly rather than deserializing from untrusted JSON.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum OrderConfiguration {
    MarketIoc(MarketIoc),
    MarketFok(MarketFok),
    LimitGtc(LimitGtc),
    LimitGtd(LimitGtd),
    LimitFok(LimitFok),
    StopLimitGtc(StopLimitGtc),
    StopLimitGtd(StopLimitGtd),
}

/// Market order with IOC fill.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketIoc {
    pub market_market_ioc: MarketParams,
}

/// Market order with FOK fill.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketFok {
    pub market_market_fok: MarketParams,
}

/// Market order parameters (shared by `market_market_ioc` and
/// `market_market_fok`; both wire shapes accept the same `base_size` /
/// `quote_size` body).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketParams {
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_optional_decimal_from_str",
        serialize_with = "serialize_optional_decimal_as_str"
    )]
    pub quote_size: Option<Decimal>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_optional_decimal_from_str",
        serialize_with = "serialize_optional_decimal_as_str"
    )]
    pub base_size: Option<Decimal>,
}

/// Limit GTC order.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LimitGtc {
    pub limit_limit_gtc: LimitGtcParams,
}

/// Limit GTC parameters.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LimitGtcParams {
    #[serde(
        serialize_with = "serialize_decimal_as_str",
        deserialize_with = "deserialize_decimal_from_str"
    )]
    pub base_size: Decimal,
    #[serde(
        serialize_with = "serialize_decimal_as_str",
        deserialize_with = "deserialize_decimal_from_str"
    )]
    pub limit_price: Decimal,
    #[serde(default)]
    pub post_only: bool,
}

/// Limit GTD order.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LimitGtd {
    pub limit_limit_gtd: LimitGtdParams,
}

/// Limit GTD parameters.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LimitGtdParams {
    #[serde(
        serialize_with = "serialize_decimal_as_str",
        deserialize_with = "deserialize_decimal_from_str"
    )]
    pub base_size: Decimal,
    #[serde(
        serialize_with = "serialize_decimal_as_str",
        deserialize_with = "deserialize_decimal_from_str"
    )]
    pub limit_price: Decimal,
    pub end_time: String,
    #[serde(default)]
    pub post_only: bool,
}

/// Limit FOK order.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LimitFok {
    pub limit_limit_fok: LimitFokParams,
}

/// Limit FOK parameters.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LimitFokParams {
    #[serde(
        serialize_with = "serialize_decimal_as_str",
        deserialize_with = "deserialize_decimal_from_str"
    )]
    pub base_size: Decimal,
    #[serde(
        serialize_with = "serialize_decimal_as_str",
        deserialize_with = "deserialize_decimal_from_str"
    )]
    pub limit_price: Decimal,
}

/// Stop-limit GTC order.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StopLimitGtc {
    pub stop_limit_stop_limit_gtc: StopLimitGtcParams,
}

/// Stop-limit GTC parameters.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StopLimitGtcParams {
    #[serde(
        serialize_with = "serialize_decimal_as_str",
        deserialize_with = "deserialize_decimal_from_str"
    )]
    pub base_size: Decimal,
    #[serde(
        serialize_with = "serialize_decimal_as_str",
        deserialize_with = "deserialize_decimal_from_str"
    )]
    pub limit_price: Decimal,
    #[serde(
        serialize_with = "serialize_decimal_as_str",
        deserialize_with = "deserialize_decimal_from_str"
    )]
    pub stop_price: Decimal,
    pub stop_direction: CoinbaseStopDirection,
}

/// Stop-limit GTD order.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StopLimitGtd {
    pub stop_limit_stop_limit_gtd: StopLimitGtdParams,
}

/// Stop-limit GTD parameters.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StopLimitGtdParams {
    #[serde(
        serialize_with = "serialize_decimal_as_str",
        deserialize_with = "deserialize_decimal_from_str"
    )]
    pub base_size: Decimal,
    #[serde(
        serialize_with = "serialize_decimal_as_str",
        deserialize_with = "deserialize_decimal_from_str"
    )]
    pub limit_price: Decimal,
    #[serde(
        serialize_with = "serialize_decimal_as_str",
        deserialize_with = "deserialize_decimal_from_str"
    )]
    pub stop_price: Decimal,
    pub stop_direction: CoinbaseStopDirection,
    pub end_time: String,
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use rstest::rstest;
    use rust_decimal::Decimal;
    use serde_json::json;

    use super::*;
    use crate::common::consts::{
        ORDER_CONFIG_BASE_SIZE, ORDER_CONFIG_LIMIT_GTC, ORDER_CONFIG_LIMIT_PRICE,
        ORDER_CONFIG_MARKET_IOC, ORDER_CONFIG_QUOTE_SIZE,
    };

    #[rstest]
    fn test_serialize_market_order() {
        let order = CreateOrderRequest {
            client_order_id: "test-123".to_string(),
            product_id: Ustr::from("BTC-USD"),
            side: CoinbaseOrderSide::Buy,
            order_configuration: OrderConfiguration::MarketIoc(MarketIoc {
                market_market_ioc: MarketParams {
                    quote_size: Some(Decimal::from_str("100").unwrap()),
                    base_size: None,
                },
            }),
            self_trade_prevention_id: None,
            leverage: None,
            margin_type: None,
            retail_portfolio_id: None,
            reduce_only: false,
        };

        let value = serde_json::to_value(&order).unwrap();
        assert_eq!(value["client_order_id"], "test-123");
        assert_eq!(value["product_id"], "BTC-USD");
        assert_eq!(value["side"], "BUY");
        assert_eq!(
            value["order_configuration"][ORDER_CONFIG_MARKET_IOC][ORDER_CONFIG_QUOTE_SIZE],
            "100"
        );
    }

    #[rstest]
    fn test_serialize_limit_gtc_order() {
        let order = CreateOrderRequest {
            client_order_id: "test-456".to_string(),
            product_id: Ustr::from("ETH-USD"),
            side: CoinbaseOrderSide::Sell,
            order_configuration: OrderConfiguration::LimitGtc(LimitGtc {
                limit_limit_gtc: LimitGtcParams {
                    base_size: Decimal::from_str("1.5").unwrap(),
                    limit_price: Decimal::from_str("3500.00").unwrap(),
                    post_only: true,
                },
            }),
            self_trade_prevention_id: None,
            leverage: None,
            margin_type: None,
            retail_portfolio_id: None,
            reduce_only: false,
        };

        let value = serde_json::to_value(&order).unwrap();
        assert_eq!(value["side"], "SELL");
        assert_eq!(
            value["order_configuration"][ORDER_CONFIG_LIMIT_GTC][ORDER_CONFIG_BASE_SIZE],
            "1.5"
        );
        assert_eq!(
            value["order_configuration"][ORDER_CONFIG_LIMIT_GTC][ORDER_CONFIG_LIMIT_PRICE],
            "3500.00"
        );
    }

    #[rstest]
    fn test_serialize_cancel_orders_request() {
        let request = CancelOrdersRequest {
            order_ids: vec!["abc".to_string(), "def".to_string()],
        };
        assert_eq!(
            serde_json::to_value(&request).unwrap(),
            json!({"order_ids": ["abc", "def"]})
        );
    }

    #[rstest]
    fn test_serialize_edit_order_request_omits_none_fields() {
        let request = EditOrderRequest {
            order_id: "venue-1".to_string(),
            price: Some("100.00".to_string()),
            size: None,
            stop_price: None,
        };
        let value = serde_json::to_value(&request).unwrap();
        assert_eq!(value["order_id"], "venue-1");
        assert_eq!(value["price"], "100.00");
        assert!(value.get("size").is_none());
        assert!(value.get("stop_price").is_none());
    }
}