openpit/marketdata/quote.rs
1// Copyright The Pit Project Owners. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// Please see https://github.com/openpitkit and the OWNERS file for details.
17
18use std::time::Duration;
19
20use crate::param::Price;
21
22/// Current market snapshot for an instrument.
23///
24/// Every field is optional: producers publish only the fields they actually
25/// have. How a `Quote` interacts with the slot's previously stored value is
26/// chosen by the publisher when calling the service:
27///
28/// - [`MarketDataService::push`](super::service::MarketDataService::push)
29/// replaces the entire snapshot - any field left as `None` in the new quote
30/// is cleared from the slot.
31/// - [`MarketDataService::push_patch`](super::service::MarketDataService::push_patch)
32/// merges the new quote into the existing snapshot - `None` fields preserve
33/// the prior value, `Some` fields overwrite it.
34///
35/// In either case the slot's publish instant is bumped to the current time.
36///
37/// `#[non_exhaustive]` keeps the door open for further optional fields in
38/// future releases.
39#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
40#[non_exhaustive]
41pub struct Quote {
42 /// Mark price.
43 pub mark: Option<Price>,
44 /// Best-bid price.
45 pub bid: Option<Price>,
46 /// Best-ask price.
47 pub ask: Option<Price>,
48}
49
50impl Quote {
51 /// Creates an empty quote with all fields unset.
52 pub fn new() -> Self {
53 Self::default()
54 }
55
56 /// Sets the mark price.
57 pub fn with_mark(mut self, mark: Price) -> Self {
58 self.mark = Some(mark);
59 self
60 }
61
62 /// Sets the best-bid price.
63 pub fn with_bid(mut self, bid: Price) -> Self {
64 self.bid = Some(bid);
65 self
66 }
67
68 /// Sets the best-ask price.
69 pub fn with_ask(mut self, ask: Price) -> Self {
70 self.ask = Some(ask);
71 self
72 }
73
74 /// Merges `patch` into `self`: every `Some` field of `patch` overwrites
75 /// the matching field of `self`; every `None` field leaves `self`
76 /// unchanged.
77 pub(crate) fn patched_with(self, patch: Quote) -> Quote {
78 Quote {
79 mark: patch.mark.or(self.mark),
80 bid: patch.bid.or(self.bid),
81 ask: patch.ask.or(self.ask),
82 }
83 }
84}
85
86/// Maximum age allowed for a stored quote before it is treated as
87/// unavailable.
88///
89/// `QuoteTtl` is the public, two-state lifetime callers supply at the setter
90/// and registration boundaries. It maps onto the internal cascade as follows:
91///
92/// - As the service-wide default on
93/// [`MarketDataBuilder`](super::builder::MarketDataBuilder) it is the lowest
94/// cascade tier, applied only when no more specific axis is set.
95/// - At registration via
96/// [`register_with_ttl`](super::service::MarketDataService::register_with_ttl)
97/// /
98/// [`register_with_id_and_ttl`](super::service::MarketDataService::register_with_id_and_ttl)
99/// it becomes the instrument-level setting.
100/// - The per-account, per-group, and instrument-qualified setters
101/// (`set_*_ttl`) pin the matching axis cell.
102///
103/// The effective lifetime for a read is resolved by the cascade for the
104/// requested `(account, group)`; see
105/// [`MarketDataService`](super::service::MarketDataService) for the tier
106/// order. After a successful [`push`](super::service::MarketDataService::push)
107/// or [`push_patch`](super::service::MarketDataService::push_patch) the quote
108/// is observable through [`get`](super::service::MarketDataService::get) until
109/// at least the effective lifetime has elapsed; reads after that point return
110/// an expired-quote error (the entry is not removed from storage, only hidden
111/// from optional consumers).
112#[derive(Clone, Copy, Debug, PartialEq, Eq)]
113pub enum QuoteTtl {
114 /// Quotes never expire on their own; only
115 /// [`clear`](super::service::MarketDataService::clear) or a new push can
116 /// change visibility.
117 Infinite,
118 /// Quotes expire `duration` after the push that wrote them.
119 Within(Duration),
120}
121
122impl QuoteTtl {
123 /// Returns the per-quote lifetime, if finite.
124 pub fn as_duration(self) -> Option<Duration> {
125 match self {
126 Self::Infinite => None,
127 Self::Within(d) => Some(d),
128 }
129 }
130}