Skip to main content

nautilus_deribit/common/
enums.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//! Common enumerations for the Deribit adapter.
17
18use std::fmt::Display;
19
20use nautilus_model::enums::TimeInForce;
21use serde::{Deserialize, Serialize};
22use strum::{AsRefStr, Display as StrumDisplay, EnumIter, EnumString};
23
24/// Deribit product type.
25#[derive(
26    Clone,
27    Copy,
28    Debug,
29    PartialEq,
30    Eq,
31    Hash,
32    AsRefStr,
33    StrumDisplay,
34    EnumIter,
35    EnumString,
36    Serialize,
37    Deserialize,
38)]
39#[serde(rename_all = "snake_case")]
40#[strum(serialize_all = "snake_case")]
41#[cfg_attr(
42    feature = "python",
43    pyo3::pyclass(eq, eq_int, module = "nautilus_trader.core.nautilus_pyo3.deribit")
44)]
45pub enum DeribitProductType {
46    /// Future contract
47    Future,
48    /// Option contract
49    Option,
50    /// Spot market
51    Spot,
52    /// Future combo
53    #[serde(rename = "future_combo")]
54    FutureCombo,
55    /// Option combo
56    #[serde(rename = "option_combo")]
57    OptionCombo,
58}
59
60/// Deribit currency.
61#[derive(
62    Clone, Copy, Debug, PartialEq, Eq, Hash, AsRefStr, EnumIter, EnumString, Serialize, Deserialize,
63)]
64#[serde(rename_all = "UPPERCASE")]
65#[strum(serialize_all = "UPPERCASE")]
66#[cfg_attr(
67    feature = "python",
68    pyo3::pyclass(eq, eq_int, module = "nautilus_trader.core.nautilus_pyo3.deribit")
69)]
70pub enum DeribitCurrency {
71    /// Bitcoin
72    BTC,
73    /// Ethereum
74    ETH,
75    /// USD Coin
76    USDC,
77    /// Tether
78    USDT,
79    /// Euro stablecoin
80    EURR,
81    /// All currencies
82    #[serde(rename = "any")]
83    ANY,
84}
85
86impl DeribitCurrency {
87    /// Returns the currency as a string.
88    #[must_use]
89    pub fn as_str(&self) -> &'static str {
90        match self {
91            Self::BTC => "BTC",
92            Self::ETH => "ETH",
93            Self::USDC => "USDC",
94            Self::USDT => "USDT",
95            Self::EURR => "EURR",
96            Self::ANY => "any",
97        }
98    }
99}
100
101impl Display for DeribitCurrency {
102    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
103        write!(f, "{}", self.as_str())
104    }
105}
106
107/// Deribit option type.
108#[derive(
109    Clone,
110    Copy,
111    Debug,
112    PartialEq,
113    Eq,
114    Hash,
115    AsRefStr,
116    StrumDisplay,
117    EnumIter,
118    EnumString,
119    Serialize,
120    Deserialize,
121)]
122#[serde(rename_all = "lowercase")]
123#[strum(serialize_all = "lowercase")]
124pub enum DeribitOptionType {
125    /// Call option
126    Call,
127    /// Put option
128    Put,
129}
130
131/// Deribit instrument lifecycle state.
132#[derive(
133    Clone, Copy, Debug, PartialEq, Eq, Hash, AsRefStr, EnumIter, EnumString, Serialize, Deserialize,
134)]
135#[serde(rename_all = "snake_case")]
136#[strum(serialize_all = "snake_case")]
137pub enum DeribitInstrumentState {
138    /// Instrument has been created but not yet active.
139    Created,
140    /// Instrument is active and trading.
141    Started,
142    /// Instrument has been settled (options/futures at expiry).
143    Settled,
144    /// Instrument is closed for trading.
145    Closed,
146    /// Instrument has been terminated.
147    Terminated,
148}
149
150impl Display for DeribitInstrumentState {
151    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
152        match self {
153            Self::Created => write!(f, "created"),
154            Self::Started => write!(f, "started"),
155            Self::Settled => write!(f, "settled"),
156            Self::Closed => write!(f, "closed"),
157            Self::Terminated => write!(f, "terminated"),
158        }
159    }
160}
161
162/// Deribit time in force values for order execution.
163#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
164#[serde(rename_all = "snake_case")]
165pub enum DeribitTimeInForce {
166    /// Good till cancelled.
167    #[serde(rename = "good_til_cancelled")]
168    GoodTilCancelled,
169    /// Good till day (expires at end of trading day).
170    #[serde(rename = "good_til_day")]
171    GoodTilDay,
172    /// Immediate or cancel.
173    #[serde(rename = "immediate_or_cancel")]
174    ImmediateOrCancel,
175    /// Fill or kill.
176    #[serde(rename = "fill_or_kill")]
177    FillOrKill,
178}
179
180impl DeribitTimeInForce {
181    /// Returns the time in force as a Deribit API string.
182    #[must_use]
183    pub fn as_str(&self) -> &'static str {
184        match self {
185            Self::GoodTilCancelled => "good_til_cancelled",
186            Self::GoodTilDay => "good_til_day",
187            Self::ImmediateOrCancel => "immediate_or_cancel",
188            Self::FillOrKill => "fill_or_kill",
189        }
190    }
191}
192
193impl TryFrom<TimeInForce> for DeribitTimeInForce {
194    type Error = String;
195
196    fn try_from(tif: TimeInForce) -> Result<Self, Self::Error> {
197        match tif {
198            TimeInForce::Gtc => Ok(Self::GoodTilCancelled),
199            TimeInForce::Ioc => Ok(Self::ImmediateOrCancel),
200            TimeInForce::Fok => Ok(Self::FillOrKill),
201            TimeInForce::Gtd => Ok(Self::GoodTilDay),
202            _ => Err(format!(
203                "TimeInForce::{tif} is not supported on Deribit (valid: GTC, IOC, FOK, GTD)"
204            )),
205        }
206    }
207}