Created
April 29, 2014 13:35
-
-
Save audubon/11400582 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from pandas.io.data import DataReader | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from QuantLib import * | |
import urllib, urllib2,json, re, pylab,dateutil,datetime | |
class OptionCalculator(object): | |
def __init__ (self,**kwargs): | |
self.strike = kwargs['strike'] | |
self.und = kwargs['underlying'] | |
self.opttype = kwargs['opttype'] | |
self.vola = kwargs['volatility'] | |
self.expiry = kwargs['expiry'] | |
self.settle = kwargs['settle'] | |
self.irate = kwargs['irate'] | |
self.engine = kwargs['engine'] | |
self.style = kwargs['style'] | |
settlementDate=self.settle | |
riskFreeRate = FlatForward(settlementDate, float(self.irate), Actual365Fixed()) | |
# option parameters | |
#print self.style | |
if self.style == 'american': | |
exercise = AmericanExercise(settlementDate, self.expiry) | |
else: | |
exercise = EuropeanExercise(self.expiry) | |
payoff = PlainVanillaPayoff(self.opttype, float(self.strike)) | |
# market data | |
underlying = SimpleQuote(float(self.und)) | |
volatility = BlackConstantVol(settlementDate, TARGET(), float(self.vola), Actual365Fixed()) | |
dividendYield = FlatForward(settlementDate, 0.00, Actual365Fixed()) | |
self.process = BlackScholesMertonProcess(QuoteHandle(underlying), | |
YieldTermStructureHandle(dividendYield), | |
YieldTermStructureHandle(riskFreeRate), | |
BlackVolTermStructureHandle(volatility)) | |
self.option = VanillaOption(payoff, exercise) | |
def __setup (self): | |
gridPoints = 800 | |
timeSteps = 801 | |
# method: binomial | |
if self.engine in ('trigeorgis', 'lr','eqp','tian','jr','crr'): | |
self.option.setPricingEngine(BinomialVanillaEngine(self.process,self.engine,timeSteps)) | |
#amer | |
elif self.engine == 'Barone-Adesi-Whaley': | |
self.option.setPricingEngine(BaroneAdesiWhaleyEngine(self.process)) | |
elif self.engine == 'Bjerksund-Stensland': | |
self.option.setPricingEngine(BjerksundStenslandEngine(self.process)) | |
elif self.engine == 'finitediff': | |
if self.style == 'american': | |
self.option.setPricingEngine(FDAmericanEngine(self.process,timeSteps,gridPoints)) | |
else: | |
self.option.setPricingEngine(FDEuropeanEngine(self.process,timeSteps,gridPoints)) | |
# | |
elif self.engine=='analytic': | |
self.option.setPricingEngine(AnalyticEuropeanEngine(self.process)) | |
elif self.engine=='integral': | |
self.option.setPricingEngine(IntegralEngine(self.process)) | |
def calculate (self): | |
self.__setup() | |
try: | |
npv=self.option.NPV() | |
except: | |
npv=None | |
try: | |
delta =self.option.delta() | |
except: | |
delta=None | |
try: | |
gamma=self.option.gamma() | |
except: | |
gamma=None | |
try: | |
vega=self.option.vega() | |
except: | |
vega=None | |
try: | |
theta=self.option.theta() | |
except: | |
theta=None | |
return [npv,delta,gamma,vega,theta] | |
def impliedvol(self,price): | |
self.__setup() | |
iv = None | |
try: | |
iv = self.option.impliedVolatility(price, self.process) | |
except: | |
pass | |
return iv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment