def implied_vol(CallPutFlag, price, underlying, strike, time_to_expiry, interest): if CallPutFlag == 1: return iv.implied_volatility(price, underlying, strike, time_to_expiry, interest, 'c') else: return iv.implied_volatility(price, underlying, strike, time_to_expiry, interest, 'p')
def test_implied_volatility(self): while self.tdi.has_next(): row = self.tdi.next_row() S,K,t,r,sigma = row['S'],row['K'],row['t'],row['R'],row['v'] C,P = black_scholes('c', S, K, t, r, sigma), black_scholes('p', S, K, t, r, sigma) try: iv = implied_volatility(C, S, K, t, r, 'c') self.assertTrue(almost_equal(sigma, iv, epsilon = .0001)) except: print 'could not calculate iv for ', C, S, K, t, r, 'c' iv = implied_volatility(P, S, K, t, r, 'p') self.assertTrue(almost_equal(sigma, iv, epsilon = .001) or (iv ==0.0))
def implied_vol_calc(exp, fut, option): global rate iv_fun = lambda x: implied_volatility(x['SETTLE_PR'], fut, x[ 'STRIKE_PR'], exp, 0.1, 'c' if x.OPTION_TYP == 'CE' else 'p') * 100 #vollib.black_scholes.greeks.numerical.delta(flag, S, K, t, r, sigma) option['VOLATILITY'] = option.apply(iv_fun, axis=1) delta_fun = lambda x: delta(('c' if x.OPTION_TYP == 'CE' else 'p'), fut, x[ 'STRIKE_PR'], exp, 0.1, x.VOLATILITY / 100) gamma_fun = lambda x: gamma(('c' if x.OPTION_TYP == 'CE' else 'p'), fut, x[ 'STRIKE_PR'], exp, 0.1, x.VOLATILITY / 100) vega_fun = lambda x: vega(('c' if x.OPTION_TYP == 'CE' else 'p'), fut, x[ 'STRIKE_PR'], exp, 0.1, x.VOLATILITY / 100) theta_fun = lambda x: theta(('c' if x.OPTION_TYP == 'CE' else 'p'), fut, x[ 'STRIKE_PR'], exp, 0.1, x.VOLATILITY / 100) rho_fun = lambda x: rho(('c' if x.OPTION_TYP == 'CE' else 'p'), fut, x[ 'STRIKE_PR'], exp, 0.1, x.VOLATILITY / 100) option['DELTA'] = option.apply(delta_fun, axis=1) option['GAMMA'] = option.apply(gamma_fun, axis=1) option['VEGA'] = option.apply(vega_fun, axis=1) option['THETA'] = option.apply(theta_fun, axis=1) option['RHO'] = option.apply(rho_fun, axis=1) #Remove option with delta <0.05 for calls and delta >-0.05 for puts option = option[(option.DELTA > 0.05) | (option.DELTA < -0.05)] #Remove where no contracts are being traded option = option[option.CONTRACTS != 0] option['FUT'] = fut return option
def test_implied_volatility(self): while self.tdi.has_next(): row = self.tdi.next_row() S, K, t, r, sigma = row['S'], row['K'], row['t'], row['R'], row[ 'v'] C, P = black_scholes('c', S, K, t, r, sigma), black_scholes('p', S, K, t, r, sigma) try: iv = implied_volatility(C, S, K, t, r, 'c') self.assertTrue(almost_equal(sigma, iv, epsilon=.0001)) except: print('could not calculate iv for ', C, S, K, t, r, 'c') iv = implied_volatility(P, S, K, t, r, 'p') self.assertTrue( almost_equal(sigma, iv, epsilon=.001) or (iv == 0.0))
def calc_iv_greeks(self): '''(price, S, K, t, r, flag (flag, S, K, t, r, sigma)''' global r self.vol=implied_volatility(self.price,self.underlying,self.strike,self.timedelta,r,self.flag) self.delta=delta(self.flag,self.underlying,self.strike,self.timedelta,r,self.vol) self.gamma=gamma(self.flag,self.underlying,self.strike,self.timedelta,r,self.vol) self.vega=vega(self.flag,self.underlying,self.strike,self.timedelta,r,self.vol) self.theta=theta(self.flag,self.underlying,self.strike,self.timedelta,r,self.vol)
def implied_vol_calc(exp,fut,option): global rate iv_fun=lambda x:implied_volatility(x['SETTLE_PR'],fut,x['STRIKE_PR'],exp,0.1,'c' if x.OPTION_TYP=='CE' else 'p')*100 #vollib.black_scholes.greeks.numerical.delta(flag, S, K, t, r, sigma) option['VOLATILITY']=option.apply(iv_fun,axis=1) delta_fun=lambda x:delta(('c' if x.OPTION_TYP=='CE' else 'p'),fut,x['STRIKE_PR'],exp,0.1,x.VOLATILITY/100) gamma_fun=lambda x:gamma(('c' if x.OPTION_TYP=='CE' else 'p'),fut,x['STRIKE_PR'],exp,0.1,x.VOLATILITY/100) vega_fun=lambda x:vega(('c' if x.OPTION_TYP=='CE' else 'p'),fut,x['STRIKE_PR'],exp,0.1,x.VOLATILITY/100) theta_fun=lambda x:theta(('c' if x.OPTION_TYP=='CE' else 'p'),fut,x['STRIKE_PR'],exp,0.1,x.VOLATILITY/100) rho_fun=lambda x:rho(('c' if x.OPTION_TYP=='CE' else 'p'),fut,x['STRIKE_PR'],exp,0.1,x.VOLATILITY/100) option['DELTA']=option.apply(delta_fun,axis=1) option['GAMMA']=option.apply(gamma_fun,axis=1) option['VEGA']=option.apply(vega_fun,axis=1) option['THETA']=option.apply(theta_fun,axis=1) option['RHO']=option.apply(rho_fun,axis=1) #Remove option with delta <0.05 for calls and delta >-0.05 for puts option=option[(option.DELTA>0.05) | (option.DELTA<-0.05)] #Remove where no contracts are being traded option=option[option.CONTRACTS!=0] option['FUT']=fut return option
data = json.load(f) else: sys.exit('ERROR: given input file does not exist') # Iterate json entries and calculate greeks using Black-Scholes for entry in data: last_price = entry['last_price'] K = entry['strike'] S = config.underlying_price # underlying_price session_date = datetime.strptime(config.session_date, "%d%m%Y").date() opt_date = datetime.strptime(entry['expiration_date'], "%d/%m/%Y").date() t = (opt_date - session_date).days / 365. # time to expiration (in years) r = config.risk_free_rate flag = entry['right'].lower() sigma = iv.implied_volatility(last_price, S, K, t, r, flag) print(sigma, last_price, S, K, t, r, flag) sigma = 0 if sigma < 1e-10 else sigma entry['iv'] = sigma entry['delta'] = gk.delta(flag, S, K, t, r, sigma) entry['gamma'] = gk.gamma(flag, S, K, t, r, sigma) entry['theta'] = gk.theta(flag, S, K, t, r, sigma) * 365 entry['vega'] = gk.vega(flag, S, K, t, r, sigma) # Remove existing json file and save the new one with the greeks #os.remove(config.input_json) TODO os.remove('test2.json') #TODO with open('test2.json', 'w') as f: json.dump(data, f, indent=4)