Skip to main content
Glama
nbiish
by nbiish

black_scholes

Calculate option prices using the Black-Scholes model for financial analysis. Input asset price, strike price, time to expiration, risk-free rate, and volatility to determine call or put option values.

Instructions

Calculate Black-Scholes option price

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
SYesCurrent price of the asset
KYesStrike price of the option
TYesTime to expiration in years
rYesRisk-free interest rate
sigmaYesVolatility of the asset
optionTypeNoOption type: "call" or "put"call

Implementation Reference

  • The handler function executed when the 'black_scholes' tool is called, which destructures inputs and invokes the core Black-Scholes function.
    async ({ S, K, T, r, sigma, optionType }) => {
      return blackScholes(S, K, T, r, sigma, optionType);
    }
  • Zod schemas defining the input parameters (S, K, T, r, sigma, optionType) and output (number or error string) for the black_scholes tool.
    inputSchema: z.object({
      S: z.number().describe('Current price of the asset'),
      K: z.number().describe('Strike price of the option'),
      T: z.number().describe('Time to expiration in years'),
      r: z.number().describe('Risk-free interest rate'),
      sigma: z.number().describe('Volatility of the asset'),
      optionType: z.enum(['call', 'put']).default('call')
        .describe('Option type: "call" or "put"')
    }),
    outputSchema: z.union([z.number(), z.string()]),
  • index.js:551-569 (registration)
    Registration of the 'black_scholes' tool using ai.defineTool, including name, description, schemas, and handler.
    ai.defineTool(
      {
        name: 'black_scholes',
        description: 'Calculate Black-Scholes option price',
        inputSchema: z.object({
          S: z.number().describe('Current price of the asset'),
          K: z.number().describe('Strike price of the option'),
          T: z.number().describe('Time to expiration in years'),
          r: z.number().describe('Risk-free interest rate'),
          sigma: z.number().describe('Volatility of the asset'),
          optionType: z.enum(['call', 'put']).default('call')
            .describe('Option type: "call" or "put"')
        }),
        outputSchema: z.union([z.number(), z.string()]),
      },
      async ({ S, K, T, r, sigma, optionType }) => {
        return blackScholes(S, K, T, r, sigma, optionType);
      }
    );
  • Core helper function that computes the Black-Scholes option price using the normal CDF approximation.
    const blackScholes = (S, K, T, r, sigma, optionType = 'call') => {
      try {
        const d1 = (Math.log(S / K) + (r + 0.5 * sigma * sigma) * T) / (sigma * Math.sqrt(T));
        const d2 = d1 - sigma * Math.sqrt(T);
    
        if (optionType === 'call') {
          return S * normalCDF(d1) - K * Math.exp(-r * T) * normalCDF(d2);
        } else if (optionType === 'put') {
          return K * Math.exp(-r * T) * normalCDF(-d2) - S * normalCDF(-d1);
        } else {
          throw new Error('Invalid option type. Must be "call" or "put".');
        }
      } catch (e) {
        return `Error: ${e.message}`;
      }
    };
  • Helper function approximating the cumulative distribution function (CDF) of the standard normal distribution, used in Black-Scholes.
    const normalCDF = (x) => {
      const t = 1 / (1 + 0.2316419 * Math.abs(x));
      const d = 0.3989423 * Math.exp(-x * x / 2);
      const p = d * t * (0.319381530 + t * (-0.356563782 + t * (1.781477937 + t * (-1.821255978 + t * 1.330274429))));
      return x >= 0 ? 1 - p : p;
    };

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/nbiish/mcp-calc-tools'

If you have feedback or need assistance with the MCP directory API, please join our Discord server