tblackscholes.py

Created by matthieumorvant

Created on February 05, 2024

1.6 KB


import math

# Fonction pour l'approximation de la fonction de répartition normale
def norm_cdf(x):
    t = 1 / (1 + 0.2316419 * abs(x))
    d = 0.3989423 * math.exp(-x * x / 2)
    probability = d * t * (0.3193815 + t * (-0.3565638 + t * (1.781478 + t * (-1.821256 + t * 1.330274))))
    if x >= 0:
        return 1 - probability
    else:
        return probability

# Demander à l'utilisateur d'entrer les données nécessaires
S0 = float(input("Valeur de l'action (S0) : "))
K = float(input("Prix d'exercice (K) : "))
T = float(input("Echeance en mois (T) : ")) / 12  # Convertir en années
r = float(input("Taux sans risque (r) : ")) / 100  # Convertir en décimal
sigma = float(input("Volatilite (sigma) : ")) / 100  # Convertir en décimal

# Calcul de D1 et D2
D1 = (math.log(S0 / K) + (r + (sigma**2) / 2) * T) / (sigma * math.sqrt(T))
D2 = D1 - sigma * math.sqrt(T)

# Calcul de N(D1) et N(D2) en utilisant l'approximation
N_D1 = norm_cdf(D1)
N_D2 = norm_cdf(D2)

# Calcul de la valeur du call
C = S0 * N_D1 - K * math.exp(-r * T) * N_D2

# Calcul de la valeur du put en utilisant la parité call-put
P = C - S0 + K * math.exp(-r * T)

# Afficher les résultats et les formules
print("D1: {:.10f} (Formule: (ln({}/{}) + ({} + {}^2/2)*{}) / ({}*sqrt({})))".format(D1, S0, K, r, sigma, T, sigma, T))
print("D2: {:.10f} (Formule: {:.10f} - {}*sqrt({}))".format(D2, D1, sigma, T))
print("Valeur du Call: {:.10f} (Formule: {} * {:.10f} - {} * exp(-{} * {}) * {:.10f})".format(C, S0, N_D1, K, r, T, N_D2))
print("Valeur du Put: {:.10f} (Formule: {:.10f} - {} + {} * exp(-{} * {}))".format(P, C, S0, K, r, T))

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.