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
use crateOptions;
use crate;
use crateblack_76;
use crateblack_scholes;
use crategarman_kohlhagen;
use crateSimulator;
use Positive;
/// Pricing engine selector for option pricing.
///
/// This enum allows selection between different pricing methods:
/// - `ClosedFormBS`: Uses the Black-Scholes closed-form formula
/// - `ClosedFormBlack76`: Uses the Black-76 closed-form formula
/// - `MonteCarlo`: Uses Monte Carlo simulation with a configured simulator
/// - `ClosedFormGK`: Uses the Garman-Kohlhagen closed-form formula for FX options
/// Prices an option using the specified pricing engine.
///
/// This is the unified entry point for option pricing that dispatches to
/// the appropriate pricing method based on the engine configuration.
///
/// # Arguments
///
/// * `option` - The option to price
/// * `engine` - The pricing engine to use
///
/// # Returns
///
/// Returns the option price as a `Positive` value, or a `PricingError` if pricing fails.
///
/// # Examples
///
/// ```rust
/// use optionstratlib::pricing::{PricingEngine, price_option};
/// use positive::{Positive, pos_or_panic};
/// use optionstratlib::{ExpirationDate, Options};
/// use optionstratlib::model::types::{OptionStyle, OptionType, Side};
/// use rust_decimal_macros::dec;
///
/// let option = Options {
/// option_type: OptionType::European,
/// side: Side::Long,
/// underlying_symbol: "AAPL".to_string(),
/// strike_price: Positive::HUNDRED,
/// expiration_date: ExpirationDate::Days(pos_or_panic!(30.0)),
/// implied_volatility: pos_or_panic!(0.2),
/// quantity: Positive::ONE,
/// underlying_price: pos_or_panic!(105.0),
/// risk_free_rate: dec!(0.05),
/// option_style: OptionStyle::Call,
/// dividend_yield: pos_or_panic!(0.01),
/// exotic_params: None,
/// };
/// let engine = PricingEngine::ClosedFormBS;
/// let price = price_option(&option, &engine)?;
/// Ok::<(), optionstratlib::error::PricingError>(())
/// ```
///
/// # Errors
///
/// Propagates the original `PricingError` returned by the selected engine
/// without wrapping, so callers can pattern-match on the structured
/// variants. From the closed-form engines (Black–Scholes, Black-76,
/// Garman–Kohlhagen) you may receive [`PricingError::ExpirationDate`],
/// [`PricingError::Greeks`] (for example zero-volatility or non-finite
/// intermediate values bubbled up from `d1`/`d2`), and (Black-76 and
/// Garman–Kohlhagen) [`PricingError::UnsupportedOptionType`] for
/// non-European inputs. From the binomial lattice you may receive
/// [`PricingError::BinomialNodeMissing`] or [`PricingError::SqrtFailure`].
/// The Monte Carlo engine surfaces failures as
/// [`PricingError::SimulationError`], and exotic engines surface their
/// own variants (barrier, binary, compound, chooser, cliquet, lookback,
/// telegraph).
/// Trait for types that can be priced using a pricing engine.
///
/// This trait provides a unified interface for pricing financial instruments.
/// Implementation of `Priceable` for `Options`.
///
/// This allows options to be priced using the unified pricing API.