black_scholes
Calculate the price of call or put options using the Black-Scholes model by inputting asset price, strike price, time to expiration, risk-free rate, and volatility.
Instructions
Calculate Black-Scholes option price
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| K | Yes | Strike price of the option | |
| S | Yes | Current price of the asset | |
| T | Yes | Time to expiration in years | |
| optionType | No | Option type: "call" or "put" | call |
| r | Yes | Risk-free interest rate | |
| sigma | Yes | Volatility of the asset |
Implementation Reference
- index.js:502-517 (handler)Core Black-Scholes option pricing function that computes d1, d2 and uses normalCDF to calculate call or put price.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}`; } };
- index.js:555-564 (schema)Input and output schema definitions using Zod for the black_scholes tool parameters and return type.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 via ai.defineTool, including name, description, schema, and handler function.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); } );
- index.js:491-500 (helper)Supporting helper functions: approximate standard normal CDF (used in BS formula) and PDF (used in Greeks but defined here).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; }; const normalPDF = (x) => { return (1 / Math.sqrt(2 * Math.PI)) * Math.exp(-0.5 * x * x); };