def __init__(self, provider: str, level: int, cap: CompactFactor = None, db_interface: DBInterface = None): self.db_interface = db_interface if db_interface else get_db_interface( ) self.calendar = TradingCalendar(self.db_interface) self.date = self.calendar.yesterday() self.data_reader = AShareDataReader(self.db_interface) self.industry = self.data_reader.industry(provider=provider, level=level) self.cap = cap if cap else self.data_reader.stock_free_floating_market_cap
def major_index_valuation(db_interface: DBInterface = None): if db_interface is None: db_interface = get_db_interface() data = db_interface.read_table('指数日行情', ['市盈率TTM', '市净率']).dropna(how='all') tmp = data.groupby('ID').rank() latest = data.groupby('ID').tail(1) percentile = tmp.groupby('ID').tail(1) / tmp.groupby('ID').max() percentile.columns = [f'{it}分位' for it in percentile.columns] ret = pd.concat([latest, percentile], axis=1) ret = ret.loc[:, sorted(ret.columns)].reset_index() index_name_dict = dict( zip(constants.STOCK_INDEXES.values(), constants.STOCK_INDEXES.keys())) ret['ID'] = ret['ID'].map(index_name_dict) return ret.set_index(['DateTime', 'ID'])
def __init__(self, date: dt.datetime = None, db_interface: DBInterface = None): self.db_interface = db_interface if db_interface else get_db_interface( ) self.calendar = TradingCalendar(self.db_interface) if date is None: date = dt.datetime.combine(dt.date.today(), dt.time()) self.date = date records = utils.load_excel('自编指数配置.xlsx') self.tickers = [it['ticker'] for it in records] self.tbd_indexes = list( set(self.tickers) - set(self.must_keep_indexes)) start_date = self.calendar.offset(date, -22) index_factor = ContinuousFactor('自合成指数', '收益率', db_interface=self.db_interface) self.cache = index_factor.get_data(start_date=start_date, end_date=date).unstack() self.industry_cache = []
def plot_factor_return(factor_name: str, weight: bool = True, industry_neutral: bool = True, bins: int = 5, start_date: DateUtils.DateType = None, end_date: DateUtils.DateType = None, db_interface: DBInterface = None) -> plt.Figure: if db_interface is None: db_interface = get_db_interface() ids = utils.generate_factor_bin_names(factor_name, weight=weight, industry_neutral=industry_neutral, bins=bins) data = db_interface.read_table('因子分组收益率', ids=ids, start_date=start_date, end_date=end_date) df = (data.unstack() + 1).cumprod() bin_names_info = [utils.decompose_bin_names(it) for it in df.columns] diff_series = df[ids[0]] - df[ids[-1]] df.columns = [it['group'] for it in bin_names_info] diff_series.name = f'{utils.decompose_bin_names(ids[0])["group"]}-{utils.decompose_bin_names(ids[-1])["group"]}' fig, axes = plt.subplots(2, 1, figsize=(15, 8), sharex='col') df.plot(ax=axes[0]) industry_neutral_str = '行业中性' if industry_neutral else '非行业中性' weight_str = '市值加权' if weight else '等权' axes[0].set_title( f'{factor_name} 分组收益率({industry_neutral_str}, {weight_str})') plot_dt = df.index.get_level_values('DateTime') axes[0].set_xlim(left=plot_dt[0], right=plot_dt[-1]) axes[0].grid(True) diff_series.plot(ax=axes[1]) axes[1].grid(True) axes[1].legend() return fig
def setUp(self) -> None: set_global_config('config.json') self.db_interface = get_db_interface()