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 from_actor(actor: &impl DataActorNative) -> Self
pub fn from_actor(actor: &impl DataActorNative) -> Self
Creates a new GreeksCalculator from a registered native actor.
§Panics
Panics if the actor has not been registered with a trader.
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>,
update_vol: 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>>,
vega_time_weight_base: Option<i32>,
vol_index_instrument_id: Option<InstrumentId>,
vol_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>, update_vol: 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>>, vega_time_weight_base: Option<i32>, vol_index_instrument_id: Option<InstrumentId>, vol_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, gamma and vega with respect to an index.
§Errors
Returns an error if the instrument definition is not found, an option instrument has no underlying identifier, or greeks calculation fails.
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>>,
vega_input: f64,
vol: f64,
expiry_in_days: i32,
vega_time_weight_base: Option<i32>,
unshocked_vol: f64,
vol_index_instrument_id: Option<InstrumentId>,
vol_beta_weights: Option<&HashMap<InstrumentId, f64>>,
index_price: Option<f64>,
vol_index_price: Option<f64>,
) -> Result<(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>>, vega_input: f64, vol: f64, expiry_in_days: i32, vega_time_weight_base: Option<i32>, unshocked_vol: f64, vol_index_instrument_id: Option<InstrumentId>, vol_beta_weights: Option<&HashMap<InstrumentId, f64>>, index_price: Option<f64>, vol_index_price: Option<f64>, ) -> Result<(f64, f64, f64)>
Modifies delta, gamma and vega 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.
Vega beta weighting follows the same change of variable with implied volatility and a volatility index.
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))
§Errors
Returns an error if vol_index_instrument_id is supplied and no explicit or cached
volatility index price is available.
Sourcepub fn portfolio_greeks(
&self,
underlyings: Option<&[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>,
update_vol: 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>>,
greeks_filter: Option<&GreeksFilter>,
vega_time_weight_base: Option<i32>,
vol_index_instrument_id: Option<InstrumentId>,
vol_beta_weights: Option<&HashMap<InstrumentId, f64>>,
) -> Result<PortfolioGreeks>
pub fn portfolio_greeks( &self, underlyings: Option<&[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>, update_vol: 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>>, greeks_filter: Option<&GreeksFilter>, vega_time_weight_base: Option<i32>, vol_index_instrument_id: Option<InstrumentId>, vol_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, gamma and vega with respect to an index.
§Errors
Returns an error if any underlying greeks calculation fails.
Sourcepub fn cache_futures_spread(
&self,
call_instrument_id: InstrumentId,
put_instrument_id: InstrumentId,
futures_instrument_id: InstrumentId,
) -> Result<Price>
pub fn cache_futures_spread( &self, call_instrument_id: InstrumentId, put_instrument_id: InstrumentId, futures_instrument_id: InstrumentId, ) -> Result<Price>
Cache a futures spread derived from a call/put pair against a reference future.
§Errors
Returns an error if instruments or prices are missing or inconsistent.
Sourcepub fn get_cached_futures_spread_price(
&self,
underlying_instrument_id: InstrumentId,
) -> Option<Price>
pub fn get_cached_futures_spread_price( &self, underlying_instrument_id: InstrumentId, ) -> Option<Price>
Resolve a cached futures spread price for an underlying future.
Sourcepub fn subscribe_greeks<F>(&self, underlying: &str, handler: Option<F>)where
F: Fn(&GreeksData) + 'static,
pub fn subscribe_greeks<F>(&self, underlying: &str, handler: Option<F>)where
F: Fn(&GreeksData) + 'static,
Subscribes to Greeks data for a given underlying instrument.
Useful for reading greeks from a backtesting data catalog and caching them for later use.