Black Scholes Model in Python: Step-By-Step Guide

In the world of finance, the Black-Scholes-Merton model stands out as a pivotal tool for pricing options. Lets create the Black Scholes model in Python. Developed through rigorous mathematical derivations, this model calculates the theoretical value of an option based on five essential parameters:

      1. Underlying Price (S): The current market price of the asset.

      1. Strike Price (K): The predetermined price at which the option can be exercised.

      1. Time to Expiration (T): The time left (in years) until the option’s expiration date.

      1. Risk Free Rate (r): The constant rate of return on a risk-free asset, such as a government bond.

      1. Volatility (σ): A measure of how much the price of the underlying asset fluctuates.

    With these components in mind, let’s dive into the implementation in Python:

    Step 1: Import Necessary Libraries

    Before we begin our calculations, it’s crucial to have the right tools at our disposal. Thus, we’ll import the required Python libraries:

    				
    					import math
    from scipy.stats import norm
    
    				
    			

    Step 2: Define the Variables

    Here, we’ll establish the variables based on the parameters mentioned earlier:

    				
    					S = 45  # Underlying Price
    K = 40  # Strike Price
    T = 2   # Time to Expiration
    r = 0.1 # Risk-Free Rate
    vol = 0.1 # Volatility (σ)
    
    				
    			

    Step 3: Calculate d1

    With our variables set, we first compute d1, a crucial intermediary value:

    				
    					d1 = (math.log(S/K) + (r + 0.5 * vol**2)*T ) / (vol * math.sqrt(T))
    
    				
    			

    Step 4: Calculate d2

    Subsequently, we derive d2:

    Here’s the code:

    				
    					d2 = d1 - (vol * math.sqrt(T))
    
    				
    			

    Step 5: Calculate Call Option Price

    To determine the theoretical price of a call option:

    				
    					C = S * norm.cdf(d1) - K * math.exp(-r * T) * norm.cdf(d2)
    
    				
    			

    Step 6: Calculate Put Option Price

    For the put option:

    				
    					P = K * math.exp(-r * T) * norm.cdf(-d2) - S * norm.cdf(-d1)
    
    				
    			

    Step 7: Print the Results

    Lastly, let’s display our results:

    				
    					print('The value of d1 is: ', round(d1, 4))
    print('The value of d2 is: ', round(d2, 4))
    print('The price of the call option is: $', round(C, 2))
    print('The price of the put option is: $', round(P, 2))
    
    				
    			

    In conclusion, we have built the Black Scholes model in Python. With just a few lines of code, you can determine the theoretical price of both call and put options.

    Find discounts on finance certificates, courses and products here.

    Explore Top Finance Certificates

    Access official certificates from Wharton Online & Columbia Business School Executive Education, powered by Wall Street Prep. Save up to $500 with code RYAN.

    Contact Me

    Feel free to reach out to discuss your freelance project needs, and let’s collaborate on bringing your vision to life!