예제 #1
0
def get_futures_ivol_by_series(futures_series=None,
                               maturity_type=None,
                               start_date=history_default_start_date,
                               end_date=market_data_date,
                               option_type="call",
                               option_deltas=None):

    # Get series id
    series_id = db.get_futures_series(futures_series)['id'].values.tolist()

    # Handle maturity type choice
    if maturity_type == 'constant_maturity':
        table_name = "futures_ivol_constant_maturity_by_delta"
    elif maturity_type == 'futures_contract':
        table_name = "futures_ivol_fixed_maturity_by_delta"
    else:
        raise ValueError("maturity_type must be constant_maturity"
                         " or futures_contract")

    # TODO: move this to someplace central
    if option_type.lower() == "c":
        option_type = "call"
    if option_type.lower() == "p":
        option_type = "put"
    if not option_type.lower() in ["put", "call"]:
        raise ValueError("option_type must be put or call")

    where_str = " option_type = '{0}'".format(option_type)
    where_str += " and date >= '{0}'".format(start_date)
    where_str += " and date <= '{0}'".format(end_date)
    where_str += " and series_id in {0}".format(
        dbutils.format_for_query(series_id))

    # Case: specific delta requested
    if option_deltas is not None and option_type is not None:
        delta_str = list()
        for delta in option_deltas:
            if delta in option_delta_grid:
                if delta < 10:
                    delta_str.append('ivol_0' + str(delta) + 'd')
                else:
                    delta_str.append('ivol_' + str(delta) + 'd')
        s = " select series_id, date, days_to_maturity, {0}".format(delta_str)
        s += " from " + table_name
    else:
        s = " select * from " + table_name
    s += " where " + where_str + " order by date asc, days_to_maturity asc"
    data = db.read_sql(s)
    return data
예제 #2
0
def get_cftc_positioning_by_series(futures_series=None,
                                   start_date=market_data_date,
                                   end_date=None,
                                   cftc_financial=True,
                                   columns=None):

    table_name = 'staging_cftc_positioning_financial'
    if not cftc_financial:
        table_name = 'staging_cftc_positioning_commodity'

    if columns is None:
        columns = [
            'report_date_as_mm_dd_yyyy', 'open_interest_all',
            'dealer_positions_long_all', 'dealer_positions_short_all',
            'asset_mgr_positions_long_all', 'asset_mgr_positions_short_all',
            'lev_money_positions_long_all', 'lev_money_positions_short_all'
        ]

    # Ensure that we get the most recent data
    one_week_prior = start_date - BDay(5)

    fs = db.get_futures_series(futures_series)
    if len(fs) == 0:
        return None

    cftc_code = str(fs['cftc_code'].values[0])

    s = "select {0}".format(columns)\
            .replace('[', '').replace(']', '').replace("'", '') + \
        " from " + table_name + \
        ' where cftc_contract_market_code in {0}'.format(
        dbutils.format_for_query([cftc_code]))

    s += " and report_date_as_mm_dd_yyyy >= '{0}'".format(one_week_prior)
    if end_date is not None:
        s += " and report_date_as_mm_dd_yyyy <= '{0}'".format(end_date)

    s += ' order by report_date_as_mm_dd_yyyy asc'
    data = db.read_sql(s)
    data = data.rename(columns={'report_date_as_mm_dd_yyyy': 'date'})

    return data
예제 #3
0
def get_futures_calendar_name(futures_series=None):
    s = db.get_futures_series(futures_series=futures_series)
    exchange_code = s.iloc[0]['exchange']
    calendar_name = utils.DateUtils.exchange_calendar_map[exchange_code]
    return calendar_name