The brief
An options desk runs a small book on one stock, which trades at $64.20. The book holds the stock, two bought puts, a sold put and a sold call. All the options expire on the same day.
The risk manager wants the book's profit at expiry as one function of the stock price. From it come the prices where the book breaks even, and the most it can lose.
Your task
- Write
book_profit(S, book): the book's profit at expiry when the stock ends atS, a number or a NumPy array. Each leg adds its quantity times its payoff minus its price. RaiseValueErrorfor a negativeS, a negative strike, or a kind other than"call","put"or"stock". - Write
break_evens(book): every stock price where the profit is zero, smallest first. Assume the profit is not zero along a whole stretch of prices. - Write
worst_case(book): the lowest profit over all . Return-math.infwhen the loss has no limit.
The data
BOOK in the code lists the four legs. Each has a kind, a strike (None for the stock), a quantity and a price. A negative quantity is a sale, and the price is the premium per option or the price paid per share.
Hint: Where to start
Each option's payoff is a ramp from case study 1.1.5: for a call and for a put. A share pays . Multiplying by a negative quantity flips the leg over the axis, and turns the premium into cash received.
Hint: Finding the break-evens
Between two neighbouring strikes every leg is a straight line. So the profit is a piecewise function (lesson 1.1.2), straight on each piece.
Find the zero of each piece, as in lesson 1.1.1. Do not forget the piece beyond the last strike: it goes on for ever.
Hint: The worst case
A function made of straight pieces is lowest at a corner or at an end. Check and every strike. Then check the slope past the last strike: if the profit falls there, it has no floor (case study 1.1.5).