Skip to main content

nautilus_deribit/common/
models.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//! Models shared between Deribit HTTP and WebSocket layers.
17//!
18//! Types with decimal fields borrow their JSON tokens, so they deserialize only from borrowed input
19//! such as `serde_json::from_str` or `serde_json::from_slice`.
20
21use nautilus_core::serialization::deserialize_optional_decimal_token_borrowed;
22use rust_decimal::Decimal;
23use serde::{Deserialize, Serialize};
24
25use crate::common::serialization::deserialize_decimal_token_or_zero_borrowed;
26
27/// A single leg entry of a Deribit combo trade.
28///
29/// Appears in the `legs` array of a combo's public trade message
30/// (`/public/get_last_trades_by_currency` with `kind=option_combo|future_combo`,
31/// and the `trades.{combo}.{interval}` subscription channel). Each leg is also
32/// published independently on the leg instrument's own `trades.{instrument}.*`
33/// stream, carrying `combo_id` and `combo_trade_id` to link back to the parent.
34#[derive(Clone, Debug, Serialize, Deserialize)]
35pub struct DeribitTradeLeg {
36    /// Trade timestamp in milliseconds.
37    pub timestamp: i64,
38    /// Trade price.
39    #[serde(deserialize_with = "deserialize_decimal_token_or_zero_borrowed")]
40    pub price: Decimal,
41    /// Trade amount.
42    #[serde(deserialize_with = "deserialize_decimal_token_or_zero_borrowed")]
43    pub amount: Decimal,
44    /// Trade direction: `buy` or `sell`.
45    pub direction: String,
46    /// Underlying index price at trade time (may be empty on older historical trades,
47    /// matching the optionality on the parent [`crate::http::models::DeribitPublicTrade`]).
48    #[serde(
49        default,
50        deserialize_with = "deserialize_optional_decimal_token_borrowed"
51    )]
52    pub index_price: Option<Decimal>,
53    /// Leg instrument name (e.g., `BTC-PERPETUAL`).
54    pub instrument_name: String,
55    /// Per-instrument trade sequence number.
56    pub trade_seq: i64,
57    /// Mark price at trade time (may be empty on older historical trades,
58    /// matching the optionality on the parent [`crate::http::models::DeribitPublicTrade`]).
59    #[serde(
60        default,
61        deserialize_with = "deserialize_optional_decimal_token_borrowed"
62    )]
63    pub mark_price: Option<Decimal>,
64    /// Tick direction: 0 = Plus, 1 = Zero-Plus, 2 = Minus, 3 = Zero-Minus.
65    pub tick_direction: i32,
66    /// The combo instrument that produced this leg.
67    pub combo_id: String,
68    /// Trade size in contract units (may be absent on historical combo trades,
69    /// matching the optionality on the parent [`crate::http::models::DeribitPublicTrade`]).
70    #[serde(
71        default,
72        deserialize_with = "deserialize_optional_decimal_token_borrowed"
73    )]
74    pub contracts: Option<Decimal>,
75    /// Unique (per currency) trade identifier for the leg.
76    pub trade_id: String,
77    /// Trade identifier of the parent combo trade.
78    pub combo_trade_id: String,
79    /// Implied volatility (option legs only).
80    #[serde(
81        default,
82        deserialize_with = "deserialize_optional_decimal_token_borrowed"
83    )]
84    pub iv: Option<Decimal>,
85}