美式期权计算Python代码报错:TypeError: 'int' object is not callable
Hey there, let's break down why you're hitting that TypeError: 'int' object is not callable error and fix your code step by step.
Root Cause of the Error
The main issue boils down to two critical mistakes:
- Parameter order mismatch: Your
american_optionfunction is defined to take parameters in this order:(type_s, S0, sigma, r, d, K, N, T). But when you call it, you pass(100, 0.3, 0.1, 0.05, 110, 12, 365,). That means you're passing the integer12to theNparameter. Inside the function, you try to useN(d1)like a function—but sinceNis now an integer, you can't call it, hence the error. - Naming conflict: You imported
normfromscipy.stats(the normal distribution function), but your function has a parameter namedN. You were mistakenly trying to use this parameter as the cumulative normal distribution function, which is a mix-up between your intended step count (I assume12is the number of steps) and the statistical function.
Other Issues to Clean Up
- Redundant imports: You have overlapping imports like
import math,from math import exp, sqrt, andfrom math import *—the last one overwrites names and causes confusion. Stick to explicit imports. - Missing argument: Your function expects 8 parameters, but your call only has 7 (the trailing comma doesn't count as an argument).
- American vs European pricing: Note that your current code calculates a European option price, not American—true American pricing requires logic for early exercise, which isn't present here.
Corrected Code
import sys from random import gauss from math import exp, sqrt, log from scipy.stats import norm def american_option(type_s, S0, sigma, r, d, K, num_steps, T): # Calculate d1 and d2 correctly with proper parentheses d1 = (log(float(S0)/K) + ((r - d + sigma**2 / 2) * T)) / (sigma * sqrt(T)) d2 = d1 - sigma * sqrt(T) if type_s == 'call': # Use norm.cdf for cumulative normal distribution m = S0 * exp(-d*T) * norm.cdf(d1) - K * exp(-r*T) * norm.cdf(d2) elif type_s == 'put': m = K * exp(-r*T) * norm.cdf(-d2) - S0 * exp(-d*T) * norm.cdf(-d1) else: # Add error handling for invalid option types raise ValueError("Option type must be 'call' or 'put'") return m # Call the function with CORRECT parameter order and complete arguments # Example: type_s='call', S0=100, sigma=0.3, r=0.1, d=0.05, K=110, num_steps=12, T=1 (1 year) result = american_option('call', 100, 0.3, 0.1, 0.05, 110, 12, 1) print(result)
Key Fixes Explained
- Fixed parameter order: The function call now starts with the option type (
'call'or'put') followed by all parameters in the order the function expects. - Renamed
Ntonum_steps: Eliminates the naming conflict with the normal distribution function. This clarifies that the parameter represents the number of steps (even though we don't use it in this simplified European pricing code). - Used
norm.cdfexplicitly: Replaced the incorrectN(d1)with the properscipy.stats.norm.cdfto calculate the cumulative normal distribution. - Cleaned up imports: Removed redundant imports to avoid name collisions and confusion.
- Added error handling: Catches invalid option types to prevent silent failures.
- Adjusted
Tto years: Option pricing formulas use time in years, so we changed365to1(representing 1 year). If you meant 365 days, you could use365/365 = 1or adjust accordingly for other time frames.
内容的提问来源于stack exchange,提问作者Ankit Sharma
相关产品推荐
相关产品推荐

