Skip to main content Skip to footer

How to Create a JavaScript Option Pricing Calculator Using the Black-Scholes Model

Black-Scholes is a pricing model used to determine the fair price or theoretical value for a call or a put option based on six variables such as volatility, type of option (call, put), underlying stock price, time, strike price, and the risk-free rate. It is largely used by options traders who buy options priced under the calculated value and sell options priced higher than the Black-Scholes calculated value.

Assumptions of the Black-Scholes Model:

  • Returns are independent over time and are normally distributed
  • The direction of the stock’s price can’t be consistently predicted and is completely random
  • The volatility is constant and known in advance
  • The risk-free rate is constant and given in advance
  • Zero transaction costs
  • Markets are perfectly liquid, so it is possible to buy or sell any quantity at any time
  • No dividends

Some advantages of this model include:

  • Speed, as it allows you to calculate various prices of European put and call options in a short time span.
  • It lets investors make better-informed investment decisions when it comes to investing in European options.

The limitations of this model include:

  • The assumptions are not always realistic (interest rates may not stay the same).
  • A lot of stocks pay dividends, so the formula cannot be used for those.
  • It can only be used for European options.
  • The underlying stock’s returns are assumed to be normally distributed, which is not always the case.

This blog will elaborate on how you can create an option pricing calculator based on the Black-Scholes model that you can use in your financial (FinTech) applications.

Black-Scholes Inputs

There are five basic inputs (parameters) to set when pricing an option using the Black-Scholes model. They are the following:

  • S0 = underlying price (USD per share)
  • X = strike price (USD per share)
  • σ = volatility (%)
  • r = risk-free interest rate (%)
  • t = time to expiration (% of the year)

Below are some more details and notes concerning individual inputs.

  • Underlying price - is the price at which the underlying security is trading on the market at the moment you are doing the option pricing.
  • Strike price - also called exercise price, is the price at which you will buy (call) or sell (put) the underlying security if you choose to exercise the option.
  • Volatility - degree of variation of a trading price series over time, usually measured by the standard deviation of logarithmic returns.
  • Risk-free interest rate - represents the interest an investor would expect from a risk-free investment over a specified period.
  • Time to expiration/maturity - time of expiration in years; for example, if the maturity time is 24 days, the parameter would be t=24/365 = 0.067.

Below is a screenshot of the inputs that we are using to calculate the options price.

Input

Calculations

SpreadJS includes a comprehensive Calculation Engine that supports 450 built-in functions, custom functions, array functions, dynamic arrays, the formula textbox, localized language function names, statistical functions, financial functions and so much more that you can use to create various calculators needed for your FinTech applications.

To calculate the Call and Put option, we have used the below functions:

  • NORM.S.DIST(value, TRUE) - is used to calculate the normal cumulative distribution of a value.
  • LN(value) - is used to determine the natural logarithm of a number.
  • EXP(value) - calculates the exponential value of a floating-point argument x.
  • SQRT(value) - square root of a value

Returning to the model, call option (C) and put option (P) prices are calculated using the following formulas:

C = S0 N(d1) - X e-rt * N(d2)

P= X e-rt N(-d2) - S0 * N(-d1)

where N(x) is the standard normal cumulative distribution function, the d1 and d2 are some auxiliary variables, used to simplify the formula.

The formulas for d1 and d2 are:

d1= [ln(S0/X) + t (r + σ2/2)]/ σ √t

d2 = d1 - σ * √t

Image2

The above table contains some calculations that would help us to determine the option prices.

After having calculated the auxiliary variables, we can continue to measure the option prices in the correct cells: In our example:

Image3

To calculate the Call and Put option prices when the inputs are different from the one above, you can change the inputs in the sheet and see the correct option prices calculated immediately.

Charts

Now that we have our model, we can go a step further and look at how the different input parameters affect the results.

We have presented in the table below the changes of Call and Pull Prices when one of the parameters changes and the others are static.

So, in the first part of the table, we have calculated the Call and Put option Price if the Exercise price, time, risk rate, and volatility stay the same (exactly as they are in our Model Inputs table) Stock Price changes. The same logic applies to the other inputs.

Image4

Below are some charts that show the effect that the Black-Scholes model inputs have on the Call and Put option.

Stock Price (S0)

The below chart explains how the Call and Put values change if the other inputs are static, and we change the Stock Price from $1 to $200. We notice that the Call Option has no value up until the stock price hits $90. We see the reverse for the Put Option, where its value hits zero once the stock price goes above $120.

Image5

Exercise Price (X)

If the other inputs stay static, we see that the Call Value goes down as the Exercise Price increases and the Put Value goes up.

Image6

Time to maturity (t)

As the time to maturity increases, when the other inputs are static, the pricing options also increase.

Image7

Annual Risk-Free Rate (r)

As the risk-free rate increases, this also increases the required rate of returns. Subsequently, this decreases the value of stocks. We notice a direct relationship. As the risk-free rate increases, the Call Option value increases as well, while the Put Option value decreases its worth.

Image8

Annualized volatility (σ)

Volatility expresses the bounce-around the ability of the stock. When the stock volatility rises, so does the call and the Pull value as the stock becomes unpredictable.

Image9

The Black-Scholes Model is just one of the many models you can use to calculate the theoretical value of an option. You can use it anytime as you practice your strategies in the financial markets, both as a trader and an investor.

Key Takeaways

The Black-Scholes model is a theoretical concept. We use it to try and estimate the behavior of the market, as the calculated values coming from this model are very near the real values at the time of expiration. However, when using the model, its assumptions should be taken into consideration as they express the derivation from reality.

By using SpreadJS, you can embed a Black-Scholes calculator inside your JavaScript applications and let the decision-making of trading stock have the security of complex mathematical calculations.

Download the latest version of SpreadJS today and discover how easily you can embed Excel-like JavaScript spreadsheets, and much more business-needed functionalities, into your enterprise applications.

Download the example here and follow along.

Contact us to learn more at us.sales@grapecity.com and visit SpreadJS.


comments powered by Disqus