Exemplo n.º 1
0
    def get_start_date(
            self,
            code_list: Union[str,
                             Tuple[str],
                             List[str]],
            factor_time_range: list
    ):
        """
        获取上市时间
        """
        # stock_list = utils.QA_fmt_code_list(code_list, style="jq")
        # df_local = jqdatasdk.get_all_securities(types="stock")
        stock_list = utils.QA_fmt_code_list(code_list)
        df_local = QA_fetch_stock_basic(status=None).set_index("code")
        intersection = list(df_local.index.intersection(stock_list))
        ss = df_local.loc[intersection]["list_date"]
        # ss.index = ss.index.map(lambda x: x[:6])
        # 日期处理
        date_range = list(map(lambda x: x.date(), factor_time_range))

        multiindex = pd.MultiIndex.from_product(
            [date_range,
             utils.QA_fmt_code_list(intersection)]
        )
        values = multiindex.map(lambda x: ss.loc[x[1]]).tolist()
        df_local = pd.Series(index=multiindex, data=values)
        df_local.index.names = ["date", "code"]
        return df_local
Exemplo n.º 2
0
def QA_ts_update_namechange():
    """
    保存所有股票的历史曾用名
    """
    # 建表
    coll = DATABASE.namechange
    coll.create_index(
        [
            ("code", ASCENDING),
            ("start_date_stamp", ASCENDING),
            ("end_date_stamp", ASCENDING),
            ("ann_date_stamp", ASCENDING),
        ],
        unique=True,
    )
    # 初始化数据接口
    pro = get_pro()
    # 获取历史所有股票
    symbol_list = sorted(
        list(set(QA_fmt_code_list(QA_fetch_stock_basic().index.tolist(),
                                  "ts"))))
    df = pd.DataFrame()
    for i, symbol in enumerate(symbol_list):
        if i % 100 == 0:
            print(f"Saving {i}th stock name, stock is {symbol}")
        try:
            df = df.append(pro.namechange(ts_code=symbol))
        except:
            time.sleep(1)
            try:
                df = df.append(pro.namechange(ts_code=symbol))
            except:
                raise ValueError("[ERROR]\t数据获取失败")
    # df.to_csv("test.csv")
    df["code"] = QA_fmt_code_list(df["ts_code"])
    df["start_date_stamp"] = df["start_date"].apply(QA_util_date_stamp)
    df["end_date_stamp"] = df["end_date"].apply(QA_util_date_stamp)
    df["ann_date_stamp"] = df["ann_date"].apply(QA_util_date_stamp)
    df = df.where(pd.notnull(df), None)
    js = QA_util_to_json_from_pandas(df.drop(columns=["ts_code"]))
    for item in js:
        if not item["end_date"]:
            item["end_date_stamp"] = 9999999999
        qry = {
            "code": item["code"],
            "start_date_stamp": item["start_date_stamp"],
            "end_date_stamp": item["end_date_stamp"],
            "ann_date_stamp": item["ann_date_stamp"],
        }
        if coll.count_documents(qry) == 0:
            coll.insert_one(item)
Exemplo n.º 3
0
def QA_fetch_factor_start_date(factor: pd.Series) -> pd.DataFrame:
    """
    获取因子池上市时间,本地获取接口,使用前先保存股票基本信息
    """
    factor = QA_fmt_factor(factor.copy())
    merged_data = pd.DataFrame(factor.rename("factor"))
    # 股票代码格式化
    stock_list = QA_fmt_code_list(
        factor.index.get_level_values("code").drop_duplicates())
    # 上市时间获取
    df_local = QA_fetch_stock_basic(status=None).set_index("code")
    intersection = df_local.index.intersection(stock_list)
    ss = df_local.loc[intersection]["list_date"]
    # 拼接上市时间
    merged_data = merged_data.loc[(slice(None), list(ss.index)), :]
    merged_data["start_date"] = merged_data.index.map(
        lambda x: ss.loc[x[1]]).tolist()
    return merged_data