use nom::number::streaming::be_u64;
use nsdq_util::{
define_enum,
StockSymbol,
Price,
NaiveTime,
parse_itch_time_bold,
parse_bool,
};
define_enum!{
SystemEvent:
"System events that can occur on the TotalView-ITCH data feed.";
['O'] BeginMessages
"Start of Messages. \n \
This is the first message sent in any trading day. \
(With the exception of Timestamp messages.)",
['S'] BeginSystemHours
"Start of System hours. \n \
NASDAQ is open and ready to start accepting orders.",
['Q'] BeginMarketHours
"Start of Market hours. \n \
Market Hours orders are available for execution.",
['M'] EndMarketHours
"End of Market hours.
Market Hours orders are no longer available for execution.",
['E'] EndSystemHours
"End of System hours. \n \
NASDAQ has closed and will not accept any new orders.
NOTE: It is still possible to receive BrokenTrade and OrderDelete
messages after the End of Day.",
['C'] EndMessages
"End of Messages. \n \
This is always the last message sent in any trading day.",
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NetOrderImbalance {
pub paired_shares: u64,
pub imbalance_shares: u64,
pub imbalance_direction: ImbalanceDirection,
pub stock: StockSymbol,
pub far_price: Price<u32, 4>,
pub near_price: Price<u32, 4>,
pub ref_price: Price<u32, 4>,
pub cross_type: ImbalanceCrossType,
pub price_variation: PriceVariation,
}
impl NetOrderImbalance {
pub(crate) fn parse(input: &[u8]) -> nom::IResult<&[u8], Self> {
let (input, paired_shares) = be_u64(input)?;
let (input, imbalance_shares) = be_u64(input)?;
let (input, imbalance_direction) = ImbalanceDirection::parse(input)?;
let (input, stock) = StockSymbol::parse(input)?;
let (input, far_price) = Price::<u32, 4>::parse(input)?;
let (input, near_price) = Price::<u32, 4>::parse(input)?;
let (input, ref_price) = Price::<u32, 4>::parse(input)?;
let (input, cross_type) = ImbalanceCrossType::parse(input)?;
let (input, price_variation) = PriceVariation::parse(input)?;
Ok((input, Self {
paired_shares,
imbalance_shares,
imbalance_direction,
stock,
far_price,
near_price,
ref_price,
cross_type,
price_variation,
}))
}
}
define_enum!{
ImbalanceDirection:
"The market side of the order imbalance.";
['B'] Buy,
['S'] Sell,
['N'] NoImbalance,
['O'] Uncalculated,
['P'] Paused,
}
define_enum!{
ImbalanceCrossType: "";
['O'] Opening,
['C'] Closing,
['H'] Halt,
['A'] ExtendedClose,
}
define_enum!{
PriceVariation:
"Absolute value of the percentage of deviation of the \
Near Indicative Clearing Price to the nearest Current Reference Price.";
['L'] Zero
"Less than 1%",
['1'] One
"1 to 1.99%",
['2'] Two
"2 to 2.99%",
['3'] Three
"3 to 3.99%",
['4'] Four
"4 to 4.99%",
['5'] Five
"5 to 5.99%",
['6'] Six
"6 to 6.99%",
['7'] Seven
"7 to 7.99%",
['8'] Eight
"8 to 8.99%",
['9'] Nine
"9 to 9.99%",
['A'] Ten
"10 to 19.99%",
['B'] Twenty
"20 to 29.99%",
['C'] ThirtyUp
"30% or greater",
[' '] Uncalculated
"Cannot be calculated",
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RetailPriceImprovement {
pub stock: StockSymbol,
pub interest_flag: InterestFlag
}
impl RetailPriceImprovement {
pub(crate) fn parse(input: &[u8]) -> nom::IResult<&[u8], Self> {
let (input, stock) = StockSymbol::parse(input)?;
let (input, interest_flag) = InterestFlag::parse(input)?;
Ok((input, Self { stock, interest_flag }))
}
}
define_enum!{
InterestFlag:
"Availability of Retail Price Improvement orders.";
['B'] BuyAvailable
"RPI orders available on the buy side.",
['S'] SellAvailable
"RPI orders available on the sell side.",
['A'] AnyAvailable
"RPI orders available on both sides (buy and sell).",
['N'] NoneAvailable
"No RPI orders available.",
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DirectListingWithCapitalRaise {
pub stock: StockSymbol,
pub eligibility: bool,
pub min_price: Price<u32, 4>,
pub max_price: Price<u32, 4>,
pub near_exec_price: Price<u32, 4>,
pub near_exec_time: NaiveTime,
pub lower_collar: Price<u32, 4>,
pub upper_collar: Price<u32, 4>,
}
impl DirectListingWithCapitalRaise {
pub(crate) fn parse(input: &[u8]) -> nom::IResult<&[u8], Self> {
let (input, stock) = StockSymbol::parse(input)?;
let (input, eligibility) = parse_bool(input)?;
let (input, min_price) = Price::<u32, 4>::parse(input)?;
let (input, max_price) = Price::<u32, 4>::parse(input)?;
let (input, near_exec_price) = Price::<u32, 4>::parse(input)?;
let (input, near_exec_time) = parse_itch_time_bold(input)?;
let (input, lower_collar) = Price::<u32, 4>::parse(input)?;
let (input, upper_collar) = Price::<u32, 4>::parse(input)?;
Ok((input, Self {
stock,
eligibility,
min_price,
max_price,
near_exec_price,
near_exec_time,
lower_collar,
upper_collar,
}))
}
}