Esempio n. 1
0
def get_capital_flow_tickbar(order_book_id, start_date, end_date, fields,
                             market):
    ensure_string(order_book_id, "order_book_id")
    start_date, end_date = ensure_date_range(start_date, end_date,
                                             datetime.timedelta(days=3))
    history_permission_denied = realtime_permission_denied = False
    try:
        data = get_client().execute("get_capital_flow_tickbar",
                                    order_book_id,
                                    start_date,
                                    end_date,
                                    fields,
                                    market=market)
    except PermissionDenied:
        data = []
        history_permission_denied = True
    today = today_int()

    if data:
        data = [(obid, {k: np.frombuffer(*v)
                        for k, v in d.items()}) for obid, d in data]
        df_list = []
        for obid, d in data:
            df = pd.DataFrame(d)
            df_list.append(df)

        df = pd.concat(df_list)  # type: pd.DataFrame
        df["datetime"] = int17_to_datetime_v(df["datetime"].values)
        history_latest_date = date_to_int8(df.iloc[-1]["datetime"])
        df.set_index("datetime", inplace=True)
    else:
        df = None
        history_latest_date = date_to_int8(
            get_previous_trading_date(today, market=market))

    if history_latest_date >= end_date or start_date > today or history_latest_date >= today:
        return df

    try:
        live_df = get_today_capital_flow_tick(order_book_id,
                                              today,
                                              market=market)
    except PermissionDenied:
        live_df = None
        realtime_permission_denied = True
    except MarketNotSupportError:
        live_df = None

    if history_permission_denied and realtime_permission_denied:
        raise PermissionDenied("get_capital_flow_tick")

    if live_df is None:
        return df
    if df is None:
        return live_df
    return pd.concat([df, live_df])
Esempio n. 2
0
def get_open_auction_info(order_book_ids, start_date=None, end_date=None, market="cn"):
    """获取盘前集合竞价数据
    :param order_book_ids: 股票代码
    :param start_date: 起始日期,默认为今天
    :param end_date: 截止日期,默认为今天
    :param market:  (Default value = "cn")
    :returns: pd.DataFrame or None
    """
    order_book_ids = ensure_order_book_ids(order_book_ids)
    start_date, end_date = ensure_date_range(start_date, end_date, datetime.timedelta(days=0))

    history_permission_denied = realtime_permission_denied = False
    try:
        # obid add prefix 'OA_'
        data = get_client().execute("get_open_auction_info_daybar", ["OA_" + obid for obid in order_book_ids],
                                    start_date, end_date, OA_FIELDS + ["datetime", "date"], market=market)
    except PermissionDenied:
        data = []
        history_permission_denied = True

    today = today_int()
    prev_trading_date = date_to_int8(get_previous_trading_date(today, market=market))
    if data:
        data = [(obid[3:], {k: np.frombuffer(*v) for k, v in d.items()}) for obid, d in data]
        df = convert_bar_to_multi_df(data, 'datetime', OA_FIELDS + ["date"], int17_to_datetime_v, default=0.0)
        if df is None:
            history_latest_date = prev_trading_date
        else:
            history_latest_date = df["date"].max()
            del df["date"]
    else:
        df = None
        history_latest_date = prev_trading_date
    if history_latest_date >= end_date or start_date > today or history_latest_date >= today or end_date < today:
        return df

    try:
        live_df = get_today_open_auction(order_book_ids, today, market=market)
    except PermissionDenied:
        live_df = None
        realtime_permission_denied = True

    if history_permission_denied and realtime_permission_denied:
        raise PermissionDenied("get_open_auction_info")

    if live_df is None:
        return df
    if df is None:
        return live_df
    df = pd.concat([df, live_df])
    df.sort_index(inplace=True)
    return df
Esempio n. 3
0
def get_capital_flow_minbar(order_book_ids, start_date, end_date, fields,
                            duration, market):
    history_permission_denied = realtime_permission_denied = False
    try:
        data = get_client().execute("get_capital_flow_minbar",
                                    order_book_ids,
                                    start_date,
                                    end_date,
                                    fields,
                                    duration,
                                    market=market)
    except PermissionDenied:
        history_permission_denied = True
        data = []

    if data:
        data = [(obid, {k: np.frombuffer(*v)
                        for k, v in d.items()}) for obid, d in data]
        df = convert_bar_to_multi_df(data, 'datetime', fields,
                                     int14_to_datetime_v)
    else:
        df = None

    today = today_int()
    if df is None:
        history_latest_date = date_to_int8(
            get_previous_trading_date(today, market=market))
    else:
        history_latest_date = date_to_int8(df.index.get_level_values(1).max())

    if history_latest_date >= end_date or start_date > today or history_latest_date >= today:
        return df
    try:
        live_df = get_today_capital_flow_minbar(order_book_ids, today, fields,
                                                duration, market)
    except PermissionDenied:
        live_df = None
        realtime_permission_denied = True

    if history_permission_denied and realtime_permission_denied:
        raise PermissionDenied("get_capital_flow_minbar")

    if live_df is None:
        return df
    if df is None:
        return live_df
    df = pd.concat([df, live_df])
    df.sort_index(inplace=True)
    return df
Esempio n. 4
0
def get_price(
    order_book_ids,
    start_date=None,
    end_date=None,
    frequency="1d",
    fields=None,
    adjust_type="pre",
    skip_suspended=False,
    expect_df=False,
    market="cn",
    **kwargs
):
    """获取证券的历史数据

    :param order_book_ids: 股票列表
    :param market: 地区代码, 如 'cn' (Default value = "cn")
    :param start_date: 开始日期, 如 '2013-01-04' (Default value = None)
    :param end_date: 结束日期, 如 '2014-01-04' (Default value = None)
    :param frequency: 可选参数, 默认为日线。日线使用 '1d', 分钟线 '1m' (Default value = "1d")
    :param fields: 可选参数。默认为所有字段。 (Default value = None)
    :param adjust_type: 可选参数, 默认为'pre', 返回前复权数据。设置为'none'将返回原始数据,
        'post'返回后复权数据,
        'internal'返回只包含拆分的前复权数据。 (Default value = "pre")
    :param skip_suspended: 可选参数,默认为False;当设置为True时,返回的数据会过滤掉停牌期间,
                    此时order_book_ids只能设置为一只股票 (Default value = False)
    :param expect_df: 返回 MultiIndex DataFrame (Default value = False)
    :returns: 如果仅传入一只股票, 返回一个 pandas.DataFrame
        如果传入多只股票, 则返回一个 pandas.Panel

    """
    if frequency == "tick":
        return get_tick_price(order_book_ids, start_date, end_date, fields, expect_df, market)
    elif frequency.endswith(("d", "m")):
        duration = int(frequency[:-1])
        frequency = frequency[-1]
        assert 1 <= duration <= 240, "frequency should in range [1, 240]"
        if market == "hk" and frequency == "m" and duration not in (1, 5, 15, 30, 60):
            raise ValueError("frequency should be str like 1m, 5m, 15m 30m,or 60m")
    else:
        raise ValueError("frequency should be str like 1d, 1m, 5m or tick")
    if "adjusted" in kwargs:
        adjusted = kwargs.pop("adjusted")
        adjust_type = "pre" if adjusted else "none"

    if kwargs:
        raise ValueError('unknown kwargs: {}'.format(kwargs))

    valid_adjust = ["pre", "post", "none"]
    ensure_string(adjust_type, "adjust_type")
    check_items_in_container(adjust_type, valid_adjust, "adjust_type")
    order_book_ids = ensure_list_of_string(order_book_ids, "order_book_ids")
    if skip_suspended and len(order_book_ids) > 1:
        raise ValueError("only accept one order_book_id or symbol if skip_suspended is True")

    assert isinstance(skip_suspended, bool), "'skip_suspended' should be a bool"
    assert isinstance(expect_df, bool), "'expect_df' should be a bool"

    order_book_ids, stocks, funds, indexes, futures, futures888, spots, options, convertibles, repos = classify_order_book_ids(
        order_book_ids, market
    )
    if not order_book_ids:
        warnings.warn("no valid instrument")
        return
    start_date, end_date = _ensure_date(
        start_date, end_date, stocks, funds, indexes, futures, spots, options, convertibles, repos
    )

    if expect_df:
        from rqdatac.services.detail.get_price_df import get_price_df
        return get_price_df(
            order_book_ids, start_date, end_date, frequency, duration, fields, adjust_type, skip_suspended,
            stocks, funds, indexes, futures, futures888, spots, options, convertibles, repos, market
        )

    if frequency == "d":
        fields, has_dominant_id = _ensure_fields(fields, DAYBAR_FIELDS, stocks, funds, futures, spots, options, convertibles, indexes, repos)
        pf = get_daybar(order_book_ids, start_date, end_date, fields, duration, market)
        if pf is None:
            return
    else:
        fields, has_dominant_id = _ensure_fields(fields, MINBAR_FIELDS, stocks, funds, futures, spots, options, convertibles, indexes, repos)
        history_permission_denied, today_permission_denied = False, False
        try:
            pf = get_minbar(order_book_ids, start_date, end_date, fields, duration, market)
        except (PermissionDenied, MarketNotSupportError):
            pf = None
            history_permission_denied = True
        history_latest_day = 0 if pf is None else date_to_int8(pf.iloc[-1].index[-1])
        if history_latest_day < end_date and end_date >= today_int():
            try:
                today_pf = get_today_minbar(order_book_ids, fields, duration, market)
            except (PermissionDenied, MarketNotSupportError):
                today_pf = None
                today_permission_denied = True
            if today_pf is None:
                today_pf_latest_day = 0
            else:
                today_pf_latest_day = date_to_int8(get_current_trading_date(today_pf.iloc[-1].index[-1]))
            if today_pf_latest_day > history_latest_day and today_pf_latest_day >= start_date:
                if history_latest_day == 0:
                    pf = today_pf
                else:
                    pf = pd.concat([pf, today_pf], axis=1)
        if pf is None:
            if history_permission_denied and today_permission_denied:
                raise PermissionDenied("Not permit to get minbar price ")
            elif history_permission_denied:
                warnings.warn("Not permit to get history minbar price")
            elif today_permission_denied:
                warnings.warn("Not permit to get realtime minbar price")
            return

    result = _adjust_pf(
        pf,
        order_book_ids,
        stocks,
        funds,
        futures888,
        start_date,
        end_date,
        frequency,
        fields,
        has_dominant_id,
        adjust_type,
        skip_suspended,
        market,
    )
    return result
Esempio n. 5
0
def get_price_df(
        order_book_ids,
        start_date,
        end_date,
        frequency,
        duration,
        fields,
        adjust_type,
        skip_suspended,
        stocks,
        funds,
        indexes,
        futures,
        futures888,
        spots,
        options,
        convertibles,
        repos,
        market
):
    if frequency == "d":
        fields, has_dominant_id = _ensure_fields(fields, DAYBAR_FIELDS, stocks, funds, futures, spots, options, convertibles, indexes, repos)
        pf, obid_slice_map = get_daybar(order_book_ids, start_date, end_date, fields, duration, market)
        if pf is None:
            return
    else:
        fields, has_dominant_id = _ensure_fields(fields, MINBAR_FIELDS, stocks, funds, futures, spots, options, convertibles, indexes, repos)
        history_permission_denied, today_permission_denied = False, False
        try:
            pf, obid_slice_map = get_minbar(order_book_ids, start_date, end_date, fields, duration, market)
        except (PermissionDenied, MarketNotSupportError):
            pf = obid_slice_map = None
            history_permission_denied = True
        history_latest_day = 0 if pf is None else date_to_int8(pf.index.get_level_values(1).max())
        if history_latest_day < end_date and end_date >= today_int():
            try:
                today_pf, obid_slice_map = get_today_minbar(order_book_ids, fields, duration, market)
            except (PermissionDenied, MarketNotSupportError):
                today_pf = None
                today_permission_denied = True
            if today_pf is None:
                today_pf_latest_day = 0
            else:
                today_pf_latest_day = date_to_int8(get_current_trading_date(today_pf.index.get_level_values(1).max()))
            if today_pf_latest_day > history_latest_day and today_pf_latest_day >= start_date:
                if history_latest_day == 0:
                    pf = today_pf
                else:
                    pf = pd.concat([pf, today_pf])
                    line_no, obid_slice_map = 0, {}
                    obids, counts = np.unique(pf.index.get_level_values(0), return_counts=True)
                    for obid, ct in zip(obids, counts):
                        obid_slice_map[obid] = slice(line_no, line_no + ct, None)
                        line_no += ct
        if pf is None:
            if history_permission_denied and today_permission_denied:
                raise PermissionDenied("Not permit to get minbar price ")
            elif history_permission_denied:
                warnings.warn("Not permit to get history minbar price")
            elif today_permission_denied:
                warnings.warn("Not permit to get realtime minbar price")
            return

    result = _adjust_pf(
        pf,
        order_book_ids,
        stocks,
        funds,
        futures888,
        start_date,
        end_date,
        has_dominant_id,
        adjust_type,
        skip_suspended,
        obid_slice_map,
        market,
    )
    return result