Skip to content

Main content

Black–Scholes Option Calculator Source Code

Browse the implementation files for Black–Scholes Option Calculator.
tool.ts typescript
import { parseFiniteNumber } from '../../parse/number/tool';

const normalCdf = (value: number): number => {
	const sign = value < 0 ? -1 : 1;
	const x = Math.abs(value) / Math.sqrt(2);
	const t = 1 / (1 + 0.3275911 * x);
	const erf =
		sign *
		(1 -
			((((1.061405429 * t - 1.453152027) * t + 1.421413741) *
				t -
				0.284496736) *
				t +
				0.254829592) *
				t *
				Math.exp(-x * x));
	return 0.5 * (1 + erf);
};

export const blackScholesPrice = (
	spotInput: unknown,
	strikeInput: unknown,
	yearsInput: unknown,
	rateInput: unknown,
	volatilityInput: unknown,
	typeInput: unknown
): string => {
	const spot = parseFiniteNumber(spotInput);
	const strike = parseFiniteNumber(strikeInput);
	const years = parseFiniteNumber(yearsInput);
	const rate = parseFiniteNumber(rateInput);
	const volatility = parseFiniteNumber(volatilityInput);
	if (
		spot === null ||
		spot <= 0 ||
		strike === null ||
		strike <= 0 ||
		years === null ||
		years <= 0 ||
		rate === null ||
		volatility === null ||
		volatility <= 0 ||
		(typeInput !== 'call' && typeInput !== 'put')
	)
		return '';
	const d1 =
		(Math.log(spot / strike) +
			(rate + volatility ** 2 / 2) * years) /
		(volatility * Math.sqrt(years));
	const d2 = d1 - volatility * Math.sqrt(years);
	const discount = strike * Math.exp(-rate * years);
	const price =
		typeInput === 'call'
			? spot * normalCdf(d1) - discount * normalCdf(d2)
			: discount * normalCdf(-d2) - spot * normalCdf(-d1);
	return Number.isFinite(price)
		? String(Number(price.toFixed(6)))
		: '';
};

export const Tool: TypeTool = {
	name: 'Black–Scholes Option Calculator',
	slug: 'finance/black-scholes',
	intro:
		'Estimate a European call or put price with the dividend-free Black–Scholes model.',
	tool: blackScholesPrice,
	i: [
		{
			example: '100',
			label: 'Spot Price',
			placeholder: 'Current asset price',
			type: 'number'
		},
		{
			example: '100',
			label: 'Strike Price',
			placeholder: 'Option strike',
			type: 'number'
		},
		{
			example: '1',
			label: 'Years to Expiration',
			placeholder: 'Time in years',
			type: 'number'
		},
		{
			example: '0.05',
			label: 'Risk-free Rate',
			placeholder: 'Decimal annual rate',
			type: 'number'
		},
		{
			example: '0.2',
			label: 'Volatility',
			placeholder: 'Decimal annual volatility',
			type: 'number'
		},
		{
			example: 'call',
			label: 'Option Type',
			options: [
				{ text: 'Call', value: 'call' },
				{ text: 'Put', value: 'put' }
			],
			placeholder: 'Call or put',
			type: 'select'
		}
	],
	o: [
		{
			example: '10.450576',
			label: 'Option Value',
			placeholder: 'Option value will appear here...',
			type: 'number'
		}
	]
};
export default Tool;

Description

Browse the implementation files for Black–Scholes Option Calculator.

References

  1. Tool.io on GitHub
  2. Tool.io TypeScript tools library

Similar or alternative tools