示例#1
0
文件: stats.py 项目: durgagokina/zvt
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)
示例#2
0
文件: stats.py 项目: stungkit/zvt
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)
示例#3
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()
示例#4
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()
示例#5
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))
示例#6
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))
示例#7
0
        values[0] if str(x) + relocation_date <= date.fromtimestamp(time.time(
        )).strftime("%Y%m%d") else np.NaN)
    buy_signal = buy_signal.query("timestamp <= @end")
    code_dict = {}
    for stock_data in buy_signal[['股票代码', 'timestamp',
                                  '股票名称']].to_dict('records'):
        stock_n = stock_data['股票代码'] + '_' + stock_data['股票名称']
        if stock_n not in code_dict:
            code_dict.update({stock_n: stock_data['timestamp']})
        else:
            if code_dict[stock_n] >= stock_data['timestamp']:
                code_dict.update({stock_n: stock_data['timestamp']})
buy_signal['CODES'] = buy_signal['股票代码'] + '_' + buy_signal['股票名称']
list(set(buy_signal['股票代码'].tolist()))
data = BlockStock.query_data(filters=[
    BlockStock.stock_code.in_(list(set(buy_signal['股票代码'].tolist()))),
    BlockStock.block_type == 'swl1'
])

buy_signaldata = buy_signal[buy_signal.year == buy_signal.year.max()]
data = BlockStock.query_data(filters=[
    BlockStock.stock_code.in_(list(set(buy_signal['股票代码'].tolist()))),
    BlockStock.block_type == 'swl1'
])
data["股票代码"] = data['stock_code']
data["行业分类"] = data['name']

data = data[["行业分类", "股票代码"]]

buy_signaldata = pd.merge(data, buy_signaldata, on=['股票代码'])
buy_signaldata['CODES'] = buy_signaldata.apply(
    lambda x: x.CODES + '_' + x.行业分类, axis=1)