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
use std::collections::{BTreeMap, HashMap};
use crate::{
core::{
collateral::SingleCurveCSADiscountPolicy,
marketdatahandling::{
constructedelementrequest::ConstructedElementRequest,
constructedelementstore::ConstructedElementStore,
marketdata::{MarketData, MarketDataProvider, MarketDataRequest},
},
},
currencies::currency::Currency,
indices::marketindex::MarketIndex,
models::ModelParameters,
quotes::{fixingstore::FixingStore, quote::Level, quotestore::QuoteStore},
rates::bootstrapping::curvespec::CurveSpec,
time::date::Date,
utils::errors::{QSError, Result},
};
/// Manages the context for instrument evaluation, including market data access, quote level preferences,
/// base currency settings, and a list of model parameter sets for multiple model types.
pub struct ContextManager {
/// The quote store provides access to direct market data quotes and reference date information.
quote_store: QuoteStore,
/// The fixing store provides access to historical fixing values for indices and other reference data.
fixing_store: FixingStore,
/// The quote level indicates the preferred type of quote (e.g., bid, ask, mid) to be used for market value extraction during pricing.
quote_level: Level,
/// The discount policy defines the approach for discounting cashflows.
#[allow(dead_code)]
discount_policy: Option<SingleCurveCSADiscountPolicy>,
/// Base currency for reporting results, allowing for consistent presentation of pricing outputs across different instruments and markets.
base_currency: Currency,
/// Model parameters for various models that may be used during pricing, allowing for flexible configuration of model inputs and assumptions.
models: Vec<ModelParameters>,
/// Curve specifications for curve construction.
#[allow(dead_code)]
curve_specs: Vec<CurveSpec>,
/// Constructed market data elements, such as discount curves, dividend curves, volatility surfaces, and simulations, that have been built in response to market data requests. This allows for caching and reuse of constructed elements across multiple pricing operations.
constructed_elements: ConstructedElementStore,
// Pricer configuration settings, such as the base currency for reporting results.
// pricer_config: PricerConfig,
}
impl ContextManager {
/// Creates a new pricing data context.
#[must_use]
pub fn new(quote_store: QuoteStore, fixing_store: FixingStore) -> Self {
Self {
quote_store,
fixing_store,
quote_level: Level::Mid,
discount_policy: None,
models: Vec::new(),
base_currency: Currency::USD,
curve_specs: Vec::new(),
constructed_elements: ConstructedElementStore::default(),
}
}
/// Sets the quote level used for market value extraction.
#[must_use]
pub const fn with_quote_level(mut self, quote_level: Level) -> Self {
self.quote_level = quote_level;
self
}
/// Returns the market data provider.
#[must_use]
pub const fn quote_store(&self) -> &QuoteStore {
&self.quote_store
}
/// Returns the fixings provider.
#[must_use]
pub const fn fixing_store(&self) -> &FixingStore {
&self.fixing_store
}
/// Returns the quote level preference.
#[must_use]
pub const fn quote_level(&self) -> Level {
self.quote_level
}
/// Returns the base currency for reporting.
#[must_use]
pub const fn base_currency(&self) -> Currency {
self.base_currency
}
/// Sets the base currency.
#[must_use]
pub const fn with_base_currency(mut self, base_currency: Currency) -> Self {
self.base_currency = base_currency;
self
}
/// Returns the current reference date.
#[must_use]
pub const fn evaluation_date(&self) -> Date {
self.quote_store.reference_date()
}
/// Sets the constructed elements store, replacing any previously registered elements.
#[must_use]
pub fn with_constructed_elements(
mut self,
constructed_elements: ConstructedElementStore,
) -> Self {
self.constructed_elements = constructed_elements;
self
}
/// Sets the model parameter list, replacing any previously registered models.
#[must_use]
pub fn with_models(mut self, models: &[ModelParameters]) -> Self {
models.clone_into(&mut self.models);
self
}
/// Returns the full list of model parameters registered in this context.
#[must_use]
pub fn models(&self) -> &[ModelParameters] {
&self.models
}
}
impl MarketDataProvider for ContextManager {
fn evaluation_date(&self) -> Date {
self.quote_store.reference_date()
}
fn handle_request(&self, request: &MarketDataRequest) -> Result<MarketData> {
// 1. Resolve constructed elements from the internal store.
let mut constructed_elements = ConstructedElementStore::default();
if let Some(element_requests) = request.constructed_elements_request() {
for req in element_requests {
match req {
ConstructedElementRequest::DiscountCurve { market_index } => {
let curve = self
.constructed_elements
.discount_curve(market_index)
.ok_or_else(|| {
QSError::NotFoundErr(format!(
"Discount curve not found for index {market_index}"
))
})?;
constructed_elements
.discount_curves_mut()
.insert(market_index.clone(), curve.clone());
}
ConstructedElementRequest::DividendCurve { market_index } => {
let curve = self
.constructed_elements
.dividend_curve(market_index)
.ok_or_else(|| {
QSError::NotFoundErr(format!(
"Dividend curve not found for index {market_index}"
))
})?;
constructed_elements
.dividend_curves_mut()
.insert(market_index.clone(), curve.clone());
}
ConstructedElementRequest::VolatilitySurface { market_index } => {
let surface = self
.constructed_elements
.volatility_surface(market_index)
.ok_or_else(|| {
QSError::NotFoundErr(format!(
"Volatility surface not found for index {market_index}"
))
})?;
constructed_elements
.volatility_surfaces_mut()
.insert(market_index.clone(), surface.clone());
}
ConstructedElementRequest::VolatilityCube { market_index } => {
let cube = self
.constructed_elements
.volatility_cube(market_index)
.ok_or_else(|| {
QSError::NotFoundErr(format!(
"Volatility cube not found for index {market_index}"
))
})?;
constructed_elements
.volatility_cubes_mut()
.insert(market_index.clone(), cube.clone());
}
ConstructedElementRequest::Simulation { market_index } => {
let sim = self
.constructed_elements
.simulations()
.get(market_index)
.ok_or_else(|| {
QSError::NotFoundErr(format!(
"Simulation not found for index {market_index}"
))
})?;
constructed_elements
.simulations_mut()
.insert(market_index.clone(), sim.clone());
}
}
}
}
// 2. Resolve fixings from the fixing store.
let mut fixings: HashMap<MarketIndex, BTreeMap<Date, f64>> = HashMap::new();
if let Some(fixing_requests) = request.fixings_request() {
for fix_req in fixing_requests {
let market_index = fix_req.market_index();
let date = fix_req.date();
let value = self.fixing_store.fixing(market_index, date)?;
fixings
.entry(market_index.clone())
.or_default()
.insert(date, value);
}
}
// 3. Assemble final MarketData with models from this context.
Ok(MarketData::new(fixings, constructed_elements, &self.models))
}
}