Пример #1
0
    def init_selectors(self,
                       entity_ids,
                       entity_schema,
                       exchanges,
                       codes,
                       start_timestamp,
                       end_timestamp,
                       adjust_type=None):
        start_timestamp = next_date(start_timestamp, -50)

        # 周线策略
        week_selector = TargetSelector(entity_ids=entity_ids,
                                       entity_schema=entity_schema,
                                       exchanges=exchanges,
                                       codes=codes,
                                       start_timestamp=next_date(
                                           start_timestamp, -200),
                                       end_timestamp=end_timestamp,
                                       long_threshold=0.7,
                                       level=IntervalLevel.LEVEL_1WEEK,
                                       provider='joinquant')
        week_bull_factor = BullFactor(entity_ids=entity_ids,
                                      entity_schema=entity_schema,
                                      exchanges=exchanges,
                                      codes=codes,
                                      start_timestamp=next_date(
                                          start_timestamp, -200),
                                      end_timestamp=end_timestamp,
                                      provider='joinquant',
                                      level=IntervalLevel.LEVEL_1WEEK)
        week_selector.add_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,
                                      long_threshold=0.7,
                                      level=IntervalLevel.LEVEL_1DAY,
                                      provider='joinquant')
        day_gold_cross_factor = GoldCrossFactor(
            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)
        day_selector.add_factor(day_gold_cross_factor)

        # 同时使用日线,周线级别
        self.selectors.append(day_selector)
        self.selectors.append(week_selector)
Пример #2
0
    def run(self):
        # 按不同类别抓取
        # 编码    基金运作方式
        # 401001    开放式基金
        # 401002    封闭式基金
        # 401003    QDII
        # 401004    FOF
        # 401005    ETF
        # 401006    LOF
        for operate_mode_id in (401001, 401002, 401005):
            year_count = 2
            while True:
                latest = Fund.query_data(
                    region=self.region,
                    filters=[Fund.operate_mode_id == operate_mode_id],
                    order=Fund.timestamp.desc(),
                    limit=1,
                    return_type='domain')
                start_timestamp = '2000-01-01'
                if latest:
                    start_timestamp = latest[0].timestamp

                end_timestamp = min(
                    next_date(start_timestamp, 365 * year_count),
                    now_pd_timestamp(self.region))

                df = jq_run_query(
                    table='finance.FUND_MAIN_INFO',
                    conditions=
                    f'operate_mode_id#=#{operate_mode_id}&start_date#>=#{to_time_str(start_timestamp)}&start_date#<=#{to_time_str(end_timestamp)}',
                    parse_dates=['start_date', 'end_date'],
                    dtype={'main_code': str})
                if not pd_is_not_null(df) or (df['start_date'].max().year <
                                              end_timestamp.year):
                    year_count = year_count + 1

                if pd_is_not_null(df):
                    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['code'] = df['main_code']
                    df['entity_id'] = df['code'].apply(
                        lambda x: to_entity_id(entity_type='fund', jq_code=x))
                    df['id'] = df['entity_id']
                    df['entity_type'] = 'fund'
                    df['exchange'] = 'sz'
                    df_to_db(df,
                             ref_df=None,
                             region=self.region,
                             data_schema=Fund,
                             provider=self.provider)
                    self.logger.info(
                        f'persist fund {operate_mode_id} list success {start_timestamp} to {end_timestamp}'
                    )

                if is_same_date(end_timestamp, now_pd_timestamp(self.region)):
                    break
Пример #3
0
    def run(self):
        # 按不同类别抓取
        # 编码	基金运作方式
        # 401001	开放式基金
        # 401002	封闭式基金
        # 401003	QDII
        # 401004	FOF
        # 401005	ETF
        # 401006	LOF
        for operate_mode_id in (401001, 401002, 401005):
            year_count = 2
            while True:
                latest = Fund.query_data(
                    filters=[Fund.operate_mode_id == operate_mode_id],
                    order=Fund.timestamp.desc(),
                    limit=1,
                    return_type="domain",
                )
                start_timestamp = "2000-01-01"
                if latest:
                    start_timestamp = latest[0].timestamp

                end_timestamp = min(
                    next_date(start_timestamp, 365 * year_count),
                    now_pd_timestamp())

                df = run_query(
                    table="finance.FUND_MAIN_INFO",
                    conditions=
                    f"operate_mode_id#=#{operate_mode_id}&start_date#>=#{to_time_str(start_timestamp)}&start_date#<=#{to_time_str(end_timestamp)}",
                    parse_dates=["start_date", "end_date"],
                    dtype={"main_code": str},
                )
                if not pd_is_not_null(df) or (df["start_date"].max().year <
                                              end_timestamp.year):
                    year_count = year_count + 1

                if pd_is_not_null(df):
                    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["code"] = df["main_code"]
                    df["entity_id"] = df["code"].apply(
                        lambda x: to_entity_id(entity_type="fund", jq_code=x))
                    df["id"] = df["entity_id"]
                    df["entity_type"] = "fund"
                    df["exchange"] = "sz"
                    df_to_db(df,
                             data_schema=Fund,
                             provider=self.provider,
                             force_update=self.force_update)
                    self.logger.info(
                        f"persist fund {operate_mode_id} list success {start_timestamp} to {end_timestamp}"
                    )

                if is_same_date(end_timestamp, now_pd_timestamp()):
                    break
Пример #4
0
    def eval_fetch_timestamps(self, entity, ref_record, http_session):
        latest_timestamp = None
        try:
            if pd_is_not_null(ref_record):
                time_field = self.get_evaluated_time_field()
                latest_timestamp = ref_record[time_field].max(axis=0)
        except Exception as e:
            self.logger.warning(f'get ref record failed with error: {e}')

        if not latest_timestamp:
            latest_timestamp = entity.timestamp

        if not latest_timestamp:
            return self.start_timestamp, self.end_timestamp, self.default_size, None

        now = now_pd_timestamp(self.region)
        now_end = now.replace(hour=18, minute=0, second=0)

        trade_day_index = 0
        if len(self.trade_day) > 0:
            if is_same_date(self.trade_day[trade_day_index], now) and now < now_end:
                trade_day_index = 1
            end = self.trade_day[trade_day_index]
        else:
            end = now

        start_timestamp = next_date(latest_timestamp)
        start = max(self.start_timestamp, start_timestamp) if self.start_timestamp else start_timestamp

        if start >= end:
            return start, end, 0, None

        size = eval_size_of_timestamp(start_timestamp=start,
                                      end_timestamp=end,
                                      level=self.level,
                                      one_day_trading_minutes=self.one_day_trading_minutes)

        return start, end, size, None
Пример #5
0
        self.selectors.append(day_selector)
        self.selectors.append(week_selector)


if __name__ == '__main__':
    start = '2019-01-01'
    end = '2021-01-01'
    trader_name = 'keep_run_trader'
    clear_trader(trader_name=trader_name)
    for time_interval in split_time_interval(start=start, end=end,
                                             interval=40):
        start_timestamp = time_interval[0]
        end_timestamp = time_interval[-1]
        # 成交量
        vol_df = get_top_volume_entities(entity_type='stock',
                                         start_timestamp=next_date(
                                             start_timestamp, -50),
                                         end_timestamp=start_timestamp,
                                         pct=0.3)
        # 机构重仓
        ii_df = get_top_fund_holding_stocks(timestamp=start_timestamp,
                                            pct=0.3,
                                            by='trading')

        current_entity_pool = list(
            set(vol_df.index.tolist()) & set(ii_df.index.tolist()))

        logger.info(
            f'current_entity_pool({len(current_entity_pool)}):{current_entity_pool}'
        )

        trader = MultipleLevelTrader(start_timestamp=start_timestamp,
Пример #6
0
def report_top_stats(periods=[7, 30, 180, 365], ignore_new_stock=True):
    latest_day: Stock1dHfqKdata = Stock1dHfqKdata.query_data(order=Stock1dHfqKdata.timestamp.desc(), limit=1,
                                                             return_type='domain')
    current_timestamp = latest_day[0].timestamp
    email_action = EmailInformer()

    # 至少上市一年
    filters = None
    if ignore_new_stock:
        pre_year = next_date(current_timestamp, -365)

        stocks = get_entity_ids(provider='joinquant', entity_schema=Stock, filters=[Stock.timestamp <= pre_year])
        filters = [Stock1dHfqKdata.entity_id.in_(stocks)]

    stats = []
    ups = []
    downs = []
    msg = ''
    for period in periods:
        start = next_date(current_timestamp, -period)
        df, _ = get_top_performance_entities(start_timestamp=start, filters=filters, pct=1, show_name=True)
        df.rename(columns={'score': f'score_{period}'}, inplace=True)
        ups.append(tabulate(df.iloc[:50], headers='keys'))
        downs.append(tabulate(df.iloc[:-100], headers='keys'))

        stats.append(tabulate(df.describe(), headers='keys'))

        # 最近一个月最靓仔的
        if period == 30:
            # add them to eastmoney
            try:
                try:
                    eastmoneypy.del_group('最靓仔')
                except:
                    pass
                eastmoneypy.create_group('最靓仔')
                for entity_id in df.index[:50]:
                    _, _, code = decode_entity_id(entity_id)
                    eastmoneypy.add_to_group(code=code, group_name='最靓仔')
            except Exception as e:
                logger.exception(e)
                email_action.send_message("*****@*****.**", f'report_top_stats error',
                                          'report_top_stats error:{}'.format(e))

        # 一年内没怎么动的
        if period == 365:
            stable_df = df[(df['score_365'] > -0.1) & (df['score_365'] < 0.1)]
            vol_df = get_top_volume_entities(entity_ids=stable_df.index.tolist(), start_timestamp=start)

            # add them to eastmoney
            try:
                try:
                    eastmoneypy.del_group('躺尸一年')
                except:
                    pass
                eastmoneypy.create_group('躺尸一年')
                for entity_id in vol_df.index[:50]:
                    _, _, code = decode_entity_id(entity_id)
                    eastmoneypy.add_to_group(code=code, group_name='躺尸一年')
            except Exception as e:
                logger.exception(e)
                email_action.send_message("*****@*****.**", f'report_top_stats error',
                                          'report_top_stats error:{}'.format(e))

    for s in stats:
        msg = msg + s + '\n'

    for up in ups:
        msg = msg + up + '\n'

    for down in downs:
        msg = msg + down + '\n'

    email_action.send_message('*****@*****.**', f'{current_timestamp} 统计报告', msg)
Пример #7
0
        filters=[
            StockActorSummary.report_date >= report_date,
            StockActorSummary.actor_type == ActorType.raised_fund.value,
            StockActorSummary.holding_ratio >= fund_ratio_nums
        ],
        columns=['entity_id', 'holding_ratio', 'actor_type'],
    )

    # fund_cap_df = FundStock.query_data(filters=[FundStock.report_date >= report_date, FundStock.timestamp <= current_timestamp, FundStock.proportion >= 0.01],
    #                                    columns=['stock_id', 'market_cap', 'proportion'])
    top_holding_stocks = fund_cap_df['entity_id'].tolist()

    # 至少上市
    least_start_day_nums = 60
    filters = None
    pre_date = next_date(current_timestamp, -least_start_day_nums)
    stocks = get_entity_ids(provider='joinquant',
                            entity_schema=Stock,
                            filters=[Stock.timestamp <= pre_date])
    filters = [Stock1dHfqKdata.entity_id.in_(stocks)]

    # 任一rps 大于90
    top_rps_stocks = []
    rps_rank_limit = 1000
    periods = [50, 120, 250]
    for period in periods:
        start = next_date(current_timestamp, -period)
        updf, _ = get_top_performance_entities(start_timestamp=start,
                                               filters=filters,
                                               pct=1,
                                               show_name=True)