Обсчет цены опциона на Python

Jan 20, 2014 23:43

Небольшой код на Python для обсчета цены 1-го опциона. Сразу поставить WinPython дабы не мучаться.

from scipy.stats import norm
from math import log
from math import sqrt
from math import pi
from math import exp
from math import e

class BlackScholes:
    #инициализатор
    def __init__(self, price, strike, time, volatility, interestRate, dividend, lookingPrice):
        self.price = float(price)
        self.strike = float(strike)
        self.time = float(time)
        self.volatility = float(volatility)
        self.interestRate = float(interestRate)
        self.dividend = float(dividend)
        self.lookingPrice = float(lookingPrice)

#расчет коэфициентов
    def d1(self):
        d1 = (log(self.price / self.strike) + (self.interestRate - self.dividend + (0.5 * pow(self.volatility, 2)) * self.time))/(self.volatility * sqrt(self.time))
        return d1

def d2(self):
        d2 = self.d1() - self.volatility * sqrt(self.time)
        return d2

def Nd1(self):
        Nd1 = norm.cdf(self.d1())
        return Nd1

def Nd2(self):
        Nd2 = norm.cdf(self.d2())
        return Nd2

def Nnd1(self):
        Nnd1 = exp(-self.d1() * self.d1() * 0.5) / sqrt(2 * pi)
        return Nnd1

def thetaUnit(self):
        thetaUnit = -1 / 365
        return thetaUnit

#расчет значений опционов
    def callPrice(self):
        callPrice = (self.price * exp(-self.dividend * self.time) * norm.cdf(self.d1())) - (self.strike * exp(-self.interestRate * self.time) * norm.cdf(self.d2()))
        return callPrice

def putPrice(self):
        putPrice  = (-1 * self.price * exp(-self.dividend * self.time) * norm.cdf(-1 * self.d1())) - (-1 * self.strike * exp(-self.interestRate * self.time) * norm.cdf(-1 * self.d2()))
        return putPrice

def deltaCall(self):
        deltaCall = self.Nd1()
        return deltaCall

def deltaPut(self):
        deltaPut = self.deltaCall() - 1
        return deltaPut

def gamma(self):
        gamma = (self.Nnd1()) / (self.price * self.volatility * sqrt(self.time))
        return gamma

def theta(self):
        theta = self.thetaUnit() * ((self.price * self.volatility * self.Nnd1()) / ((2*sqrt(self.time)) + self.strike * self.interestRate * e * (-self.interestRate * self.time) * self.Nd2()))
        return theta

def vega(self):
        vega = self.price * sqrt(self.time) * self.Nnd1() * 0.01
        return vega

def ratioThetaVega(self):
        if self.theta() < 0:
            return -1 * self.theta() / self.vega() * 100
        else:
            return self.theta() / self.vega() * 100

def stepOfRehedge(self):
        stepOfRehedge = 1 / self.gamma()
        return stepOfRehedge

#вероятность достижения ценой какого либо заданного значения
    def normalLowerPrice(self):
        v = self.volatility * sqrt(self.time)
        normalLowerPrice = norm.cdf(log((self.price) / self.lookingPrice) / v ) * 100
        return normalLowerPrice

def normalHigherPrice(self):
        normalHigherPrice = (1 - self.normalLowerPrice() / 100) * 100
        return normalHigherPrice

#вывод значений
    def print(self):
        print('Call price: %0.2f' % self.callPrice())
        print('Put price: %0.2f' % self.putPrice())
        print('Delta call: %0.2f' % self.deltaCall())
        print('Delta put: %0.2f' % self.deltaPut())
        print('Gamma: %0.6f' % self.gamma())
        print('Theta: %0.2f' % self.theta())
        print('Vega: %0.2f' % self.vega())
        print('Theta / Vega: %0.2f' % self.ratioThetaVega() + '%')
        print('Step of rehedge: %0.0f' % self.stepOfRehedge())
        print('Вероятность что ниже = % 0.2f' % self.normalLowerPrice() + '%')
        print('Вероятность что выше = % 0.2f' % self.normalHigherPrice() + '%')

#для проверки промежуточных значений
        #print('d1: %0.10f' % self.d1())
        #print('d2: %0.10f' % self.d2())
        #print('Nd1: %0.10f' % self.Nd1())
        #print('Nd2: %0.10f' % self.Nd2())
        #print('Nnd1: %0.10f' % self.Nnd1())
        #print('thetaUnit: %0.10f' % self.thetaUnit())

Программа для запуска самого кода:
===============================
from Option.BlackScholesModel import BlackScholes

o = BlackScholes(136860, 135000, 30/365, 0.229, 0.0, 0, 135000)
o.print()

options, rts, опционы, python

Previous post Next post
Up