@@ -18,12 +18,12 @@ impl Instrument for EquityOption {
18
18
value
19
19
}
20
20
Engine :: MonteCarlo => {
21
- println ! ( "Using MonteCarlo " ) ;
21
+ println ! ( "Using MonteCarlo Engine " ) ;
22
22
let value = montecarlo:: npv ( & self , false ) ;
23
23
value
24
24
}
25
25
Engine :: Binomial => {
26
- println ! ( "Using Binomial " ) ;
26
+ println ! ( "Using Binomial Engine " ) ;
27
27
let value = binomial:: npv ( & self ) ;
28
28
value
29
29
}
@@ -59,7 +59,7 @@ impl EquityOption{
59
59
}
60
60
}
61
61
impl EquityOption {
62
- pub fn equityoption_from_json ( data : Contract ) -> Box < EquityOption > {
62
+ pub fn from_json ( data : Contract ) -> Box < EquityOption > {
63
63
let market_data = data. market_data . unwrap ( ) ;
64
64
let underlying_quote = Quote :: new ( market_data. underlying_price ) ;
65
65
//TODO: Add term structure
@@ -83,13 +83,20 @@ impl EquityOption {
83
83
Some ( x) => x. unwrap ( ) ,
84
84
None => 0.0 ,
85
85
} ) ;
86
+ //let volatility = Some(market_data.volatility);
87
+ let volatility = match market_data. volatility {
88
+ Some ( x) => {
89
+ x
90
+ }
91
+ None => 0.2
92
+ } ;
86
93
let mut option = EquityOption {
87
94
option_type : side,
88
- transection : trade :: Transection :: Buy ,
95
+ transection : Transection :: Buy ,
89
96
underlying_price : underlying_quote,
90
97
current_price : option_price,
91
98
strike_price : market_data. strike_price ,
92
- volatility : 0.2 ,
99
+ volatility : volatility ,
93
100
maturity_date : future_date,
94
101
risk_free_rate : risk_free_rate. unwrap_or ( 0.0 ) ,
95
102
dividend_yield : dividend. unwrap_or ( 0.0 ) ,
@@ -129,3 +136,50 @@ impl EquityOption {
129
136
return Box :: new ( option) ;
130
137
}
131
138
}
139
+
140
+ #[ cfg( test) ]
141
+ mod tests {
142
+ //write a unit test for from_json
143
+ use super :: * ;
144
+ use crate :: core:: utils:: { Contract , MarketData } ;
145
+ use crate :: core:: trade:: OptionType ;
146
+ use crate :: core:: trade:: Transection ;
147
+ use crate :: core:: utils:: ContractStyle ;
148
+ use crate :: core:: termstructure:: YieldTermStructure ;
149
+ use crate :: core:: quotes:: Quote ;
150
+ use chrono:: { Datelike , Local , NaiveDate } ;
151
+ #[ test]
152
+ fn test_from_json ( ) {
153
+ let data = Contract {
154
+ action : "PV" . to_string ( ) ,
155
+ market_data : Some ( MarketData {
156
+ underlying_price : 100.0 ,
157
+ strike_price : 100.0 ,
158
+ volatility : None ,
159
+ option_price : Some ( 10.0 ) ,
160
+ risk_free_rate : Some ( 0.05 ) ,
161
+ dividend : Some ( 0.0 ) ,
162
+ maturity : "2024-01-01" . to_string ( ) ,
163
+ option_type : "C" . to_string ( ) ,
164
+ simulation : None
165
+ } ) ,
166
+ pricer : "Analytical" . to_string ( ) ,
167
+ asset : "" . to_string ( ) ,
168
+ style : Some ( "European" . to_string ( ) ) ,
169
+ rate_data : None
170
+ } ;
171
+ let option = EquityOption :: from_json ( data) ;
172
+ assert_eq ! ( option. option_type, OptionType :: Call ) ;
173
+ assert_eq ! ( option. transection, Transection :: Buy ) ;
174
+ assert_eq ! ( option. underlying_price. value, 100.0 ) ;
175
+ assert_eq ! ( option. strike_price, 100.0 ) ;
176
+ assert_eq ! ( option. current_price. value, 10.0 ) ;
177
+ assert_eq ! ( option. dividend_yield, 0.0 ) ;
178
+ assert_eq ! ( option. volatility, 0.2 ) ;
179
+ assert_eq ! ( option. maturity_date, NaiveDate :: from_ymd( 2024 , 1 , 1 ) ) ;
180
+ assert_eq ! ( option. valuation_date, Local :: today( ) . naive_utc( ) ) ;
181
+ assert_eq ! ( option. engine, Engine :: BlackScholes ) ;
182
+ assert_eq ! ( option. style, ContractStyle :: European ) ;
183
+ }
184
+ }
185
+
0 commit comments