Skip to content

API Reference


API Reference / GreeksCalculator

Class: GreeksCalculator

Defined in: src/analytics/GreeksCalculator.ts:79

Real-time Greeks Calculator for options.

This module calculates Delta, Gamma, Theta, Vega, and Rho in real-time by combining WebSocket tick data with Black-Scholes calculations.

For options buyers, this is critical for:

  • Monitoring theta decay tick-by-tick
  • Tracking delta exposure for directional bias
  • Setting informed stop-losses based on Greeks
  • Calculating theoretical fair value vs market price

Example

ts
const calculator = new GreeksCalculator({
  defaultRiskFreeRate: 0.065, // 6.5%
  defaultImpliedVolatility: 0.18, // 18%
});

// Calculate Greeks for a NIFTY call option
const result = calculator.calculate({
  spotPrice: 24500,
  strike: 24500,
  expiryDate: "2026-01-30",
  riskFreeRate: 0.065,
  impliedVolatility: 0.20,
  optionType: "call",
});

console.log(result.greeks);
// Output: { delta: 0.52, gamma: 0.003, theta: -2.5, vega: 0.15, rho: 0.08 }

Constructors

Constructor

new GreeksCalculator(config?): GreeksCalculator

Defined in: src/analytics/GreeksCalculator.ts:84

Parameters

config?

GreeksCalculatorConfig = {}

Returns

GreeksCalculator

Methods

calculate()

calculate(input): object

Defined in: src/analytics/GreeksCalculator.ts:95

Calculate Greeks for an option given current market conditions.

Parameters

input

GreeksInput

Returns

object

Object containing Greeks and optionally theoretical price

greeks

greeks: Greeks

theoreticalPrice?

optional theoreticalPrice?: number

timeToExpiry

timeToExpiry: number


processTick()

processTick(tick, optionDetails, underlyingSpot): TickWithGreeks

Defined in: src/analytics/GreeksCalculator.ts:153

Process a WebSocket tick and attach Greeks to it.

This is designed to be called in your WebSocket message handler to enrich every tick with real-time Greeks calculations.

Parameters

tick

MarketTickerEvent | MarketQuoteEvent | MarketFullEvent

The raw WebSocket market feed event

optionDetails

Details about the option contract

strike

number

expiryDate

string | Date

optionType

OptionKind

impliedVolatility?

number

underlyingSpot

number

Current spot price of the underlying index

Returns

TickWithGreeks

The tick enriched with Greeks data

Example

ts
ws.on('market-feed', (tick) => {
  const enrichedTick = calculator.processTick(tick, {
    strike: 24500,
    expiryDate: "2026-01-30",
    optionType: "call",
    impliedVolatility: 0.20,
  }, 24500); // Nifty spot
  
  console.log(`Delta: ${enrichedTick.greeks?.delta}, Theta: ${enrichedTick.greeks?.theta}`);
});

calculatePortfolioGreeks()

calculatePortfolioGreeks(positions, currentSpots): object

Defined in: src/analytics/GreeksCalculator.ts:222

Calculate net portfolio Greeks from multiple positions.

Essential for AI agents to understand overall portfolio risk:

  • Net Delta: Directional exposure
  • Net Theta: Time decay bleeding
  • Net Vega: Volatility exposure

Parameters

positions

object[]

Array of option positions with quantities

currentSpots

Record<string, number>

Map of security IDs to current underlying spot prices

Returns

object

Aggregated portfolio Greeks

netDelta

netDelta: number

netGamma

netGamma: number

netTheta

netTheta: number

netVega

netVega: number

netRho

netRho: number

positionBreakdown

positionBreakdown: object[]

Example

ts
const portfolioGreeks = calculator.calculatePortfolioGreeks([
  {
    securityId: "44000",
    quantity: 50, // Long 1 lot
    strike: 24500,
    expiryDate: "2026-01-30",
    optionType: "call",
    impliedVolatility: 0.20,
  },
  {
    securityId: "44050",
    quantity: -50, // Short 1 lot
    strike: 24600,
    expiryDate: "2026-01-30",
    optionType: "call",
    impliedVolatility: 0.18,
  },
], {
  "1333": 24500, // Nifty spot
});

console.log(`Net Delta: ${portfolioGreeks.netDelta}`);
console.log(`Net Theta: ${portfolioGreeks.netTheta} (daily decay)`);

setRiskFreeRate()

setRiskFreeRate(rate): void

Defined in: src/analytics/GreeksCalculator.ts:300

Update default risk-free rate.

Parameters

rate

number

Returns

void


setImpliedVolatility()

setImpliedVolatility(iv): void

Defined in: src/analytics/GreeksCalculator.ts:307

Update default implied volatility.

Parameters

iv

number

Returns

void

Community project — not affiliated with Dhan. MIT License.