示例#1
0
def get_futures_daily(start_day=None,
                      end_day=None,
                      market="CFFEX",
                      index_bar=False):
    """
        获取交易所日交易数据
    Parameters
    ------
        start_day: 开始日期 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date对象 为空时为当天
        end_day: 结束数据 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date对象 为空时为当天
        market: 'CFFEX' 中金所, 'CZCE' 郑商所,  'SHFE' 上期所, 'DCE' 大商所 之一。默认为中金所
        index_bar: bool  是否合成指数K线
    Return
    -------
        DataFrame
            中金所日交易数据(DataFrame):
                symbol      合约代码
                date       日期
                open       开盘价
                high       最高价
                low       最低价
                close      收盘价
                volume      成交量
                open_interest 持仓量
                turnover    成交额
                settle     结算价
                pre_settle   前结算价
                variety     合约类别
        或 None(给定日期没有交易数据)
    """
    if market.upper() == "CFFEX":
        f = get_cffex_daily
    elif market.upper() == "CZCE":
        f = get_czce_daily
    elif market.upper() == "SHFE":
        f = get_shfe_daily
    elif market.upper() == "DCE":
        f = get_dce_daily
    else:
        print("Invalid Market Symbol")
        return

    start_day = (cons.convert_date(start_day)
                 if start_day is not None else datetime.date.today())
    end_day = (cons.convert_date(end_day)
               if end_day is not None else cons.convert_date(
                   cons.get_latest_data_date(datetime.datetime.now())))

    df_list = list()
    while start_day <= end_day:
        df = f(start_day)
        if df is not None:
            df_list.append(df)
            if index_bar:
                df_list.append(get_futures_index(df))
        start_day += datetime.timedelta(days=1)

    if len(df_list) > 0:
        return pd.concat(df_list).reset_index(drop=True)
示例#2
0
def get_futures_daily(
    start_date: str = "20220208",
    end_date: str = "20220208",
    market: str = "INE",
    index_bar: bool = False,
) -> pd.DataFrame:
    """
    交易所日交易数据
    :param start_date: 开始日期 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date对象 为空时为当天
    :type start_date: str
    :param end_date: 结束数据 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date对象 为空时为当天
    :type end_date: str
    :param market: 'CFFEX' 中金所, 'CZCE' 郑商所,  'SHFE' 上期所, 'DCE' 大商所 之一, 'INE' 上海国际能源交易中心。默认为中金所
    :type market: str
    :param index_bar: 是否合成指数K线, 默认为 False 否则影响 roll_yield 的计算
    :type index_bar: bool
    :return: 交易所日交易数据
    :rtype: pandas.DataFrame
    """
    if market.upper() == "CFFEX":
        f = get_cffex_daily
    elif market.upper() == "CZCE":
        f = get_czce_daily
    elif market.upper() == "SHFE":
        f = get_shfe_daily
    elif market.upper() == "DCE":
        f = get_dce_daily
    elif market.upper() == "INE":
        f = get_ine_daily
    else:
        print("Invalid Market Symbol")
        return

    start_date = (
        cons.convert_date(start_date)
        if start_date is not None
        else datetime.date.today()
    )
    end_date = (
        cons.convert_date(end_date)
        if end_date is not None
        else cons.convert_date(cons.get_latest_data_date(datetime.datetime.now()))
    )

    df_list = list()
    while start_date <= end_date:
        df = f(date=str(start_date).replace("-", ""))
        if df is not None:
            df_list.append(df)
            if index_bar:
                df_list.append(get_futures_index(df))
        start_date += datetime.timedelta(days=1)

    if len(df_list) > 0:
        temp_df = pd.concat(df_list).reset_index(drop=True)
        temp_df = temp_df[~temp_df["symbol"].str.contains("efp")]
        return temp_df
示例#3
0
def get_roll_yield_bar(type_method='symbol', var='RB', date=None, start_day=None, end_day=None, plot=False):
    """
    获取展期收益率
    :param type_method: 'symbol':获取某天某品种所有交割月合约的收盘价, 'var':获取某天所有品种两个主力合约的展期收益率(展期收益率横截面), ‘date’:获取某品种每天的两个主力合约的展期收益率(展期收益率时间序列)
    :param var: 合约品种如RB、AL等
    :param date: 某一天日期 format: YYYYMMDD
    :param start_day: 开始日期 format:YYYYMMDD
    :param end_day: 结束数据 format:YYYYMMDD
    :param plot: True or False作图
    :return: pd.DataFrame
    展期收益率数据(DataFrame):
        ry      展期收益率
        index   日期或品种
    """

    date = cons.convert_date(date) if date is not None else datetime.date.today()
    start_day = cons.convert_date(start_day) if start_day is not None else datetime.date.today()
    end_day = cons.convert_date(end_day) if end_day is not None else cons.convert_date(
        cons.get_latest_data_date(datetime.datetime.now()))

    if type_method == 'symbol':
        df = get_futures_daily(start_day=date, end_day=date, market=symbol_market(var))
        df = df[df['variety'] == var]
        if plot:
            _plot_bar_2(df[['symbol', 'close']])
        return df

    if type_method == 'var':
        df = pd.DataFrame()
        for market in ['dce', 'cffex', 'shfe', 'czce']:
            df = df.append(get_futures_daily(start_day=date, end_day=date, market=market))
        var_list = list(set(df['variety']))
        df_l = pd.DataFrame()
        for var in var_list:
            ry = get_roll_yield(date, var, df=df)
            if ry:
                df_l = df_l.append(pd.DataFrame([ry], index=[var], columns=['roll_yield', 'near_by', 'deferred']))
        df_l['date'] = date
        df_l = df_l.sort_values('roll_yield')
        if plot:
            _plot_bar(df_l['roll_yield'])
        return df_l

    if type_method == 'date':
        df_l = pd.DataFrame()
        while start_day <= end_day:
            try:
                ry = get_roll_yield(start_day, var)
                if ry:
                    df_l = df_l.append(pd.DataFrame([ry], index=[start_day], columns=['roll_yield', 'near_by', 'deferred']))
            except:
                pass
            start_day += datetime.timedelta(days=1)
        if plot:
            _plot(df_l['roll_yield'])
        return df_l
示例#4
0
文件: cot.py 项目: dgczy/akshare
def get_rank_sum_daily(start_day=None,
                       end_day=None,
                       vars_list=cons.contract_symbols):
    """
    采集四个期货交易所前5、前10、前15、前20会员持仓排名数据
    注1:由于上期所和中金所只公布每个品种内部的标的排名,没有公布品种的总排名;
        所以函数输出的品种排名是由品种中的每个标的加总获得,并不是真实的品种排名列表
    注2:大商所只公布了品种排名,未公布标的排名
    :param start_day: 开始日期 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date对象 为空时为当天
    :param end_day: 结束数据 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date对象 为空时为当天
    :param vars_list: 合约品种如RB、AL等列表 为空时为所有商品
    :return: pd.DataFrame
    展期收益率数据(DataFrame):
    symbol                           标的合约                     string
    var                              商品品种                     string
    vol_top5                         成交量前5会员成交量总和         int
    vol_chg_top5                     成交量前5会员成交量变化总和      int
    long_open_interest_top5          持多单前5会员持多单总和         int
    long_open_interest_chg_top5      持多单前5会员持多单变化总和      int
    short_open_interest_top5         持空单前5会员持空单总和         int
    short_open_interest_chg_top5     持空单前5会员持空单变化总和      int
    vol_top10                        成交量前10会员成交量总和        int
    ...
    date                             日期                         string YYYYMMDD
    """
    start_day = cons.convert_date(
        start_day) if start_day is not None else datetime.date.today()
    end_day = cons.convert_date(
        end_day) if end_day is not None else cons.convert_date(
            cons.get_latest_data_date(datetime.datetime.now()))
    records = pd.DataFrame()
    while start_day <= end_day:
        print(start_day)
        if start_day.strftime('%Y%m%d') in calendar:
            data = get_rank_sum(start_day, vars_list)
            if data is False:
                print(
                    f"{start_day.strftime('%Y-%m-%d')}日交易所数据连接失败,已超过20次,您的地址被网站墙了,请保存好返回数据,稍后从该日期起重试"
                )
                return records.reset_index(drop=True)
            records = records.append(data)
        else:
            warnings.warn(f"{start_day.strftime('%Y%m%d')}非交易日")
        start_day += datetime.timedelta(days=1)

    return records.reset_index(drop=True)
示例#5
0
def futures_spot_price_daily(
    start_day: str = "20210201",
    end_day: str = "20210208",
    vars_list=cons.contract_symbols,
):
    """
    指定时间段内大宗商品现货价格及相应基差
    http://www.100ppi.com/sf/
    :param start_day: str 开始日期 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date对象; 默认为当天
    :param end_day: str 结束数据 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date对象; 默认为当天
    :param vars_list: list 合约品种如 [RB, AL]; 默认参数为所有商品
    :return: pandas.DataFrame
    展期收益率数据:
    var               商品品种                      string
    sp                现货价格                      float
    near_symbol       临近交割合约                  string
    near_price        临近交割合约结算价             float
    dom_symbol        主力合约                      string
    dom_price         主力合约结算价                 float
    near_basis        临近交割合约相对现货的基差      float
    dom_basis         主力合约相对现货的基差          float
    near_basis_rate   临近交割合约相对现货的基差率    float
    dom_basis_rate    主力合约相对现货的基差率        float
    date              日期                          string YYYYMMDD
    """
    start_day = (
        cons.convert_date(start_day) if start_day is not None else datetime.date.today()
    )
    end_day = (
        cons.convert_date(end_day)
        if end_day is not None
        else cons.convert_date(cons.get_latest_data_date(datetime.datetime.now()))
    )
    df_list = []
    while start_day <= end_day:
        temp_df = futures_spot_price(start_day, vars_list)
        if temp_df is False:
            return pd.concat(df_list).reset_index(drop=True)
        elif temp_df is not None:
            df_list.append(temp_df)
        start_day += datetime.timedelta(days=1)
    if len(df_list) > 0:
        temp_df = pd.concat(df_list)
        temp_df.reset_index(drop=True, inplace=True)
        return temp_df
示例#6
0
def get_futures_daily(start_date="20201026", end_date="20201027", market="SHFE", index_bar=False):
    """
    交易所日交易数据
    Parameters
    ------
    start_date: 开始日期 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date对象 为空时为当天
    end_date: 结束数据 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date对象 为空时为当天
    market: 'CFFEX' 中金所, 'CZCE' 郑商所,  'SHFE' 上期所, 'DCE' 大商所 之一, 'INE' 上海国际能源交易中心。默认为中金所
    index_bar: bool  是否合成指数K线, 默认为 False 否则影响 roll_yield 的计算
    """
    if market.upper() == "CFFEX":
        f = get_cffex_daily
    elif market.upper() == "CZCE":
        f = get_czce_daily
    elif market.upper() == "SHFE":
        f = get_shfe_daily
    elif market.upper() == "DCE":
        f = get_dce_daily
    elif market.upper() == "INE":
        f = get_ine_daily
    else:
        print("Invalid Market Symbol")
        return None

    start_date = (
        cons.convert_date(start_date) if start_date is not None else datetime.date.today()
    )
    end_date = (
        cons.convert_date(end_date)
        if end_date is not None
        else cons.convert_date(cons.get_latest_data_date(datetime.datetime.now()))
    )

    df_list = list()
    while start_date <= end_date:
        df = f(date=str(start_date).replace("-", ""))
        if df is not None:
            df_list.append(df)
            if index_bar:
                df_list.append(get_futures_index(df))
        start_date += datetime.timedelta(days=1)

    if len(df_list) > 0:
        return pd.concat(df_list).reset_index(drop=True)
示例#7
0
def get_shfe_inv(start_day: str = None, end_day: str = None):
    """
    大宗商品注册仓单数量
    :param start_day: 开始日期 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date对象 为空时为当天
    :type start_day: str
    :param end_day: 结束数据 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date对象 为空时为当天
    :type end_day: str
    :param vars_list: 合约品种如RB、AL等列表 为空时为所有商品
    :type vars_list: str
    :return: 展期收益率数据
    :rtype: pandas.DataFrame
    """
    start_day = cons.convert_date(
        start_day) if start_day is not None else datetime.date.today()
    end_day = cons.convert_date(
        end_day) if end_day is not None else cons.convert_date(
            cons.get_latest_data_date(datetime.datetime.now()))
    records = pd.DataFrame()
    s_date = end_day - datetime.timedelta(days=end_day.weekday())
    while s_date >= start_day - datetime.timedelta(days=start_day.weekday()):
        wkdays = [s_date + datetime.timedelta(days=k) for k in range(5)]
        bzdays = [d for d in wkdays if d.strftime('%Y%m%d') in calendar]
        if len(bzdays) > 0:
            rpt_d = bzdays[-1]
            if rpt_d > datetime.date.today():
                f = None
                print('report date is after today')
            elif rpt_d > datetime.date(2014, 5, 16):
                f = get_shfe_inv_2
            else:
                f = get_shfe_inv_1
            if f is not None:
                records = records.append(f(rpt_d))
        s_date -= datetime.timedelta(days=7)
    records.reset_index(drop=True, inplace=True)
    return records
示例#8
0
def get_roll_yield_bar(
    type_method="var", var="RB", date="20200622", start_day=None, end_day=None, plot=False
):
    """
    展期收益率
    :param type_method: 'symbol': 获取指定交易日指定品种所有交割月合约的收盘价; 'var': 获取指定交易日所有品种两个主力合约的展期收益率(展期收益率横截面); 'date': 获取指定品种每天的两个主力合约的展期收益率(展期收益率时间序列)
    :param var: 合约品种如 "RB", "AL" 等
    :param date: 指定交易日 format: YYYYMMDD
    :param start_day: 开始日期 format:YYYYMMDD
    :param end_day: 结束日期 format:YYYYMMDD
    :param plot: True or False 是否作图
    :return: pandas.DataFrame
    展期收益率数据(DataFrame)
    ry      展期收益率
    index   日期或品种
    """

    date = cons.convert_date(date) if date is not None else datetime.date.today()
    start_day = (
        cons.convert_date(start_day) if start_day is not None else datetime.date.today()
    )
    end_day = (
        cons.convert_date(end_day)
        if end_day is not None
        else cons.convert_date(cons.get_latest_data_date(datetime.datetime.now()))
    )

    if type_method == "symbol":
        df = get_futures_daily(start_day=date, end_day=date, market=symbol_market(var))
        df = df[df["variety"] == var]
        if plot:
            _plot_bar_2(df[["symbol", "close"]])
        return df

    if type_method == "var":
        df = pd.DataFrame()
        for market in ["dce", "cffex", "shfe", "czce"]:
            df = df.append(
                get_futures_daily(start_day=date, end_day=date, market=market)
            )
        var_list = list(set(df["variety"]))
        if "IO" in var_list:
            var_list.remove("IO")  # IO 为期权
        df_l = pd.DataFrame()
        for var in var_list:
            ry = get_roll_yield(date, var, df=df)
            if ry:
                df_l = df_l.append(
                    pd.DataFrame(
                        [ry], index=[var], columns=["roll_yield", "near_by", "deferred"]
                    )
                )
        df_l["date"] = date
        df_l = df_l.sort_values("roll_yield")
        if plot:
            _plot_bar(df_l["roll_yield"])
        return df_l

    if type_method == "date":
        df_l = pd.DataFrame()
        while start_day <= end_day:
            try:
                ry = get_roll_yield(start_day, var)
                if ry:
                    df_l = df_l.append(
                        pd.DataFrame(
                            [ry],
                            index=[start_day],
                            columns=["roll_yield", "near_by", "deferred"],
                        )
                    )
            except:
                pass
            start_day += datetime.timedelta(days=1)
        if plot:
            _plot(df_l["roll_yield"])
        return df_l
示例#9
0
def get_receipt(start_day: str = None,
                end_day: str = None,
                vars_list: List = cons.contract_symbols):
    """
    大宗商品注册仓单数量
    :param start_day: 开始日期 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date对象 为空时为当天
    :type start_day: str
    :param end_day: 结束数据 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date对象 为空时为当天
    :type end_day: str
    :param vars_list: 合约品种如RB、AL等列表 为空时为所有商品
    :type vars_list: str
    :return: 展期收益率数据
    :rtype: pandas.DataFrame
    """
    start_day = cons.convert_date(
        start_day) if start_day is not None else datetime.date.today()
    end_day = cons.convert_date(
        end_day) if end_day is not None else cons.convert_date(
            cons.get_latest_data_date(datetime.datetime.now()))
    records = pd.DataFrame()
    while start_day <= end_day:
        if start_day.strftime('%Y%m%d') not in calendar:
            warnings.warn(f"{start_day.strftime('%Y%m%d')}非交易日")
        else:
            print(start_day)
            for market, market_vars in cons.market_exchange_symbols.items():

                if market == 'dce':
                    if start_day >= datetime.date(2009, 4, 7):
                        f = get_dce_receipt
                    else:
                        print('20090407起,dce每交易日更新仓单数据')
                        f = None
                elif market == 'shfe':
                    if datetime.date(2008, 10,
                                     6) <= start_day <= datetime.date(
                                         2014, 5, 16):
                        f = get_shfe_receipt_1
                    elif start_day > datetime.date(2014, 5, 16):
                        f = get_shfe_receipt_2
                    else:
                        f = None
                        print('20081006起,shfe每交易日更新仓单数据')
                elif market == 'czce':
                    if datetime.date(2008, 3, 3) <= start_day <= datetime.date(
                            2010, 8, 24):
                        f = get_czce_receipt_1
                    elif datetime.date(2010, 8,
                                       24) < start_day <= datetime.date(
                                           2015, 11, 11):
                        f = get_czce_receipt_2
                    elif start_day > datetime.date(2015, 11, 11):
                        f = get_czce_receipt_3
                    else:
                        f = None
                        print('20080303起,czce每交易日更新仓单数据')
                get_vars = [var for var in vars_list if var in market_vars]
                if market != 'cffex' and get_vars != []:
                    if f is not None:
                        records = records.append(f(start_day, get_vars))

        start_day += datetime.timedelta(days=1)
    records.reset_index(drop=True, inplace=True)
    if records.empty:
        return records
    if "MA" in records["var"].to_list():
        replace_index = records[records["var"] == "MA"]["receipt"].astype(
            str).str.split("0", expand=True)[0].index
        records.loc[replace_index, "receipt"] = records[
            records["var"] == "MA"]["receipt"].astype(str).str.split(
                "0", expand=True)[0]
    return records
示例#10
0
def get_receipt(start_day: str = None,
                end_day: str = None,
                vars_list: List = cons.contract_symbols):
    """
        获取大宗商品注册仓单数量
        Parameters
        ------
            start_day: 开始日期 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date对象 为空时为当天
            end_day: 结束数据 format:YYYY-MM-DD 或 YYYYMMDD 或 datetime.date对象 为空时为当天
            vars_list: 合约品种如RB、AL等列表 为空时为所有商品
        Return
        -------
            DataFrame
                展期收益率数据(DataFrame):
                    var             商品品种                     string
                    receipt         仓单数量                     int
                    date            日期                        string YYYYMMDD
    """
    start_day = cons.convert_date(
        start_day) if start_day is not None else datetime.date.today()
    end_day = cons.convert_date(
        end_day) if end_day is not None else cons.convert_date(
            cons.get_latest_data_date(datetime.datetime.now()))
    records = pd.DataFrame()
    while start_day <= end_day:
        if start_day.strftime('%Y%m%d') not in calendar:
            warnings.warn(f"{start_day.strftime('%Y%m%d')}非交易日")
        else:
            print(start_day)
            for market, market_vars in cons.market_exchange_symbols.items():

                if market == 'dce':
                    if start_day >= datetime.date(2009, 4, 7):
                        f = get_dce_receipt
                    else:
                        print('20090407起,dce每交易日更新仓单数据')
                        f = None
                elif market == 'shfe':
                    if datetime.date(2008, 10,
                                     6) <= start_day <= datetime.date(
                                         2014, 5, 16):
                        f = get_shfe_receipt_1
                    elif start_day > datetime.date(2014, 5, 16):
                        f = get_shfe_receipt_2
                    else:
                        f = None
                        print('20081006起,shfe每交易日更新仓单数据')
                elif market == 'czce':
                    if datetime.date(2008, 3, 3) <= start_day <= datetime.date(
                            2010, 8, 24):
                        f = get_czce_receipt_1
                    elif datetime.date(2010, 8,
                                       24) < start_day <= datetime.date(
                                           2015, 11, 11):
                        f = get_czce_receipt_2
                    elif start_day > datetime.date(2015, 11, 11):
                        f = get_czce_receipt_3
                    else:
                        f = None
                        print('20080303起,czce每交易日更新仓单数据')
                get_vars = [var for var in vars_list if var in market_vars]
                if market != 'cffex' and get_vars != []:
                    if f is not None:
                        records = records.append(f(start_day, get_vars))

        start_day += datetime.timedelta(days=1)
    return records.reset_index(drop=True)