self.selectors.append(week_selector) if __name__ == '__main__': start = '2019-01-01' end = '2021-01-01' trader_name = 'keep_run_trader' clear_trader(trader_name=trader_name) for time_interval in split_time_interval(start=start, end=end, interval=40): start_timestamp = time_interval[0] end_timestamp = time_interval[-1] # 成交量 vol_df = get_top_volume_entities(entity_type='stock', start_timestamp=next_date( start_timestamp, -50), end_timestamp=start_timestamp, pct=0.3) # 机构重仓 ii_df = get_top_fund_holding_stocks(timestamp=start_timestamp, pct=0.3, by='trading') current_entity_pool = list( set(vol_df.index.tolist()) & set(ii_df.index.tolist())) logger.info( f'current_entity_pool({len(current_entity_pool)}):{current_entity_pool}' ) trader = MultipleLevelTrader(start_timestamp=start_timestamp,
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)
def report_targets( factor_cls: Type[Factor], entity_provider, data_provider, title, entity_type="stock", informer: EmailInformer = None, em_group=None, em_group_over_write=True, filter_by_volume=True, adjust_type=None, start_timestamp="2019-01-01", **factor_kv, ): logger.info( f"entity_provider: {entity_provider}, data_provider: {data_provider}, entity_type: {entity_type}, start_timestamp: {start_timestamp}" ) error_count = 0 while error_count <= 10: try: if not adjust_type: adjust_type = default_adjust_type(entity_type=entity_type) target_date = get_latest_kdata_date(provider=data_provider, entity_type=entity_type, adjust_type=adjust_type) logger.info(f"target_date :{target_date}") current_entity_pool = None if filter_by_volume: # 成交量 vol_df = get_top_volume_entities( entity_type=entity_type, start_timestamp=next_date(target_date, -30), end_timestamp=target_date, adjust_type=adjust_type, pct=0.4, data_provider=data_provider, ) current_entity_pool = vol_df.index.tolist() logger.info( f"current_entity_pool({len(current_entity_pool)}): {current_entity_pool}" ) kdata_schema = get_kdata_schema(entity_type, level=IntervalLevel.LEVEL_1DAY, adjust_type=adjust_type) filters = [] if "turnover_threshold" in factor_kv: filters = filters + [ kdata_schema.turnover >= factor_kv.get("turnover_threshold") ] if "turnover_rate_threshold" in factor_kv: filters = filters + [ kdata_schema.turnover_rate >= factor_kv.get("turnover_rate_threshold") ] if filters: filters = filters + [kdata_schema.timestamp == target_date] kdata_df = kdata_schema.query_data( provider=data_provider, filters=filters, columns=["entity_id", "timestamp"], index="entity_id") if current_entity_pool: current_entity_pool = set(current_entity_pool) & set( kdata_df.index.tolist()) else: current_entity_pool = kdata_df.index.tolist() if "entity_ids" in factor_kv: if current_entity_pool: current_entity_pool = set(current_entity_pool) & set( factor_kv.pop("entity_ids")) else: current_entity_pool = set(factor_kv.pop("entity_ids")) # add the factor my_selector = TargetSelector(start_timestamp=start_timestamp, end_timestamp=target_date, select_mode=SelectMode.condition_or) entity_schema = get_entity_schema(entity_type=entity_type) tech_factor = factor_cls( entity_schema=entity_schema, entity_provider=entity_provider, provider=data_provider, entity_ids=current_entity_pool, start_timestamp=start_timestamp, end_timestamp=target_date, adjust_type=adjust_type, **factor_kv, ) my_selector.add_factor(tech_factor) my_selector.run() long_stocks = my_selector.get_open_long_targets( timestamp=target_date) inform( informer, entity_ids=long_stocks, target_date=target_date, title=title, entity_provider=entity_provider, entity_type=entity_type, em_group=em_group, em_group_over_write=em_group_over_write, ) break except Exception as e: logger.exception("report error:{}".format(e)) time.sleep(60 * 3) error_count = error_count + 1 if error_count == 10: informer.send_message( zvt_config["email_username"], f"report {entity_type}{factor_cls.__name__} error", f"report {entity_type}{factor_cls.__name__} error: {e}", )
def report_vol_up(): while True: error_count = 0 email_action = EmailInformer() try: # 抓取k线数据 # StockTradeDay.record_data(provider='joinquant') # Stock1dKdata.record_data(provider='joinquant') latest_day: Stock1dHfqKdata = Stock1dHfqKdata.query_data( order=Stock1dHfqKdata.timestamp.desc(), limit=1, return_type='domain') target_date = latest_day[0].timestamp start_timestamp = next_date(target_date, -50) # 成交量 vol_df = get_top_volume_entities(entity_type='stock', start_timestamp=start_timestamp, end_timestamp=target_date, pct=0.4) current_entity_pool = vol_df.index.tolist() # 计算均线 start = '2019-01-01' my_selector = TargetSelector(start_timestamp=start, end_timestamp=target_date, select_mode=SelectMode.condition_or) # add the factors factor1 = VolumeUpMaFactor(entity_ids=current_entity_pool, start_timestamp=start, end_timestamp=target_date, windows=[120, 250], over_mode='or') my_selector.add_factor(factor1) my_selector.run() long_stocks = my_selector.get_open_long_targets( timestamp=target_date) msg = 'no targets' if long_stocks: stocks = get_entities(provider='joinquant', entity_schema=Stock, entity_ids=long_stocks, return_type='domain') # add them to eastmoney try: try: eastmoneypy.del_group('tech') except: pass eastmoneypy.create_group('tech') for stock in stocks: eastmoneypy.add_to_group(stock.code, group_name='tech') except Exception as e: email_action.send_message( zvt_config['email_username'], f'report_vol_up error', 'report_vol_up error:{}'.format(e)) infos = stocks_with_info(stocks) msg = '\n'.join(infos) + '\n' logger.info(msg) email_action.send_message(zvt_config['email_username'], f'{target_date} 改进版放量突破(半)年线选股结果', msg) break except Exception as e: logger.exception('report_vol_up error:{}'.format(e)) time.sleep(60 * 3) error_count = error_count + 1 if error_count == 10: email_action.send_message(zvt_config['email_username'], f'report_vol_up error', 'report_vol_up error:{}'.format(e))
def report_targets( factor_cls: Type[Factor], entity_provider, data_provider, title, entity_type="stock", em_group=None, em_group_over_write=True, filter_by_volume=True, adjust_type=None, start_timestamp="2019-01-01", **factor_kv, ): logger.info( f"entity_provider: {entity_provider}, data_provider: {data_provider}, entity_type: {entity_type}, start_timestamp: {start_timestamp}" ) error_count = 0 while error_count <= 10: email_action = EmailInformer() try: if entity_type == "stock" and not adjust_type: adjust_type = AdjustType.hfq target_date = get_latest_kdata_date(provider=data_provider, entity_type=entity_type, adjust_type=adjust_type) logger.info(f"target_date :{target_date}") current_entity_pool = None if filter_by_volume: # 成交量 vol_df = get_top_volume_entities( entity_type=entity_type, start_timestamp=next_date(target_date, -30), end_timestamp=target_date, adjust_type=adjust_type, pct=0.4, ) current_entity_pool = vol_df.index.tolist() logger.info( f"current_entity_pool({len(current_entity_pool)}): {current_entity_pool}" ) # add the factor my_selector = TargetSelector(start_timestamp=start_timestamp, end_timestamp=target_date, select_mode=SelectMode.condition_or) entity_schema = get_entity_schema(entity_type=entity_type) tech_factor = factor_cls( entity_schema=entity_schema, entity_provider=entity_provider, provider=data_provider, entity_ids=current_entity_pool, start_timestamp=start_timestamp, end_timestamp=target_date, adjust_type=adjust_type, **factor_kv, ) my_selector.add_factor(tech_factor) my_selector.run() long_stocks = my_selector.get_open_long_targets( timestamp=target_date) msg = "no targets" if long_stocks: entities = get_entities(provider=entity_provider, entity_type=entity_type, entity_ids=long_stocks, return_type="domain") if em_group: try: codes = [entity.code for entity in entities] add_to_eastmoney(codes=codes, entity_type=entity_type, group=em_group, over_write=em_group_over_write) except Exception as e: email_action.send_message( zvt_config["email_username"], f"report {entity_type}{factor_cls.__name__} error", f"report {entity_type}{factor_cls.__name__} error: {e}", ) infos = [ f"{entity.name}({entity.code})" for entity in entities ] msg = "\n".join(infos) + "\n" logger.info(msg) email_action.send_message(zvt_config["email_username"], f"{target_date} {title}", msg) break except Exception as e: logger.exception("report error:{}".format(e)) time.sleep(60 * 3) error_count = error_count + 1 if error_count == 10: email_action.send_message( zvt_config["email_username"], f"report {entity_type}{factor_cls.__name__} error", f"report {entity_type}{factor_cls.__name__} error: {e}", )