pub struct GreeksCalculator { /* private fields */ }
Expand description
Calculates instrument and portfolio greeks (sensitivities of price moves with respect to market data moves).
Useful for risk management of options and futures portfolios.
Currently implemented greeks are:
- Delta (first derivative of price with respect to spot move).
- Gamma (second derivative of price with respect to spot move).
- Vega (first derivative of price with respect to implied volatility of an option).
- Theta (first derivative of price with respect to time to expiry).
Vega is expressed in terms of absolute percent changes ((dV / dVol) / 100). Theta is expressed in terms of daily changes ((dV / d(T-t)) / 365.25, where T is the expiry of an option and t is the current time).
Also note that for ease of implementation we consider that american options (for stock options for example) are european for the computation of greeks.
Implementations§
Source§impl GreeksCalculator
impl GreeksCalculator
Sourcepub fn new(cache: Rc<RefCell<Cache>>, clock: Rc<RefCell<dyn Clock>>) -> Self
pub fn new(cache: Rc<RefCell<Cache>>, clock: Rc<RefCell<dyn Clock>>) -> Self
Creates a new GreeksCalculator
instance.
Sourcepub fn instrument_greeks(
&self,
instrument_id: InstrumentId,
flat_interest_rate: Option<f64>,
flat_dividend_yield: Option<f64>,
spot_shock: Option<f64>,
vol_shock: Option<f64>,
time_to_expiry_shock: Option<f64>,
use_cached_greeks: Option<bool>,
cache_greeks: Option<bool>,
publish_greeks: Option<bool>,
ts_event: Option<UnixNanos>,
position: Option<Position>,
percent_greeks: Option<bool>,
index_instrument_id: Option<InstrumentId>,
beta_weights: Option<HashMap<InstrumentId, f64>>,
) -> Result<GreeksData>
pub fn instrument_greeks( &self, instrument_id: InstrumentId, flat_interest_rate: Option<f64>, flat_dividend_yield: Option<f64>, spot_shock: Option<f64>, vol_shock: Option<f64>, time_to_expiry_shock: Option<f64>, use_cached_greeks: Option<bool>, cache_greeks: Option<bool>, publish_greeks: Option<bool>, ts_event: Option<UnixNanos>, position: Option<Position>, percent_greeks: Option<bool>, index_instrument_id: Option<InstrumentId>, beta_weights: Option<HashMap<InstrumentId, f64>>, ) -> Result<GreeksData>
Calculates option or underlying greeks for a given instrument and a quantity of 1.
Additional features:
- Apply shocks to the spot value of the instrument’s underlying, implied volatility or time to expiry.
- Compute percent greeks.
- Compute beta-weighted delta and gamma with respect to an index.
Sourcepub fn modify_greeks(
&self,
delta_input: f64,
gamma_input: f64,
underlying_instrument_id: InstrumentId,
underlying_price: f64,
unshocked_underlying_price: f64,
percent_greeks: bool,
index_instrument_id: Option<InstrumentId>,
beta_weights: Option<&HashMap<InstrumentId, f64>>,
) -> (f64, f64)
pub fn modify_greeks( &self, delta_input: f64, gamma_input: f64, underlying_instrument_id: InstrumentId, underlying_price: f64, unshocked_underlying_price: f64, percent_greeks: bool, index_instrument_id: Option<InstrumentId>, beta_weights: Option<&HashMap<InstrumentId, f64>>, ) -> (f64, f64)
Modifies delta and gamma based on beta weighting and percentage calculations.
The beta weighting of delta and gamma follows this equation linking the returns of a stock x to the ones of an index I: (x - x0) / x0 = alpha + beta (I - I0) / I0 + epsilon
beta can be obtained by linear regression of stock_return = alpha + beta index_return, it’s equal to: beta = Covariance(stock_returns, index_returns) / Variance(index_returns)
Considering alpha == 0: x = x0 + beta x0 / I0 (I-I0) I = I0 + 1 / beta I0 / x0 (x - x0)
These two last equations explain the beta weighting below, considering the price of an option is V(x) and delta and gamma are the first and second derivatives respectively of V.
Also percent greeks assume a change of variable to percent returns by writing: V(x = x0 * (1 + stock_percent_return / 100)) or V(I = I0 * (1 + index_percent_return / 100))
Sourcepub fn portfolio_greeks(
&self,
underlyings: Option<Vec<String>>,
venue: Option<Venue>,
instrument_id: Option<InstrumentId>,
strategy_id: Option<StrategyId>,
side: Option<PositionSide>,
flat_interest_rate: Option<f64>,
flat_dividend_yield: Option<f64>,
spot_shock: Option<f64>,
vol_shock: Option<f64>,
time_to_expiry_shock: Option<f64>,
use_cached_greeks: Option<bool>,
cache_greeks: Option<bool>,
publish_greeks: Option<bool>,
percent_greeks: Option<bool>,
index_instrument_id: Option<InstrumentId>,
beta_weights: Option<HashMap<InstrumentId, f64>>,
) -> Result<PortfolioGreeks>
pub fn portfolio_greeks( &self, underlyings: Option<Vec<String>>, venue: Option<Venue>, instrument_id: Option<InstrumentId>, strategy_id: Option<StrategyId>, side: Option<PositionSide>, flat_interest_rate: Option<f64>, flat_dividend_yield: Option<f64>, spot_shock: Option<f64>, vol_shock: Option<f64>, time_to_expiry_shock: Option<f64>, use_cached_greeks: Option<bool>, cache_greeks: Option<bool>, publish_greeks: Option<bool>, percent_greeks: Option<bool>, index_instrument_id: Option<InstrumentId>, beta_weights: Option<HashMap<InstrumentId, f64>>, ) -> Result<PortfolioGreeks>
Calculates the portfolio Greeks for a given set of positions.
Aggregates the Greeks data for all open positions that match the specified criteria.
Additional features:
- Apply shocks to the spot value of an instrument’s underlying, implied volatility or time to expiry.
- Compute percent greeks.
- Compute beta-weighted delta and gamma with respect to an index.