Beispiel #1
0
def _history_bars_fundamentals_jq(order_book_ids,
                                  bar_count,
                                  fields=None,
                                  dt=None):
    #
    start_date = DataProxy.get_instance().get_previous_trading_date(
        dt, n=bar_count)
    start_date = convert_timestamp_to_str(start_date)
    trading_dates = DataProxy.get_instance().get_trading_dates(
        start_date=start_date, end_date=dt)

    date_str_list = [
        convert_timestamp_to_str(timestamp) for timestamp in trading_dates
    ]
    data_df = get_fundamental_data(order_book_ids=order_book_ids,
                                   fields=fields,
                                   dt_list=date_str_list)
    return data_df
Beispiel #2
0
def history_bars(order_book_ids,
                 bar_count,
                 frequency,
                 fields=None,
                 dt=None,
                 skip_suspended=False,
                 include_now=True,
                 adjust_type='pre',
                 adjust_orig=None):
    """
    :param order_book_id: 合约代码
    :type order_book_id: `str`
    :param int bar_count: 获取的历史数据数量,必填项
    :param str frequency: 获取数据什么样的频率进行。'1d'或'1m'分别表示每日和每分钟,必填项
    :param str fields: 返回数据字段。必填项。见下方列表。
    =========================   ===================================================
    fields                      字段名
    =========================   ===================================================
    datetime                    时间戳
    open                        开盘价
    high                        最高价
    low                         最低价
    close                       收盘价
    volume                      成交量
    total_turnover              成交额
    open_interest               持仓量(期货专用)
    basis_spread                期现差(股指期货专用)
    settlement                  结算价(期货日线专用)
    prev_settlement             结算价(期货日线专用)
    =========================   ===================================================
    :param bool skip_suspended: 是否跳过停牌数据
    :param bool include_now: 是否包含当前数据
    :param str adjust_type: 复权类型,默认为前复权 pre;可选 pre, none, post
    :return: `ndarray`, 方便直接与talib等计算库对接,效率较history返回的DataFrame更高。
    """

    data_proxy = DataProxy.get_instance()
    if dt is None:
        dt = datetime.datetime.now()

    if frequency[-1] not in {'d', "w", "M"}:
        raise TypeError("only support 'd','w' and 'M' frequency data type")
    if fields is None:
        fields = ["date", "open", "high", "low", "close", "volume"]

    bars = data_proxy.history_bars(order_book_ids=order_book_ids,
                                   bar_count=bar_count,
                                   frequency=frequency,
                                   fields=fields,
                                   dt=dt,
                                   skip_suspended=skip_suspended,
                                   include_now=include_now,
                                   adjust_type=adjust_type,
                                   adjust_orig=adjust_orig)
    return bars
Beispiel #3
0
def get_trading_dates(start_date, end_date):
    """
    获取某个国家市场的交易日列表(起止日期加入判断)。目前仅支持中国市场。
    :param start_date: 开始日期
    :type start_date: `str` | `date` | `datetime` | `pandas.Timestamp`
    :param end_date: 结束如期
    :type end_date: `str` | `date` | `datetime` | `pandas.Timestamp`
    :return: list[`datetime.date`]
    :example:
    ..  code-block:: python3
        :linenos:
        [In]get_trading_dates(start_date='2016-05-05', end_date='20160505')
        [Out]
        [datetime.date(2016, 5, 5)]
    """
    return DataProxy.get_instance().get_trading_dates(start_date, end_date)
Beispiel #4
0
def history_tradings(order_book_ids,
                     bar_count,
                     frequency,
                     dt,
                     fields=['date', 'open', 'high', 'low', 'close'],
                     skip_suspended=False,
                     include_now=True,
                     adjust_type="pre",
                     adjust_orig=None):
    #get_bars(security, count, unit='1d', fields=['date','open','high','low','close'], include_now=False, end_dt=None, fq_ref_date=None)

    dt = DataProxy.get_instance().get_next_trading_date(dt, n=1)
    if frequency == "1d":
        data = _get_price(order_book_ids=order_book_ids,
                          bar_count=bar_count,
                          dt=dt,
                          frequency="daily",
                          fields=fields,
                          skip_suspended=skip_suspended,
                          include_now=include_now,
                          adjust_type=adjust_type,
                          adjust_orig=adjust_orig)
        return data

    if "date" not in fields:
        fields.append("date")
    data = get_bars(security=order_book_ids,
                    count=bar_count,
                    unit=frequency,
                    end_dt=dt,
                    fields=fields,
                    include_now=include_now,
                    fq_ref_date=adjust_orig)
    data["datetime"] = data["date"].apply(convert_to_timestamp)
    data = data.reset_index().rename(columns={"level_0": "order_book_id"})
    data = data.set_index(["order_book_id", "datetime"])
    data = data.drop(columns=["level_1", "date"])
    return data
Beispiel #5
0
def history_bars_fundamentals_jq(order_book_ids,
                                 bar_count,
                                 frequency,
                                 fields=None,
                                 dt=None):
    """
    return: [pd.Panel] 
           Items: date
           Major: order_boook_id
           Minor: fields
    """
    if (frequency[-1] not in ['d']) and (frequency not in ['1w', '1M']):
        raise Exception("only support xd, 1w and 1M data")

    if frequency == '1d':
        data_df = _history_bars_fundamentals_jq(order_book_ids=order_book_ids,
                                                bar_count=bar_count,
                                                fields=fields,
                                                dt=dt)
        return data_df

    dt = DataProxy.get_instance().get_next_trading_date(dt, n=1)
    trading_data = get_bars(security='000001.XSHG',
                            count=bar_count,
                            unit=frequency,
                            fields=['date'],
                            include_now=True,
                            end_dt=dt,
                            fq_ref_date=None)
    date_list = trading_data['date'].tolist()
    date_str_list = [date.strftime("%Y-%m-%d") for date in date_list]
    #pdb.set_trace()
    if 'turnover_ratio' not in fields:
        data_df = get_fundamental_data(order_book_ids=order_book_ids,
                                       fields=fields,
                                       dt_list=date_str_list)
        return data_df
    else:
        fields.remove('turnover_ratio')
        data_df = get_fundamental_data(
            order_book_ids=order_book_ids,
            fields=fields,
            dt_list=date_str_list)  # difference frequency
        # scale
        scale = 1
        if frequency[-1] == 'd':
            scale *= int(frequency[:-1])
        elif frequency == "1w":
            scale *= 5
        elif frequency == '1M':
            scale *= 20
        bar_count_ = bar_count * scale

        # get turnover_ratio data
        turnover_df = _history_bars_fundamentals_jq(
            order_book_ids=order_book_ids,
            bar_count=bar_count_,
            fields=['turnover_ratio'],
            dt=dt)['turnover_ratio']

        timestamp_list = [
            convert_to_timestamp(date_str) for date_str in date_str_list
        ]
        frequency_date_df = pd.DataFrame(timestamp_list,
                                         index=timestamp_list,
                                         columns=['datetime'])
        frequency_date_df.index.name = "datetime"
        #pdb.set_trace()
        turnover = frequency_date_df.join(turnover_df, how='outer')

        turnover['datetime1'] = turnover.groupby(
            level="order_book_id")['datetime'].fillna(method="bfill")
        turnover = turnover.reset_index(level="order_book_id").groupby(
            by=['order_book_id', 'datetime1'])["turnover_ratio"].sum()
        turnover.rename_axis(index={"datetime1": "datetime"}, inplace=True)

        data = turnover.to_frame().join(data_df, how='outer')
        return data
Beispiel #6
0
def get_trading_calendar():
    """
    获取中国A股当年所有的交易日期列表
    return pd.Series
    """
    return DataProxy.get_instance().get_trading_calendar()
Beispiel #7
0
def get_next_trading_date(date, n=1):
    return DataProxy.get_instance().get_next_trading_date(date, n)
Beispiel #8
0
def get_previous_trading_date(date, n=1):
    return DataProxy.get_instance().get_previous_trading_date(date, n)
Beispiel #9
0
def get_instrument_industry(order_book_ids, dt=None):
    return DataProxy.get_instance().get_instrument_industry(
        order_book_ids=order_book_ids, dt=dt)
Beispiel #10
0
def index_components(order_book_id, dt=None):
    return DataProxy.get_instance().index_components(
        order_book_id=order_book_id, dt=dt)