|
1 | | -use chrono::NaiveDate; |
| 1 | +use chrono::{Local, NaiveDate}; |
2 | 2 | use crate::core::quotes::Quote; |
| 3 | +use crate::core::traits::Instrument; |
| 4 | +use crate::core::utils::Contract; |
| 5 | +use crate::equity::equity_future::EquityFuture; |
3 | 6 | use crate::equity::utils::LongShort; |
| 7 | +///A forward contract is an agreement between two parties to buy or sell, as the case may be, |
| 8 | +/// a commodity (or financial instrument or currency or any other underlying) |
| 9 | +/// on a pre-determined future date at a price agreed when the contract is entered into. |
4 | 10 | pub struct EquityForward { |
5 | 11 | pub underlying_price: Quote, |
6 | | - pub forward_price: Quote, |
| 12 | + pub forward_price: Quote, //Forward price you actually locked in. |
7 | 13 | pub risk_free_rate: f64, |
8 | 14 | pub dividend_yield: f64, |
9 | 15 | pub maturity_date: NaiveDate, |
10 | 16 | pub valuation_date: NaiveDate, |
11 | 17 | pub long_short:LongShort, |
12 | 18 | pub notional:f64 |
13 | 19 | } |
| 20 | +impl EquityForward { |
| 21 | + pub fn from_json(data: &Contract) -> Box<Self> { |
| 22 | + let market_data = data.market_data.as_ref().unwrap(); |
| 23 | + //let future_date = NaiveDate::parse_from_str(&maturity_date, "%Y-%m-%d").expect("Invalid date format"); |
| 24 | + let today = Local::today(); |
| 25 | + let maturity_date = NaiveDate::parse_from_str(&market_data.maturity, "%Y-%m-%d") |
| 26 | + .expect("Invalid maturity date"); |
| 27 | + |
| 28 | + let underlying_price = Quote::new(market_data.underlying_price); |
| 29 | + let quote = Some(market_data.current_price).unwrap(); |
| 30 | + let current_quote = Quote::new(quote.unwrap_or(0.0)); |
| 31 | + let risk_free_rate = Some(market_data.risk_free_rate).unwrap(); |
| 32 | + let dividend = Some(market_data.dividend).unwrap(); |
| 33 | + let long_short = market_data.long_short.unwrap(); |
| 34 | + let position = match long_short{ |
| 35 | + 1=>LongShort::LONG, |
| 36 | + -1=>LongShort::SHORT, |
| 37 | + _=>LongShort::LONG, |
| 38 | + }; |
| 39 | + Box::new(Self { |
| 40 | + underlying_price: underlying_price, |
| 41 | + forward_price:current_quote, |
| 42 | + risk_free_rate: risk_free_rate.unwrap_or(0.0), |
| 43 | + dividend_yield: dividend.unwrap_or(0.0), |
| 44 | + maturity_date: maturity_date, |
| 45 | + valuation_date: today.naive_utc(), |
| 46 | + notional:market_data.notional.unwrap_or(1.0), |
| 47 | + long_short:position |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + fn time_to_maturity(&self) -> f64 { |
| 52 | + let days = (self.maturity_date - self.valuation_date).num_days(); |
| 53 | + (days as f64) / 365.0 |
| 54 | + } |
| 55 | + fn premiun(&self)->f64{ |
| 56 | + self.forward()-self.underlying_price.value() |
| 57 | + } |
| 58 | + fn forward(&self)->f64{ |
| 59 | + let discount_df = 1.0/(self.risk_free_rate*self.time_to_maturity()).exp(); |
| 60 | + let dividend_df = 1.0/(self.dividend_yield*self.time_to_maturity()).exp(); |
| 61 | + let forward = self.underlying_price.value()*dividend_df/discount_df; |
| 62 | + forward |
| 63 | + } |
| 64 | +} |
| 65 | +impl Instrument for EquityForward { |
| 66 | + fn npv(&self) -> f64 { |
| 67 | + // e −r(T−t) (Ft −K), |
| 68 | + let df_r = 1.0/(self.risk_free_rate*self.time_to_maturity()).exp(); |
| 69 | + match self.long_short{ |
| 70 | + LongShort::LONG => (self.forward()-self.forward_price.value()) * self.notional*df_r, |
| 71 | + LongShort::SHORT => -(self.forward()-self.forward_price.value()) * self.notional*df_r, |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | +} |
14 | 76 |
|
0 commit comments