Fintech 535 · Closing the loop
My Nightly Volatility Algorithm
Every night at 10pm, it asks one question about Microsoft: are options expensive or cheap right now?
What I built
For this assignment I built a trading algorithm that runs on its own every night at 10pm. It looks at Microsoft and asks one question: are options on MSFT expensive or cheap right now? If the answer is "very," it trades. If not, it does nothing. Either way, it writes its decision to a file called open_orders.json, and our course system, Sanji, picks it up from there.
In class we said an algorithm is just a function that takes inputs and spits out open orders. Mine takes in Microsoft's prices, option data from LSEG, and my current positions, and turns them into tomorrow's orders.
Why volatility?
Last homework I backtested the wheel, and I didn't want to just do the same thing again. Volatility trading felt like a natural next step.
The basic idea is that the options market is always making a guess about how much a stock will move. That guess is implied volatility. Then the stock actually moves however it moves, and that's realized volatility. The two don't usually match. In fact, implied vol tends to come in a bit higher than what actually happens, which is why people talk about a "variance risk premium" (Carr & Wu, 2009). But that's not always the case, and sometimes options are actually cheap. So I wanted an algorithm that could go both ways.
Microsoft is the stock the course server tracks for my repo.
How it decides
Every night, the algorithm works out two numbers.
The first is implied volatility. I look at the MSFT options expiring about a week out, pick the strike closest to the current price, and take the average implied vol of the call and the put. That's the market's forecast for the coming week.
The second is realized volatility, which is how much MSFT has actually been moving. I take the last 21 daily returns, compute their standard deviation, and annualize it.
Then I subtract one from the other. If IV is a lot higher than RV, options look expensive. If it's a lot lower, they look cheap. The problem is that "a lot" depends on the period: a gap that looks huge in a calm month might be totally normal in a crazy one. So instead of using the raw gap, I compare it with its own last three months (63 trading days) using a z-score. That tells me how unusual today's gap really is.
If z is above +2, options are unusually expensive, so I sell a straddle.
If z is below −2, they're unusually cheap, so I buy one.
Otherwise, I sit out.
I picked 2 because it's the standard cutoff for mean-reversion trades. It's the same threshold Gatev, Goetzmann and Rouwenhorst used in their well-known pairs-trading paper (2006).
How the trades work
A straddle is just a call and a put at the same strike with the same expiry. Buying one is a bet that the stock will move a lot, in either direction. Selling one is a bet that it'll stay calm. Once you hedge out the direction, what's left is pretty much a bet on realized vol versus implied vol, which is exactly what my signal is measuring.
I only open new trades on Fridays, and each one expires the next Friday, so every trade lasts a week. I trade 100 contracts on each leg at the at-the-money strike, using a limit order at the mid. Once a trade is on, I hold it until expiry.
Every day in between, the algorithm hedges. It works out how much my options would gain or lose if MSFT moved a dollar (their delta) and buys or sells enough shares to cancel that out. When the straddle expires, there's nothing left to hedge, so any shares I'm still holding get closed.
Here's what a sell signal actually looks like in Sanji's order language:
{"id": "msft-straddle-call-2026-11-27", "for": "2026-11-27", "order_type": "lmt",
"action": "SHT", "qty": 100, "underlying": 272093, "contract": "call",
"expiry": "5DTE+", "strike": "atm", "ref": "open", "lmt_prc": "mid"}
There's a matching one for the put. For a buy signal, SHT just becomes BUY.
How it runs on its own
I set this up the same way as Professor Vestal's nightly refresh. Every night at 10pm, a GitHub Action wakes up and logs in to LSEG with my credentials, which are stored as GitHub secrets. It then runs two scripts. The first, vol_signal.py, adds the newest day to my volatility history. The second, vol_orders.py, reads the latest signal and my positions and writes open_orders.json. Then it commits everything back to my repo.
For stock prices I use the course server's price_history.json, so my algorithm works off the same data Sanji does. That file only goes back to September 22, though, which isn't enough for a three-month z-score. So I filled in the earlier dates with closing prices from LSEG. I double-checked that the two sources agree, and they both have MSFT at $508.29 on November 18.
Where things stand
As of November 19, MSFT's one-week implied vol was 34.6% and its realized vol was 36.9%. So options were actually a little cheap, but only slightly: the z-score was −0.32, nowhere near −2. Over the last couple of months the z-score has stayed between about −0.5 and +1.8, so the algorithm hasn't traded yet. That's on purpose. It's only supposed to act when things get really out of line.
Both charts read data/vol_history.csv, which the nightly run updates, so they stay current on their own.