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://openpit.dev 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 that exist in
25/// one observation. A publication always replaces the stored snapshot, so an
26/// absent field clears the previous value rather than retaining it. A publisher
27/// that needs to combine observations must merge them itself, because it owns
28/// both the observation relationship and the combined source age.
29///
30/// The service records the current monotonic publish instant and the source age
31/// supplied by the publisher. Reads advance that source age by the elapsed time
32/// since publication when evaluating freshness.
33///
34/// `#[non_exhaustive]` keeps the door open for further optional fields in
35/// future releases.
36#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
37#[non_exhaustive]
38pub struct Quote {
39 /// Mark price.
40 pub mark: Option<Price>,
41 /// Best-bid price.
42 pub bid: Option<Price>,
43 /// Best-ask price.
44 pub ask: Option<Price>,
45}
46
47impl Quote {
48 /// Creates an empty quote with all fields unset.
49 pub fn new() -> Self {
50 Self::default()
51 }
52
53 /// Sets the mark price.
54 pub fn with_mark(mut self, mark: Price) -> Self {
55 self.mark = Some(mark);
56 self
57 }
58
59 /// Sets the best-bid price.
60 pub fn with_bid(mut self, bid: Price) -> Self {
61 self.bid = Some(bid);
62 self
63 }
64
65 /// Sets the best-ask price.
66 pub fn with_ask(mut self, ask: Price) -> Self {
67 self.ask = Some(ask);
68 self
69 }
70}
71
72/// Maximum age allowed for a stored quote before it is treated as
73/// unavailable.
74///
75/// `QuoteTtl` is the public, two-state lifetime callers supply at the setter
76/// and registration boundaries. It maps onto the internal cascade as follows:
77///
78/// - As the service-wide default on
79/// [`MarketDataBuilder`](super::builder::MarketDataBuilder) it is the lowest
80/// cascade tier, applied only when no more specific axis is set.
81/// - At registration via
82/// [`register_with_ttl`](super::service::MarketDataService::register_with_ttl)
83/// /
84/// [`register_with_id_and_ttl`](super::service::MarketDataService::register_with_id_and_ttl)
85/// it becomes the instrument-level setting.
86/// - The per-account, per-group, and instrument-qualified setters
87/// (`set_*_ttl`) pin the matching axis cell.
88///
89/// The effective lifetime for a read is resolved by the cascade for the
90/// requested `(account, group)`; see
91/// [`MarketDataService`](super::service::MarketDataService) for the tier
92/// order. A publication supplies the quote's age at the source. The quote is
93/// observable through [`get`](super::service::MarketDataService::get) only
94/// while that source age plus the elapsed time since publication remains below
95/// the effective lifetime. Expired entries remain stored and are returned in
96/// the expired-quote error.
97#[derive(Clone, Copy, Debug, PartialEq, Eq)]
98pub enum QuoteTtl {
99 /// Quotes never expire on their own; only
100 /// [`clear`](super::service::MarketDataService::clear) or a new push can
101 /// change visibility.
102 Infinite,
103 /// Quotes expire when their source age plus time elapsed since publication
104 /// reaches `duration`.
105 Within(Duration),
106}
107
108impl QuoteTtl {
109 /// Returns the per-quote lifetime, if finite.
110 pub fn as_duration(self) -> Option<Duration> {
111 match self {
112 Self::Infinite => None,
113 Self::Within(d) => Some(d),
114 }
115 }
116}