def get_top_performance_entities( entity_type="stock", start_timestamp=None, end_timestamp=None, pct=0.1, return_type=None, adjust_type: Union[AdjustType, str] = None, entity_filters=None, kdata_filters=None, show_name=False, list_days=None, entity_provider=None, data_provider=None, ): if not adjust_type: adjust_type = default_adjust_type(entity_type=entity_type) data_schema = get_kdata_schema(entity_type=entity_type, adjust_type=adjust_type) if not entity_filters: entity_filters = [] if list_days: entity_schema = get_entity_schema(entity_type=entity_type) list_date = next_date(start_timestamp, -list_days) entity_filters += [entity_schema.list_date <= list_date] filter_entities = get_entity_ids( provider=entity_provider, entity_type=entity_type, filters=entity_filters, ) if not filter_entities: logger.warning(f"no entities selected") return None, None if not kdata_filters: kdata_filters = [] kdata_filters = kdata_filters + [ data_schema.entity_id.in_(filter_entities) ] return get_top_entities( data_schema=data_schema, start_timestamp=start_timestamp, end_timestamp=end_timestamp, column="close", pct=pct, method=WindowMethod.change, return_type=return_type, kdata_filters=kdata_filters, show_name=show_name, data_provider=data_provider, )
def get_top_performance_entities( entity_type="stock", start_timestamp=None, end_timestamp=None, pct=0.1, return_type=None, adjust_type: Union[AdjustType, str] = None, filters=None, show_name=False, list_days=None, entity_provider=None, data_provider=None, ): if not adjust_type: adjust_type = default_adjust_type(entity_type=entity_type) data_schema = get_kdata_schema(entity_type=entity_type, adjust_type=adjust_type) if list_days: entity_schema = get_entity_schema(entity_type=entity_type) list_date = next_date(start_timestamp, -list_days) ignore_entities = get_entity_ids( provider=entity_provider, entity_type=entity_type, filters=[entity_schema.list_date >= list_date], ) if ignore_entities: logger.info(f"ignore size: {len(ignore_entities)}") logger.info(f"ignore entities: {ignore_entities}") f = [data_schema.entity_id.notin_(ignore_entities)] if filters: filters = filters + f else: filters = f return get_top_entities( data_schema=data_schema, start_timestamp=start_timestamp, end_timestamp=end_timestamp, column="close", pct=pct, method=WindowMethod.change, return_type=return_type, filters=filters, show_name=show_name, data_provider=data_provider, )
def get_entity_ids_by_filter(provider="em", ignore_st=True, ignore_new_stock=True, target_date=None, entity_schema=Stock, entity_ids=None): filters = [] if ignore_new_stock: if not target_date: target_date = current_date() pre_year = next_date(target_date, -365) filters += [entity_schema.timestamp <= pre_year] if ignore_st: filters += [ entity_schema.name.not_like("%退%"), entity_schema.name.not_like("%ST%"), entity_schema.name.not_like("%*ST%"), ] return get_entity_ids(provider=provider, entity_schema=entity_schema, filters=filters, entity_ids=entity_ids)
def get_top_performance_entities(entity_type='stock', start_timestamp=None, end_timestamp=None, pct=0.1, return_type=None, adjust_type: Union[AdjustType, str] = None, filters=None, show_name=False, list_days=None): if not adjust_type and entity_type == 'stock': adjust_type = AdjustType.hfq data_schema = get_kdata_schema(entity_type=entity_type, adjust_type=adjust_type) if list_days: entity_schema = get_entity_schema(entity_type=entity_type) list_date = next_date(start_timestamp, -list_days) ignore_entities = get_entity_ids(entity_type=entity_type, filters=[entity_schema.list_date >= list_date]) if ignore_entities: logger.info(f'ignore size: {len(ignore_entities)}') logger.info(f'ignore entities: {ignore_entities}') f = [data_schema.entity_id.notin_(ignore_entities)] if filters: filters = filters + f else: filters = f return get_top_entities(data_schema=data_schema, start_timestamp=start_timestamp, end_timestamp=end_timestamp, column='close', pct=pct, method=WindowMethod.change, return_type=return_type, filters=filters, show_name=show_name)
def report_top_stats(periods=[7, 30, 180, 365], ignore_new_stock=True): latest_day: Stock1dHfqKdata = Stock1dHfqKdata.query_data(order=Stock1dHfqKdata.timestamp.desc(), limit=1, return_type='domain') current_timestamp = latest_day[0].timestamp email_action = EmailInformer() # 至少上市一年 filters = None if ignore_new_stock: pre_year = next_date(current_timestamp, -365) stocks = get_entity_ids(provider='joinquant', entity_schema=Stock, filters=[Stock.timestamp <= pre_year]) filters = [Stock1dHfqKdata.entity_id.in_(stocks)] stats = [] ups = [] downs = [] msg = '' for period in periods: start = next_date(current_timestamp, -period) df, _ = get_top_performance_entities(start_timestamp=start, filters=filters, pct=1, show_name=True) df.rename(columns={'score': f'score_{period}'}, inplace=True) ups.append(tabulate(df.iloc[:50], headers='keys')) downs.append(tabulate(df.iloc[:-100], headers='keys')) stats.append(tabulate(df.describe(), headers='keys')) # 最近一个月最靓仔的 if period == 30: # add them to eastmoney try: try: eastmoneypy.del_group('最靓仔') except: pass eastmoneypy.create_group('最靓仔') for entity_id in df.index[:50]: _, _, code = decode_entity_id(entity_id) eastmoneypy.add_to_group(code=code, group_name='最靓仔') except Exception as e: logger.exception(e) email_action.send_message("*****@*****.**", f'report_top_stats error', 'report_top_stats error:{}'.format(e)) # 一年内没怎么动的 if period == 365: stable_df = df[(df['score_365'] > -0.1) & (df['score_365'] < 0.1)] vol_df = get_top_volume_entities(entity_ids=stable_df.index.tolist(), start_timestamp=start) # add them to eastmoney try: try: eastmoneypy.del_group('躺尸一年') except: pass eastmoneypy.create_group('躺尸一年') for entity_id in vol_df.index[:50]: _, _, code = decode_entity_id(entity_id) eastmoneypy.add_to_group(code=code, group_name='躺尸一年') except Exception as e: logger.exception(e) email_action.send_message("*****@*****.**", f'report_top_stats error', 'report_top_stats error:{}'.format(e)) for s in stats: msg = msg + s + '\n' for up in ups: msg = msg + up + '\n' for down in downs: msg = msg + down + '\n' email_action.send_message('*****@*****.**', f'{current_timestamp} 统计报告', msg)
StockActorSummary.actor_type == ActorType.raised_fund.value, StockActorSummary.holding_ratio >= fund_ratio_nums ], columns=['entity_id', 'holding_ratio', 'actor_type'], ) # fund_cap_df = FundStock.query_data(filters=[FundStock.report_date >= report_date, FundStock.timestamp <= current_timestamp, FundStock.proportion >= 0.01], # columns=['stock_id', 'market_cap', 'proportion']) top_holding_stocks = fund_cap_df['entity_id'].tolist() # 至少上市 least_start_day_nums = 60 filters = None pre_date = next_date(current_timestamp, -least_start_day_nums) stocks = get_entity_ids(provider='joinquant', entity_schema=Stock, filters=[Stock.timestamp <= pre_date]) filters = [Stock1dHfqKdata.entity_id.in_(stocks)] # 任一rps 大于90 top_rps_stocks = [] rps_rank_limit = 1000 periods = [50, 120, 250] for period in periods: start = next_date(current_timestamp, -period) updf, _ = get_top_performance_entities(start_timestamp=start, filters=filters, pct=1, show_name=True) top_rps_stocks.extend(updf.iloc[:rps_rank_limit].index.tolist())
def report_top_stats( entity_provider, data_provider, periods=[7, 30, 180, 365], ignore_new_stock=True, entity_type="stock", adjust_type=None, top_count=30, turnover_threshold=100000000, turnover_rate_threshold=0.02, em_group_over_write=True, ): if not adjust_type: adjust_type = default_adjust_type(entity_type=entity_type) kdata_schema = get_kdata_schema(entity_type=entity_type, adjust_type=adjust_type) entity_schema = get_entity_schema(entity_type=entity_type) latest_day = kdata_schema.query_data(provider=data_provider, order=kdata_schema.timestamp.desc(), limit=1, return_type="domain") current_timestamp = latest_day[0].timestamp email_action = EmailInformer() # 至少上市一年 filter_entity_ids = [] if ignore_new_stock: pre_year = next_date(current_timestamp, -365) entity_ids = get_entity_ids( provider=entity_provider, entity_schema=entity_schema, filters=[entity_schema.timestamp <= pre_year]) if not entity_ids: msg = f"{entity_type} no entity_ids listed one year" logger.error(msg) email_action.send_message(zvt_config["email_username"], "report_top_stats error", msg) return filter_entity_ids = entity_ids filter_turnover_df = kdata_schema.query_data( filters=[ kdata_schema.turnover >= turnover_threshold, kdata_schema.turnover_rate >= turnover_rate_threshold, ], provider=data_provider, start_timestamp=current_timestamp, index="entity_id", columns=["entity_id", "code"], ) if filter_entity_ids: filter_entity_ids = set(filter_entity_ids) & set( filter_turnover_df.index.tolist()) else: filter_entity_ids = filter_turnover_df.index.tolist() if not filter_entity_ids: msg = f"{entity_type} no entity_ids selected" logger.error(msg) email_action.send_message(zvt_config["email_username"], "report_top_stats error", msg) return logger.info( f"{entity_type} filter_entity_ids size: {len(filter_entity_ids)}") filters = [kdata_schema.entity_id.in_(filter_entity_ids)] stats = [] ups = [] downs = [] for period in periods: start = next_date(current_timestamp, -period) df, _ = get_top_performance_entities( entity_type=entity_type, start_timestamp=start, filters=filters, pct=1, show_name=True, entity_provider=entity_provider, data_provider=data_provider, ) df.rename(columns={"score": f"score_{period}"}, inplace=True) ups.append(tabulate(df.iloc[:top_count], headers="keys")) downs.append(tabulate(df.iloc[-top_count:], headers="keys")) stats.append(tabulate(df.describe(), headers="keys")) # 最近一个月和一周最靓仔的 if period == 7 or period == 30: try: codes = [ decode_entity_id(entity_id)[2] for entity_id in df.index[:top_count] ] add_to_eastmoney(codes=codes, entity_type=entity_type, group="最靓仔", over_write=em_group_over_write) except Exception as e: logger.exception(e) email_action.send_message( zvt_config["email_username"], f"report_top_stats error", "report_top_stats error:{}".format(e)) # 一年内跌幅最大的 if period == 365: try: codes = [ decode_entity_id(entity_id)[2] for entity_id in df.index[-top_count:] ] add_to_eastmoney(codes=codes, entity_type=entity_type, group="谁有我惨", over_write=em_group_over_write) except Exception as e: logger.exception(e) email_action.send_message( zvt_config["email_username"], f"report_top_stats error", "report_top_stats error:{}".format(e)) msg = "\n" for s in stats: msg = msg + s + "\n" email_action.send_message(zvt_config["email_username"], f"{current_timestamp} {entity_type}统计报告", msg) msg = "\n" for up in ups: msg = msg + up + "\n" email_action.send_message(zvt_config["email_username"], f"{current_timestamp} {entity_type}涨幅统计报告", msg) msg = "\n" for down in downs: msg = msg + down + "\n" email_action.send_message(zvt_config["email_username"], f"{current_timestamp} {entity_type}跌幅统计报告", msg)