1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
#![allow(deprecated)] // TODO: remove with SType::Smart
//! Enums used in Databento APIs.
use std::fmt::{self, Display, Formatter};
use num_enum::{IntoPrimitive, TryFromPrimitive};
use serde::Serialize;
use crate::error::ConversionError;
/// A side of the market. The side of the market for resting orders, or the side
/// of the aggressor for trades.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum Side {
/// A sell order.
Ask = b'A',
/// A buy order.
Bid = b'B',
/// None or unknown.
None = b'N',
}
impl From<Side> for char {
fn from(side: Side) -> Self {
u8::from(side) as char
}
}
impl Serialize for Side {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_char(char::from(*self))
}
}
/// A tick action.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum Action {
/// An existing order was modified.
Modify = b'M',
/// A trade executed.
Trade = b'T',
/// An order was cancelled.
Cancel = b'C',
/// A new order was added.
Add = b'A',
/// Reset the book; clear all orders for an instrument.
Clear = b'R',
}
impl From<Action> for char {
fn from(action: Action) -> Self {
u8::from(action) as char
}
}
impl serde::Serialize for Action {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_char(char::from(*self))
}
}
/// The class of instrument.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum InstrumentClass {
/// A bond.
Bond = b'B',
/// A call option.
Call = b'C',
/// A future.
Future = b'F',
/// A stock.
Stock = b'K',
/// A spread composed of multiple instrument classes.
MixedSpread = b'M',
/// A put option.
Put = b'P',
/// A spread composed of futures.
FutureSpread = b'S',
/// A spread composed of options.
OptionSpread = b'T',
/// A foreign exchange spot.
FxSpot = b'X',
}
impl From<InstrumentClass> for char {
fn from(class: InstrumentClass) -> Self {
u8::from(class) as char
}
}
impl serde::Serialize for InstrumentClass {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_char(char::from(*self))
}
}
/// The type of matching algorithm used for the instrument at the exchange.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum MatchAlgorithm {
/// First-in-first-out matching.
Fifo = b'F',
/// A configurable match algorithm.
Configurable = b'K',
/// Trade quantity is allocated to resting orders based on a pro-rata percentage:
/// resting order quantity divided by total quantity.
ProRata = b'C',
/// Like [`Self::Fifo`] but with LMM allocations prior to FIFO allocations.
FifoLmm = b'T',
/// Like [`Self::ProRata`] but includes a configurable allocation to the first order that
/// improves the market.
ThresholdProRata = b'O',
/// Like [`Self::FifoLmm`] but includes a configurable allocation to the first order that
/// improves the market.
FifoTopLmm = b'S',
/// Like [`Self::ThresholdProRata`] but includes a special priority to LMMs.
ThresholdProRataLmm = b'Q',
/// Special variant used only for Eurodollar futures on CME. See
/// [CME documentation](https://www.cmegroup.com/confluence/display/EPICSANDBOX/Supported+Matching+Algorithms#SupportedMatchingAlgorithms-Pro-RataAllocationforEurodollarFutures).
EurodollarFutures = b'Y',
}
impl From<MatchAlgorithm> for char {
fn from(algo: MatchAlgorithm) -> Self {
u8::from(algo) as char
}
}
impl serde::Serialize for MatchAlgorithm {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_char(char::from(*self))
}
}
/// Whether the instrument is user-defined.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive, Default)]
#[repr(u8)]
pub enum UserDefinedInstrument {
/// The instrument is not user-defined.
#[default]
No = b'N',
/// The instrument is user-defined.
Yes = b'Y',
}
impl From<UserDefinedInstrument> for char {
fn from(user_defined_instrument: UserDefinedInstrument) -> Self {
u8::from(user_defined_instrument) as char
}
}
impl serde::Serialize for UserDefinedInstrument {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_char(char::from(*self))
}
}
/// A symbology type. Refer to the [symbology documentation](https://docs.databento.com/api-reference-historical/basics/symbology)
/// for more information.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, TryFromPrimitive)]
#[serde(rename_all = "snake_case")]
#[repr(u8)]
pub enum SType {
/// Symbology using a unique numeric ID.
InstrumentId = 0,
/// Symbology using the original symbols provided by the publisher.
RawSymbol = 1,
/// A set of Databento-specific symbologies for referring to groups of symbols.
#[deprecated(since = "0.5.0", note = "Smart was split into Continuous and Parent.")]
Smart = 2,
/// A Databento-specific symbology where one symbol may point to different
/// instruments at different points of time, e.g. to always refer to the front month
/// future.
Continuous = 3,
/// A Databento-specific symbology for referring to a group of symbols by one
/// "parent" symbol, e.g. ES.FUT to refer to all ES futures.
Parent = 4,
}
impl std::str::FromStr for SType {
type Err = ConversionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"instrument_id" | "product_id" => Ok(SType::InstrumentId),
"raw_symbol" | "native" => Ok(SType::RawSymbol),
"smart" => Ok(SType::Smart),
"continuous" => Ok(SType::Continuous),
"parent" => Ok(SType::Parent),
_ => Err(ConversionError::TypeConversion(
"Value doesn't match a valid symbol type",
)),
}
}
}
impl AsRef<str> for SType {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl SType {
/// Convert the symbology type to its `str` representation.
pub const fn as_str(&self) -> &'static str {
match self {
SType::InstrumentId => "instrument_id",
SType::RawSymbol => "raw_symbol",
#[allow(deprecated)]
SType::Smart => "smart",
SType::Continuous => "continuous",
SType::Parent => "parent",
}
}
}
impl Display for SType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
pub use rtype::RType;
/// Record types, possible values for [`RecordHeader::rtype`][crate::record::RecordHeader::rtype]
#[allow(deprecated)]
pub mod rtype {
use num_enum::TryFromPrimitive;
use serde::Serialize;
use super::Schema;
/// A type of record, i.e. a struct implementing [`HasRType`](crate::record::HasRType).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, TryFromPrimitive)]
#[repr(u8)]
pub enum RType {
/// Market by price with a book depth of 0 (used for trades).
Mbp0 = MBP_0,
/// Market by price with a book depth of 1 (also used for TBBO).
Mbp1 = MBP_1,
/// Market by price with a book depth of 10.
Mbp10 = MBP_10,
/// Open, high, low, close, and volume at an unspecified cadence.
#[deprecated(
since = "0.3.3",
note = "Separated into separate rtypes for each OHLCV schema."
)]
OhlcvDeprecated = OHLCV_DEPRECATED,
/// Open, high, low, close, and volume at a 1-second cadence.
Ohlcv1S = OHLCV_1S,
/// Open, high, low, close, and volume at a 1-minute cadence.
Ohlcv1M = OHLCV_1M,
/// Open, high, low, close, and volume at a daily cadence.
Ohlcv1H = OHLCV_1H,
/// Open, high, low, close, and volume at a daily cadence.
Ohlcv1D = OHLCV_1D,
/// Exchange status.
Status = STATUS,
/// Instrument definition.
InstrumentDef = INSTRUMENT_DEF,
/// Order imbalance.
Imbalance = IMBALANCE,
/// Error from gateway.
Error = ERROR,
/// Symbol mapping.
SymbolMapping = SYMBOL_MAPPING,
/// A non-error message. Also used for heartbeats.
System = SYSTEM,
/// Statistics from the publisher (not calculated by Databento).
Statistics = STATISTICS,
/// Market by order.
Mbo = MBO,
}
/// Market by price with a book depth of 0 (used for trades).
pub const MBP_0: u8 = 0x00;
/// Market by price with a book depth of 1 (also used for TBBO).
pub const MBP_1: u8 = 0x01;
/// Market by price with a book depth of 10.
pub const MBP_10: u8 = 0x0A;
/// Open, high, low, close, and volume at an unspecified cadence.
#[deprecated(
since = "0.3.3",
note = "Separated into separate rtypes for each OHLCV schema."
)]
pub const OHLCV_DEPRECATED: u8 = 0x11;
/// Open, high, low, close, and volume at a 1-second cadence.
pub const OHLCV_1S: u8 = 0x20;
/// Open, high, low, close, and volume at a 1-minute cadence.
pub const OHLCV_1M: u8 = 0x21;
/// Open, high, low, close, and volume at an hourly cadence.
pub const OHLCV_1H: u8 = 0x22;
/// Open, high, low, close, and volume at a daily cadence.
pub const OHLCV_1D: u8 = 0x23;
/// Exchange status.
pub const STATUS: u8 = 0x12;
/// Instrument definition.
pub const INSTRUMENT_DEF: u8 = 0x13;
/// Order imbalance.
pub const IMBALANCE: u8 = 0x14;
/// Error from gateway.
pub const ERROR: u8 = 0x15;
/// Symbol mapping.
pub const SYMBOL_MAPPING: u8 = 0x16;
/// A non-error message. Also used for heartbeats.
pub const SYSTEM: u8 = 0x17;
/// Statistics from the publisher (not calculated by Databento).
pub const STATISTICS: u8 = 0x18;
/// Market by order.
pub const MBO: u8 = 0xA0;
/// Get the corresponding `rtype` for the given `schema`.
impl From<Schema> for RType {
fn from(schema: Schema) -> Self {
match schema {
Schema::Mbo => RType::Mbo,
Schema::Mbp1 | Schema::Tbbo => RType::Mbp1,
Schema::Mbp10 => RType::Mbp10,
Schema::Trades => RType::Mbp0,
Schema::Ohlcv1S => RType::Ohlcv1S,
Schema::Ohlcv1M => RType::Ohlcv1M,
Schema::Ohlcv1H => RType::Ohlcv1H,
Schema::Ohlcv1D => RType::Ohlcv1D,
Schema::Definition => RType::InstrumentDef,
Schema::Statistics => RType::Statistics,
Schema::Status => RType::Status,
Schema::Imbalance => RType::Imbalance,
}
}
}
/// Tries to convert the given rtype to a [`Schema`].
///
/// Returns `None` if there's no corresponding `Schema` for the given rtype or
/// in the case of [`OHLCV_DEPRECATED`], it doesn't map to a single `Schema`.
pub fn try_into_schema(rtype: u8) -> Option<Schema> {
match rtype {
MBP_0 => Some(Schema::Trades),
MBP_1 => Some(Schema::Mbp1),
MBP_10 => Some(Schema::Mbp10),
OHLCV_1S => Some(Schema::Ohlcv1S),
OHLCV_1M => Some(Schema::Ohlcv1M),
OHLCV_1H => Some(Schema::Ohlcv1H),
OHLCV_1D => Some(Schema::Ohlcv1D),
STATUS => Some(Schema::Status),
INSTRUMENT_DEF => Some(Schema::Definition),
IMBALANCE => Some(Schema::Imbalance),
STATISTICS => Some(Schema::Statistics),
MBO => Some(Schema::Mbo),
_ => None,
}
}
}
/// A data record schema.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TryFromPrimitive)]
#[repr(u16)]
pub enum Schema {
/// Market by order.
Mbo = 0,
/// Market by price with a book depth of 1.
Mbp1 = 1,
/// Market by price with a book depth of 10.
Mbp10 = 2,
/// All trade events with the best bid and offer (BBO) immediately **before** the
/// effect of the trade.
Tbbo = 3,
/// All trade events.
Trades = 4,
/// Open, high, low, close, and volume at a one-second interval.
Ohlcv1S = 5,
/// Open, high, low, close, and volume at a one-minute interval.
Ohlcv1M = 6,
/// Open, high, low, close, and volume at an hourly interval.
Ohlcv1H = 7,
/// Open, high, low, close, and volume at a daily interval.
Ohlcv1D = 8,
/// Instrument definitions.
Definition = 9,
/// Additional data disseminated by publishers.
Statistics = 10,
/// Exchange status.
#[doc(hidden)]
Status = 11,
/// Auction imbalance events.
Imbalance = 12,
}
/// The number of [`Schema`]s.
pub const SCHEMA_COUNT: usize = 13;
impl std::str::FromStr for Schema {
type Err = ConversionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"mbo" => Ok(Schema::Mbo),
"mbp-1" => Ok(Schema::Mbp1),
"mbp-10" => Ok(Schema::Mbp10),
"tbbo" => Ok(Schema::Tbbo),
"trades" => Ok(Schema::Trades),
"ohlcv-1s" => Ok(Schema::Ohlcv1S),
"ohlcv-1m" => Ok(Schema::Ohlcv1M),
"ohlcv-1h" => Ok(Schema::Ohlcv1H),
"ohlcv-1d" => Ok(Schema::Ohlcv1D),
"definition" => Ok(Schema::Definition),
"statistics" => Ok(Schema::Statistics),
"status" => Ok(Schema::Status),
"imbalance" => Ok(Schema::Imbalance),
_ => Err(ConversionError::TypeConversion(
"Value doesn't match a valid schema",
)),
}
}
}
impl AsRef<str> for Schema {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Schema {
/// Converts the given schema to a `&'static str`.
pub const fn as_str(&self) -> &'static str {
match self {
Schema::Mbo => "mbo",
Schema::Mbp1 => "mbp-1",
Schema::Mbp10 => "mbp-10",
Schema::Tbbo => "tbbo",
Schema::Trades => "trades",
Schema::Ohlcv1S => "ohlcv-1s",
Schema::Ohlcv1M => "ohlcv-1m",
Schema::Ohlcv1H => "ohlcv-1h",
Schema::Ohlcv1D => "ohlcv-1d",
Schema::Definition => "definition",
Schema::Statistics => "statistics",
Schema::Status => "status",
Schema::Imbalance => "imbalance",
}
}
}
impl Serialize for Schema {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
impl Display for Schema {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
/// A data encoding format.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, TryFromPrimitive)]
#[serde(rename_all = "lowercase")]
#[repr(u8)]
pub enum Encoding {
/// Databento Binary Encoding.
Dbn = 0,
/// Comma-separated values.
Csv = 1,
/// JavaScript object notation.
Json = 2,
}
impl std::str::FromStr for Encoding {
type Err = ConversionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"dbn" | "dbz" => Ok(Encoding::Dbn),
"csv" => Ok(Encoding::Csv),
"json" => Ok(Encoding::Json),
_ => Err(ConversionError::TypeConversion(
"Value doesn't match a valid encoding",
)),
}
}
}
impl AsRef<str> for Encoding {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Encoding {
/// Converts the given encoding to a `&'static str`.
pub const fn as_str(&self) -> &'static str {
match self {
Encoding::Dbn => "dbn",
Encoding::Csv => "csv",
Encoding::Json => "json",
}
}
}
impl Display for Encoding {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
/// A compression format or none if uncompressed.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, TryFromPrimitive)]
#[serde(rename_all = "lowercase")]
#[repr(u8)]
pub enum Compression {
/// Uncompressed.
None = 0,
/// Zstandard compressed.
ZStd = 1,
}
impl std::str::FromStr for Compression {
type Err = ConversionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"none" => Ok(Compression::None),
"zstd" => Ok(Compression::ZStd),
_ => Err(ConversionError::TypeConversion(
"Value doesn't match a valid compression",
)),
}
}
}
impl AsRef<str> for Compression {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Compression {
/// Converts the given compression to a `&'static str`.
pub const fn as_str(&self) -> &'static str {
match self {
Compression::None => "none",
Compression::ZStd => "zstd",
}
}
}
impl Display for Compression {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
/// Constants for the bit flag record fields.
pub mod flags {
/// Indicates it's the last message in the packet from the venue for a given
/// `instrument_id`.
pub const LAST: u8 = 1 << 7;
/// Indicates the message was sourced from a replay, such as a snapshot server.
pub const SNAPSHOT: u8 = 1 << 5;
/// Indicates an aggregated price level message, not an individual order.
pub const MBP: u8 = 1 << 4;
/// Indicates the `ts_recv` value is inaccurate due to clock issues or packet
/// reordering.
pub const BAD_TS_RECV: u8 = 1 << 3;
/// Indicates an unrecoverable gap was detected in the channel.
pub const MAYBE_BAD_BOOK: u8 = 1 << 2;
}
/// The type of [`InstrumentDefMsg`](crate::record::InstrumentDefMsg) update.
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, IntoPrimitive, TryFromPrimitive)]
pub enum SecurityUpdateAction {
/// A new instrument definition.
Add = b'A',
/// A modified instrument definition of an existing one.
Modify = b'M',
/// Removal of an instrument definition.
Delete = b'D',
#[doc(hidden)]
#[deprecated = "Still present in legacy files."]
Invalid = b'~',
}
impl Serialize for SecurityUpdateAction {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_char(char::from(*self as u8))
}
}
/// The type of statistic contained in a [`StatMsg`](crate::record::StatMsg).
#[repr(u16)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, IntoPrimitive, TryFromPrimitive)]
pub enum StatType {
/// The price of the first trade of an instrument. `price` will be set.
OpeningPrice = 1,
/// The probable price of the first trade of an instrument published during pre-
/// open. Both `price` and `quantity` will be set.
IndicativeOpeningPrice = 2,
/// The settlement price of an instrument. `price` will be set and `flags` indicate
/// whether the price is final or preliminary and actual or theoretical.
SettlementPrice = 3,
/// The lowest trade price of an instrument during the trading session. `price` will
/// be set.
TradingSessionLowPrice = 4,
/// The highest trade price of an instrument during the trading session. `price` will
/// be set.
TradingSessionHighPrice = 5,
/// The number of contracts cleared for an instrument on the previous trading date.
/// `quantity` will be set.
ClearedVolume = 6,
/// The lowest offer price for an instrument during the trading session. `price`
/// will be set.
LowestOffer = 7,
/// The highest bid price for an instrument during the trading session. `price`
/// will be set.
HighestBid = 8,
/// The current number of outstanding contracts of an instrument. `quantity` will
// be set.
OpenInterest = 9,
/// The volume-weighted average price (VWAP) for a fixing period. `price` will be
/// set.
FixingPrice = 10,
}
/// The type of [`StatMsg`](crate::record::StatMsg) update.
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, IntoPrimitive, TryFromPrimitive)]
pub enum StatUpdateAction {
/// A new statistic.
New = 1,
/// A removal of a statistic.
Delete = 2,
}