Skip to content

Commit d96821c

Browse files
committed
added long short for future
1 parent 44c5785 commit d96821c

File tree

4 files changed

+76
-29
lines changed

4 files changed

+76
-29
lines changed

src/core/utils.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ pub struct MarketData {
4949
pub simulation:Option<u64>,
5050
pub current_price:Option<f64>,
5151
pub notional: Option<f64>,
52-
pub long_short:Option<i32>
52+
pub long_short:Option<i32>,
53+
pub multiplier:Option<f64>,
54+
pub entry_price:Option<f64>,
5355
}
5456

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

src/equity/equity_forward.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ impl EquityForward {
2626
.expect("Invalid maturity date");
2727

2828
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));
29+
let quote = Some(market_data.entry_price).unwrap();
30+
let entry_quote = Quote::new(quote.unwrap_or(0.0));
3131
let risk_free_rate = Some(market_data.risk_free_rate).unwrap();
3232
let dividend = Some(market_data.dividend).unwrap();
33-
let long_short = market_data.long_short.unwrap();
33+
let long_short = market_data.long_short.unwrap_or(1);
3434
let position = match long_short{
3535
1=>LongShort::LONG,
3636
-1=>LongShort::SHORT,
3737
_=>LongShort::LONG,
3838
};
3939
Box::new(Self {
4040
underlying_price: underlying_price,
41-
forward_price:current_quote,
41+
forward_price:entry_quote,
4242
risk_free_rate: risk_free_rate.unwrap_or(0.0),
4343
dividend_yield: dividend.unwrap_or(0.0),
4444
maturity_date: maturity_date,
@@ -66,9 +66,10 @@ impl Instrument for EquityForward {
6666
fn npv(&self) -> f64 {
6767
// e −r(T−t) (Ft −K),
6868
let df_r = 1.0/(self.risk_free_rate*self.time_to_maturity()).exp();
69+
let share = self.notional/self.forward_price.value();
6970
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,
71+
LongShort::LONG => (self.forward()-self.forward_price.value()) * share *df_r,
72+
LongShort::SHORT => -(self.forward()-self.forward_price.value()) * share *df_r,
7273
}
7374

7475
}

src/equity/equity_future.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ 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::utils::LongShort;
67
//use crate::equity::vanila_option::EquityOption;
78

89
pub struct EquityFuture {
910
pub underlying_price: Quote,
1011
pub current_price: Quote,
12+
pub entry_price: f64,
13+
pub multiplier: f64,
1114
pub risk_free_rate: f64,
1215
pub dividend_yield: f64,
1316
pub maturity_date: NaiveDate,
1417
pub valuation_date: NaiveDate,
18+
pub long_short:LongShort,
19+
1520
}
1621

1722
impl EquityFuture {
@@ -27,29 +32,52 @@ impl EquityFuture {
2732
let current_quote = Quote::new(quote.unwrap_or(0.0));
2833
let risk_free_rate = Some(market_data.risk_free_rate).unwrap();
2934
let dividend = Some(market_data.dividend).unwrap();
35+
let long_short = market_data.long_short.unwrap_or(1);
36+
let position = match long_short{
37+
1=>LongShort::LONG,
38+
-1=>LongShort::SHORT,
39+
_=>LongShort::LONG,
40+
};
3041
Box::new(Self {
3142
underlying_price: underlying_quote,
3243
current_price:current_quote,
44+
entry_price: market_data.entry_price.unwrap(),
45+
multiplier: market_data.multiplier.unwrap(),
3346
risk_free_rate: risk_free_rate.unwrap_or(0.0),
3447
dividend_yield: dividend.unwrap_or(0.0),
3548
maturity_date: maturity_date,
3649
valuation_date: today.naive_utc(),
50+
long_short:position
3751
})
3852
}
39-
53+
fn notional(&self) -> f64 {
54+
self.multiplier * self.current_price.value()
55+
}
4056
fn time_to_maturity(&self) -> f64 {
4157
let days = (self.maturity_date - self.valuation_date).num_days();
4258
(days as f64) / 365.0
4359
}
4460
fn premiun(&self)->f64{
4561
self.current_price.value()-self.underlying_price.value()
4662
}
63+
fn pnl(&self)->f64{
64+
let pnl = (self.current_price.value()-self.entry_price)*self.multiplier;
65+
match self.long_short {
66+
LongShort::LONG => pnl,
67+
LongShort::SHORT => -pnl,
68+
_=>0.0,
69+
}
70+
}
71+
fn forward_price(&self)->f64{
72+
let discount_df = 1.0/(self.risk_free_rate*self.time_to_maturity()).exp();
73+
let dividend_df = 1.0/(self.dividend_yield*self.time_to_maturity()).exp();
74+
let forward = self.underlying_price.value()*dividend_df/discount_df;
75+
forward
76+
}
4777
}
4878
impl Instrument for EquityFuture {
4979
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()
80+
self.pnl()
5381
}
5482
}
5583
impl EquityFuture{
@@ -58,5 +86,4 @@ impl EquityFuture{
5886
pub fn vega(&self) -> f64 { 0.0 }
5987
pub fn theta(&self) -> f64 { 0.0 }
6088
pub fn rho(&self) -> f64 { 0.0 }
61-
}
62-
// }
89+
}
Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
{"asset":"EQ",
22
"contracts" : [
3-
{
4-
"action":"PV",
5-
"pricer":"Analytical",
6-
"asset":"EQ",
7-
"product_type": "Option",
8-
"payoff_type": "Vanilla",
9-
"market_data":{
10-
"underlying_price":100,
11-
"option_type":"C",
12-
"strike_price":100,
13-
"volatility":0.4,
14-
"risk_free_rate":0.06,
15-
"maturity":"2025-03-31",
16-
"dividend": 0.01
17-
}
18-
}]
3+
{
4+
"action": "PV",
5+
"pricer": "Analytical",
6+
"asset": "EQ",
7+
"product_type": "Forward",
8+
"payoff_type": "Vanilla",
9+
"market_data": {
10+
"underlying_price": 96.0,
11+
"entry_price": 103.0,
12+
"risk_free_rate": 0.06,
13+
"maturity": "2025-03-31",
14+
"dividend": 0.01,
15+
"notional": 10000,
16+
"long_short": 1
17+
}
18+
},
19+
{
20+
"action":"PV",
21+
"pricer":"Analytical",
22+
"asset":"EQ",
23+
"product_type": "Future",
24+
"payoff_type": "Vanilla",
25+
"market_data":{
26+
"underlying_price":96.0,
27+
"current_price": 99.0,
28+
"entry_price": 103.0,
29+
"risk_free_rate":0.06,
30+
"maturity":"2025-03-31",
31+
"dividend": 0.01,
32+
"multiplier": 1000,
33+
"long_short": -1
34+
}
35+
}]
1936
}

0 commit comments

Comments
 (0)