Exemplo n.º 1
0
def option_finance_board(symbol="华泰柏瑞沪深300ETF期权", end_month="2003"):
    """
    获取期权的当日具体的行情数据, 主要为三个: 华夏上证50ETF期权, 华泰柏瑞沪深300ETF期权, 嘉实沪深300ETF期权, 沪深300股指期权
    http://www.sse.com.cn/assortment/options/price/
    http://www.szse.cn/market/derivative/derivative_list/index.html
    http://www.cffex.com.cn/hs300gzqq/
    :param symbol: 华夏上证50ETF期权 or 华泰柏瑞沪深300ETF期权 or 嘉实沪深300ETF期权 or 沪深300股指期权
    :param end_month: 2003 2020年3月到期的期权
    :return: pandas.DataFrame
    """
    end_month = end_month[-2:]
    if symbol == "华夏上证50ETF期权":
        res = requests.get(SH_OPTION_URL_KING_50.format(end_month),
                           params=SH_OPTION_PAYLOAD_OTHER)
        data_json = res.json()
        raw_data = pd.DataFrame(data_json["list"])
        raw_data.index = [str(data_json["date"]) + str(data_json["time"])
                          ] * data_json["total"]
        raw_data.columns = ["合约交易代码", "当前价", "涨跌幅", "前结价", "行权价"]
        raw_data["数量"] = [data_json["total"]] * data_json["total"]
        return raw_data
    elif symbol == "华泰柏瑞沪深300ETF期权":
        res = requests.get(SH_OPTION_URL_KING_300.format(end_month),
                           params=SH_OPTION_PAYLOAD_OTHER)
        data_json = res.json()
        raw_data = pd.DataFrame(data_json["list"])
        raw_data.index = [str(data_json["date"]) + str(data_json["time"])
                          ] * data_json["total"]
        raw_data.columns = ["合约交易代码", "当前价", "涨跌幅", "前结价", "行权价"]
        raw_data["数量"] = [data_json["total"]] * data_json["total"]
        return raw_data
    elif symbol == "嘉实沪深300ETF期权":
        raw_df = pd.read_excel(SZ_OPTION_URL_300)
        raw_df["期权行权日"] = pd.to_datetime(raw_df["期权行权日"])
        raw_df["end_month"] = raw_df["期权行权日"].dt.month.astype(str).str.zfill(2)
        raw_df = raw_df[raw_df["end_month"] == end_month]
        del raw_df["end_month"]
        return raw_df
    elif symbol == "沪深300股指期权":
        raw_df = pd.read_table(CFFEX_OPTION_URL_300, sep=",")
        raw_df["end_month"] = raw_df["instrument"].str.split(
            "-", expand=True).iloc[:, 0].str.slice(4, )
        raw_df = raw_df[raw_df["end_month"] == end_month]
        del raw_df["end_month"]
        return raw_df
Exemplo n.º 2
0
def option_finance_board(symbol: str = "嘉实沪深300ETF期权",
                         end_month: str = "2103") -> pd.DataFrame:
    """
    期权的当日具体的行情数据, 主要为三个: 华夏上证50ETF期权, 华泰柏瑞沪深300ETF期权, 嘉实沪深300ETF期权, 沪深300股指期权
    http://www.sse.com.cn/assortment/options/price/
    http://www.szse.cn/market/product/option/index.html
    http://www.cffex.com.cn/hs300gzqq/
    :param symbol: 华夏上证50ETF期权 or 华泰柏瑞沪深300ETF期权 or 嘉实沪深300ETF期权 or 沪深300股指期权
    :type symbol: str
    :param end_month: 2003; 2020年3月到期的期权
    :type end_month: str
    :return: 当日行情
    :rtype: pandas.DataFrame
    """
    end_month = end_month[-2:]
    if symbol == "华夏上证50ETF期权":
        res = requests.get(SH_OPTION_URL_KING_50.format(end_month),
                           params=SH_OPTION_PAYLOAD_OTHER)
        data_json = res.json()
        raw_data = pd.DataFrame(data_json["list"])
        raw_data.index = [str(data_json["date"]) + str(data_json["time"])
                          ] * data_json["total"]
        raw_data.columns = ["合约交易代码", "当前价", "涨跌幅", "前结价", "行权价"]
        raw_data["数量"] = [data_json["total"]] * data_json["total"]
        return raw_data
    elif symbol == "华泰柏瑞沪深300ETF期权":
        res = requests.get(SH_OPTION_URL_KING_300.format(end_month),
                           params=SH_OPTION_PAYLOAD_OTHER)
        data_json = res.json()
        raw_data = pd.DataFrame(data_json["list"])
        raw_data.index = [str(data_json["date"]) + str(data_json["time"])
                          ] * data_json["total"]
        raw_data.columns = ["合约交易代码", "当前价", "涨跌幅", "前结价", "行权价"]
        raw_data["数量"] = [data_json["total"]] * data_json["total"]
        return raw_data
    elif symbol == "嘉实沪深300ETF期权":
        url = "http://www.szse.cn/api/report/ShowReport/data"
        params = {
            "SHOWTYPE": "JSON",
            "CATALOGID": "ysplbrb",
            "TABKEY": "tab1",
            "PAGENO": "1",
            "random": "0.10642298535346595",
        }
        r = requests.get(url, params=params)
        data_json = r.json()
        page_num = data_json[0]["metadata"]["pagecount"]
        big_df = pd.DataFrame()
        for page in range(1, page_num + 1):
            params = {
                "SHOWTYPE": "JSON",
                "CATALOGID": "ysplbrb",
                "TABKEY": "tab1",
                "PAGENO": page,
                "random": "0.10642298535346595",
            }
            r = requests.get(url, params=params)
            data_json = r.json()
            temp_df = pd.DataFrame(data_json[0]["data"])
            big_df = big_df.append(temp_df, ignore_index=True)
        big_df.columns = [
            "合约编码",
            "合约简称",
            "标的名称",
            "类型",
            "行权价",
            "合约单位",
            "期权行权日",
            "行权交收日",
        ]
        big_df["期权行权日"] = pd.to_datetime(big_df["期权行权日"])
        big_df["end_month"] = big_df["期权行权日"].dt.month.astype(str).str.zfill(2)
        big_df = big_df[big_df["end_month"] == end_month]
        del big_df["end_month"]
        return big_df
    elif symbol == "沪深300股指期权":
        raw_df = pd.read_table(CFFEX_OPTION_URL_300, sep=",")
        raw_df["end_month"] = (raw_df["instrument"].str.split(
            "-", expand=True).iloc[:, 0].str.slice(4, ))
        raw_df = raw_df[raw_df["end_month"] == end_month]
        del raw_df["end_month"]
        return raw_df