Struct GreeksCalculator

Source
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

Source

pub fn new(cache: Rc<RefCell<Cache>>, clock: Rc<RefCell<dyn Clock>>) -> Self

Creates a new GreeksCalculator instance.

Source

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.
Source

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))

Source

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.
Source

pub fn subscribe_greeks<F>(&self, underlying: &str, handler: Option<F>)
where F: Fn(GreeksData) + 'static + Send + Sync,

Subscribes to Greeks data for a given underlying instrument.

Useful for reading greeks from a backtesting data catalog and caching them for later use.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more