Esempio n. 1
0
def report_block():
    while True:
        error_count = 0
        email_action = EmailInformer(ssl=True)

        try:
            latest_day: BlockMoneyFlow = BlockMoneyFlow.query_data(
                order=BlockMoneyFlow.timestamp.desc(),
                limit=1,
                return_type='domain')
            target_date = latest_day[0].timestamp

            msg = ''
            # 行业板块
            industry_block_selector = IndustryBlockSelector(
                start_timestamp='2020-01-01', long_threshold=0.8)
            industry_block_selector.run()
            industry_long_blocks = industry_block_selector.get_open_long_targets(
                timestamp=target_date)

            if industry_long_blocks:
                blocks: List[Block] = Block.query_data(
                    provider='sina',
                    entity_ids=industry_long_blocks,
                    return_type='domain')

                info = [f'{block.name}({block.code})' for block in blocks]
                msg = msg + '行业板块:' + ' '.join(info) + '\n'

            # 概念板块
            concept_block_selector = ConceptBlockSelector(
                start_timestamp='2020-01-01', long_threshold=0.85)
            concept_block_selector.run()
            concept_long_blocks = concept_block_selector.get_open_long_targets(
                timestamp=target_date)

            if concept_long_blocks:
                blocks: List[Block] = Block.query_data(
                    provider='sina',
                    entity_ids=concept_long_blocks,
                    return_type='domain')

                info = [f'{block.name}({block.code})' for block in blocks]
                msg = msg + '概念板块' + ' '.join(info) + '\n'

            logger.info(msg)
            email_action.send_message('*****@*****.**',
                                      f'{target_date} 资金流入板块评分结果', msg)
            break
        except Exception as e:
            logger.exception('report_block error:{}'.format(e))
            time.sleep(60 * 3)
            error_count = error_count + 1
            if error_count == 10:
                email_action.send_message("*****@*****.**",
                                          f'report_block error',
                                          'report_block error:{}'.format(e))
Esempio n. 2
0
def record_stock_data(data_provider="em", entity_provider="em", sleeping_time=2):
    # A股标的
    run_data_recorder(domain=Stock, data_provider=data_provider, force_update=False)
    # A股后复权行情
    run_data_recorder(
        domain=Stock1dHfqKdata,
        data_provider=data_provider,
        entity_provider=entity_provider,
        day_data=True,
        sleeping_time=sleeping_time,
    )

    # 板块(概念,行业)
    run_data_recorder(domain=Block, data_provider="eastmoney", force_update=False)
    # 板块行情(概念,行业)
    run_data_recorder(domain=Block1dKdata, data_provider="em", day_data=True, sleeping_time=sleeping_time)

    # 报告新概念和行业
    email_action = EmailInformer()
    list_date = next_date(current_date(), -90)
    df = Block.query_data(
        filters=[Block.category == BlockCategory.concept.value, Block.list_date >= list_date], index="entity_id"
    )

    # add them to eastmoney
    try:
        add_to_eastmoney(codes=df["code"], entity_type="block", group="新概念", over_write=False)
    except Exception as e:
        email_action.send_message(
            zvt_config["email_username"], f"report_concept error", "report_concept error:{}".format(e)
        )
Esempio n. 3
0
 def __init__(
     self,
     provider: str = 'sina',
     entity_provider: str = 'sina',
     the_timestamp: Union[str, pd.Timestamp] = None,
     start_timestamp: Union[str, pd.Timestamp] = None,
     end_timestamp: Union[str, pd.Timestamp] = None,
     columns: List = [
         BlockMoneyFlow.net_inflows, BlockMoneyFlow.net_main_inflows
     ],
     category=BlockCategory.industry.value,
     window=20,
     scorer: Scorer = RankScorer(ascending=True)) -> None:
     df = Block.query_data(provider=entity_provider,
                           filters=[Block.category == category])
     entity_ids = df['entity_id'].tolist()
     self.window = window
     super().__init__(BlockMoneyFlow,
                      Block,
                      provider=provider,
                      entity_provider=entity_provider,
                      entity_ids=entity_ids,
                      the_timestamp=the_timestamp,
                      start_timestamp=start_timestamp,
                      end_timestamp=end_timestamp,
                      columns=columns,
                      scorer=scorer)
Esempio n. 4
0
def show_industry_composition(entity_ids, timestamp):
    block_df = Block.query_data(provider='eastmoney', filters=[Block.category == 'industry'], index='entity_id')
    block_ids = block_df.index.tolist()

    block_df = BlockStock.query_data(entity_ids=block_ids, filters=[BlockStock.stock_id.in_(entity_ids)])

    s = block_df['name'].value_counts()

    cycle_df = pd.DataFrame(columns=s.index, data=[s.tolist()])
    cycle_df['entity_id'] = 'stock_cn_industry'
    cycle_df['timestamp'] = timestamp
    drawer = Drawer(main_df=cycle_df)
    drawer.draw_pie(show=True)
Esempio n. 5
0
def record_block():
    while True:
        email_action = EmailInformer()

        try:
            Block.record_data(provider='eastmoney', force_update=False)

            # 只抓取概念行情
            df = Block.query_data(filters=[Block.category == BlockCategory.concept.value], index='entity_id')
            entity_ids = df.index.tolist()
            Block1dKdata.record_data(provider='em', entity_ids=entity_ids)

            # 报告新概念
            list_date = next_date(current_date(), -90)
            df = Block.query_data(filters=[Block.category == BlockCategory.concept.value, Block.list_date >= list_date],
                                  index='entity_id')

            # add them to eastmoney
            try:
                try:
                    eastmoneypy.create_group('概念')
                except:
                    pass
                for code in df['code']:
                    eastmoneypy.add_to_group(code, group_name='概念', entity_type='block')
            except Exception as e:
                email_action.send_message(zvt_config['email_username'], f'report_concept error',
                                          'report_concept error:{}'.format(e))

            email_action.send_message(zvt_config['email_username'], 'record block finished', '')
            break
        except Exception as e:
            msg = f'record block error:{e}'
            logger.exception(msg)

            email_action.send_message(zvt_config['email_username'], 'record block error', msg)
            time.sleep(60)
Esempio n. 6
0
def record_stock_data(data_provider="em",
                      entity_provider="em",
                      sleeping_time=2):
    # A股标的
    run_data_recorder(domain=Stock,
                      data_provider=data_provider,
                      force_update=False)
    # A股后复权行情
    run_data_recorder(
        domain=Stock1dHfqKdata,
        data_provider=data_provider,
        entity_provider=entity_provider,
        day_data=True,
        sleeping_time=sleeping_time,
    )

    # 板块(概念,行业)
    run_data_recorder(domain=Block,
                      entity_provider=entity_provider,
                      data_provider=entity_provider,
                      force_update=False)
    # 板块行情(概念,行业)
    run_data_recorder(
        domain=Block1dKdata,
        entity_provider=entity_provider,
        data_provider=entity_provider,
        day_data=True,
        sleeping_time=sleeping_time,
    )

    # 报告新概念和行业
    email_action = EmailInformer()
    df = Block.query_data(
        filters=[Block.category == BlockCategory.concept.value],
        order=Block.list_date.desc(),
        index="entity_id",
        limit=5,
    )

    inform(
        action=email_action,
        entity_ids=df.index.tolist(),
        target_date=current_date(),
        title="report 新概念",
        entity_provider=entity_provider,
        entity_type="block",
        em_group="关注板块",
        em_group_over_write=True,
    )
Esempio n. 7
0
def show_industry_composition(entity_ids, timestamp):
    block_df = Block.query_data(provider="eastmoney",
                                filters=[Block.category == "industry"],
                                index="entity_id")
    block_ids = block_df.index.tolist()

    block_df = BlockStock.query_data(
        entity_ids=block_ids, filters=[BlockStock.stock_id.in_(entity_ids)])

    s = block_df["name"].value_counts()

    cycle_df = pd.DataFrame(columns=s.index, data=[s.tolist()])
    cycle_df["entity_id"] = "stock_cn_industry"
    cycle_df["timestamp"] = timestamp
    drawer = Drawer(main_df=cycle_df)
    drawer.draw_pie(show=True)
Esempio n. 8
0
def report_top_blocks():
    df = Block.query_data(filters=[Block.category == BlockCategory.industry.value], index="entity_id")

    entity_ids = df.index.tolist()
    report_top_entities(
        entity_type="block",
        entity_provider="em",
        data_provider="em",
        periods=[3, 5, 8, 30],
        ignore_new_stock=False,
        ignore_st=False,
        adjust_type=None,
        top_count=10,
        turnover_threshold=0,
        turnover_rate_threshold=0,
        informer=email_informer,
        em_group="关注板块",
        em_group_over_write=False,
        return_type=TopType.positive,
        entity_ids=entity_ids,
    )

    report_top_entities(
        entity_type="block",
        entity_provider="em",
        data_provider="em",
        periods=[365, 750],
        ignore_new_stock=False,
        ignore_st=False,
        adjust_type=None,
        top_count=10,
        turnover_threshold=0,
        turnover_rate_threshold=0,
        informer=email_informer,
        em_group="关注板块",
        em_group_over_write=False,
        return_type=TopType.negative,
        entity_ids=entity_ids,
    )
Esempio n. 9
0
    def tag(self, timestamp):
        stock_df = Stock.query_data(filters=[Stock.list_date <= timestamp],
                                    index="entity_id")
        block_df = Block.query_data(provider="eastmoney",
                                    filters=[Block.category == "industry"],
                                    index="entity_id")
        block_ids = block_df.index.tolist()
        block_stock_df = BlockStock.query_data(
            provider="eastmoney",
            entity_ids=block_ids,
            filters=[BlockStock.stock_id.in_(stock_df.index.tolist())],
            index="stock_id",
        )
        block_stock_df["cycle_tag"] = block_stock_df["name"].apply(
            lambda name: get_cycle_tag(name))
        strong_cycle_stocks = block_stock_df[block_stock_df.cycle_tag ==
                                             "strong_cycle"]["stock_id"]
        weak_cycle_stocks = block_stock_df[block_stock_df.cycle_tag ==
                                           "weak_cycle"]["stock_id"]
        non_cycle_stocks = block_stock_df[block_stock_df.cycle_tag ==
                                          "non_cycle"]["stock_id"]

        strong_cycle_domains = self.get_tag_domains(
            entity_ids=strong_cycle_stocks,
            timestamp=timestamp,
            cycle_tag=CycleTag.strong_cycle.value)
        weak_cycle_domains = self.get_tag_domains(
            entity_ids=weak_cycle_stocks,
            timestamp=timestamp,
            cycle_tag=CycleTag.weak_cycle.value)
        non_cycle_domains = self.get_tag_domains(
            entity_ids=non_cycle_stocks,
            timestamp=timestamp,
            cycle_tag=CycleTag.non_cycle.value)

        self.session.add_all(strong_cycle_domains)
        self.session.add_all(weak_cycle_domains)
        self.session.add_all(non_cycle_domains)
        self.session.commit()
Esempio n. 10
0
    def tag(self, timestamp):
        stock_df = Stock.query_data(filters=[Stock.list_date <= timestamp],
                                    index='entity_id')
        block_df = Block.query_data(provider='eastmoney',
                                    filters=[Block.category == 'industry'],
                                    index='entity_id')
        block_ids = block_df.index.tolist()
        block_stock_df = BlockStock.query_data(
            provider='eastmoney',
            entity_ids=block_ids,
            filters=[BlockStock.stock_id.in_(stock_df.index.tolist())],
            index='stock_id')
        block_stock_df['cycle_tag'] = block_stock_df['name'].apply(
            lambda name: get_cycle_tag(name))
        strong_cycle_stocks = block_stock_df[block_stock_df.cycle_tag ==
                                             'strong_cycle']['stock_id']
        weak_cycle_stocks = block_stock_df[block_stock_df.cycle_tag ==
                                           'weak_cycle']['stock_id']
        non_cycle_stocks = block_stock_df[block_stock_df.cycle_tag ==
                                          'non_cycle']['stock_id']

        strong_cycle_domains = self.get_tag_domains(
            entity_ids=strong_cycle_stocks,
            timestamp=timestamp,
            cycle_tag=CycleTag.strong_cycle.value)
        weak_cycle_domains = self.get_tag_domains(
            entity_ids=weak_cycle_stocks,
            timestamp=timestamp,
            cycle_tag=CycleTag.weak_cycle.value)
        non_cycle_domains = self.get_tag_domains(
            entity_ids=non_cycle_stocks,
            timestamp=timestamp,
            cycle_tag=CycleTag.non_cycle.value)

        self.session.add_all(strong_cycle_domains)
        self.session.add_all(weak_cycle_domains)
        self.session.add_all(non_cycle_domains)
        self.session.commit()
Esempio n. 11
0
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))
Esempio n. 12
0
                 exchanges=None,
                 codes=None,
                 the_timestamp=None,
                 start_timestamp=None,
                 end_timestamp=None,
                 long_threshold=0.8,
                 short_threshold=0.2,
                 level=IntervalLevel.LEVEL_1DAY,
                 provider='sina',
                 block_selector=None) -> None:
        super().__init__(entity_ids, entity_schema, exchanges, codes,
                         the_timestamp, start_timestamp, end_timestamp,
                         long_threshold, short_threshold, level, provider,
                         block_selector)

    def init_factors(self, entity_ids, entity_schema, exchanges, codes,
                     the_timestamp, start_timestamp, end_timestamp, level):
        block_factor = BlockMoneyFlowFactor(start_timestamp=start_timestamp,
                                            end_timestamp=end_timestamp,
                                            provider='sina',
                                            window=10)
        self.score_factors.append(block_factor)


if __name__ == '__main__':
    block_selector = BlockSelector(start_timestamp='2019-01-01')
    block_selector.run()
    entity_ids = block_selector.get_targets(timestamp='2020-01-23')
    df = Block.query_data(provider='sina', entity_ids=entity_ids)
    print(df)
Esempio n. 13
0
def report_real(region):
    while True:
        error_count = 0
        email_action = EmailInformer(ssl=True)

        try:
            latest_day: Stock1dKdata = Stock1dKdata.query_data(
                region=region,
                order=Stock1dKdata.timestamp.desc(),
                limit=1,
                return_type='domain')
            target_date = latest_day[0].timestamp
            # target_date = '2020-02-04'

            # 计算均线
            my_selector = TargetSelector(region=region,
                                         start_timestamp='2018-01-01',
                                         end_timestamp=target_date)
            # add the factors
            factor1 = VolumeUpMa250Factor(region=region,
                                          start_timestamp='2018-01-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(region=region,
                                         start_timestamp=pe_date,
                                         limit=1,
                                         return_type='domain'):
                positive_df = StockValuation.query_data(
                    region=region,
                    provider=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(region=region,
                                          provider=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(region=region,
                                               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: List[Block] = Block.query_data(
                        region=region,
                        provider=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: List[BlockStock] = BlockStock.query_data(
                        region=region,
                        provider=Provider.Sina,
                        filters=[BlockStock.stock_id.in_(long_stocks)],
                        entity_ids=long_blocks,
                        return_type='domain')
                    if block_stocks:
                        # add them to eastmoney
                        try:
                            try:
                                eastmoneypy.del_group('real')
                            except:
                                pass
                            eastmoneypy.create_group('real')
                            for block_stock in block_stocks:
                                eastmoneypy.add_to_group(
                                    block_stock.stock_code, group_name='real')
                        except Exception as e:
                            email_action.send_message(
                                "*****@*****.**", f'report_real error',
                                'report_real error:{}'.format(e))

                        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'

            logger.info(msg)
            email_action.send_message('*****@*****.**',
                                      f'{target_date} 放量突破年线real选股结果', msg)
            break
        except Exception as e:
            logger.exception('report_real error:{}'.format(e))
            time.sleep(60 * 3)
            error_count = error_count + 1
            if error_count == 10:
                email_action.send_message("*****@*****.**",
                                          f'report_real error',
                                          'report_real error:{}'.format(e))
Esempio n. 14
0
                 end_timestamp=None,
                 long_threshold=0.8,
                 short_threshold=0.2,
                 level=IntervalLevel.LEVEL_1DAY,
                 provider=Provider.Sina,
                 block_selector=None) -> None:
        super().__init__(entity_ids, region, entity_schema, exchanges, codes,
                         the_timestamp, start_timestamp, end_timestamp,
                         long_threshold, short_threshold, level, provider,
                         block_selector)

    def init_factors(self, entity_ids, entity_schema, exchanges, codes,
                     the_timestamp, start_timestamp, end_timestamp, level):
        block_factor = BlockMoneyFlowFactor(region=self.region,
                                            start_timestamp=start_timestamp,
                                            end_timestamp=end_timestamp,
                                            provider=Provider.Sina,
                                            window=10)
        self.score_factors.append(block_factor)


if __name__ == '__main__':
    block_selector = BlockSelector(region=Region.CHN,
                                   start_timestamp='2019-01-01')
    block_selector.run()
    entity_ids = block_selector.get_targets(timestamp='2020-01-23')
    df = Block.query_data(region=Region.CHN,
                          provider=Provider.Sina,
                          entity_ids=entity_ids)
    print(df)
Esempio n. 15
0

class IndexSelector(TargetSelector):

    def __init__(self, entity_ids=None, entity_schema='stock', exchanges=['sh', 'sz'], codes=None, the_timestamp=None,
                 start_timestamp=None, end_timestamp=None, long_threshold=0.8, short_threshold=0.2,
                 level=IntervalLevel.LEVEL_1DAY, provider='sina', block_selector=None) -> None:
        super().__init__(entity_ids, entity_schema, exchanges, codes, the_timestamp, start_timestamp, end_timestamp,
                         long_threshold, short_threshold, level, provider, block_selector)

    def init_factors(self, entity_ids, entity_schema, exchanges, codes, the_timestamp, start_timestamp, end_timestamp,
                     level):
        index_factor = BlockMoneyFlowFactor(start_timestamp=start_timestamp, end_timestamp=end_timestamp, level=level,
                                            provider='sina', codes=codes)
        self.score_factors.append(index_factor)


if __name__ == '__main__':
    df = Block.query_data(provider='sina', filters=[Block.category == 'industry'])

    index_selector = IndexSelector(entity_schema='index', exchanges=None, start_timestamp='2019-01-01',
                                   end_timestamp=now_pd_timestamp(), codes=df['code'].to_list())
    index_selector.run()

    # index_selector.draw()

    s = TechnicalSelector(codes=SAMPLE_STOCK_CODES, start_timestamp='2019-01-01', end_timestamp='2019-06-30',
                          block_selector=index_selector)
    s.run()
    s.draw()