Example #1
0
    def run(self):
        # 抓取fund列表
        df = finance.run_query(query(finance.FUND_MAIN_INFO))
        df.index.name = 'entity_id'
        df = df.reset_index()
        # 上市日期
        df.rename(columns={'start_date': 'timestamp'}, inplace=True)
        df['timestamp'] = pd.to_datetime(df['timestamp'])
        df['list_date'] = df['timestamp']
        df['end_date'] = pd.to_datetime(df['end_date'])

        df['entity_id'] = df.main_code.apply(lambda x: normalize_code(x))
        df['entity_id'] = df['entity_id'].apply(
            lambda x: to_entity_id(entity_type='fund', jq_code=x))

        df['id'] = df['entity_id']
        df['entity_type'] = 'fund'
        df['exchange'] = df['entity_id'].apply(
            lambda x: get_entity_exchange(x))
        df['code'] = df['entity_id'].apply(lambda x: get_entity_code(x))
        df['category'] = 'fund'
        # df['choice_code'] = df.apply(lambda x:x.main_code+'.'+x.exchange.upper(),axis=1)
        # loginResult = c.start("ForceLogin=1", '')
        # df['underlying_index_code'] = df.apply(lambda x:c.css(x.choice_code, "BMINDEXCODE", "Rank=1").Data if x.operate_mode == 'ETF' else None,axis=1)
        # df['underlying_index_code'] = df['underlying_index_code'].apply(lambda x:[i for i in x.values()][0][0].lower().replace(".","_") if x else None)
        # c.stop()
        df_to_db(df,
                 data_schema=FundDetail,
                 provider=self.provider,
                 force_update=self.force_update)

        # self.logger.info(df_index)
        self.logger.info("persist etf list success")
        logout()
Example #2
0
    def run(self):
        # 抓取fund列表
        df = finance.run_query(query(finance.FUND_MAIN_INFO))
        df.index.name = 'entity_id'
        df = df.reset_index()
        # 上市日期
        df.rename(columns={'start_date': 'timestamp'}, inplace=True)
        df['timestamp'] = pd.to_datetime(df['timestamp'])
        df['list_date'] = df['timestamp']
        df['end_date'] = pd.to_datetime(df['end_date'])

        df['entity_id'] = df.main_code.apply(lambda x: normalize_code(x))
        df['entity_id'] = df['entity_id'].apply(
            lambda x: to_entity_id(entity_type='fund', jq_code=x))

        df['id'] = df['entity_id']
        df['entity_type'] = 'fund'
        df['exchange'] = df['entity_id'].apply(
            lambda x: get_entity_exchange(x))
        df['code'] = df['entity_id'].apply(lambda x: get_entity_code(x))

        df['category'] = 'fund'

        df_to_db(df,
                 data_schema=FundDetail,
                 provider=self.provider,
                 force_update=self.force_update)

        # self.logger.info(df_index)
        self.logger.info("persist etf list success")
        logout()
Example #3
0
    def to_zvt_entity(self, df, entity_type, category=None):
        df.index.name = 'entity_id'
        df = df.reset_index()
        # 上市日期
        df.rename(columns={'start_date': 'timestamp'}, inplace=True)
        df['timestamp'] = pd.to_datetime(df['timestamp'])
        df['list_date'] = df['timestamp']
        df['end_date'] = pd.to_datetime(df['end_date'])

        df['entity_id'] = df['entity_id'].apply(lambda x: to_entity_id(entity_type=entity_type, jq_code=x))
        df['id'] = df['entity_id']
        df['entity_type'] = entity_type
        df['exchange'] = df['entity_id'].apply(lambda x: get_entity_exchange(x))
        df['code'] = df['entity_id'].apply(lambda x: get_entity_code(x))
        df['name'] = df['display_name']
        if entity_type == 'etf':
            # ETF  查询标的指数
            df['choice_code'] = df.apply(lambda x: x.code + '.' + x.exchange.upper(), axis=1)
            loginResult = c.start("ForceLogin=1", '')
            df['underlying_index_code'] = df.apply(lambda x: c.css(x.choice_code, "BMINDEXCODE", "Rank=1").Data, axis=1)
            df['index_codes'] = df['underlying_index_code'].apply(lambda x: [i for i in x.values()][0][0])
            df['index_exchange'] = df['index_codes'].apply(lambda x: str(x).split('.'))
            df['index_code'] = df['index_codes'].apply(lambda x: str(x).split('.')[0])
            df['index_exchange'] = df['index_exchange'].apply(
                lambda x: x[1] if isinstance(x, list) and len(x) > 1 else None)
            df['underlying_index_code'] = df.apply(
                lambda x: 'index_' + x.index_exchange.lower() + '_' + x.index_code if x.index_exchange else None,
                axis=1)
            loginResult = c.stop()
        if category:
            df['category'] = category

        return df
Example #4
0
    def to_zvt_entity(self, df, entity_type, category=None):
        df.index.name = 'entity_id'
        df = df.reset_index()
        # 上市日期
        df.rename(columns={'start_date': 'timestamp'}, inplace=True)
        df['timestamp'] = pd.to_datetime(df['timestamp'])
        df['list_date'] = df['timestamp']
        df['end_date'] = pd.to_datetime(df['end_date'])
        df['entity_id'] = df['entity_id'].apply(lambda x: x.lower())
        df['entity_id'] = df['entity_id'].apply(lambda x: to_entity_id(entity_type=entity_type, jq_code=x))
        df['id'] = df['entity_id']
        df['entity_type'] = entity_type
        df['exchange'] = df['entity_id'].apply(lambda x: get_entity_exchange(x))
        df['code'] = df['entity_id'].apply(lambda x: get_entity_code(x))
        df['end_date'].fillna(pd.to_datetime("22000101"),inplace=True)
        if category:
            df['category'] = category

        return df
Example #5
0
def get_economy_data(entity_id, indicators=None, date=None):
    country = get_entity_code(entity_id=entity_id)
    if not indicators:
        indicators = _economy_indicator_map.keys()
    dfs = []
    for indicator in indicators:
        data = get_indicator_data(
            indicator=_economy_indicator_map.get(indicator),
            indicator_name=indicator,
            country=country,
            date=date)
        dfs.append(data)
    df = pd.concat(dfs, axis=1)
    df = df.reset_index(drop=False)
    df["entity_id"] = entity_id
    df["id"] = df[["entity_id",
                   "timestamp"]].apply(lambda x: "_".join(x.astype(str)),
                                       axis=1)
    df = normal_index_df(df, drop=False)
    return df
    def to_zvt_entity(self, df, entity_type: EntityType, category=None):
        df.index.name = 'entity_id'
        df = df.reset_index()
        # 上市日期
        df.rename(columns={'start_date': 'timestamp'}, inplace=True)
        df['timestamp'] = pd.to_datetime(df['timestamp'])
        df['list_date'] = df['timestamp']
        df['end_date'] = pd.to_datetime(df['end_date'])

        df['entity_id'] = df['entity_id'].apply(lambda x: to_entity_id(entity_type=entity_type, jq_code=x))
        df['id'] = df['entity_id']
        df['entity_type'] = entity_type.value
        df['exchange'] = df['entity_id'].apply(lambda x: get_entity_exchange(x))
        df['code'] = df['entity_id'].apply(lambda x: get_entity_code(x))
        df['name'] = df['display_name']

        if category:
            df['category'] = category

        return df
Example #7
0
    def to_zvt_entity(self, df, entity_type, category=None):
        df = df.set_index("code")
        df.index.name = "entity_id"
        df = df.reset_index()
        # 上市日期
        df.rename(columns={"start_date": "timestamp"}, inplace=True)
        df["timestamp"] = pd.to_datetime(df["timestamp"])
        df["list_date"] = df["timestamp"]
        df["end_date"] = pd.to_datetime(df["end_date"])

        df["entity_id"] = df["entity_id"].apply(
            lambda x: to_entity_id(entity_type=entity_type, jq_code=x))
        df["id"] = df["entity_id"]
        df["entity_type"] = entity_type
        df["exchange"] = df["entity_id"].apply(
            lambda x: get_entity_exchange(x))
        df["code"] = df["entity_id"].apply(lambda x: get_entity_code(x))
        df["name"] = df["display_name"]

        if category:
            df["category"] = category

        return df
Example #8
0
def report_state(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 = to_pd_timestamp('2020-01-02')

            # 计算均线
            my_selector = TargetSelector(region=region,
                                         start_timestamp='2018-01-01',
                                         end_timestamp=target_date)
            # add the factors
            factor1 = ImprovedMaFactor(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)
            stock_map_slope = {}

            logger.info(long_stocks)

            msg = 'no targets'

            if long_stocks:
                pre_date = target_date - datetime.timedelta(2 * 365)
                ma_state = MaStateStatsFactor(region=region,
                                              entity_ids=long_stocks,
                                              start_timestamp=pre_date,
                                              end_timestamp=target_date,
                                              need_persist=False)

                ma_state.factor_df['slope'] = 100 * ma_state.factor_df[
                    'current_pct'] / ma_state.factor_df['current_count']

                high_stocks = []
                for entity_id, df in ma_state.factor_df.groupby(level=0):
                    if df['current_pct'].max() >= 0.7:
                        high_stocks.append(entity_id)

                    stock_map_slope[entity_id] = round(df['slope'].iat[-1], 2)

                if high_stocks:
                    stocks = get_entities(region=region,
                                          provider=Provider.JoinQuant,
                                          entity_schema=Stock,
                                          entity_ids=high_stocks,
                                          return_type='domain')
                    info = [
                        f'{stock.name}({stock.code})[{stock_map_slope.get(stock.entity_id)}]'
                        for stock in stocks
                    ]
                    msg = msg + '2年内高潮过:' + ' '.join(info) + '\n'

            # 过滤风险股
            if long_stocks:
                risky_codes = risky_company(region=region,
                                            the_date=target_date,
                                            entity_ids=long_stocks,
                                            income_yoy=-0.8,
                                            profit_yoy=-0.8)

                if risky_codes:
                    long_stocks = [
                        entity_id for entity_id in long_stocks
                        if get_entity_code(entity_id) not in risky_codes
                    ]

                    stocks = get_entities(region=region,
                                          provider=Provider.JoinQuant,
                                          entity_schema=Stock,
                                          codes=risky_codes,
                                          return_type='domain')
                    info = [
                        f'{stock.name}({stock.code})[{stock_map_slope.get(stock.entity_id)}]'
                        for stock in stocks
                    ]
                    msg = msg + '风险股:' + ' '.join(info) + '\n'
            if long_stocks:
                stocks = get_entities(region=region,
                                      provider=Provider.JoinQuant,
                                      entity_schema=Stock,
                                      entity_ids=long_stocks,
                                      return_type='domain')
                # add them to eastmoney
                try:
                    try:
                        eastmoneypy.del_group('real')
                    except:
                        pass
                    eastmoneypy.create_group('real')
                    for stock in stocks:
                        eastmoneypy.add_to_group(stock.code, group_name='real')
                except Exception as e:
                    email_action.send_message(
                        "*****@*****.**", f'report state error',
                        'report state error:{}'.format(e))

                info = [
                    f'{stock.name}({stock.code})[{stock_map_slope.get(stock.entity_id)}]'
                    for stock in stocks
                ]
                msg = msg + '选中:' + ' '.join(info) + '\n'

            logger.info(msg)
            email_action.send_message('*****@*****.**',
                                      f'{target_date} 放量突破年线state选股结果', msg)
            break
        except Exception as e:
            logger.exception('report state error:{}'.format(e))
            time.sleep(60 * 3)
            error_count = error_count + 1
            if error_count == 10:
                email_action.send_message("*****@*****.**",
                                          f'report state error',
                                          'report state error:{}'.format(e))