def init_selectors(self, entity_ids, entity_schema, exchanges, codes, start_timestamp, end_timestamp, adjust_type=None): myselector = TargetSelector(region=self.region, entity_ids=entity_ids, entity_schema=entity_schema, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, provider=Provider.JoinQuant) myselector.add_filter_factor( BullFactor(region=self.region, entity_ids=entity_ids, entity_schema=entity_schema, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, adjust_type='hfq')) self.selectors.append(myselector)
def init_selectors(self, entity_ids, entity_schema, exchanges, codes, start_timestamp, end_timestamp, adjust_type=None): myselector = TargetSelector(region=self.region, entity_ids=entity_ids, entity_schema=entity_schema, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, provider=Provider.JoinQuant) myselector.add_filter_factor( CrossMaFactor(region=self.region, entity_ids=entity_ids, entity_schema=entity_schema, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, windows=[5, 10], need_persist=False)) self.selectors.append(myselector)
def init_selectors(self, entity_ids, entity_schema, exchanges, codes, start_timestamp, end_timestamp, adjust_type=None): myselector = TargetSelector( entity_ids=entity_ids, entity_schema=entity_schema, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, provider="joinquant", ) myselector.add_factor( CrossMaFactor( entity_ids=entity_ids, entity_schema=entity_schema, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, windows=[5, 10], need_persist=False, adjust_type=adjust_type, )) self.selectors.append(myselector)
def init_selectors(self, entity_ids, entity_schema, exchanges, codes, start_timestamp, end_timestamp, adjust_type=None): myselector = TargetSelector( entity_ids=entity_ids, entity_schema=entity_schema, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, provider="joinquant", ) myselector.add_factor( BullFactor( entity_ids=entity_ids, entity_schema=entity_schema, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, adjust_type=adjust_type, )) self.selectors.append(myselector)
def test_cross_ma_selector(): region = Region.CHN entity_ids = ['stock_sz_000338'] entity_type = EntityType.Stock start_timestamp = '2018-01-01' end_timestamp = '2019-06-30' my_selector = TargetSelector(region=region, entity_ids=entity_ids, entity_schema=entity_type, start_timestamp=start_timestamp, end_timestamp=end_timestamp) # add the factors my_selector \ .add_filter_factor(CrossMaFactor(region=region, entity_ids=entity_ids, start_timestamp=start_timestamp, end_timestamp=end_timestamp, computing_window=10, windows=[5, 10], need_persist=False, level=IntervalLevel.LEVEL_1DAY, adjust_type='qfq')) my_selector.run() print(my_selector.open_long_df) print(my_selector.open_short_df) assert 'stock_sz_000338' in my_selector.get_open_short_targets( '2018-01-29')
def init_selectors(self, entity_ids, entity_type, exchanges, codes, start_timestamp, end_timestamp): myselector = TargetSelector(entity_ids=entity_ids, entity_type=entity_type, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, provider='joinquant') myselector.add_filter_factor( BullFactor(entity_ids=entity_ids, entity_type=entity_type, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp)) self.selectors.append(myselector)
def init_selectors(self, entity_ids, entity_type, exchanges, codes, start_timestamp, end_timestamp): myselector = TargetSelector(entity_ids=entity_ids, entity_type=entity_type, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, provider='ccxt', level=IntervalLevel.LEVEL_1MIN) myselector.add_filter_factor( CrossMaFactor(entity_ids=entity_ids, entity_type=entity_type, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, provider='ccxt', level=IntervalLevel.LEVEL_1MIN)) self.selectors.append(myselector)
def test_cross_ma_selector(): entity_ids = ["stock_sz_000338"] entity_type = "stock" start_timestamp = "2018-01-01" end_timestamp = "2019-06-30" my_selector = TargetSelector(entity_ids=entity_ids, entity_schema=entity_type, start_timestamp=start_timestamp, end_timestamp=end_timestamp) # add the factors my_selector.add_factor( CrossMaFactor( entity_ids=entity_ids, start_timestamp=start_timestamp, end_timestamp=end_timestamp, computing_window=10, windows=[5, 10], need_persist=False, level=IntervalLevel.LEVEL_1DAY, adjust_type="hfq", )) my_selector.run() print(my_selector.open_long_df) print(my_selector.open_short_df) assert "stock_sz_000338" in my_selector.get_open_short_targets( "2018-01-29")
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 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: Stock1dKdata = Stock1dKdata.query_data(order=Stock1dKdata.timestamp.desc(), limit=1, return_type='domain') target_date = latest_day[0].timestamp # 计算均线 my_selector = TargetSelector(start_timestamp='2018-01-01', end_timestamp=target_date) # add the factors factor1 = VolumeUpMa250Factor(start_timestamp='2018-01-01', end_timestamp=target_date) my_selector.add_filter_factor(factor1) my_selector.run() long_targets = my_selector.get_open_long_targets(timestamp=target_date) if long_targets: stocks = get_entities(provider='joinquant', entity_schema=Stock, entity_ids=long_targets, 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 = ' '.join(info) else: msg = 'no targets' 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 report_cross_ma(): while True: error_count = 0 email_action = EmailInformer() try: # 抓取k线数据 # StockTradeDay.record_data(provider='joinquant') # Stock1dKdata.record_data(provider='joinquant') latest_day: StockTradeDay = StockTradeDay.query_data( order=StockTradeDay.timestamp.desc(), limit=1, return_type='domain') if latest_day: target_date = latest_day[0].timestamp else: target_date = now_pd_timestamp() # 计算均线 my_selector = TargetSelector(start_timestamp='2018-01-01', end_timestamp=target_date) # add the factors ma_factor = CrossMaFactor(start_timestamp='2018-01-01', end_timestamp=target_date) my_selector.add_filter_factor(ma_factor) my_selector.run() long_targets = my_selector.get_open_long_targets( timestamp=target_date) if long_targets: stocks = get_entities(provider='joinquant', entity_schema=Stock, entity_ids=long_targets, return_type='domain') info = [f'{stock.name}({stock.code})' for stock in stocks] msg = ' '.join(info) else: msg = 'no targets' logger.info(msg) email_action.send_message("*****@*****.**", f'{target_date} 均线选股结果', msg) break except Exception as e: logger.exception('report_cross_ma error:{}'.format(e)) time.sleep(60 * 3) error_count = error_count + 1 if error_count == 10: email_action.send_message("*****@*****.**", f'report_cross_ma error', 'report_cross_ma error:{}'.format(e))
def test_cross_ma_selector(): entity_ids = ['stock_sz_000338'] entity_type = 'stock' start_timestamp = '2018-01-01' end_timestamp = '2019-06-30' my_selector = TargetSelector(entity_ids=entity_ids, entity_type=entity_type, start_timestamp=start_timestamp, end_timestamp=end_timestamp) # add the factors my_selector \ .add_filter_factor(CrossMaFactor(entity_ids=entity_ids, entity_type=entity_type, start_timestamp=start_timestamp, end_timestamp=end_timestamp, level=IntervalLevel.LEVEL_1DAY)) my_selector.run() print(my_selector.open_long_df) print(my_selector.open_short_df) assert 'stock_sz_000338' in my_selector.get_open_short_targets( '2018-01-29')
def init_selectors(self, entity_ids, entity_schema, exchanges, codes, start_timestamp, end_timestamp): myselector = TargetSelector(entity_ids=entity_ids, entity_schema=entity_schema, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, provider='joinquant') myselector.add_filter_factor( CrossMaFactor(entity_ids=entity_ids, entity_schema=entity_schema, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, windows=[5, 10], need_persist=False)) self.selectors.append(myselector)
def every_day_report(): while True: try: t = now_pd_timestamp() if t.dayofweek in (5, 6): logger.info(f'today:{t} is {t.day_name()},just ignore') today = to_time_str(t) # 抓取k线数据 JqChinaStockKdataRecorder(level=IntervalLevel.LEVEL_1DAY).run() JqChinaStockKdataRecorder(level=IntervalLevel.LEVEL_1WEEK).run() JqChinaStockKdataRecorder(level=IntervalLevel.LEVEL_1MON).run() # 计算均线 my_selector = TargetSelector(start_timestamp='2016-01-01', end_timestamp=today) # add the factors # 设置dry_run为True,因为我们只需要最近的数据,不需要加载全量数据进行回测 ma_factor = CrossMaFactor(start_timestamp='2016-01-01', end_timestamp=today, dry_run=True) my_selector.add_filter_factor(ma_factor) my_selector.run() long_targets = my_selector.get_open_long_targets(timestamp=today) if long_targets: df = get_entities(provider='eastmoney', entity_schema=Stock, entity_ids=long_targets, columns=['code', 'name']) info = [ df.loc[i, 'code'] + ' ' + df.loc[i, 'name'] for i in df.index ] msg = ' '.join(info) else: msg = 'no targets' logger.info(msg) email_action = EmailInformer() email_action.send_message("*****@*****.**", f'{today} 均线选股结果', msg) break except Exception as e: logger.exception('report1 sched error:{}'.format(e)) time.sleep(60 * 3)
def report_tm(): while True: error_count = 0 discord_informer = DiscordInformer() try: # 抓取k线数据 # Coin1dKdata.record_data() target_date = date.today() - timedelta(days=1) start_date = target_date - timedelta(60) # 计算 my_selector = TargetSelector(entity_schema=Coin, provider='ccxt', start_timestamp=start_date, end_timestamp=target_date) # add the factors tm_factor = TMFactor(entity_schema=Coin, provider='ccxt', start_timestamp=start_date, end_timestamp=target_date) my_selector.add_filter_factor(tm_factor) my_selector.run() long_targets = list( map( lambda x: x.replace('_', '.'), filter( lambda k: ('USDT' in k) or ('PERP' in k), my_selector.get_open_long_targets( timestamp=target_date)))) logger.info(long_targets) discord_informer.send_message( f'{target_date} TM选股结果 {long_targets}') break except Exception as e: logger.exception('report_tm error:{}'.format(e)) time.sleep(60 * 3) error_count = error_count + 1 if error_count == 10: discord_informer.send_message(f'report_tm error', 'report_tm error:{}'.format(e))
from zvdata.utils.time_utils import now_pd_timestamp from zvt.factors.ma.ma_factor import CrossMaFactor from zvt.factors.target_selector import TargetSelector my_selector = TargetSelector(start_timestamp='2018-10-01', end_timestamp=now_pd_timestamp()) # add the factors ma_factor = CrossMaFactor(start_timestamp='2018-10-01', end_timestamp=now_pd_timestamp()) my_selector.add_filter_factor(ma_factor) my_selector.run() long_targets = my_selector.get_open_long_targets(timestamp='2019-11-11') print(long_targets) print(len(long_targets))
def report_tm(): while True: error_count = 0 discord_informer = DiscordInformer() try: # 抓取k线数据 # StockTradeDay.record_data(provider='baostock', sleeping_time=2) # Stock1dKdata.record_data(provider='baostock', sleeping_time=1.5) latest_day: StockTradeDay = StockTradeDay.query_data(order=StockTradeDay.timestamp.desc(), limit=1,provider='joinquant', return_type='domain') if latest_day: target_date = latest_day[0].timestamp else: target_date = now_pd_timestamp() start_date = target_date - timedelta(60) # 计算 my_selector = TargetSelector(entity_schema=Stock, provider='joinquant', start_timestamp=start_date, end_timestamp=target_date) # add the factors tm_factor = TMFactor(entity_schema=Stock, provider='joinquant', start_timestamp=start_date, end_timestamp=target_date) my_selector.add_filter_factor(tm_factor) my_selector.run() long_targets = my_selector.get_open_long_targets(timestamp=target_date) logger.info(long_targets) msg = 'no targets' # 过滤亏损股 # check StockValuation data pe_date = target_date - 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_targets, start_timestamp=pe_date, filters=[StockValuation.pe > 0], columns=['entity_id']) bad_stocks = set(long_targets) - 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: # use block to filter block_selector = BlockSelector(start_timestamp='2020-01-01', long_threshold=0.8) block_selector.run() long_blocks = block_selector.get_open_long_targets(timestamp=target_date) if long_blocks: blocks = Block.query_data(provider='sina', entity_ids=long_blocks, return_type='domain') info = [f'{block.name}({block.code})' for block in blocks] msg = ' '.join(info) + '\n' block_stocks = BlockStock.query_data(provider='sina', filters=[ BlockStock.stock_id.in_(long_stocks)], entity_ids=long_blocks, return_type='domain') block_map_stocks = {} for block_stock in block_stocks: stocks = block_map_stocks.get(block_stock.name) if not stocks: stocks = [] block_map_stocks[block_stock.name] = stocks stocks.append(f'{block_stock.stock_name}({block_stock.stock_code})') for block in block_map_stocks: stocks = block_map_stocks[block] stock_msg = ' '.join(stocks) msg = msg + f'{block}:\n' + stock_msg + '\n' discord_informer.send_message(f'{target_date} TM选股结果 {msg}') break except Exception as e: logger.exception('report_tm error:{}'.format(e)) time.sleep(60 * 3) error_count = error_count + 1 if error_count == 10: discord_informer.send_message(f'report_tm error', 'report_tm error:{}'.format(e))
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 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))