Price options using Black-Scholes in Python

Price options using Black-Scholes in Python
The biggest myth in options trading:
Traders only use models for getting the fair value of options.
Option traders need models not just for pricing, but for managing risk through the Greeks.
The Greeks change constantly with market conditions, and without proper tools to compute them, you're exposed to unexpected losses in volatile markets.
Here's the good news: Python makes it easy to compute the Greeks and use them for trading.
You can understand your portfolio's exposure and build precise hedging strategies.
By reading today's newsletter, you'll get Python code to calculate Black-Scholes values, Delta, and Gamma.
Let's go!
Price options using Black-Scholes in Python
The Black-Scholes formula is the cornerstone of option pricing in modern finance. It gives you a fast, theoretical price for European options based on market inputs like volatility, time, and interest rates.
Black-Scholes revolutionized options pricing in the early 1970s. Fischer Black, Myron Scholes, and Robert Merton cracked the code by modeling how option values evolve over time with market uncertainty. Their work reshaped how markets approach risk, making pricing transparent and systematic.
That breakthrough led to today’s standard for trading, risk management, and regulatory reporting.
Now, pros rely on models like Black-Scholes to quote options instantly, extract implied volatility, and compute Greeks that drive real-time risk management. It’s the backbone for portfolio hedging, market making, and employee stock option valuations.
Let’s see how it works with Python.
Imports and setup
We use these libraries to grab stock data from the web and handle math calculations. This gives us fast access to reliable information and powerful number-crunching tools.
1import numpy as np
2import yfinance as yf
Here, we're setting up all the key variables for our analysis. We define what stock ticker to use, what type of option to price, and set all the basic financial stats.
1symbol = "PLTR"
2option_type = "call"
3risk_free_rate = 0.0425
4time_to_expiry = 81 / 365
5strike_price = 140
We'll use this setup every time we want to price an option. These values set the framework for all calculations that follow and can easily swap in new values as needed.
Pull and prepare market data
We're reaching out to the web to grab recent stock prices. This lets us see where the stock has been trading over the past 60 days and gives us the current price to use in our model.
1data = yf.download(symbol, period="60d", interval="1d", progress=False)
2close_prices = data["Close"]
3spot_price = close_prices.iat[-1, 0]
4returns = np.log(close_prices.PLTR / close_prices.PLTR.shift(1)).dropna()
5volatility = np.std(returns) * np.sqrt(252)
We grab the full set of historical closing prices, then pull the very latest price for our calculations. The returns are the day-to-day changes in price, and measuring how much those fluctuate tells us how volatile the stock has been. We adjust that measure to reflect a one-year timeframe, which aligns with standard practice for option pricing.
Calculate Black-Scholes parameters
Here we're calculating important numbers used in the Black-Scholes option pricing model. These values help us figure out the expected risk and potential payout of the option.
1d1 = (np.log(spot_price / strike_price) + (risk_free_rate + 0.5 * volatility ** 2) * time_to_expiry) / (volatility * np.sqrt(time_to_expiry))
2d2 = d1 - volatility * np.sqrt(time_to_expiry)
By running these calculations, we set up the groundwork for fair pricing. We factor in how far the current stock price is from the strike, how interest rates nudge values, and how much the price bounces around. The outputs serve as building blocks for the final option price and sensitivity.
Compute option price and Greeks
Now we calculate the fair price of the option, plus sensitivity measures that tell us how that price moves with the market. This gives us the full picture on both expected cost and risk.
1if option_type == "call":
2 option_price = spot_price * np.exp(-0) * 0.5 * (1 + np.math.erf(d1 / np.sqrt(2))) - strike_price * np.exp(-risk_free_rate * time_to_expiry) * 0.5 * (1 + np.math.erf(d2 / np.sqrt(2)))
3 delta = 0.5 * (1 + np.math.erf(d1 / np.sqrt(2)))
4else:
5 option_price = strike_price * np.exp(-risk_free_rate * time_to_expiry) * 0.5 * (1 - np.math.erf(d2 / np.sqrt(2))) - spot_price * np.exp(-0) * 0.5 * (1 - np.math.erf(d1 / np.sqrt(2)))
6 delta = -0.5 * (1 - np.math.erf(d1 / np.sqrt(2)))
7
8gamma = np.exp(-d1 ** 2 / 2) / (spot_price * volatility * np.sqrt(2 * np.pi * time_to_expiry))
We decide if we're pricing a call or put, and then follow the path for the right formula. We get the expected option price, and also calculate how much the price could wiggle as the stock moves (Delta and Gamma). This gives us everything we need to judge whether an option is fairly priced or too risky to buy.
1option_price, delta, gamma
We finish by lining up our final answers: What would we pay for the option, and how would the price respond if the market moves? These results make it easy to weigh potential trades with real numbers in hand.
Your next steps
You can now calculate option prices and Greeks for any stock using real data. Try swapping in a different ticker or adjusting the strike price at the top. Change the time-to-expiry or option type to see how those shifts impact pricing and risk. That’s the fastest way to see how these factors shape real-world trades.
