def sync_equity_to_sw_industry(sw_industry_lv: str, start_t: date, end_t: date, force_reinit: bool = False): lv_to_lib_name = {"L1": TuShareProData.DERIVED_TS_EQUITY_SW_INDUSTRY_L1, "L2": TuShareProData.DERIVED_TS_EQUITY_SW_INDUSTRY_L2, "L3": TuShareProData.DERIVED_TS_EQUITY_SW_INDUSTRY_L3} assert sw_industry_lv in lv_to_lib_name.keys() tushare = TuShareProData(use_l3_cache=False) req_freq_controller = TushareReqSleepController(tushare) df_sw_index = tushare.index_classify(level=sw_industry_lv, src="SW") df_sw_index = df_sw_index.reset_index(drop=True) ls_df_index_equities = [] i_num = 0 for idx_num, row in df_sw_index.iterrows(): i_num += 1 lv = row["level"] ind_name = row["industry_name"] ind_code = row["index_code"] req_freq_controller.begin_internal_check() df_index_equities = tushare.period_index_member(index_code=ind_code, resample_freq="B", start=start_t, end=end_t) req_freq_controller.end_internal_check() df_index_equities["index_code"] = ind_code ls_df_index_equities.append(df_index_equities) df_all_index_equities = pd.concat(ls_df_index_equities) for i, symbol in enumerate(df_all_index_equities["symbol"].unique()): df_symbol_in_industry = df_all_index_equities[df_all_index_equities["symbol"] == symbol] df_symbol_in_industry.drop(columns=["symbol"], inplace=True) print(f"\rsync_equity_to_sw_industry {i}:{symbol}-{df_symbol_in_industry.index.min()}-{df_symbol_in_industry.index.max()} ", end="") tushare.ts_upsert_arctic_storage(lv_to_lib_name[sw_industry_lv], symbol, df_symbol_in_industry, force_reinit=force_reinit)
def _calc_sw_industry_data(tushare: TuShareProData, sw_industry_code: str, start_t: date, end_t: date, f_equity_quotation_daily=None, f_equity_basic_daily=None) -> pd.DataFrame: # 行业指数在 date 的持仓 df_stocks_in_industry = tushare.period_index_member(sw_industry_code, start=start_t, end=end_t, resample_freq="B") if df_stocks_in_industry is None: # 区间内没有股票,直接返回 return df_stocks_in_industry = df_stocks_in_industry.reset_index().set_index(keys=["date", "symbol"]) if f_equity_quotation_daily is None: f_equity_quotation_daily = tushare.cs_equity_quotation_daily if f_equity_basic_daily is None: f_equity_basic_daily = functools.partial(tushare.cs_equity_basic_daily, cols=["turnover_rate", "turnover_rate_f", "volume_ratio", "pe", "pe_ttm", "pb", "ps", "ps_ttm", "dv_ratio", "dv_ttm", "total_share", "float_share", "free_share", "total_mv", "circ_mv"]) # 每日指标 mv_weighted_cols = ["open", "high", "low", "close", "pre_close", "change", "pct_chg", "turnover_rate", "turnover_rate_f", "volume_ratio", "pe", "pe_ttm", "pb", "ps", "ps_ttm", "dv_ratio", "dv_ttm"] # 总市值加权的列 # sum_cols = ["vol", "amount", "total_share", "float_share", "free_share", "total_mv", "circ_mv"] # 得到个股的区间数据 df_cs_daily_industry_membership = df_stocks_in_industry.join( f_equity_quotation_daily(start=start_t, end=end_t), how="left").join( f_equity_basic_daily(start=start_t, end=end_t), how="left") # 市值加权列的加总计算 for col in mv_weighted_cols: df_cs_daily_industry_membership[f"_{col}"] = df_cs_daily_industry_membership[col] * \ df_cs_daily_industry_membership["total_mv"] df_cs_daily_industry_membership.drop(columns=mv_weighted_cols, inplace=True) df_cs_daily_industry_membership.rename(columns={f"_{col}": col for col in mv_weighted_cols}, inplace=True) df_cs_daily_industry_membership = df_cs_daily_industry_membership.reset_index().groupby("date").sum() for col in mv_weighted_cols: df_cs_daily_industry_membership[f"_{col}"] = df_cs_daily_industry_membership[col] / \ df_cs_daily_industry_membership["total_mv"] df_cs_daily_industry_membership.drop(columns=mv_weighted_cols, inplace=True) df_cs_daily_industry_membership.rename(columns={f"_{col}": col for col in mv_weighted_cols}, inplace=True) return df_cs_daily_industry_membership