rustyqlib/equity/
utils.rs1use serde::Deserialize;
2use crate::equity::vanila_option::EquityOptionBase;
3use std::str::FromStr;
4use std::error::Error;
5use crate::core::trade::{PutOrCall};
6use std::fmt::Debug;
7use crate::core::utils::ContractStyle;
8
9#[derive(PartialEq,Clone,Debug)]
11pub enum Engine{
12 BlackScholes,
13 MonteCarlo,
14 Binomial,
15 FiniteDifference
16}
17#[derive(Debug)]
18pub enum LongShort{
19 LONG,
20 SHORT
21}
22#[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
23#[serde(rename_all = "snake_case")]
24pub enum PayoffType {
25 Vanilla,
26 Binary,
27 Barrier,
28 Asian,
29 ForwardStart,
30 Autocallable,
31}
32impl FromStr for PayoffType {
33 type Err = Box<dyn Error>;
34 fn from_str(s: &str) -> Result<Self, Self::Err> {
35 match s.to_lowercase().as_str() {
36 "vanilla" => Ok(PayoffType::Vanilla),
37 "binary" => Ok(PayoffType::Binary),
38 "barrier" => Ok(PayoffType::Barrier),
39 "asian" => Ok(PayoffType::Asian),
40 "forward_start" | "forwardstart" => Ok(PayoffType::ForwardStart),
41 "autocallable" | "autocall" => Ok(PayoffType::Autocallable),
42 _ => Err("Invalid payoff type".into()),
43 }
44 }
45}
46
47
48pub trait Payoff: Debug + Send + Sync {
56 fn payoff(&self, spot: f64, strike: f64) -> f64;
59
60 fn path_payoff(&self, path: &[f64], strike: f64) -> f64 {
64 self.payoff(*path.last().expect("empty path"), strike)
65 }
66
67 fn is_path_dependent(&self) -> bool {
70 false
71 }
72
73 fn payoff_amount(&self, base: &EquityOptionBase) -> f64 {
75 self.payoff(base.underlying_price.value(), base.strike_price)
76 }
77
78 fn payoff_kind(&self) -> PayoffType;
79 fn put_or_call(&self) -> &PutOrCall;
80 fn exercise_style(&self)->&ContractStyle;
81
82 fn as_any(&self) -> &dyn std::any::Any;
86}