示例#1
0
def get_portfolio_stocks(portfolio_entity=Fund, code=None, codes=None, ids=None, timestamp=now_pd_timestamp(),
                         provider=None):
    portfolio_stock = f'{portfolio_entity.__name__}Stock'
    data_schema: PortfolioStockHistory = get_schema_by_name(portfolio_stock)
    latests: List[PortfolioStockHistory] = data_schema.query_data(provider=provider, code=code, end_timestamp=timestamp,
                                                                  order=data_schema.timestamp.desc(), limit=1,
                                                                  return_type='domain')
    if latests:
        latest_record = latests[0]
        # 获取最新的报表
        df = data_schema.query_data(provider=provider, code=code, codes=codes, ids=ids, end_timestamp=timestamp,
                                    filters=[data_schema.report_date == latest_record.report_date])
        # 最新的为年报或者半年报
        if latest_record.report_period == ReportPeriod.year or latest_record.report_period == ReportPeriod.half_year:
            return df
        # 季报,需要结合 年报或半年报 来算持仓
        else:
            step = 0
            while step <= 20:
                report_date = get_recent_report_date(latest_record.report_date, step=step)

                pre_df = data_schema.query_data(provider=provider, code=code, codes=codes, ids=ids,
                                                end_timestamp=timestamp,
                                                filters=[data_schema.report_date == to_pd_timestamp(report_date)])
                df = df.append(pre_df)

                # 半年报和年报
                if (ReportPeriod.half_year.value in pre_df['report_period'].tolist()) or (
                        ReportPeriod.year.value in pre_df['report_period'].tolist()):
                    # 保留最新的持仓
                    df = df.drop_duplicates(subset=['stock_code'], keep='first')
                    return df
                step = step + 1
示例#2
0
文件: factor_app.py 项目: ywjb/zvt
def update_column_selector(schema_name):
    if schema_name:
        schema = get_schema_by_name(name=schema_name)
        cols = get_schema_columns(schema=schema)

        return [{'label': col, 'value': col} for col in cols]
    raise dash.PreventUpdate()
示例#3
0
文件: schema.py 项目: eromoe/zvt
    def get_stocks(cls,
                   code=None,
                   codes=None,
                   ids=None,
                   timestamp=now_pd_timestamp(),
                   provider=None):
        """
        the publishing policy of portfolio positions is different for different types,
        overwrite this function for get the holding stocks in specific date

        :param code: portfolio(etf/block/index...) code
        :param codes: portfolio(etf/block/index...) codes
        :param ids: portfolio(etf/block/index...) ids
        :param timestamp: the date of the holding stocks
        :param provider: the data provider
        :return:
        """
        from zvt.contract.api import get_schema_by_name
        schema_str = f'{cls.__name__}Stock'
        portfolio_stock = get_schema_by_name(schema_str)
        return portfolio_stock.query_data(provider=provider,
                                          code=code,
                                          codes=codes,
                                          timestamp=timestamp,
                                          ids=ids)
示例#4
0
def get_ma_stats_factor_schema(entity_type: str, level: Union[IntervalLevel, str] = IntervalLevel.LEVEL_1DAY):
    if type(level) == str:
        level = IntervalLevel(level)

    schema_str = "{}{}MaStatsFactor".format(entity_type.capitalize(), level.value.capitalize())

    return get_schema_by_name(schema_str)
示例#5
0
def update_factor_details(region: Region, factor, entity_type, entity, levels,
                          columns, trader_index, schema_name):
    if factor and entity_type and entity and levels:
        sub_df = None
        # add sub graph
        if columns:
            if type(columns) == str:
                columns = [columns]
            columns = columns + ['entity_id', 'timestamp']
            schema: Mixin = get_schema_by_name(name=schema_name)
            sub_df = schema.query_data(region=region,
                                       entity_id=entity,
                                       columns=columns)

        # add trading signals as annotation
        annotation_df = None
        if trader_index is not None:
            order_reader = order_readers[trader_index]
            annotation_df = order_reader.data_df.copy()
            annotation_df = annotation_df[annotation_df.entity_id ==
                                          entity].copy()
            if pd_is_not_null(annotation_df):
                annotation_df['value'] = annotation_df['order_price']
                annotation_df['flag'] = annotation_df['order_type'].apply(
                    lambda x: order_type_flag(x))
                annotation_df['color'] = annotation_df['order_type'].apply(
                    lambda x: order_type_color(x))
            print(annotation_df.tail())

        if type(levels) is list and len(levels) >= 2:
            levels.sort()
            drawers = []
            for level in levels:
                drawers.append(zvt_context.factor_cls_registry[factor](
                    entity_schema=zvt_context.entity_schema_map[entity_type],
                    level=level,
                    entity_ids=[entity]).drawer())
            stacked = StackedDrawer(*drawers)

            return dcc.Graph(id=f'{factor}-{entity_type}-{entity}',
                             figure=stacked.draw_kline(show=False, height=900))
        else:
            if type(levels) is list:
                level = levels[0]
            else:
                level = levels
            drawer = zvt_context.factor_cls_registry[factor](
                entity_schema=zvt_context.entity_schema_map[entity_type],
                level=level,
                entity_ids=[entity],
                need_persist=False).drawer()
            if pd_is_not_null(sub_df):
                drawer.add_sub_df(sub_df)
            if pd_is_not_null(annotation_df):
                drawer.annotation_df = annotation_df

            return dcc.Graph(id=f'{factor}-{entity_type}-{entity}',
                             figure=drawer.draw_kline(show=False, height=800))
    raise dash.PreventUpdate()
示例#6
0
def get_z_factor_schema(entity_type: str, level: Union[IntervalLevel, str] = IntervalLevel.LEVEL_1DAY):
    if type(level) == str:
        level = IntervalLevel(level)

    # z factor schema rule
    # 1)name:{SecurityType.value.capitalize()}{IntervalLevel.value.upper()}ZFactor
    schema_str = "{}{}ZFactor".format(entity_type.capitalize(), level.value.capitalize())

    return get_schema_by_name(schema_str)
示例#7
0
def get_ma_state_stats_schema(entity_type: str,
                              level: Union[IntervalLevel,
                                           str] = IntervalLevel.LEVEL_1DAY):
    if type(level) == str:
        level = IntervalLevel(level)

    # ma state stats schema rule
    # 1)name:{SecurityType.value.capitalize()}{IntervalLevel.value.upper()}MaStateStats
    schema_str = '{}{}MaStateStats'.format(entity_type.capitalize(),
                                           level.value.capitalize())

    return get_schema_by_name(schema_str)
示例#8
0
def get_kdata_schema(entity_type: str,
                     level: Union[IntervalLevel, str] = IntervalLevel.LEVEL_1DAY,
                     adjust_type: Union[AdjustType, str] = None):
    if type(level) == str:
        level = IntervalLevel(level)
    if type(adjust_type) == str:
        adjust_type = AdjustType(adjust_type)

    # kdata schema rule
    # 1)name:{entity_type.capitalize()}{IntervalLevel.value.upper()}Kdata
    if adjust_type and (adjust_type != AdjustType.qfq):
        schema_str = '{}{}{}Kdata'.format(entity_type.capitalize(), level.value.capitalize(),
                                          adjust_type.value.capitalize())
    else:
        schema_str = '{}{}Kdata'.format(entity_type.capitalize(), level.value.capitalize())
    return get_schema_by_name(schema_str)
示例#9
0
def update_factor_details(factor, entity_type, code, levels, columns,
                          schema_name):
    if factor and entity_type and code and levels:
        sub_df = None
        if columns:
            if type(columns) == str:
                columns = [columns]
            columns = columns + ['entity_id', 'timestamp']
            schema: Mixin = get_schema_by_name(name=schema_name)
            sub_df = schema.query_data(code=code, columns=columns)
        if type(levels) is list and len(levels) >= 2:
            levels.sort()
            drawers = []
            for level in levels:
                drawers.append(zvt_context.factor_cls_registry[factor](
                    entity_schema=zvt_context.entity_schema_map[entity_type],
                    level=level,
                    codes=[code]).drawer())
            stacked = StackedDrawer(*drawers)

            return dcc.Graph(id=f'{factor}-{entity_type}-{code}',
                             figure=stacked.draw_kline(show=False, height=900))
        else:
            if type(levels) is list:
                level = levels[0]
            else:
                level = levels
            drawer = zvt_context.factor_cls_registry[factor](
                entity_schema=zvt_context.entity_schema_map[entity_type],
                level=level,
                codes=[code],
                need_persist=False).drawer()
            if pd_is_not_null(sub_df):
                drawer.add_sub_df(sub_df)

            return dcc.Graph(id=f'{factor}-{entity_type}-{code}',
                             figure=drawer.draw_kline(show=False, height=800))
    raise dash.PreventUpdate()