def report_bull(): while True: error_count = 0 email_action = EmailInformer() try: latest_day: Stock1dHfqKdata = Stock1dHfqKdata.query_data(order=Stock1dHfqKdata.timestamp.desc(), limit=1, return_type='domain') target_date = latest_day[0].timestamp # 计算均线 start = '2019-01-01' my_selector = TargetSelector(start_timestamp=start, end_timestamp=target_date) # add the factors factor1 = BullFactor(start_timestamp=start, end_timestamp=target_date) factor2 = CrossMaVolumeFactor(start_timestamp=start, end_timestamp=target_date, windows=[5, 120, 250]) my_selector.add_factor(factor1) my_selector.add_factor(factor2) 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('bull') except: pass eastmoneypy.create_group('bull') for stock in stocks: eastmoneypy.add_to_group(stock.code, group_name='bull') except Exception as e: email_action.send_message(zvt_config['email_username'], f'report_bull error', 'report_bull 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} bull选股结果', msg) break except Exception as e: logger.exception('report_bull 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_bull error', 'report_bull error:{}'.format(e))
def get_player_performance(start_timestamp, end_timestamp=None, days=5, players="机构专用", provider="em", buy_rate=5): filters = [] if isinstance(players, str): players = [players] if isinstance(players, list): for player in players: filters.append( or_( and_(DragonAndTiger.dep1 == player, DragonAndTiger.dep1_rate >= buy_rate), and_(DragonAndTiger.dep2 == player, DragonAndTiger.dep2_rate >= buy_rate), and_(DragonAndTiger.dep3 == player, DragonAndTiger.dep3_rate >= buy_rate), and_(DragonAndTiger.dep4 == player, DragonAndTiger.dep4_rate >= buy_rate), and_(DragonAndTiger.dep5 == player, DragonAndTiger.dep5_rate >= buy_rate), )) else: raise AssertionError("players should be list or str type") df = DragonAndTiger.query_data( start_timestamp=start_timestamp, end_timestamp=end_timestamp, filters=filters, index=["entity_id", "timestamp"], provider=provider, ) df = df[~df.index.duplicated(keep="first")] records = [] for entity_id, timestamp in df.index: end_date = next_date(timestamp, days + round(days + days * 2 / 5 + 30)) kdata = Stock1dHfqKdata.query_data( entity_id=entity_id, start_timestamp=timestamp, end_timestamp=end_date, provider=provider, index="timestamp", ) if len(kdata) <= days: logger.warning(f"ignore {timestamp} -> end_timestamp: {end_date}") break close = kdata["close"] change_pct = (close[days] - close[0]) / close[0] records.append({ "entity_id": entity_id, "timestamp": timestamp, f"change_pct": change_pct }) return pd.DataFrame.from_records(records)
def report_vol_up_250(): 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 # 计算均线 my_selector = TargetSelector(start_timestamp='2018-10-01', end_timestamp=target_date) # add the factors factor1 = VolumeUpMaFactor(start_timestamp='2018-10-01', end_timestamp=target_date) my_selector.add_filter_factor(factor1) my_selector.run() long_stocks = my_selector.get_open_long_targets( timestamp=target_date) msg = 'no targets' # 过滤亏损股 # check StockValuation data pe_date = target_date - datetime.timedelta(10) if StockValuation.query_data(start_timestamp=pe_date, limit=1, return_type='domain'): positive_df = StockValuation.query_data( provider='joinquant', entity_ids=long_stocks, start_timestamp=pe_date, filters=[StockValuation.pe > 0], columns=['entity_id']) bad_stocks = set(long_stocks) - set( positive_df['entity_id'].tolist()) if bad_stocks: stocks = get_entities(provider='joinquant', entity_schema=Stock, entity_ids=bad_stocks, return_type='domain') info = [f'{stock.name}({stock.code})' for stock in stocks] msg = '亏损股:' + ' '.join(info) + '\n' long_stocks = set(positive_df['entity_id'].tolist()) 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( "*****@*****.**", f'report_vol_up_250 error', 'report_vol_up_250 error:{}'.format(e)) info = [f'{stock.name}({stock.code})' for stock in stocks] msg = msg + '盈利股:' + ' '.join(info) + '\n' logger.info(msg) email_action.send_message(get_subscriber_emails(), f'{target_date} 改进版放量突破年线选股结果', msg) break except Exception as e: logger.exception('report_vol_up_250 error:{}'.format(e)) time.sleep(60 * 3) error_count = error_count + 1 if error_count == 10: email_action.send_message( "*****@*****.**", f'report_vol_up_250 error', 'report_vol_up_250 error:{}'.format(e))
def transform(self, input_df: pd.DataFrame) -> pd.DataFrame: col = f"ma{self.window}" if col not in input_df.columns: group_result = ( group_by_entity_id(input_df["close"]).rolling(window=self.window, min_periods=self.window).mean() ) group_result = normalize_group_compute_result(group_result=group_result) input_df[col] = group_result # 连续3(interval)日收在10(window)日线下 s = input_df["close"] < input_df[col] s = ( group_by_entity_id(s) .rolling(window=self.interval, min_periods=self.interval) .apply(lambda x: np.logical_and.reduce(x)) ) s = normalize_group_compute_result(group_result=s) # 构造卖点 s[s == False] = None s[s == True] = False input_df = merge_filter_result(input_df=input_df, filter_result=s) return input_df if __name__ == "__main__": df = Stock1dHfqKdata.query_data(codes=["000338"], index=["entity_id", "timestamp"]) df = FallBelowTransformer().transform(df) print(df["filter_result"])
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 record_dragon_tiger(data_provider="em", entity_provider="em", sleeping_time=2): # 龙虎榜数据 run_data_recorder( domain=DragonAndTiger, data_provider=data_provider, entity_provider=entity_provider, day_data=True, sleeping_time=sleeping_time, ) email_action = EmailInformer() # recent year start_timestamp = next_date(current_date(), -400) # 最近一年牛x的营业部 players = get_big_players(start_timestamp=start_timestamp) # 最近30天有牛x的营业部上榜的个股 recent_date = next_date(current_date(), -30) selected = [] for player in players: filters = [ or_( and_(DragonAndTiger.dep1 == player, DragonAndTiger.dep1_rate >= 5), and_(DragonAndTiger.dep2 == player, DragonAndTiger.dep2_rate >= 5), and_(DragonAndTiger.dep3 == player, DragonAndTiger.dep3_rate >= 5), and_(DragonAndTiger.dep4 == player, DragonAndTiger.dep4_rate >= 5), and_(DragonAndTiger.dep5 == player, DragonAndTiger.dep5_rate >= 5), ) ] df = DragonAndTiger.query_data( start_timestamp=recent_date, filters=filters, columns=[ DragonAndTiger.timestamp, DragonAndTiger.entity_id, DragonAndTiger.code, DragonAndTiger.name ], index="entity_id", ) selected = selected + df.index.tolist() if selected: selected = list(set(selected)) target_date = get_latest_kdata_date(provider=data_provider, entity_type="stock", adjust_type="hfq") df = Stock1dHfqKdata.query_data( provider=data_provider, entity_ids=selected, filters=[ Stock1dHfqKdata.turnover_rate > 0.02, Stock1dHfqKdata.timestamp == to_pd_timestamp(target_date), Stock1dHfqKdata.turnover > 300000000, ], index=["entity_id"], ) inform( action=email_action, entity_ids=df.index.tolist(), target_date=current_date(), title="report 龙虎榜", entity_provider=entity_provider, entity_type="stock", em_group="重要指数", em_group_over_write=False, )
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))
logger = logging.getLogger(__name__) def to_tradingview_code(code): # 上海 if code >= '333333': return f'SSE:{code}' else: return f'SZSE:{code}' if __name__ == '__main__': print('start') latest_day: Stock1dHfqKdata = Stock1dHfqKdata.query_data( order=Stock1dHfqKdata.timestamp.desc(), limit=1, return_type='domain') current_timestamp = latest_day[0].timestamp # 基金持股 fund_ratio_nums = 0.01 report_date = get_recent_report_date(current_timestamp, 1) fund_cap_df = StockActorSummary.query_data( order=StockActorSummary.timestamp.desc(), filters=[ StockActorSummary.report_date >= report_date, StockActorSummary.actor_type == ActorType.raised_fund.value, StockActorSummary.holding_ratio >= fund_ratio_nums ], columns=['entity_id', 'holding_ratio', 'actor_type'], )
# arrowcolor='#030813', ax=-10, ay=-30, bordercolor='#c7c7c7', borderwidth=1, bgcolor=color, opacity=0.8)) return annotations return None if __name__ == '__main__': # from zvt.factors.pattern import ZenFactor # # data_reader1 = ZenFactor(codes=['000338'], level='1d') # data_reader2 = ZenFactor(codes=['000338'], level='1wk') # print(data_reader2.data_df) # # stacked = StackedDrawer(data_reader1.drawer(), data_reader2.drawer()).draw_kline() df = Stock1dHfqKdata.query_data(code='000338', start_timestamp='2015-01-01') sub_df = FinanceFactor.query_data(code='000338', start_timestamp='2015-01-01', columns=[ FinanceFactor.roe, FinanceFactor.entity_id, FinanceFactor.timestamp ]) Drawer(main_df=df, sub_df_list=[sub_df]).draw_kline(show=True)