Black-Scholes Python Example

Run a European option pricing example on your computer. The ZIP includes the pricing module, a sample script and three tests. All use the Python standard library.

Download Python example and tests (.zip)

Python 3.10 or later · 3.1 KB · Version 1.0.0

Run the downloaded example

Extract the ZIP. Open a terminal in the folder containing example.py and black_scholes.py.

python example.py
First line · Call per share
10.4505835722
Second line · Put per share
5.5735260223
Check Python and run the tests

Run python --version to check for Python 3.10 or later. If your interpreter is named python3, use that name in each command.

From the extracted folder, run python -m unittest -v. All three tests should pass.

Inputs in example.py

Import price and greeks from black_scholes.py. Pass time in years and rates, yield and volatility as decimal fractions. The price function returns both call and put, in that order.

example.py
from black_scholes import price

call, put = price(S=100, K=100, T=1, r=5/100, sigma=20/100, q=0)
print(f'{call:.10f}')  # 10.4505835722
print(f'{put:.10f}')   # 5.5735260223

Read the price call in example.py as a description of the contract. S is the current share price and K is the exercise price, both in the same currency per share. T is the time left in years. Use 1 for a year or 30/365 for 30 calendar days.

The remaining inputs are annual decimal fractions. Set r=0.05 for a 5% continuously compounded risk-free rate and sigma=0.20 for 20% volatility. Use q=0 for a stock paying no dividends or q=0.02 for a 2% continuous dividend yield. If you omit q, the function uses zero.

Check the Python prices for one year, 30 days and expiry

These results come from running price() in the downloadable Python module. Keep K=100, r=0.05, sigma=0.20 and q=0 to match the Excel and Google Sheets examples. Set S and T as shown in each row. The first returned value is the call and the second is the put.

Verified Python results in dollars per share
Scenario and inputsCallPut
One year (S=100, T=1)10.450583575.57352602
30 days (S=100, T=30/365)2.493376822.08326120
At expiry (S=120, T=0)20.000000000.00000000

For the 30-day row, pass T=30/365 so Python evaluates the fraction in years. In the expiry row, the stock is at $120 and the strike is $100, so the call pays $20 per share and the put pays zero.

Compare these prices with the other tools

Compare call and put prices at three strikes

Save this code as compare_strikes.py in the extracted folder beside black_scholes.py. Run python compare_strikes.py in the same terminal. Each line prints the strike, the call premium and the put premium. With dollar inputs, both premiums are in dollars per share.

from black_scholes import price

for strike in (95, 100, 105):
    call, put = price(
        S=100, K=strike, T=1,
        r=0.05, sigma=0.20, q=0,
    )
    print(f"{strike} {call:.2f} {put:.2f}")
Expected output with the stock at $100 and one year to expiry
StrikeCallPut
9513.353.71
10010.455.57
1058.027.90

Why does a higher strike lower the call price?

A call lets you buy at the strike. Raising it from $95 to $105 makes that right less valuable, so the call falls from $13.35 to $8.02. A put lets you sell at the strike, so the same change raises its value from $3.71 to $7.90. Only the strike changes in the loop.

Use Vega to check a one-point change in volatility

From the extracted folder, import greeks alongside price. Call greeks(S=100, K=100, T=1, r=0.05, sigma=0.20) and read the vega_per_point entry in the returned dictionary. It should be about 0.37524 currency units per underlying unit. That is the estimated price change for a move from 20% volatility to 21%.

Run price twice with the same inputs, first with sigma=0.20 and then with sigma=0.21. The call rises from about 10.45058 to 10.82629. Its increase of about 0.37570 is close to the Vega estimate. The small difference reflects the change in sensitivity over that interval.

Test a position on its expiry date

Try price(S=95, K=100, T=0, r=0.05, sigma=0.20). The returned pair is (0, 5). Exercising the call has no value at 95, while the put lets its holder sell for 5 above spot. The greeks function raises ValueError at T=0. If you loop through contracts with different maturities, keep the expiry payoff and skip the smooth-Greeks call for that row.

Calculation method

The module uses math.erfc for the normal CDF, price parity for the in-the-money leg, and explicit expiry and deterministic branches.

Scope and limitations

The package covers European pricing and smooth Greeks. Python 3.10 or later is required.

References

Related tools and resources