def test_cross_ma(): factor = CrossMaFactor(codes=['000338'], start_timestamp='2019-01-01', end_timestamp='2019-06-10', level=IntervalLevel.LEVEL_1DAY, provider=Provider.JoinQuant, windows=[5, 10], adjust_type='qfq') print(factor.factor_df.tail()) print(factor.result_df.tail()) score = factor.result_df['score'] assert score[('stock_sz_000338', '2019-06-03')] == True assert score[('stock_sz_000338', '2019-06-04')] == True assert ('stock_sz_000338', '2019-06-05') not in score or score[('stock_sz_000338', '2019-06-05')] == False assert ('stock_sz_000338', '2019-06-06') not in score or score[('stock_sz_000338', '2019-06-06')] == False assert ('stock_sz_000338', '2019-06-10') not in score or score[('stock_sz_000338', '2019-06-10')] == False factor.move_on() score = factor.result_df['score'] assert score[('stock_sz_000338', '2019-06-17')] == True
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_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, region: Region, entity_ids, entity_schema, exchanges, codes, start_timestamp, end_timestamp, adjust_type=None): myselector = TargetSelector(region=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=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, adjust_type=adjust_type)) 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_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 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 every_day_report(): while True: try: today = now_pd_timestamp() long_targets = select_by_finance(today) logger.info(f'selected:{len(long_targets)}') if long_targets: ma_factor = CrossMaFactor(start_timestamp='2015-01-01', end_timestamp=today, dry_run=True, persist_factor=False, entity_ids=long_targets, windows=[5, 30, 120]) my_selector = TargetSelector(start_timestamp='2015-01-01', end_timestamp=today, entity_ids=long_targets) my_selector.add_filter_factor(ma_factor) my_selector.run() final_targets = my_selector.get_open_long_targets(today) final_targets = list(set(final_targets)) logger.info(f'final selected:{len(final_targets)}') if final_targets: df = get_entities(provider='eastmoney', entity_schema=Stock, entity_ids=final_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('report3 sched error:{}'.format(e)) time.sleep(60 * 3)
def test_cross_ma(): factor = CrossMaFactor( codes=["000338"], start_timestamp="2019-01-01", end_timestamp="2019-06-10", level=IntervalLevel.LEVEL_1DAY, provider="joinquant", windows=[5, 10], adjust_type="qfq", ) print(factor.factor_df.tail()) print(factor.result_df.tail()) score = factor.result_df["filter_result"] assert score[("stock_sz_000338", "2019-06-03")] == True assert score[("stock_sz_000338", "2019-06-04")] == True assert ("stock_sz_000338", "2019-06-05") not in score or score[("stock_sz_000338", "2019-06-05")] == False assert ("stock_sz_000338", "2019-06-06") not in score or score[("stock_sz_000338", "2019-06-06")] == False assert ("stock_sz_000338", "2019-06-10") not in score or score[("stock_sz_000338", "2019-06-10")] == False factor.move_on() score = factor.result_df["filter_result"] assert score[("stock_sz_000338", "2019-06-17")] == True
def init_selectors(self, entity_ids, entity_schema, exchanges, codes, start_timestamp, end_timestamp): # 周线策略 week_selector = TargetSelector(entity_ids=entity_ids, entity_schema=entity_schema, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, provider='joinquant', level=IntervalLevel.LEVEL_1WEEK) week_bull_factor = GoldBullFactor(entity_ids=entity_ids, entity_schema=entity_schema, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, provider='joinquant', level=IntervalLevel.LEVEL_1WEEK) week_selector.add_filter_factor(week_bull_factor) # 日线策略 day_selector = TargetSelector(entity_ids=entity_ids, entity_schema=entity_schema, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, provider='joinquant', level=IntervalLevel.LEVEL_1DAY) cross_ma_factor = CrossMaFactor(entity_ids=entity_ids, entity_schema=entity_schema, exchanges=exchanges, codes=codes, start_timestamp=start_timestamp, end_timestamp=end_timestamp, provider='joinquant', level=IntervalLevel.LEVEL_1DAY, windows=[5, 250]) day_selector.add_filter_factor(cross_ma_factor) self.selectors.append(week_selector) self.selectors.append(day_selector)
def test_cross_ma(): factor = CrossMaFactor(entity_type='stock', codes=['000338'], start_timestamp='2019-01-01', end_timestamp='2019-06-10', level=IntervalLevel.LEVEL_1DAY, provider='joinquant', windows=[5,10]) print(factor.get_factor_df().tail()) print(factor.get_result_df().tail()) score = factor.get_result_df()['score'] assert score[('stock_sz_000338', '2019-06-03')] == True assert score[('stock_sz_000338', '2019-06-04')] == True assert ('stock_sz_000338', '2019-06-05') not in score or score[('stock_sz_000338', '2019-06-05')] == False assert ('stock_sz_000338', '2019-06-06') not in score or score[('stock_sz_000338', '2019-06-06')] == False assert ('stock_sz_000338', '2019-06-10') not in score or score[('stock_sz_000338', '2019-06-10')] == False factor.move_on() score = factor.get_result_df()['score'] assert score[('stock_sz_000338', '2019-06-17')] == True
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 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='ccxt', level=IntervalLevel.LEVEL_1MIN) myselector.add_filter_factor( CrossMaFactor(entity_ids=entity_ids, entity_type=entity_schema, 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_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, computing_window=10, windows=[5,10], persist_factor=False, 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')
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))