コード例 #1
0
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
コード例 #2
0
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)
コード例 #3
0
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)
コード例 #4
0
def main():
    app = algo.Algo()
    report_date = exp.doubledate_shift_bus_days()
    todays_date = cu.get_doubledate()
    con = msu.get_my_sql_connection()
    vcs_output = vcs.generate_vcs_sheet_4date(date_to=report_date)
    vcs_pairs = vcs_output['vcs_pairs']

    filter_out = of.get_vcs_filters(data_frame_input=vcs_pairs,
                                    filter_list=['long2', 'short2'])
    vcs_pairs = filter_out['selected_frame']

    vcs_pairs = vcs_pairs[vcs_pairs['downside'].notnull()
                          & vcs_pairs['upside'].notnull()]
    # &(vcs_pairs.tickerClass!='Energy')
    vcs_pairs = vcs_pairs[(vcs_pairs['trDte1'] >= 50)
                          & (vcs_pairs.tickerClass != 'Metal') &
                          (vcs_pairs.tickerClass != 'FX') &
                          (vcs_pairs.tickerClass != 'Energy')]
    vcs_pairs = vcs_pairs[((vcs_pairs['Q'] <= 30) &
                           (vcs_pairs['fwdVolQ'] >= 30)) |
                          ((vcs_pairs['Q'] >= 70) &
                           (vcs_pairs['fwdVolQ'] <= 70))]
    vcs_pairs.reset_index(drop=True, inplace=True)

    vcs_pairs['underlying_ticker1'] = [
        omu.get_option_underlying(ticker=x) for x in vcs_pairs['ticker1']
    ]
    vcs_pairs['underlying_ticker2'] = [
        omu.get_option_underlying(ticker=x) for x in vcs_pairs['ticker2']
    ]

    vcs_pairs['underlying_tickerhead'] = [
        cmi.get_contract_specs(x)['ticker_head']
        for x in vcs_pairs['underlying_ticker1']
    ]
    futures_data_dictionary = {
        x: gfp.get_futures_price_preloaded(ticker_head=x)
        for x in vcs_pairs['underlying_tickerhead'].unique()
    }

    proxy_output_list1 = [
        up.get_underlying_proxy_ticker(
            ticker=x,
            settle_date=report_date,
            futures_data_dictionary=futures_data_dictionary)
        for x in vcs_pairs['underlying_ticker1']
    ]
    vcs_pairs['proxy_ticker1'] = [x['ticker'] for x in proxy_output_list1]
    vcs_pairs['add_2_proxy1'] = [x['add_2_proxy'] for x in proxy_output_list1]

    proxy_output_list2 = [
        up.get_underlying_proxy_ticker(
            ticker=x,
            settle_date=report_date,
            futures_data_dictionary=futures_data_dictionary)
        for x in vcs_pairs['underlying_ticker2']
    ]
    vcs_pairs['proxy_ticker2'] = [x['ticker'] for x in proxy_output_list2]
    vcs_pairs['add_2_proxy2'] = [x['add_2_proxy'] for x in proxy_output_list2]

    vcs_pairs['expiration_date1'] = [
        int(
            exp.get_expiration_from_db(instrument='options', ticker=x,
                                       con=con).strftime('%Y%m%d'))
        for x in vcs_pairs['ticker1']
    ]
    vcs_pairs['expiration_date2'] = [
        int(
            exp.get_expiration_from_db(instrument='options', ticker=x,
                                       con=con).strftime('%Y%m%d'))
        for x in vcs_pairs['ticker2']
    ]

    vcs_pairs['interest_date1'] = [
        grfs.get_simple_rate(as_of_date=report_date, date_to=x)['rate_output']
        for x in vcs_pairs['expiration_date1']
    ]
    vcs_pairs['interest_date2'] = [
        grfs.get_simple_rate(as_of_date=report_date, date_to=x)['rate_output']
        for x in vcs_pairs['expiration_date2']
    ]
    vcs_pairs['exercise_type'] = [
        cmi.get_option_exercise_type(ticker_head=x)
        for x in vcs_pairs['tickerHead']
    ]

    admin_dir = dna.get_directory_name(ext='admin')
    risk_file_out = su.read_text_file(file_name=admin_dir +
                                      '/RiskParameter.txt')
    vcs_risk_parameter = 5 * 2 * float(risk_file_out[0])

    vcs_pairs['long_quantity'] = vcs_risk_parameter / abs(
        vcs_pairs['downside'])
    vcs_pairs['short_quantity'] = vcs_risk_parameter / vcs_pairs['upside']
    vcs_pairs['long_quantity'] = vcs_pairs['long_quantity'].round()
    vcs_pairs['short_quantity'] = vcs_pairs['short_quantity'].round()

    vcs_pairs['alias'] = [
        generate_vcs_alias(vcs_row=vcs_pairs.iloc[x])
        for x in range(len(vcs_pairs.index))
    ]

    vcs_pairs['call_mid_price1'] = np.nan
    vcs_pairs['put_mid_price1'] = np.nan
    vcs_pairs['call_mid_price2'] = np.nan
    vcs_pairs['put_mid_price2'] = np.nan
    vcs_pairs['call_iv1'] = np.nan
    vcs_pairs['put_iv1'] = np.nan
    vcs_pairs['call_iv2'] = np.nan
    vcs_pairs['put_iv2'] = np.nan
    vcs_pairs['underlying_mid_price1'] = np.nan
    vcs_pairs['underlying_mid_price2'] = np.nan
    vcs_pairs['proxy_mid_price1'] = np.nan
    vcs_pairs['proxy_mid_price2'] = np.nan
    vcs_pairs['current_strike1'] = np.nan
    vcs_pairs['current_strike2'] = np.nan

    ta_folder = dn.get_dated_directory_extension(folder_date=todays_date,
                                                 ext='ta')

    app.vcs_pairs = vcs_pairs
    app.con = con
    app.futures_data_dictionary = futures_data_dictionary
    app.report_date = report_date
    app.todays_date = todays_date
    app.log = lg.get_logger(file_identifier='vcs', log_level='INFO')
    app.trade_file = ta_folder + '/trade_dir.csv'
    app.vcs_risk_parameter = vcs_risk_parameter
    app.connect(client_id=3)
    app.run()