Exemple #1
0
    def days_to_expire(self, date=None):
        if (self.type != "Future" or self.listed_date == "0000-00-00"  # 排除虚拟合约
            ):
            return -1

        date = to_date(date) if date else datetime.date.today()

        maturity_date = to_date(self.maturity_date)
        days = (maturity_date - date).days
        return days if days >= 0 else -1
Exemple #2
0
    def days_from_listed(self, date=None):
        if self.listed_date == "0000-00-00":
            return -1

        date = to_date(date) if date else datetime.date.today()
        if self.de_listed_date != "0000-00-00" and date > to_date(
                self.de_listed_date):
            # 晚于退市日期
            return -1

        listed_date = to_date(self.listed_date)
        ipo_days = (date - listed_date).days
        return ipo_days if ipo_days >= 0 else -1
Exemple #3
0
def ensure_dates_base_on_listed_date(instrument, start_date, end_date, market):
    from rqdatac.services.calendar import get_previous_trading_date, get_latest_trading_date
    if to_date(instrument.listed_date) > datetime.date.today():
        raise ValueError("instrument {} is not listed yet".format(
            instrument.order_book_id))

    if start_date is None:
        start_date = instrument.listed_date
    elif to_date(start_date) < to_date(instrument.listed_date):
        start_date = to_date(instrument.listed_date)
    elif instrument.de_listed_date != "0000-00-00" and to_date(
            start_date) >= to_date(instrument.de_listed_date):
        warnings.warn("{} has been delisted on {}".format(
            instrument.order_book_id, instrument.de_listed_date))
        return None, None

    if end_date is None:
        if instrument.de_listed_date != "0000-00-00":
            end_date = get_previous_trading_date(instrument.de_listed_date,
                                                 market=market)
        else:
            end_date = get_latest_trading_date(market=market)
    elif instrument.de_listed_date != "0000-00-00" and to_date(
            end_date) >= to_date(instrument.de_listed_date):
        warnings.warn("{} has been delisted on {}".format(
            instrument.order_book_id, instrument.de_listed_date))
        end_date = get_previous_trading_date(instrument.de_listed_date,
                                             market=market)
    return start_date, end_date
Exemple #4
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 #5
0
def get_etf_components(order_book_id, trading_date=None, market="cn"):
    """获取etf基金份额数据

    :param order_book_id: 基金代码 str
    :param trading_date: 交易日期,默认为当天
    :param market: (Default value = "cn")
    :return: DataFrame

    """
    if order_book_id.endswith(".XSHG") or order_book_id.endswith(".XSHE"):
        pass
    elif order_book_id.startswith("1"):
        order_book_id = order_book_id + ".XSHE"
    elif order_book_id.startswith("5"):
        order_book_id = order_book_id + ".XSHG"
    else:
        return None

    if trading_date is not None:
        trading_date = to_date(trading_date)
        if trading_date > datetime.date.today():
            return None
    else:
        trading_date = datetime.date.today()

    trading_date = ensure_date_int(ensure_trading_date(trading_date))
    result = get_client().execute("fund.get_etf_components", order_book_id, trading_date, market=market)
    if not result:
        return None

    columns = ["trading_date", "order_book_id", "stock_code", "stock_amount", "cash_substitute",
               "cash_substitute_proportion", "fixed_cash_substitute"]
    df = pd.DataFrame(result, columns=columns).sort_values(by=["trading_date", "order_book_id", "stock_code"],
                                                           ascending=[True, True, True])
    return df
Exemple #6
0
def ensure_date_range(start_date, end_date, delta=relativedelta(months=3)):
    if start_date is None and end_date is None:
        return _to_date_int(datetime.date.today() - delta), _to_date_int(
            datetime.date.today())

    if start_date is None:
        end_date = to_date(end_date)
        return _to_date_int(end_date - delta), _to_date_int(end_date)

    if end_date is None:
        start_date = to_date(start_date)
        return _to_date_int(start_date), _to_date_int(start_date + delta)

    s, e = ensure_date_int(start_date), ensure_date_int(end_date)
    if s > e:
        raise ValueError("invalid date range: [{!r}, {!r}]".format(
            start_date, end_date))
    return s, e
Exemple #7
0
def ensure_date_int(date):
    date = to_date(date)
    return date.year * 10000 + date.month * 100 + date.day
Exemple #8
0
def ensure_date_str(date):
    # type: (...) -> str
    date = to_date(date)
    return "%04d-%02d-%02d" % (date.year, date.month, date.day)