-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathoption.rs
More file actions
211 lines (189 loc) · 5.16 KB
/
Copy pathoption.rs
File metadata and controls
211 lines (189 loc) · 5.16 KB
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
use crate::options::types::*;
use crate::options::Instrument;
use std::any::Any;
use super::OptionPricing;
/// Supertrait that combines OptionPricing and Greeks.
pub trait Option: Clone + Send + Sync {
/// Get the underlying instrument of the option.
///
/// # Returns
///
/// The underlying instrument of the option.
fn instrument(&self) -> &Instrument;
/// Get the underlying instrument of the option (mutable).
///
/// # Returns
///
/// The underlying instrument of the option.
fn instrument_mut(&mut self) -> &mut Instrument;
/// Set the underlying instrument of the option.
///
/// # Arguments
///
/// * `instrument` - The underlying instrument.
fn set_instrument(&mut self, instrument: Instrument);
/// Get the strike price of the option.
///
/// # Returns
///
/// The strike price of the option.
fn strike(&self) -> f64;
/// Time horizon (in years).
///
/// # Returns
///
/// The time horizon (in years).
fn time_to_maturity(&self) -> f64;
/// Get the expiration dates of the option.
///
/// # Returns
///
/// The expiration dates of the option. (Only for Bermudan options)
fn expiration_dates(&self) -> std::option::Option<&Vec<f64>> {
None
}
/// Set the time horizon (in years).
///
/// # Arguments
///
/// * `time_to_maturity` - The time horizon (in years).
fn set_time_to_maturity(&mut self, time_to_maturity: f64);
/// Get the type of the option.
///
/// # Returns
///
/// The type of the option.
fn option_type(&self) -> OptionType;
/// Get the style of the option.
///
/// # Returns
///
/// The style of the option.
fn style(&self) -> OptionStyle;
/// Flip the option type (Call to Put or Put to Call).
///
/// # Returns
///
/// The flipped option.
fn flip(&self) -> Self;
/// Calculate the payoff of the option at maturity.
///
/// # Arguments
///
/// * `spot` - The price of the underlying asset at maturity (optional).
///
/// # Returns
///
/// The payoff of the option.
fn payoff(&self, spot: std::option::Option<f64>) -> f64 {
let spot_price = spot.unwrap_or_else(|| self.instrument().spot());
match self.option_type() {
OptionType::Call => (spot_price - self.strike()).max(0.0),
OptionType::Put => (self.strike() - spot_price).max(0.0),
}
}
/// Calculate the price of the option.
///
/// # Arguments
///
/// * `model` - The pricing model.
///
/// # Returns
///
/// The price of the option.
fn price<T: OptionPricing>(&self, model: T) -> f64 {
model.price(self)
}
/// Calculate the time value of the option.
///
/// # Arguments
///
/// * `spot` - The price of the underlying asset.
/// * `model` - The pricing model.
///
/// # Returns
///
/// The time value of the option.
fn time_value<T: OptionPricing>(&self, model: T) -> f64 {
model.price(self) - self.payoff(None)
}
/// Return the option as a call.
///
/// # Returns
///
/// The option as a call.
fn as_call(&self) -> Self {
if self.is_call() {
self.clone()
} else {
self.flip()
}
}
/// Return the option as a put.
///
/// # Returns
///
/// The option as a put.
fn as_put(&self) -> Self {
if self.is_put() {
self.clone()
} else {
self.flip()
}
}
/// Check if the option is a call.
///
/// # Returns
///
/// True if the option is a call, false otherwise.
fn is_call(&self) -> bool {
matches!(self.option_type(), OptionType::Call)
}
/// Check if the option is a put.
///
/// # Returns
///
/// True if the option is a put, false otherwise.
fn is_put(&self) -> bool {
matches!(self.option_type(), OptionType::Put)
}
/// Check if the option is ATM
///
/// # Returns
///
/// True if the option is ATM, false otherwise.
fn atm(&self) -> bool {
match self.option_type() {
OptionType::Call => self.instrument().spot() == self.strike(),
OptionType::Put => self.instrument().spot() == self.strike(),
}
}
/// Check if the option is ITM
///
/// # Returns
///
/// True if the option is ITM, false otherwise.
fn itm(&self) -> bool {
match self.option_type() {
OptionType::Call => self.instrument().spot() > self.strike(),
OptionType::Put => self.instrument().spot() < self.strike(),
}
}
/// Check if the option is OTM
///
/// # Returns
///
/// True if the option is OTM, false otherwise.
fn otm(&self) -> bool {
match self.option_type() {
OptionType::Call => self.instrument().spot() < self.strike(),
OptionType::Put => self.instrument().spot() > self.strike(),
}
}
/// Get the option as a trait object.
///
/// # Returns
///
/// The option as a trait object.
fn as_any(&self) -> &dyn Any;
}