Skip to content

Commit 60cd2f8

Browse files
committed
added npv of equity forward
1 parent ce02da8 commit 60cd2f8

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Files are in JSON format and can be easily edited with any text editor.
4747

4848
### Instruments:
4949
#### Equity
50+
- [x] Equity Forward
51+
- [x] Equity Future
5052
- [x] Equity Option
5153
- [ ] Equity Forward Start Option
5254
- [ ] Equity Basket
@@ -57,7 +59,7 @@ Files are in JSON format and can be easily edited with any text editor.
5759
- [ ] Equity Chooser
5860
#### Interest Rate
5961
- [x] Deposit
60-
- [x] FRA
62+
- [ ] FRA
6163
- [ ] Interest Rate Swap
6264
#### Commodities
6365
- [x] Commodity Option

src/core/utils.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ pub struct MarketData {
4747
pub maturity:String,
4848
pub dividend: Option<f64>,
4949
pub simulation:Option<u64>,
50-
pub current_price:Option<f64>
50+
pub current_price:Option<f64>,
51+
pub notional: Option<f64>,
52+
pub long_short:Option<i32>
5153
}
5254

5355
#[derive(Clone,Debug,Deserialize,Serialize)]

src/equity/equity_forward.rs

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,76 @@
1-
use chrono::NaiveDate;
1+
use chrono::{Local, NaiveDate};
22
use crate::core::quotes::Quote;
3+
use crate::core::traits::Instrument;
4+
use crate::core::utils::Contract;
5+
use crate::equity::equity_future::EquityFuture;
36
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.
410
pub struct EquityForward {
511
pub underlying_price: Quote,
6-
pub forward_price: Quote,
12+
pub forward_price: Quote, //Forward price you actually locked in.
713
pub risk_free_rate: f64,
814
pub dividend_yield: f64,
915
pub maturity_date: NaiveDate,
1016
pub valuation_date: NaiveDate,
1117
pub long_short:LongShort,
1218
pub notional:f64
1319
}
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+
}
1476

src/equity/equity_future.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use chrono::{Datelike, Local, NaiveDate};
33
use crate::core::quotes::Quote;
44
use crate::core::traits::Instrument;
55
use crate::core::utils::{Contract,ContractStyle};
6-
use crate::equity::vanila_option::EquityOption;
6+
//use crate::equity::vanila_option::EquityOption;
77

88
pub struct EquityFuture {
99
pub underlying_price: Quote,

0 commit comments

Comments
 (0)