Esempio n. 1
0
def implied_volatility(price, S, K, t, r, flag):
    """Calculate the Black-Scholes implied volatility.

    :param price: the Black-Scholes option price
    :type price: float
    :param S: underlying asset price
    :type S: float
    :param K: strike price
    :type K: float
    :param t: time to expiration in years
    :type t: float
    :param r: risk-free interest rate
    :type r: float
    :param flag: 'c' or 'p' for call or put.
    :type flag: str 
    
    >>> S = 100
    >>> K = 100
    >>> sigma = .2
    >>> r = .01
    >>> flag = 'c'
    >>> t = .5

    >>> price = black_scholes(flag, S, K, t, r, sigma)
    >>> iv = implied_volatility(price, S, K, t, r, flag)

    >>> print price, iv
    5.87602423383 0.2
    """

    adjusted_price = price / e**(-r * t)

    return lets_be_rational.implied_volatility_from_a_transformed_rational_guess(
        adjusted_price, forward_price(S, t, r), K, t, binary_flag[flag])
Esempio n. 2
0
def implied_volatility_limited_iterations(price, S, K, t, r, flag, N):

    """Calculate the Black-Scholes implied volatility with limited iterations.

    :param price: the Black-Scholes option price
    :type price: float
    :param S: underlying asset price
    :type S: float
    :param K: strike price
    :type K: float
    :param t: time to expiration in years
    :type t: float
    :param r: risk-free interest rate
    :type r: float
    :param flag: 'c' or 'p' for call or put.
    :type flag: str  
    :param N: the maximum number of iterations to perform
    :type N: int

    >>> S = 100
    >>> K = 100
    >>> sigma = .232323232
    >>> r = .01
    >>> flag = 'c'
    >>> t = .5

    >>> price = black_scholes(flag, S, K, t, r, sigma)
    >>> iv = implied_volatility_limited_iterations(price, S, K, t, r, flag,1)

    >>> print price, iv    
    6.78242400926 0.232323232
    """  

    adjusted_price = price / e**(-r*t)

    return lets_be_rational.implied_volatility_from_a_transformed_rational_guess_with_limited_iterations(
        adjusted_price, 
        forward_price(S, t, r), 
        K, 
        t, 
        binary_flag[flag],
        N
    )
Esempio n. 3
0
def implied_volatility(price, S, K, t, r, flag):


    """Calculate the Black-Scholes implied volatility.

    :param price: the Black-Scholes option price
    :type price: float
    :param S: underlying asset price
    :type S: float
    :param K: strike price
    :type K: float
    :param t: time to expiration in years
    :type t: float
    :param r: risk-free interest rate
    :type r: float
    :param flag: 'c' or 'p' for call or put.
    :type flag: str 
    
    >>> S = 100
    >>> K = 100
    >>> sigma = .2
    >>> r = .01
    >>> flag = 'c'
    >>> t = .5

    >>> price = black_scholes(flag, S, K, t, r, sigma)
    >>> iv = implied_volatility(price, S, K, t, r, flag)

    >>> print price, iv
    5.87602423383 0.2
    """  

    adjusted_price = price / e**(-r*t)

    return lets_be_rational.implied_volatility_from_a_transformed_rational_guess(
        adjusted_price, 
        forward_price(S, t, r), 
        K, 
        t, 
        binary_flag[flag]
    )
Esempio n. 4
0
def implied_volatility_limited_iterations(price, S, K, t, r, flag, N):
    """Calculate the Black-Scholes implied volatility with limited iterations.

    :param price: the Black-Scholes option price
    :type price: float
    :param S: underlying asset price
    :type S: float
    :param K: strike price
    :type K: float
    :param t: time to expiration in years
    :type t: float
    :param r: risk-free interest rate
    :type r: float
    :param flag: 'c' or 'p' for call or put.
    :type flag: str  
    :param N: the maximum number of iterations to perform
    :type N: int

    >>> S = 100
    >>> K = 100
    >>> sigma = .232323232
    >>> r = .01
    >>> flag = 'c'
    >>> t = .5

    >>> price = black_scholes(flag, S, K, t, r, sigma)
    >>> iv = implied_volatility_limited_iterations(price, S, K, t, r, flag,1)

    >>> print price, iv    
    6.78242400926 0.232323232
    """

    adjusted_price = price / e**(-r * t)

    return lets_be_rational.implied_volatility_from_a_transformed_rational_guess_with_limited_iterations(
        adjusted_price, forward_price(S, t, r), K, t, binary_flag[flag], N)