Exemple #1
0
def _weekly_start_end_date_handler(start_date, end_date):
    start_date = to_date(start_date)
    monday = start_date - datetime.timedelta(days=start_date.weekday())
    first_trading_day_in_week = monday if is_trading_date(monday) else get_next_trading_date(monday)
    if first_trading_day_in_week < start_date:
        start_date = monday + datetime.timedelta(weeks=1)

    end_date = to_date(end_date)
    if end_date > datetime.date.today():
        end_date = datetime.date.today()
    friday = end_date - datetime.timedelta(days=end_date.weekday()) + datetime.timedelta(days=4)
    last_trading_day_in_week = friday if is_trading_date(friday) else get_previous_trading_date(friday)
    if last_trading_day_in_week > end_date:
        end_date = friday - datetime.timedelta(weeks=1)

    return to_date_int(start_date), to_date_int(end_date)
Exemple #2
0
def get_factor_covariance(date, horizon='daily'):
    """ 获取因子协方差矩阵

    :param date: str 日期(例如:‘2017-03-20’)
    :param horizon: str 预测期限。默认为日度('daily'),可选月度(‘monthly’)或季度('quarterly')。

    :return: pandas.DataFrame,其中 index 和 column 均为因子名称。
    """
    date = get_previous_trading_date(get_next_trading_date(date))
    date = ensure_date_int(date)
    ensure_string_in(horizon, HORIZON_CONTAINER, 'horizon')

    df = get_client().execute('get_factor_covariance', date, horizon)
    if not df:
        return
    df = pd.DataFrame(df)
    df.drop("date", axis=1, inplace=True)
    return df.reindex(columns=df.index)
Exemple #3
0
def get_current_trading_date(dt):
    if 7 <= dt.hour < 18:
        return datetime.datetime(year=dt.year, month=dt.month, day=dt.day)
    return get_next_trading_date(dt - datetime.timedelta(hours=4))
Exemple #4
0
def get_tick_price_multi_df(order_book_ids, start_date, end_date, fields, market):
    ins_list = ensure_instruments(order_book_ids)
    order_book_ids = [ins.order_book_id for ins in ins_list]
    types = {ins.type for ins in ins_list}

    start_date, end_date = ensure_date_range(start_date, end_date, datetime.timedelta(days=3))

    if "Future" in types or "Option" in types:
        base_fields = FUTURE_TICK_FIELDS
        base_columns = FUTURE_TICK_COLUMNS
    else:
        base_fields = EQUITIES_TICK_FIELDS
        base_columns = EQUITIES_TICK_COLUMNS

    if fields:
        fields = ensure_list_of_string(fields, "fields")
        check_items_in_container(fields, base_fields, "fields")
        columns = [f for f in base_columns if f in fields]
    else:
        fields = base_fields
        columns = base_columns

    gtw_fields = set(fields) | {"date", "time"}
    if set(fields) & RELATED_DABAR_FIELDS:
        gtw_fields.update({"volume", "last"})

    history_df = get_history_tick(order_book_ids, start_date, end_date, list(gtw_fields), columns, market)
    history_latest_date = 0 if history_df is None else date_to_int8(get_current_trading_date(
                                                            history_df.index.get_level_values(1).max()))
    today = today_int()
    next_trading_date = date_to_int8(get_next_trading_date(today, market=market))
    if history_latest_date >= end_date or start_date > next_trading_date or end_date < today:
        return history_df

    if end_date >= next_trading_date and (start_date > today or history_latest_date >= today):
        live_date = next_trading_date
    else:
        live_date = today
    if history_latest_date >= live_date:
        return history_df
    live_dfs = []
    for ins in ins_list:
        try:
            live_df = get_ticks(ins.order_book_id, start_date=live_date, end_date=live_date, expect_df=True,
                                market=market)
        except (PermissionDenied, MarketNotSupportError):
            pass
        else:
            if live_df is None:
                continue
            if "trading_date" not in live_df.columns:
                live_df["trading_date"] = int8_to_datetime(live_date)
            else:
                live_df["trading_date"] = live_df["trading_date"].apply(to_datetime)
            if ins.type in ("Future", "Option"):
                live_df["change_rate"] = live_df["last"] / live_df["prev_settlement"] - 1
            else:
                live_df["change_rate"] = live_df["last"] / live_df["prev_close"] - 1
            live_df = live_df.reindex(columns=columns)
            live_dfs.append(live_df)

    if not live_dfs:
        return history_df

    if history_df is None:
        return pd.concat(live_dfs)
    return pd.concat([history_df] + live_dfs)