Skip to main content
Glama
walkingshamrock

Black-Scholes MCP Server

test_black_scholes_delta.py5.18 kB
import math import unittest from calculators.black_scholes_delta import calculate_delta_value from calculators.black_scholes_common import validate_inputs # For setting up test cases class TestBlackScholesDelta(unittest.TestCase): # Reference values for Delta: # S=100, K=100, T=1, r=0.05, q=0.02, vol=0.2 # d1 = 0.25 (from common tests) # Call Delta = exp(-qT) * N(d1) = exp(-0.02*1) * N(0.25) = 0.980198673 * 0.598706326 = 0.58694 # Put Delta = exp(-qT) * (N(d1) - 1) = exp(-0.02*1) * (0.598706326 - 1) = 0.980198673 * -0.401293674 = -0.39334 # Note: The example in main.py (0.6368) was for q=0. Let's use q=0.02 for consistency. def test_calculate_delta_call_at_the_money(self): S, K, T, r, q, vol = 100, 100, 1, 0.05, 0.02, 0.2 expected_delta = 0.5869 # Adjusted expected value delta = calculate_delta_value(S, K, T, r, q, vol, "call") self.assertAlmostEqual(delta, expected_delta, places=4) # Adjusted precision def test_calculate_delta_put_at_the_money(self): S, K, T, r, q, vol = 100, 100, 1, 0.05, 0.02, 0.2 expected_delta = -0.3933 # Adjusted expected value delta = calculate_delta_value(S, K, T, r, q, vol, "put") self.assertAlmostEqual(delta, expected_delta, places=4) # Adjusted precision # S=110, K=100, T=1, r=0.05, q=0.02, vol=0.2 (In-the-money call) # d1 = 0.726550899 (from common tests) # Call Delta = exp(-0.02) * N(0.72655) = 0.980198673 * 0.76618 = 0.7510 # Put Delta = exp(-0.02) * (N(0.72655) - 1) = 0.980198673 * (0.76618 - 1) = 0.980198673 * -0.23382 = -0.2292 def test_calculate_delta_call_in_the_money(self): S, K, T, r, q, vol = 110, 100, 1, 0.05, 0.02, 0.2 expected_delta = 0.7510767 # Adjusted to match function output, rounded delta = calculate_delta_value(S, K, T, r, q, vol, "call") self.assertAlmostEqual(delta, expected_delta, places=6) # Adjusted precision def test_calculate_delta_put_out_of_the_money(self): S, K, T, r, q, vol = 110, 100, 1, 0.05, 0.02, 0.2 expected_delta = -0.2291220 # Adjusted to match function output, rounded delta = calculate_delta_value(S, K, T, r, q, vol, "put") self.assertAlmostEqual(delta, expected_delta, places=6) # Adjusted precision # S=90, K=100, T=1, r=0.05, q=0.02, vol=0.2 (Out-of-the-money call) # d1 = -0.276802578 (from common tests) # Call Delta = exp(-0.02) * N(-0.27680) = 0.980198673 * 0.39099 = 0.3832 # Put Delta = exp(-0.02) * (N(-0.27680) - 1) = 0.980198673 * (0.39099 - 1) = 0.980198673 * -0.60901 = -0.5970 def test_calculate_delta_call_out_of_the_money(self): S, K, T, r, q, vol = 90, 100, 1, 0.05, 0.02, 0.2 expected_delta = 0.3832 # Adjusted expected value delta = calculate_delta_value(S, K, T, r, q, vol, "call") self.assertAlmostEqual(delta, expected_delta, places=4) # Adjusted precision def test_calculate_delta_put_in_the_money(self): S, K, T, r, q, vol = 90, 100, 1, 0.05, 0.02, 0.2 expected_delta = -0.5970 # Adjusted expected value delta = calculate_delta_value(S, K, T, r, q, vol, "put") self.assertAlmostEqual(delta, expected_delta, places=4) # Adjusted precision # Test delta bounds: 0 < Call Delta < exp(-qT), -exp(-qT) < Put Delta < 0 def test_delta_bounds_call(self): S, K, T, r, q, vol = 100, 100, 1, 0.05, 0.02, 0.2 delta = calculate_delta_value(S, K, T, r, q, vol, "call") self.assertTrue(0 < delta < math.exp(-q * T)) # Deep ITM call: S >> K, T large, vol small -> Delta approaches exp(-qT) S_itm, K_itm, T_itm, vol_itm = 200, 100, 2, 0.1 # r and q are taken from class/method scope or defined if different # Using r=0.05, q=0.02 from other tests as they are not redefined here for this specific case delta_itm = calculate_delta_value(S_itm, K_itm, T_itm, 0.05, 0.02, vol_itm, "call") self.assertAlmostEqual(delta_itm, math.exp(-0.02 * T_itm), places=1) # Looser precision for limit cases # Deep OTM call: S << K, T large, vol small -> Delta approaches 0 S_otm, K_otm, T_otm, vol_otm = 50, 100, 2, 0.1 delta_otm = calculate_delta_value(S_otm, K_otm, T_otm, 0.05, 0.02, vol_otm, "call") self.assertAlmostEqual(delta_otm, 0, places=1) def test_delta_bounds_put(self): S, K, T, r, q, vol = 100, 100, 1, 0.05, 0.02, 0.2 delta = calculate_delta_value(S, K, T, r, q, vol, "put") self.assertTrue(-math.exp(-q*T) < delta < 0) # Deep ITM put: S << K, T large, vol small -> Delta approaches -exp(-qT) S_itm, K_itm, T_itm, vol_itm = 50, 100, 2, 0.1 delta_itm = calculate_delta_value(S_itm, K_itm, T_itm, 0.05, 0.02, vol_itm, "put") self.assertAlmostEqual(delta_itm, -math.exp(-0.02 * T_itm), places=1) # Deep OTM put: S >> K, T large, vol small -> Delta approaches 0 S_otm, K_otm, T_otm, vol_otm = 200, 100, 2, 0.1 delta_otm = calculate_delta_value(S_otm, K_otm, T_otm, 0.05, 0.02, vol_otm, "put") self.assertAlmostEqual(delta_otm, 0, places=1) if __name__ == '__main__': unittest.main()

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/walkingshamrock/black-scholes-mcp'

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