Skip to main content

nautilus_deribit/http/
query.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//! Deribit HTTP API query parameter builders.
17
18use derive_builder::Builder;
19use serde::{Deserialize, Serialize};
20
21use super::models::{DeribitCurrency, DeribitProductType};
22
23/// Query parameters for `/public/get_instruments` endpoint.
24#[derive(Clone, Debug, Deserialize, Serialize, Builder)]
25#[builder(setter(into, strip_option))]
26pub struct GetInstrumentsParams {
27    /// Currency filter
28    pub currency: DeribitCurrency,
29    /// Optional product type filter
30    #[serde(skip_serializing_if = "Option::is_none")]
31    #[builder(default)]
32    pub kind: Option<DeribitProductType>,
33    /// Whether to include expired instruments
34    #[serde(skip_serializing_if = "Option::is_none")]
35    #[builder(default)]
36    pub expired: Option<bool>,
37}
38
39impl GetInstrumentsParams {
40    /// Creates a new builder for [`GetInstrumentsParams`].
41    #[must_use]
42    pub fn builder() -> GetInstrumentsParamsBuilder {
43        GetInstrumentsParamsBuilder::default()
44    }
45
46    /// Creates parameters for a specific currency.
47    #[must_use]
48    pub fn new(currency: DeribitCurrency) -> Self {
49        Self {
50            currency,
51            kind: None,
52            expired: None,
53        }
54    }
55
56    /// Creates parameters for a specific currency and product type.
57    #[must_use]
58    pub fn with_kind(currency: DeribitCurrency, kind: DeribitProductType) -> Self {
59        Self {
60            currency,
61            kind: Some(kind),
62            expired: None,
63        }
64    }
65}
66
67/// Query parameters for `/public/get_instrument` endpoint.
68#[derive(Clone, Debug, Deserialize, Serialize, Builder)]
69pub struct GetInstrumentParams {
70    /// Instrument name (e.g., "BTC-PERPETUAL", "ETH-25MAR23-2000-C")
71    pub instrument_name: String,
72}
73
74/// Query parameters for `/private/get_account_summaries` endpoint.
75#[derive(Clone, Debug, Default, Deserialize, Serialize)]
76pub struct GetAccountSummariesParams {
77    /// The user id for the subaccount.
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub subaccount_id: Option<String>,
80    /// Include extended fields
81    #[serde(skip_serializing_if = "Option::is_none")]
82    pub extended: Option<bool>,
83}
84
85impl GetAccountSummariesParams {
86    /// Creates a new instance with both subaccount ID and extended flag.
87    #[must_use]
88    pub fn new(subaccount_id: String, extended: bool) -> Self {
89        Self {
90            subaccount_id: Some(subaccount_id),
91            extended: Some(extended),
92        }
93    }
94}
95
96/// Query parameters for `/public/get_last_trades_by_instrument_and_time` endpoint.
97#[derive(Clone, Debug, Deserialize, Serialize, Builder)]
98#[builder(setter(into, strip_option))]
99pub struct GetLastTradesByInstrumentAndTimeParams {
100    /// Instrument name (e.g., "BTC-PERPETUAL")
101    pub instrument_name: String,
102    /// The earliest timestamp to return result from (milliseconds since the UNIX epoch)
103    pub start_timestamp: i64,
104    /// The most recent timestamp to return result from (milliseconds since the UNIX epoch)
105    pub end_timestamp: i64,
106    /// Number of requested items, default - 10, maximum - 1000
107    #[serde(skip_serializing_if = "Option::is_none")]
108    #[builder(default)]
109    pub count: Option<u32>,
110    /// Direction of results sorting: "asc", "desc", or "default"
111    #[serde(skip_serializing_if = "Option::is_none")]
112    #[builder(default)]
113    pub sorting: Option<String>,
114}
115
116impl GetLastTradesByInstrumentAndTimeParams {
117    /// Creates a new instance with the required parameters.
118    #[must_use]
119    pub fn new(
120        instrument_name: impl Into<String>,
121        start_timestamp: i64,
122        end_timestamp: i64,
123        count: Option<u32>,
124        sorting: Option<String>,
125    ) -> Self {
126        Self {
127            instrument_name: instrument_name.into(),
128            start_timestamp,
129            end_timestamp,
130            count,
131            sorting,
132        }
133    }
134}
135
136/// Query parameters for `/public/get_tradingview_chart_data` endpoint.
137#[derive(Clone, Debug, Deserialize, Serialize)]
138pub struct GetTradingViewChartDataParams {
139    /// Instrument name (e.g., "BTC-PERPETUAL")
140    pub instrument_name: String,
141    /// The earliest timestamp to return result from (milliseconds since UNIX epoch)
142    pub start_timestamp: i64,
143    /// The most recent timestamp to return result from (milliseconds since UNIX epoch)
144    pub end_timestamp: i64,
145    /// Chart bars resolution given in full minutes or keyword "1D"
146    /// Supported resolutions: 1, 3, 5, 10, 15, 30, 60, 120, 180, 360, 720, 1D
147    pub resolution: String,
148}
149
150impl GetTradingViewChartDataParams {
151    /// Creates new parameters for chart data request.
152    #[must_use]
153    pub fn new(
154        instrument_name: String,
155        start_timestamp: i64,
156        end_timestamp: i64,
157        resolution: String,
158    ) -> Self {
159        Self {
160            instrument_name,
161            start_timestamp,
162            end_timestamp,
163            resolution,
164        }
165    }
166}
167
168/// Query parameters for `/public/get_order_book` endpoint.
169#[derive(Clone, Debug, Deserialize, Serialize, Builder)]
170#[builder(setter(into, strip_option))]
171pub struct GetOrderBookParams {
172    /// Instrument name (e.g., "BTC-PERPETUAL")
173    pub instrument_name: String,
174    /// The number of entries to return for bids and asks.
175    /// Valid values: 1, 5, 10, 20, 50, 100, 1000, 10000
176    /// Maximum: 10000
177    #[serde(skip_serializing_if = "Option::is_none")]
178    #[builder(default)]
179    pub depth: Option<u32>,
180}
181
182impl GetOrderBookParams {
183    /// Creates parameters with required fields.
184    #[must_use]
185    pub fn new(instrument_name: String, depth: Option<u32>) -> Self {
186        Self {
187            instrument_name,
188            depth,
189        }
190    }
191}
192
193/// Query parameters for `/private/get_order_state` endpoint.
194/// Retrieves a single order by its ID.
195#[derive(Clone, Debug, Deserialize, Serialize)]
196pub struct GetOrderStateParams {
197    /// The order ID to look up.
198    pub order_id: String,
199}
200
201impl GetOrderStateParams {
202    /// Creates parameters for a specific order ID.
203    #[must_use]
204    pub fn new(order_id: impl Into<String>) -> Self {
205        Self {
206            order_id: order_id.into(),
207        }
208    }
209}
210
211/// Query parameters for `/private/get_open_orders` endpoint.
212/// Retrieves all open orders across all currencies and instruments.
213#[derive(Clone, Debug, Default, Deserialize, Serialize)]
214pub struct GetOpenOrdersParams {}
215
216impl GetOpenOrdersParams {
217    /// Creates parameters to get all open orders.
218    #[must_use]
219    pub fn new() -> Self {
220        Self {}
221    }
222}
223
224/// Query parameters for `/private/get_open_orders_by_instrument` endpoint.
225/// Retrieves open orders for a specific instrument.
226#[derive(Clone, Debug, Deserialize, Serialize, Builder)]
227#[builder(setter(into, strip_option))]
228pub struct GetOpenOrdersByInstrumentParams {
229    /// Instrument name (e.g., "BTC-PERPETUAL")
230    pub instrument_name: String,
231    /// Order type filter: "all", "limit", "stop_all", "stop_limit", "stop_market",
232    /// "take_all", "take_limit", "take_market", "trailing_all", "trailing_stop"
233    #[serde(skip_serializing_if = "Option::is_none")]
234    #[builder(default)]
235    pub r#type: Option<String>,
236}
237
238impl GetOpenOrdersByInstrumentParams {
239    /// Creates parameters for a specific instrument.
240    #[must_use]
241    pub fn new(instrument_name: impl Into<String>) -> Self {
242        Self {
243            instrument_name: instrument_name.into(),
244            r#type: None,
245        }
246    }
247}
248
249/// Query parameters for `/private/get_order_history_by_instrument` endpoint.
250/// Retrieves historical orders for a specific instrument.
251#[derive(Clone, Debug, Deserialize, Serialize, Builder)]
252#[builder(setter(into, strip_option))]
253pub struct GetOrderHistoryByInstrumentParams {
254    /// Instrument name (e.g., "BTC-PERPETUAL")
255    pub instrument_name: String,
256    /// Number of requested items, default - 20
257    #[serde(skip_serializing_if = "Option::is_none")]
258    #[builder(default)]
259    pub count: Option<u32>,
260    /// Offset for pagination
261    #[serde(skip_serializing_if = "Option::is_none")]
262    #[builder(default)]
263    pub offset: Option<u32>,
264    /// Include orders older than 3 days
265    #[serde(skip_serializing_if = "Option::is_none")]
266    #[builder(default)]
267    pub include_old: Option<bool>,
268    /// Include unfilled orders
269    #[serde(skip_serializing_if = "Option::is_none")]
270    #[builder(default)]
271    pub include_unfilled: Option<bool>,
272}
273
274impl GetOrderHistoryByInstrumentParams {
275    /// Creates parameters for a specific instrument.
276    #[must_use]
277    pub fn new(instrument_name: impl Into<String>) -> Self {
278        Self {
279            instrument_name: instrument_name.into(),
280            count: None,
281            offset: None,
282            include_old: None,
283            include_unfilled: None,
284        }
285    }
286
287    /// Creates a new builder for [`GetOrderHistoryByInstrumentParams`].
288    #[must_use]
289    pub fn builder() -> GetOrderHistoryByInstrumentParamsBuilder {
290        GetOrderHistoryByInstrumentParamsBuilder::default()
291    }
292}
293
294/// Query parameters for `/private/get_order_history_by_currency` endpoint.
295/// Retrieves historical orders for a specific currency.
296#[derive(Clone, Debug, Deserialize, Serialize, Builder)]
297#[builder(setter(into, strip_option))]
298pub struct GetOrderHistoryByCurrencyParams {
299    /// Currency filter
300    pub currency: DeribitCurrency,
301    /// Optional product type filter
302    #[serde(skip_serializing_if = "Option::is_none")]
303    #[builder(default)]
304    pub kind: Option<DeribitProductType>,
305    /// Number of requested items, default - 20, maximum - 10000
306    #[serde(skip_serializing_if = "Option::is_none")]
307    #[builder(default)]
308    pub count: Option<u32>,
309    /// Include unfilled orders
310    #[serde(skip_serializing_if = "Option::is_none")]
311    #[builder(default)]
312    pub include_unfilled: Option<bool>,
313}
314
315impl GetOrderHistoryByCurrencyParams {
316    /// Creates parameters for a specific currency.
317    #[must_use]
318    pub fn new(currency: DeribitCurrency) -> Self {
319        Self {
320            currency,
321            kind: None,
322            count: None,
323            include_unfilled: None,
324        }
325    }
326
327    /// Creates a new builder for [`GetOrderHistoryByCurrencyParams`].
328    #[must_use]
329    pub fn builder() -> GetOrderHistoryByCurrencyParamsBuilder {
330        GetOrderHistoryByCurrencyParamsBuilder::default()
331    }
332}
333
334/// Query parameters for `/private/get_user_trades_by_instrument_and_time` endpoint.
335/// Retrieves user trades for a specific instrument within a time range.
336#[derive(Clone, Debug, Deserialize, Serialize, Builder)]
337#[builder(setter(into, strip_option))]
338pub struct GetUserTradesByInstrumentAndTimeParams {
339    /// Instrument name (e.g., "BTC-PERPETUAL")
340    pub instrument_name: String,
341    /// Start timestamp in milliseconds since UNIX epoch
342    pub start_timestamp: i64,
343    /// End timestamp in milliseconds since UNIX epoch
344    pub end_timestamp: i64,
345    /// Number of requested items, default - 10, maximum - 1000
346    #[serde(skip_serializing_if = "Option::is_none")]
347    #[builder(default)]
348    pub count: Option<u32>,
349    /// Direction of results sorting: "asc", "desc", or "default"
350    #[serde(skip_serializing_if = "Option::is_none")]
351    #[builder(default)]
352    pub sorting: Option<String>,
353}
354
355impl GetUserTradesByInstrumentAndTimeParams {
356    /// Creates parameters with required fields.
357    #[must_use]
358    pub fn new(
359        instrument_name: impl Into<String>,
360        start_timestamp: i64,
361        end_timestamp: i64,
362    ) -> Self {
363        Self {
364            instrument_name: instrument_name.into(),
365            start_timestamp,
366            end_timestamp,
367            count: None,
368            sorting: None,
369        }
370    }
371
372    /// Creates a new builder for [`GetUserTradesByInstrumentAndTimeParams`].
373    #[must_use]
374    pub fn builder() -> GetUserTradesByInstrumentAndTimeParamsBuilder {
375        GetUserTradesByInstrumentAndTimeParamsBuilder::default()
376    }
377}
378
379/// Query parameters for `/private/get_user_trades_by_currency_and_time` endpoint.
380/// Retrieves user trades for a specific currency within a time range.
381#[derive(Clone, Debug, Deserialize, Serialize, Builder)]
382#[builder(setter(into, strip_option))]
383pub struct GetUserTradesByCurrencyAndTimeParams {
384    /// Currency filter
385    pub currency: DeribitCurrency,
386    /// Start timestamp in milliseconds since UNIX epoch
387    pub start_timestamp: i64,
388    /// End timestamp in milliseconds since UNIX epoch
389    pub end_timestamp: i64,
390    /// Optional product type filter
391    #[serde(skip_serializing_if = "Option::is_none")]
392    #[builder(default)]
393    pub kind: Option<DeribitProductType>,
394    /// Number of requested items, default - 10, maximum - 1000
395    #[serde(skip_serializing_if = "Option::is_none")]
396    #[builder(default)]
397    pub count: Option<u32>,
398}
399
400impl GetUserTradesByCurrencyAndTimeParams {
401    /// Creates parameters with required fields.
402    #[must_use]
403    pub fn new(currency: DeribitCurrency, start_timestamp: i64, end_timestamp: i64) -> Self {
404        Self {
405            currency,
406            start_timestamp,
407            end_timestamp,
408            kind: None,
409            count: None,
410        }
411    }
412
413    /// Creates a new builder for [`GetUserTradesByCurrencyAndTimeParams`].
414    #[must_use]
415    pub fn builder() -> GetUserTradesByCurrencyAndTimeParamsBuilder {
416        GetUserTradesByCurrencyAndTimeParamsBuilder::default()
417    }
418}
419
420/// Query parameters for `/private/get_positions` endpoint.
421/// Retrieves positions for a specific currency.
422#[derive(Clone, Debug, Deserialize, Serialize, Builder)]
423#[builder(setter(into, strip_option))]
424pub struct GetPositionsParams {
425    /// Currency filter
426    pub currency: DeribitCurrency,
427    /// Optional product type filter
428    #[serde(skip_serializing_if = "Option::is_none")]
429    #[builder(default)]
430    pub kind: Option<DeribitProductType>,
431}
432
433impl GetPositionsParams {
434    /// Creates parameters for a specific currency.
435    #[must_use]
436    pub fn new(currency: DeribitCurrency) -> Self {
437        Self {
438            currency,
439            kind: None,
440        }
441    }
442
443    /// Creates a new builder for [`GetPositionsParams`].
444    #[must_use]
445    pub fn builder() -> GetPositionsParamsBuilder {
446        GetPositionsParamsBuilder::default()
447    }
448}