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.core.nautilus_pyo3.interactive_brokers",
26 from_py_object
27 )
28)]
29pub enum IbSecurityType {
30 Stock,
32 Option,
34 Future,
36 ContinuousFuture,
38 Index,
40 FuturesOption,
42 ForexPair,
44 Spread,
46 Warrant,
48 Bond,
50 Commodity,
52 News,
54 MutualFund,
56 Crypto,
58 Cfd,
60}
61
62impl IbSecurityType {
63 #[must_use]
65 pub const fn as_str(self) -> &'static str {
66 match self {
67 Self::Stock => "STK",
68 Self::Option => "OPT",
69 Self::Future => "FUT",
70 Self::ContinuousFuture => "CONTFUT",
71 Self::Index => "IND",
72 Self::FuturesOption => "FOP",
73 Self::ForexPair => "CASH",
74 Self::Spread => "BAG",
75 Self::Warrant => "WAR",
76 Self::Bond => "BOND",
77 Self::Commodity => "CMDTY",
78 Self::News => "NEWS",
79 Self::MutualFund => "FUND",
80 Self::Crypto => "CRYPTO",
81 Self::Cfd => "CFD",
82 }
83 }
84
85 #[must_use]
87 pub const fn ibapi_security_type(self) -> ibapi::contracts::SecurityType {
88 match self {
89 Self::Stock => ibapi::contracts::SecurityType::Stock,
90 Self::Option => ibapi::contracts::SecurityType::Option,
91 Self::Future => ibapi::contracts::SecurityType::Future,
92 Self::ContinuousFuture => ibapi::contracts::SecurityType::ContinuousFuture,
93 Self::Index => ibapi::contracts::SecurityType::Index,
94 Self::FuturesOption => ibapi::contracts::SecurityType::FuturesOption,
95 Self::ForexPair => ibapi::contracts::SecurityType::ForexPair,
96 Self::Spread => ibapi::contracts::SecurityType::Spread,
97 Self::Warrant => ibapi::contracts::SecurityType::Warrant,
98 Self::Bond => ibapi::contracts::SecurityType::Bond,
99 Self::Commodity => ibapi::contracts::SecurityType::Commodity,
100 Self::News => ibapi::contracts::SecurityType::News,
101 Self::MutualFund => ibapi::contracts::SecurityType::MutualFund,
102 Self::Crypto => ibapi::contracts::SecurityType::Crypto,
103 Self::Cfd => ibapi::contracts::SecurityType::CFD,
104 }
105 }
106}
107
108impl TryFrom<&ibapi::contracts::SecurityType> for IbSecurityType {
109 type Error = anyhow::Error;
110
111 fn try_from(value: &ibapi::contracts::SecurityType) -> Result<Self, Self::Error> {
112 match value {
113 ibapi::contracts::SecurityType::Stock => Ok(Self::Stock),
114 ibapi::contracts::SecurityType::Option => Ok(Self::Option),
115 ibapi::contracts::SecurityType::Future => Ok(Self::Future),
116 ibapi::contracts::SecurityType::ContinuousFuture => Ok(Self::ContinuousFuture),
117 ibapi::contracts::SecurityType::Index => Ok(Self::Index),
118 ibapi::contracts::SecurityType::FuturesOption => Ok(Self::FuturesOption),
119 ibapi::contracts::SecurityType::ForexPair => Ok(Self::ForexPair),
120 ibapi::contracts::SecurityType::Spread => Ok(Self::Spread),
121 ibapi::contracts::SecurityType::Warrant => Ok(Self::Warrant),
122 ibapi::contracts::SecurityType::Bond => Ok(Self::Bond),
123 ibapi::contracts::SecurityType::Commodity => Ok(Self::Commodity),
124 ibapi::contracts::SecurityType::News => Ok(Self::News),
125 ibapi::contracts::SecurityType::MutualFund => Ok(Self::MutualFund),
126 ibapi::contracts::SecurityType::Crypto => Ok(Self::Crypto),
127 ibapi::contracts::SecurityType::CFD => Ok(Self::Cfd),
128 ibapi::contracts::SecurityType::Other(value) => {
129 anyhow::bail!("Unknown IB security type: {value}")
130 }
131 }
132 }
133}
134
135impl FromStr for IbSecurityType {
136 type Err = anyhow::Error;
137
138 fn from_str(value: &str) -> Result<Self, Self::Err> {
139 match value.to_ascii_uppercase().as_str() {
140 "STK" => Ok(Self::Stock),
141 "OPT" => Ok(Self::Option),
142 "FUT" => Ok(Self::Future),
143 "CONTFUT" => Ok(Self::ContinuousFuture),
144 "IND" => Ok(Self::Index),
145 "FOP" => Ok(Self::FuturesOption),
146 "CASH" => Ok(Self::ForexPair),
147 "BAG" => Ok(Self::Spread),
148 "WAR" => Ok(Self::Warrant),
149 "BOND" => Ok(Self::Bond),
150 "CMDTY" => Ok(Self::Commodity),
151 "NEWS" => Ok(Self::News),
152 "FUND" => Ok(Self::MutualFund),
153 "CRYPTO" => Ok(Self::Crypto),
154 "CFD" => Ok(Self::Cfd),
155 _ => anyhow::bail!("Unknown IB security type: {value}"),
156 }
157 }
158}
159
160impl Display for IbSecurityType {
161 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
162 f.write_str(self.as_str())
163 }
164}
165
166#[derive(Debug, Clone, Copy, PartialEq, Eq)]
168#[cfg_attr(
169 feature = "python",
170 pyo3::pyclass(
171 module = "nautilus_trader.core.nautilus_pyo3.interactive_brokers",
172 from_py_object
173 )
174)]
175pub enum IbOptionRight {
176 Call,
178 Put,
180}
181
182impl IbOptionRight {
183 #[must_use]
185 pub const fn as_str(self) -> &'static str {
186 match self {
187 Self::Call => "C",
188 Self::Put => "P",
189 }
190 }
191
192 #[must_use]
194 pub const fn option_kind(self) -> OptionKind {
195 match self {
196 Self::Call => OptionKind::Call,
197 Self::Put => OptionKind::Put,
198 }
199 }
200}
201
202impl FromStr for IbOptionRight {
203 type Err = anyhow::Error;
204
205 fn from_str(value: &str) -> Result<Self, Self::Err> {
206 match value.to_ascii_uppercase().as_str() {
207 "C" | "CALL" => Ok(Self::Call),
208 "P" | "PUT" => Ok(Self::Put),
209 _ => anyhow::bail!("Unknown IB option right: {value}"),
210 }
211 }
212}
213
214impl Display for IbOptionRight {
215 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
216 f.write_str(self.as_str())
217 }
218}