1+ // An equity
2+ use chrono:: { Datelike , Local , NaiveDate } ;
3+ use crate :: core:: quotes:: Quote ;
4+ use crate :: core:: traits:: Instrument ;
5+ use crate :: core:: utils:: { Contract , ContractStyle } ;
6+ use crate :: equity:: vanila_option:: EquityOption ;
7+
8+ pub struct EquityFuture {
9+ pub underlying_price : Quote ,
10+ pub current_price : Quote ,
11+ pub risk_free_rate : f64 ,
12+ pub dividend_yield : f64 ,
13+ pub maturity_date : NaiveDate ,
14+ pub valuation_date : NaiveDate ,
15+ }
16+
17+ impl EquityFuture {
18+ pub fn from_json ( data : & Contract ) -> Box < Self > {
19+ let market_data = data. market_data . as_ref ( ) . unwrap ( ) ;
20+ //let future_date = NaiveDate::parse_from_str(&maturity_date, "%Y-%m-%d").expect("Invalid date format");
21+ let today = Local :: today ( ) ;
22+ let maturity_date = NaiveDate :: parse_from_str ( & market_data. maturity , "%Y-%m-%d" )
23+ . expect ( "Invalid maturity date" ) ;
24+
25+ let underlying_quote = Quote :: new ( market_data. underlying_price ) ;
26+ let quote = Some ( market_data. current_price ) . unwrap ( ) ;
27+ let current_quote = Quote :: new ( quote. unwrap_or ( 0.0 ) ) ;
28+ let risk_free_rate = Some ( market_data. risk_free_rate ) . unwrap ( ) ;
29+ let dividend = Some ( market_data. dividend ) . unwrap ( ) ;
30+ Box :: new ( Self {
31+ underlying_price : underlying_quote,
32+ current_price : current_quote,
33+ risk_free_rate : risk_free_rate. unwrap_or ( 0.0 ) ,
34+ dividend_yield : dividend. unwrap_or ( 0.0 ) ,
35+ maturity_date : maturity_date,
36+ valuation_date : today. naive_utc ( ) ,
37+ } )
38+ }
39+
40+ fn time_to_maturity ( & self ) -> f64 {
41+ let days = ( self . maturity_date - self . valuation_date ) . num_days ( ) ;
42+ ( days as f64 ) / 365.0
43+ }
44+ fn premiun ( & self ) ->f64 {
45+ self . current_price . value ( ) -self . underlying_price . value ( )
46+ }
47+ }
48+ impl Instrument for EquityFuture {
49+ fn npv ( & self ) -> f64 {
50+ // F_0 = S_0 * e^{(r - q)*t}
51+ let t = self . time_to_maturity ( ) ;
52+ self . underlying_price . value ( ) * ( ( self . risk_free_rate - self . dividend_yield ) * t) . exp ( )
53+ }
54+ }
55+ impl EquityFuture {
56+ pub fn delta ( & self ) -> f64 { 1.0 }
57+ pub fn gamma ( & self ) -> f64 { 0.0 }
58+ pub fn vega ( & self ) -> f64 { 0.0 }
59+ pub fn theta ( & self ) -> f64 { 0.0 }
60+ pub fn rho ( & self ) -> f64 { 0.0 }
61+ }
62+ // }
0 commit comments