You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

美式期权计算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_option function 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 integer 12 to the N parameter. Inside the function, you try to use N(d1) like a function—but since N is now an integer, you can't call it, hence the error.
  • Naming conflict: You imported norm from scipy.stats (the normal distribution function), but your function has a parameter named N. 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 assume 12 is 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, and from 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

  1. 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.
  2. Renamed N to num_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).
  3. Used norm.cdf explicitly: Replaced the incorrect N(d1) with the proper scipy.stats.norm.cdf to calculate the cumulative normal distribution.
  4. Cleaned up imports: Removed redundant imports to avoid name collisions and confusion.
  5. Added error handling: Catches invalid option types to prevent silent failures.
  6. Adjusted T to years: Option pricing formulas use time in years, so we changed 365 to 1 (representing 1 year). If you meant 365 days, you could use 365/365 = 1 or adjust accordingly for other time frames.

内容的提问来源于stack exchange,提问作者Ankit Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:31:12