Example #1
0
    def test_analytical_delta(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']
            self.assertTrue(
                almost_equal(analytical.delta('c', S, K, t, r, sigma),
                             row['CD'],
                             epsilon=.000001))
            self.assertTrue(
                almost_equal(analytical.delta('p', S, K, t, r, sigma),
                             row['PD'],
                             epsilon=.000001))
Example #2
0
 def test_analytical_delta(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']
         self.assertTrue(
             almost_equal(
                 analytical.delta('c', S, K, t, r, sigma), row['CD'], epsilon=.000001
             )
         )
         self.assertTrue(
             almost_equal(
                 analytical.delta('p', S, K, t, r, sigma), row['PD'], epsilon=.000001
             )
         )
Example #3
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
Example #4
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)
Example #5
0
    def test_delta(self):

        S = 100.0
        for flag in ['c', 'p']:
            for K in numpy.linspace(20, 200, 10):
                for r in numpy.linspace(0, 0.2, 10):
                    for sigma in numpy.linspace(0.1, 0.5, 10):
                        for t in numpy.linspace(0.01, 2, 10):

                            for i in range(5):
                                val1 = delta(flag, S, K, t, r, sigma)
                                val2 = ndelta(flag, S, K, t, r, sigma)
                                results_match = abs(val1 - val2) < epsilon
                                if not results_match:
                                    print flag, val1, val2
                                self.assertTrue(results_match)
Example #6
0
    def test_delta(self):
        

        S = 100.0
        for flag in ['c','p']:
            for K in numpy.linspace(20,200,10):
                for r in numpy.linspace(0,0.2,10):
                    for sigma in numpy.linspace(0.1,0.5,10):
                        for t in numpy.linspace(0.01,2,10):
                            
                            for i in range(5):
                                val1 = delta(flag, S, K, t, r, sigma)
                                val2 = ndelta(flag, S, K, t, r, sigma)
                                results_match = abs(val1-val2)<epsilon
                                if not results_match:
                                    print flag, val1, val2
                                self.assertTrue(results_match)
Example #7
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
Example #8
0
            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)
                     help='[Required] Option expiration date. Required format: ddmmyyyy')
 parser.add_argument('-r', '--risk_free_rate', type=float, required=True,
                     help='[Required] Risk free rate. Example: 0.01')
 parser.add_argument('-p', '--option_price', type=float, required=True,
                     help='[Required] Current option price')
 parser.add_argument('-ri', '--right', type=str, required=True,
                     help='[Required] Option right (C for call, P for Put)')
 config = parser.parse_args()
 
 last_price = config.option_price
 K = config.strike
 S = config.underlying_price  # underlying_price
 opt_date = datetime.strptime(config.expiration_date, "%d%m%Y").date()
 t = (opt_date - date.today()).days / 365.  # time to expiration (in years)
 r = config.risk_free_rate
 flag = config.right.lower()
 
 if flag != 'c' and flag != 'p':
     print('ERROR: flag shall be \'C\' or \'P\'')
 else:
     sigma = iv.implied_volatility(last_price, S, K, t, r, flag)
     delta = gk.delta(flag, S, K, t, r, sigma)
     gamma = gk.gamma(flag, S, K, t, r, sigma)
     theta = gk.theta(flag, S, K, t, r, sigma)
     vega = gk.vega(flag, S, K, t, r, sigma)
     
     print('Delta: ' + str(delta))
     print('Gamma: ' + str(gamma))
     print('Theta: ' + str(theta) + ' --> (annual): ' + str(theta * 365))
     print('Vega: ' + str(vega))
     print('IV: ' + str(sigma))