def calc_σ(self, V): try: self.__verify(V) except ValueError: return np.NaN σ = self.__σinit n = 0 σdiff = 1 while σdiff >= self.__tol and n < self.__nmax: bs = BlackScholes(self.__S, self.__K, self.__r, self.__T, σ, self.__t, self.__q) c = bs.call() if self.__type == 'C' else bs.put() if c == V: return σ vega = self.__vega(bs.get_d1()) if vega == 0: return np.NaN increment = (c - V) / vega σ -= increment n += 1 σdiff = abs(increment) return σ
def blackScholes(): data = request.json strike = float(data['strike']) stockPrice = float(data['stockPrice']) volatility = float(data['volatility']) / 100 interestRate = float(data['interestRate']) / 100 repoRate = float(data['repoRate']) / 100 optionType = data['optionType'] time = float(data['time']) blackScholes = BlackScholes(S=stockPrice, K=strike, r=interestRate, T=time, σ=volatility, q=repoRate) if (optionType == 'call'): return str(round(blackScholes.call(), 2)) else: return str(round(blackScholes.put(), 2))
def barrier_downandout(self, barrier): sum = 0 bsm = BlackScholes(self.__S, self.__K, self.__r, self.__Δ, self.__σ) for j in range(0, self.__m): sT = self.__S out = False for i in range(0, int(self.__n)): z = np.random.normal() sT *= np.exp((self.__r - 0.5 * self.__σ * self.__σ) * self.__dt + self.__σ * z * np.sqrt(self.__dt)) if sT < barrier: out = True if self.__option_type == 'call': if not out: sum += bsm.call() if self.__option_type == 'put': if not out: sum += bsm.put() return sum / self.__m
def barrier_upandin(self, barrier): sum = 0 np.random.seed(51) bsm = BlackScholes(self.__S, self.__K, self.__r, self.__Δ, self.__σ) for j in range(0, self.__m): sT = self.__S inop = False for i in range(0, int(self.__n)): z = np.random.normal() sT *= np.exp((self.__r - 0.5 * self.__σ * self.__σ) * self.__dt + self.__σ * z * np.sqrt(self.__dt)) if sT > barrier: inop = True if self.__option_type == 'call': if inop == True: sum += bsm.call() if self.__option_type == 'put': if inop == True: sum += bsm.put() return sum / self.__m