nautilus_interactive_brokers/common/enums/
contracts.rs1use std::{fmt::Display, str::FromStr};
17
18use nautilus_model::enums::OptionKind;
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22#[cfg_attr(
23 feature = "python",
24 pyo3::pyclass(
25 module = "nautilus_trader.adapters.interactive_brokers",
26 from_py_object,
27 rename_all = "SCREAMING_SNAKE_CASE"
28 )
29)]
30#[cfg_attr(
31 feature = "python",
32 pyo3_stub_gen::derive::gen_stub_pyclass_enum(
33 module = "nautilus_trader.adapters.interactive_brokers"
34 )
35)]
36pub enum IbSecurityType {
37 Stock,
39 Option,
41 Future,
43 ContinuousFuture,
45 Index,
47 FuturesOption,
49 ForexPair,
51 Spread,
53 Warrant,
55 Bond,
57 Commodity,
59 News,
61 MutualFund,
63 Crypto,
65 Cfd,
67}
68
69impl IbSecurityType {
70 #[must_use]
72 pub const fn as_str(self) -> &'static str {
73 match self {
74 Self::Stock => "STK",
75 Self::Option => "OPT",
76 Self::Future => "FUT",
77 Self::ContinuousFuture => "CONTFUT",
78 Self::Index => "IND",
79 Self::FuturesOption => "FOP",
80 Self::ForexPair => "CASH",
81 Self::Spread => "BAG",
82 Self::Warrant => "WAR",
83 Self::Bond => "BOND",
84 Self::Commodity => "CMDTY",
85 Self::News => "NEWS",
86 Self::MutualFund => "FUND",
87 Self::Crypto => "CRYPTO",
88 Self::Cfd => "CFD",
89 }
90 }
91
92 #[must_use]
94 pub const fn ibapi_security_type(self) -> ibapi::contracts::SecurityType {
95 match self {
96 Self::Stock => ibapi::contracts::SecurityType::Stock,
97 Self::Option => ibapi::contracts::SecurityType::Option,
98 Self::Future => ibapi::contracts::SecurityType::Future,
99 Self::ContinuousFuture => ibapi::contracts::SecurityType::ContinuousFuture,
100 Self::Index => ibapi::contracts::SecurityType::Index,
101 Self::FuturesOption => ibapi::contracts::SecurityType::FuturesOption,
102 Self::ForexPair => ibapi::contracts::SecurityType::ForexPair,
103 Self::Spread => ibapi::contracts::SecurityType::Spread,
104 Self::Warrant => ibapi::contracts::SecurityType::Warrant,
105 Self::Bond => ibapi::contracts::SecurityType::Bond,
106 Self::Commodity => ibapi::contracts::SecurityType::Commodity,
107 Self::News => ibapi::contracts::SecurityType::News,
108 Self::MutualFund => ibapi::contracts::SecurityType::MutualFund,
109 Self::Crypto => ibapi::contracts::SecurityType::Crypto,
110 Self::Cfd => ibapi::contracts::SecurityType::CFD,
111 }
112 }
113}
114
115impl TryFrom<&ibapi::contracts::SecurityType> for IbSecurityType {
116 type Error = anyhow::Error;
117
118 fn try_from(value: &ibapi::contracts::SecurityType) -> Result<Self, Self::Error> {
119 match value {
120 ibapi::contracts::SecurityType::Stock => Ok(Self::Stock),
121 ibapi::contracts::SecurityType::Option => Ok(Self::Option),
122 ibapi::contracts::SecurityType::Future => Ok(Self::Future),
123 ibapi::contracts::SecurityType::ContinuousFuture => Ok(Self::ContinuousFuture),
124 ibapi::contracts::SecurityType::Index => Ok(Self::Index),
125 ibapi::contracts::SecurityType::FuturesOption => Ok(Self::FuturesOption),
126 ibapi::contracts::SecurityType::ForexPair => Ok(Self::ForexPair),
127 ibapi::contracts::SecurityType::Spread => Ok(Self::Spread),
128 ibapi::contracts::SecurityType::Warrant => Ok(Self::Warrant),
129 ibapi::contracts::SecurityType::Bond => Ok(Self::Bond),
130 ibapi::contracts::SecurityType::Commodity => Ok(Self::Commodity),
131 ibapi::contracts::SecurityType::News => Ok(Self::News),
132 ibapi::contracts::SecurityType::MutualFund => Ok(Self::MutualFund),
133 ibapi::contracts::SecurityType::Crypto => Ok(Self::Crypto),
134 ibapi::contracts::SecurityType::CFD => Ok(Self::Cfd),
135 ibapi::contracts::SecurityType::Other(value) => {
136 anyhow::bail!("Unknown IB security type: {value}")
137 }
138 }
139 }
140}
141
142impl FromStr for IbSecurityType {
143 type Err = anyhow::Error;
144
145 fn from_str(value: &str) -> Result<Self, Self::Err> {
146 match value.to_ascii_uppercase().as_str() {
147 "STK" => Ok(Self::Stock),
148 "OPT" => Ok(Self::Option),
149 "FUT" => Ok(Self::Future),
150 "CONTFUT" => Ok(Self::ContinuousFuture),
151 "IND" => Ok(Self::Index),
152 "FOP" => Ok(Self::FuturesOption),
153 "CASH" => Ok(Self::ForexPair),
154 "BAG" => Ok(Self::Spread),
155 "WAR" => Ok(Self::Warrant),
156 "BOND" => Ok(Self::Bond),
157 "CMDTY" => Ok(Self::Commodity),
158 "NEWS" => Ok(Self::News),
159 "FUND" => Ok(Self::MutualFund),
160 "CRYPTO" => Ok(Self::Crypto),
161 "CFD" => Ok(Self::Cfd),
162 _ => anyhow::bail!("Unknown IB security type: {value}"),
163 }
164 }
165}
166
167impl Display for IbSecurityType {
168 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
169 f.write_str(self.as_str())
170 }
171}
172
173#[derive(Debug, Clone, Copy, PartialEq, Eq)]
175#[cfg_attr(
176 feature = "python",
177 pyo3::pyclass(
178 module = "nautilus_trader.adapters.interactive_brokers",
179 from_py_object,
180 rename_all = "SCREAMING_SNAKE_CASE"
181 )
182)]
183#[cfg_attr(
184 feature = "python",
185 pyo3_stub_gen::derive::gen_stub_pyclass_enum(
186 module = "nautilus_trader.adapters.interactive_brokers"
187 )
188)]
189pub enum IbOptionRight {
190 Call,
192 Put,
194}
195
196impl IbOptionRight {
197 #[must_use]
199 pub const fn as_str(self) -> &'static str {
200 match self {
201 Self::Call => "C",
202 Self::Put => "P",
203 }
204 }
205
206 #[must_use]
208 pub const fn option_kind(self) -> OptionKind {
209 match self {
210 Self::Call => OptionKind::Call,
211 Self::Put => OptionKind::Put,
212 }
213 }
214}
215
216impl FromStr for IbOptionRight {
217 type Err = anyhow::Error;
218
219 fn from_str(value: &str) -> Result<Self, Self::Err> {
220 match value.to_ascii_uppercase().as_str() {
221 "C" | "CALL" => Ok(Self::Call),
222 "P" | "PUT" => Ok(Self::Put),
223 _ => anyhow::bail!("Unknown IB option right: {value}"),
224 }
225 }
226}
227
228impl Display for IbOptionRight {
229 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
230 f.write_str(self.as_str())
231 }
232}