def option_model_wrapper(**kwargs): # enter underlying, strike, (option_price or implied_vol) # calculation_date, exercise_type, option_type con = msu.get_my_sql_connection(**kwargs) ticker = kwargs['ticker'] calculation_date = kwargs['calculation_date'] if 'interest_rate_date' in kwargs.keys(): interest_rate_date = kwargs['interest_rate_date'] else: interest_rate_date = calculation_date #print(ticker) #print(kwargs['exercise_type']) contract_specs_output = cmi.get_contract_specs(ticker) ticker_head = contract_specs_output['ticker_head'] contract_multiplier = cmi.contract_multiplier[ticker_head] expiration_datetime = exp.get_expiration_from_db(ticker=ticker, instrument='options', con=con) expiration_date = int(expiration_datetime.strftime('%Y%m%d')) if 'con' not in kwargs.keys(): con.close() interest_rate = grfs.get_simple_rate( as_of_date=interest_rate_date, date_to=expiration_date)['rate_output'] if np.isnan(interest_rate): option_greeks = { 'implied_vol': np.NaN, 'delta': np.NaN, 'vega': np.NaN, 'dollar_vega': np.NaN, 'theta': np.NaN, 'dollar_theta': np.NaN, 'gamma': np.NaN, 'dollar_gamma': np.NaN, 'interest_rate': np.NaN, 'cal_dte': np.NaN } else: option_greeks = qom.get_option_greeks(risk_free_rate=interest_rate, expiration_date=expiration_date, **kwargs) option_greeks['implied_vol'] = 100 * option_greeks['implied_vol'] option_greeks[ 'dollar_vega'] = option_greeks['vega'] * contract_multiplier / 100 option_greeks[ 'dollar_theta'] = option_greeks['theta'] * contract_multiplier option_greeks[ 'dollar_gamma'] = option_greeks['gamma'] * contract_multiplier option_greeks['interest_rate'] = interest_rate return option_greeks
def cal_greeks_4option_maturity(**kwargs): option_prices = gop.get_options_price_from_db(**kwargs) if option_prices.empty: return pd.DataFrame() option_prices = option_prices[option_prices['strike'] > 0] option_prices.reset_index(drop=True, inplace=True) contract_specs_out = cmi.get_contract_specs(kwargs['ticker']) exercise_type = cmi.get_option_exercise_type(**contract_specs_out) underlying_ticker = oput.get_option_underlying(**kwargs) futures_price_output = gfp.get_futures_price_preloaded( ticker=underlying_ticker, settle_date=kwargs['settle_date']) if futures_price_output.empty: return pd.DataFrame() underlying_price = futures_price_output['close_price'].iloc[0] expiration_datetime = exp.get_expiration_from_db(instrument='options', **kwargs) expiration_date = int(expiration_datetime.strftime('%Y%m%d')) interest_rate = grfs.get_simple_rate( as_of_date=kwargs['settle_date'], date_to=expiration_date)['rate_output'] option_greeks = [ qom.get_option_greeks(underlying=underlying_price, option_price=float( option_prices['close_price'].iloc[x]), strike=float(option_prices['strike'].iloc[x]), risk_free_rate=interest_rate, expiration_date=expiration_date, calculation_date=kwargs['settle_date'], option_type=option_prices['option_type'].iloc[x], exercise_type=exercise_type) for x in range(len(option_prices.index)) ] greek_frame = pd.DataFrame(option_greeks) return pd.concat([ greek_frame[['delta', 'gamma', 'implied_vol', 'theta', 'vega']], option_prices ], axis=1)
def cal_greeks_4option_maturity(**kwargs): option_prices = gop.get_options_price_from_db(**kwargs) if option_prices.empty: return pd.DataFrame() contract_specs_out = cmi.get_contract_specs(kwargs['ticker']) exercise_type = cmi.get_option_exercise_type(**contract_specs_out) underlying_ticker = oput.get_option_underlying(**kwargs) futures_price_output = gfp.get_futures_price_preloaded(ticker=underlying_ticker, settle_date=kwargs['settle_date']) if futures_price_output.empty: return pd.DataFrame() underlying_price = futures_price_output['close_price'].iloc[0] expiration_datetime = exp.get_expiration_from_db(instrument='options', **kwargs) expiration_date = int(expiration_datetime.strftime('%Y%m%d')) interest_rate = grfs.get_simple_rate(as_of_date=kwargs['settle_date'], date_to=expiration_date)['rate_output'] #print(kwargs['settle_date']) #print(expiration_date) option_greeks = [qom.get_option_greeks(underlying=underlying_price, option_price=float(option_prices['close_price'].iloc[x]), strike=float(option_prices['strike'].iloc[x]), risk_free_rate=interest_rate, expiration_date=expiration_date, calculation_date=kwargs['settle_date'], option_type=option_prices['option_type'].iloc[x], exercise_type=exercise_type) for x in range(len(option_prices.index))] greek_frame = pd.DataFrame(option_greeks) return pd.concat([greek_frame[['delta', 'gamma', 'implied_vol', 'theta', 'vega']], option_prices], axis=1)
def calc_imp_vols(self): vcs_pairs = self.vcs_pairs vcs_pairs['validQ'] = False print('YYYYUUUUUPPPP!') for i in range(len(vcs_pairs.index)): for j in [1, 2]: if np.isnan(vcs_pairs.loc[i, 'current_strike' + str(j)]): continue call_option_ticker_string = vcs_pairs.loc[ i, 'ticker' + str(j)] + '_C_' + str( vcs_pairs.loc[i, 'current_strike' + str(j)]) put_option_ticker_string = vcs_pairs.loc[ i, 'ticker' + str(j)] + '_P_' + str( vcs_pairs.loc[i, 'current_strike' + str(j)]) ib_underlying_multiplier = ib_contract.ib_underlying_multiplier_dictionary.get( vcs_pairs.loc[i, 'tickerHead'], 1) if (self.bid_price_dictionary[call_option_ticker_string] > 0 ) and (self.ask_price_dictionary[call_option_ticker_string] > 0): vcs_pairs.loc[i, 'call_mid_price' + str(j)] = ( self.bid_price_dictionary[call_option_ticker_string] + self.ask_price_dictionary[call_option_ticker_string] ) / (2 * ib_underlying_multiplier) option_greeks = qom.get_option_greeks( underlying=vcs_pairs.loc[i, 'underlying_mid_price' + str(j)], option_price=vcs_pairs.loc[i, 'call_mid_price' + str(j)], strike=vcs_pairs.loc[i, 'current_strike' + str(j)], risk_free_rate=vcs_pairs.loc[i, 'interest_date' + str(j)], expiration_date=vcs_pairs.loc[i, 'expiration_date' + str(j)], calculation_date=self.todays_date, option_type='C', exercise_type=vcs_pairs.loc[i, 'exercise_type']) vcs_pairs.loc[i, 'call_iv' + str(j)] = 100 * option_greeks['implied_vol'] if (self.bid_price_dictionary[put_option_ticker_string] > 0 ) and (self.ask_price_dictionary[put_option_ticker_string] > 0): vcs_pairs.loc[i, 'put_mid_price' + str(j)] = ( self.bid_price_dictionary[put_option_ticker_string] + self.ask_price_dictionary[put_option_ticker_string] ) / (2 * ib_underlying_multiplier) option_greeks = qom.get_option_greeks( underlying=vcs_pairs.loc[i, 'underlying_mid_price' + str(j)], option_price=vcs_pairs.loc[i, 'put_mid_price' + str(j)], strike=vcs_pairs.loc[i, 'current_strike' + str(j)], risk_free_rate=vcs_pairs.loc[i, 'interest_date' + str(j)], expiration_date=vcs_pairs.loc[i, 'expiration_date' + str(j)], calculation_date=self.todays_date, option_type='P', exercise_type=vcs_pairs.loc[i, 'exercise_type']) vcs_pairs.loc[i, 'put_iv' + str(j)] = 100 * option_greeks['implied_vol'] for j in [1, 2]: vcs_pairs['straddle_iv' + str(j)] = (vcs_pairs['put_iv' + str(j)] + vcs_pairs['call_iv' + str(j)]) / 2 vcs_pairs['straddle_price' + str(j)] = (vcs_pairs['call_mid_price' + str(j)] + vcs_pairs['put_mid_price' + str(j)]) vcs_pairs['current_atm_vol_ratio'] = vcs_pairs[ 'straddle_iv1'] / vcs_pairs['straddle_iv2'] for i in range(len(vcs_pairs.index)): if np.isnan(vcs_pairs.loc[i, 'current_atm_vol_ratio']): continue intraday_vcs_output = ic.get_intraday_vcs( report_date=self.report_date, ticker1=vcs_pairs.loc[i, 'ticker1'], ticker2=vcs_pairs.loc[i, 'ticker2'], atm_vol_ratio=vcs_pairs.loc[i, 'current_atm_vol_ratio']) vcs_pairs.loc[i, 'QC'] = intraday_vcs_output['Q'] vcs_pairs.loc[i, 'Q1C'] = intraday_vcs_output['Q1'] vcs_pairs.loc[i, 'validQ'] = intraday_vcs_output['validQ'] writer = pd.ExcelWriter('C:\Research\daily\kuzu.xlsx') vcs_pairs.to_excel(writer, 'Sheet1') writer.save() self.vcs_pairs = vcs_pairs self.prepare_orders()