ZenQuant.Helpers.Greeks (zen_quant v0.2.0)

Copy Markdown View Source

Options-related calculation helpers.

Pure functions for calculating days to expiry and moneyness for options contracts. Useful for options trading strategies and filtering.

Example

# Days until option expires
expiry_ms = 1_735_689_600_000  # 2025-01-01 00:00:00 UTC
ZenQuant.Helpers.Greeks.days_to_expiry(expiry_ms)
# => 5.5 (days from now)

# Determine moneyness
ZenQuant.Helpers.Greeks.moneyness(50_000.0, 48_000.0, :call)
# => :itm (in the money)

Summary

Functions

Calculates days until expiry from a timestamp in milliseconds.

Calculates days until expiry from now to a given DateTime.

Determines the moneyness of an option.

Types

moneyness()

@type moneyness() :: :itm | :atm | :otm

Functions

days_to_expiry(expiry_ms)

@spec days_to_expiry(integer()) :: float()

Calculates days until expiry from a timestamp in milliseconds.

Returns a float representing the number of days (can be fractional). Negative values indicate the option has already expired.

Examples

# 1 day in the future
future_ms = System.system_time(:millisecond) + 86_400_000
ZenQuant.Helpers.Greeks.days_to_expiry(future_ms)
# => ~1.0

# Already expired (returns negative)
past_ms = System.system_time(:millisecond) - 86_400_000
ZenQuant.Helpers.Greeks.days_to_expiry(past_ms)
# => ~-1.0

days_to_expiry_from_datetime(expiry)

@spec days_to_expiry_from_datetime(DateTime.t()) :: float()

Calculates days until expiry from now to a given DateTime.

Example

expiry = ~U[2025-01-01 00:00:00Z]
ZenQuant.Helpers.Greeks.days_to_expiry_from_datetime(expiry)

moneyness(spot, strike, option_type)

@spec moneyness(number(), number(), :call | :put) :: moneyness()

Determines the moneyness of an option.

Returns :itm (in the money), :atm (at the money), or :otm (out of the money).

ATM threshold: spot price within 0.1% of strike is considered at-the-money.

Call Options

  • ITM: spot > strike
  • OTM: spot < strike

Put Options

  • ITM: spot < strike
  • OTM: spot > strike

Examples

iex> ZenQuant.Helpers.Greeks.moneyness(50_000.0, 48_000.0, :call)
:itm

iex> ZenQuant.Helpers.Greeks.moneyness(50_000.0, 52_000.0, :call)
:otm

iex> ZenQuant.Helpers.Greeks.moneyness(50_000.0, 48_000.0, :put)
:otm

iex> ZenQuant.Helpers.Greeks.moneyness(50_000.0, 52_000.0, :put)
:itm

iex> ZenQuant.Helpers.Greeks.moneyness(50_000.0, 50_000.0, :call)
:atm