LessonThis is a lesson restated in new words. Any figure in it is an example from the source, not this desk's record.
A live implied-volatility surface is a grid of Black-Scholes volatilities indexed by strike and expiry, updated as quotes arrive. This lecture builds one for SPY in Python on the Interactive Brokers API, with a second panel for the nearest expiry. You can store the broker's implied volatilities or solve for them yourself. The grid is rebuilt from the current chain so tomorrow's expired options and yesterday's fixed strikes do not define today's picture.
Why it matters
The surface is three-dimensional because maturity is an axis, not a single date. The lecture plots out-of-the-money calls above the at-the-money strike and out-of-the-money puts below it. Zero-day options can show different implied volatilities for the same distance up and down. Downside protection is priced richer. Upside is priced cheaper. That skew is what stochastic-volatility and local-volatility models are trying to match. The lecture names the 1994 local-volatility model, a discrete-time cousin, and the Heston model as responses after the Black-Scholes generation met the 1987 crash. The right-hand panel is one slice, the nearest expiry, with linear interpolation between strikes.
Before any code, the API has to accept socket clients. If you use the broker's volatility numbers, the lecture says to confirm they are annualized. Scaling an annual figure a second time produces absurd levels.
The idea in plain words
The application is both an API client and a wrapper. The client requests. The wrapper receives. One class stores implied volatility by request id, and a second map sends that id to a pair, expiry and strike. Spot starts at 0 until a tick arrives. Two threading events, one for the underlying contract and one for the option chain, keep the program from building a surface before those replies exist.
Connection acknowledgement prints that Trader Workstation recognized the socket. Error codes 2104, 2106, and 2158 are ignored. Other codes print the request id, the code, and the text. Contract details capture the underlying's contract id, which is how SPY the fund is separated from an option on SPY. Spot comes from request id 999, tick type 4 or 9, and only if the price is positive. The chain callback keeps the reply when the exchange is SMART, and stores sorted expiries and strikes.
Implied volatility arrives in the option-computation callback. Tick type 13, with a nonzero volatility, is stored under the request id. The lecture notes you could instead invert Black-Scholes from the option price, spot, strike, time, and a rate proxy, and that the two numbers should agree.
How to use it
The startup wrapper defaults to SPY, connects on the paper-trading port 7497 with client id 35, and runs the message loop on a background thread. The underlying contract is a USD stock routed SMART. Contract details use request id 1 and wait up to five seconds. Market data for spot uses id 999 and polls every tenth of a second until spot leaves zero. Option parameters use request id 2, also with a five-second wait.
Expiries before today are dropped, and the build keeps a short front slice of what remains. Strikes are kept only from 98 percent of spot to 102 percent. Farther strikes are called a liquidity problem, and widening that band toward 50 percent and 150 percent is how the lecture says the interpolation starts to look strange. From request id 1000 upward, each kept expiry and strike becomes an option contract. The right is a call when the strike is at or above spot, and a put otherwise. Generic tick 106 asks for implied volatility. A short pause spaces the subscriptions.
What usually goes wrong
Hard-coding strikes or expiries fails as soon as spot moves or the front expiry dies. Requesting the whole listed chain pulls illiquid wings, and forward-fill then paints a smooth surface across missing quotes. The lecture flags calendar arbitrage and butterfly arbitrage as the deeper constraints that simple fill-in ignores.
Questions people ask
Which strikes are kept?
Those between 0.98 and 1.02 times the current spot. Expiries earlier than today are dropped, and only a short front list of the remaining dates is plotted.
Where does the implied volatility come from?
Tick type 13 on the option-computation callback, stored by request id. You can also back it out of the option price with Black-Scholes. The lecture says the two should match.
What does the lock button do?
It freezes the drawing so the camera and the skew stay still. Unlocking lets the half-second refresh resume. Quotes are still arriving either way.