Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Compute Portfolio Greeks

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Compute Portfolio Greeks. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Write a function to calculate the Greek values of a portfolio.

Assume every position is a European call or put valued with the Black-Scholes model. Return the portfolio-level delta, gamma, vega, theta, and rho by multiplying each position's Greeks by its signed quantity and summing across positions.

The function accepts a list of position dictionaries and returns a dictionary of five floating-point Greek values. Inputs use annualized time, volatility, and interest rate. For positions at expiry, use intrinsic-value delta, with delta 0.5 for an at-the-money option, and return zero for the other Greeks.

Signature: def calculate_portfolio_greeks(portfolio):

Each position contains type, quantity, spot, strike, time_to_expiry, volatility, and rate. Quantities may be negative for short positions.

Constraints

  • 0 <= len(portfolio) <= 10000
  • Each position type is either "call" or "put"
  • Quantities are signed integers
  • spot > 0 and strike > 0
  • time_to_expiry >= 0
  • volatility > 0 when time_to_expiry > 0
  • rate is an annualized continuously compounded risk-free rate
  • All time, rate, and volatility values use annualized units

Function Signature

def calculate_portfolio_greeks(portfolio):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output