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
pub mod marketdata;
pub mod dependencies;
pub mod cache;
pub mod bumptime;
pub mod deltagamma;
pub mod timebumped;
pub mod vegavolga;
use risk::timebumped::{TimeBumpedReportGenerator, TimeBumpedReport};
use risk::deltagamma::{DeltaGammaReportGenerator, DeltaGammaReport};
use risk::vegavolga::{VegaVolgaReportGenerator, VegaVolgaReport};
use core::qm;
use core::factories::{Qrc, Qbox, TypeId, Registry};
use data::bump::Bump;
use risk::bumptime::BumpTime;
use risk::marketdata::MarketData;
use instruments::PricingContext;
use risk::dependencies::DependencyCollector;
use erased_serde as esd;
use serde as sd;
use serde_tagged as sdt;
use serde_tagged::de::BoxFnSeed;
use std::fmt::Debug;
use std::any::Any;
use std::fmt;
use std::ops::Deref;
use math::numerics::ApproxEq;
pub trait Bumpable {
fn bump(&mut self, bump: &Bump, save: Option<&mut Saveable>)
-> Result<bool, qm::Error>;
fn dependencies(&self) -> Result<&DependencyCollector, qm::Error>;
fn context(&self) -> &PricingContext;
fn new_saveable(&self) -> Box<Saveable>;
fn restore(&mut self, saved: &Saveable) -> Result<(), qm::Error>;
}
pub trait BumpablePricingContext: Bumpable + PricingContext + BumpablePricingContextClone {
fn as_bumpable(&self) -> &Bumpable;
fn as_mut_bumpable(&mut self) -> &mut Bumpable;
fn as_pricing_context(&self) -> &PricingContext;
fn raw_market_data(&self) -> &MarketData;
}
pub trait BumpablePricingContextClone {
fn clone_box(&self) -> Box<BumpablePricingContext>;
}
impl<T> BumpablePricingContextClone for T
where T: 'static + BumpablePricingContext + Clone,
{
fn clone_box(&self) -> Box<BumpablePricingContext> {
Box::new(self.clone())
}
}
impl Clone for Box<BumpablePricingContext> {
fn clone(&self) -> Box<BumpablePricingContext> {
self.clone_box()
}
}
pub trait TimeBumpable {
fn bump_time(&mut self, bump: &BumpTime) -> Result<(), qm::Error>;
}
pub trait Pricer : Bumpable + TimeBumpable + PricerClone {
fn as_bumpable(&self) -> &Bumpable;
fn as_mut_bumpable(&mut self) -> &mut Bumpable;
fn as_mut_time_bumpable(&mut self) -> &mut TimeBumpable;
fn price(&self ) -> Result<f64, qm::Error>;
}
pub trait PricerClone {
fn clone_box(&self) -> Box<Pricer>;
}
pub trait Saveable : Any {
fn as_any(&self) -> &Any;
fn as_mut_any(&mut self) -> &mut Any;
fn clear(&mut self);
}
pub trait Report : esd::Serialize + ApproxEqReport + TypeId + Debug + Any {
fn as_any(&self) -> &Any;
}
pub trait ApproxEqReport {
fn validate_report(&self, other: &Report, tol: &ReportTolerances,
msg: &str, diffs: &mut fmt::Formatter) -> fmt::Result;
}
pub struct ReportTolerances {
price: f64,
currency_risk: f64,
unit_risk: f64
}
impl ReportTolerances {
pub fn new(price: f64, currency_risk: f64, unit_risk: f64) -> ReportTolerances {
ReportTolerances { price, currency_risk, unit_risk }
}
pub fn price(&self) -> f64 { self.price }
pub fn currency_risk(&self) -> f64 { self.currency_risk }
pub fn unit_risk(&self) -> f64 { self.unit_risk }
}
pub trait ReportGenerator : esd::Serialize + TypeId + Sync + Send + Debug {
fn generate(&self, pricer: &mut Pricer, saveable: &mut Saveable, unbumped: f64)
-> Result<BoxReport, qm::Error>;
}
pub type RcReportGenerator = Qrc<ReportGenerator>;
pub type GeneratorTypeRegistry = Registry<BoxFnSeed<Qrc<ReportGenerator>>>;
impl<'de> sd::Deserialize<'de> for Qrc<ReportGenerator> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: sd::Deserializer<'de>
{
sdt::de::external::deserialize(deserializer, get_generator_registry())
}
}
pub fn get_generator_registry() -> &'static GeneratorTypeRegistry {
lazy_static! {
static ref REG: GeneratorTypeRegistry = {
let mut reg = GeneratorTypeRegistry::new();
reg.insert("DeltaGammaReportGenerator", BoxFnSeed::new(DeltaGammaReportGenerator::from_serial));
reg.insert("VegaVolgaReportGenerator", BoxFnSeed::new(VegaVolgaReportGenerator::from_serial));
reg.insert("TimeBumpedReportGenerator", BoxFnSeed::new(TimeBumpedReportGenerator::from_serial));
reg
};
}
®
}
pub type BoxReport = Qbox<Report>;
pub type ReportTypeRegistry = Registry<BoxFnSeed<BoxReport>>;
impl<'de> sd::Deserialize<'de> for BoxReport {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: sd::Deserializer<'de>
{
sdt::de::external::deserialize(deserializer, get_report_registry())
}
}
impl<'v> ApproxEq<ReportTolerances, &'v BoxReport> for &'v BoxReport {
fn validate(self, other: &'v BoxReport, tol: &ReportTolerances,
msg: &str, diffs: &mut fmt::Formatter) -> fmt::Result {
let self_report : &Report = self.deref();
let other_report : &Report = other.deref();
self_report.validate_report(other_report, tol, msg, diffs)
}
}
impl<'v> ApproxEq<ReportTolerances, &'v [BoxReport]> for &'v [BoxReport] {
fn validate(self, other: &'v [BoxReport], tol: &ReportTolerances,
msg: &str, diffs: &mut fmt::Formatter) -> fmt::Result {
if self.len() != other.len() {
write!(diffs, "Slice: length {} != {}", self.len(), other.len())?;
}
for (self_item, other_item) in self.iter().zip(other.iter()) {
self_item.validate(other_item, tol, msg, diffs)?;
}
Ok(())
}
}
pub fn get_report_registry() -> &'static ReportTypeRegistry {
lazy_static! {
static ref REG: ReportTypeRegistry = {
let mut reg = ReportTypeRegistry::new();
reg.insert("DeltaGammaReport", BoxFnSeed::new(DeltaGammaReport::from_serial));
reg.insert("VegaVolgaReport", BoxFnSeed::new(VegaVolgaReport::from_serial));
reg.insert("TimeBumpedReport", BoxFnSeed::new(TimeBumpedReport::from_serial));
reg
};
}
®
}
pub fn bumped_price(bump: &Bump, pricer: &mut Pricer, saveable: Option<&mut Saveable>, unbumped: f64)
-> Result<f64, qm::Error> {
if pricer.as_mut_bumpable().bump(bump, saveable)? {
pricer.price()
} else {
Ok(unbumped)
}
}