Ejemplo n.º 1
0
def get_traders() -> List[str]:
    df = get_group(provider='zvt',
                   data_schema=SimAccount,
                   column=SimAccount.trader_name,
                   group_func=None)
    if df_is_not_null(df):
        return df['trader_name'].tolist()
    return []
Ejemplo n.º 2
0
def df_to_db(df, data_schema, provider):
    db_engine = get_db_engine(provider, data_schema=data_schema)

    current = get_data(data_schema=data_schema, columns=[data_schema.id], provider=provider)
    if df_is_not_null(current):
        df = df[~df['id'].isin(current['id'])]

    df.to_sql(data_schema.__tablename__, db_engine, index=False, if_exists='append')
Ejemplo n.º 3
0
    def do_compute(self):
        """
        calculate tech indicators  self.depth_df

        """
        if df_is_not_null(self.depth_df) and self.indicators:
            for idx, indicator in enumerate(self.indicators):
                if indicator == 'ma':
                    window = self.indicators_param[idx].get('window')

                    col = 'ma{}'.format(window)
                    self.indicator_cols.add(col)

                    if self.entity_type == 'stock' and self.fq == 'qfq':
                        df = self.depth_df['qfq_close'].groupby(
                            level=0).rolling(window=window,
                                             min_periods=window).mean()
                    else:
                        df = self.depth_df['close'].groupby(level=0).rolling(
                            window=window, min_periods=window).mean()

                    df = df.reset_index(level=0, drop=True)

                    self.depth_df[col] = df
                    # self.depth_df = pd.concat([self.depth_df, df], axis=1, sort=False)

                if indicator == 'macd':
                    slow = self.indicators_param[idx].get('slow')
                    fast = self.indicators_param[idx].get('fast')
                    n = self.indicators_param[idx].get('n')

                    self.indicator_cols.add('diff')
                    self.indicator_cols.add('dea')
                    self.indicator_cols.add('macd')

                    # for entity_id, df in self.depth_df.groupby('entity_id'):
                    #     if self.entity_type == 'stock' and self.fq == 'qfq':
                    #         diff, dea, m = macd(df['qfq_close'], slow=slow, fast=fast, n=n)
                    #     else:
                    #         diff, dea, m = macd(df['close'], slow=slow, fast=fast, n=n)
                    #
                    #     self.depth_df.loc[entity_id, 'diff'] = diff
                    #     self.depth_df.loc[entity_id, 'dea'] = dea
                    #     self.depth_df.loc[entity_id, 'macd'] = m

                    if self.entity_type == 'stock' and self.fq == 'qfq':
                        df = self.depth_df.groupby(
                            level=0)['qfq_close'].apply(lambda x: macd(
                                x, slow=slow, fast=fast, n=n, return_type='df')
                                                        )
                    else:
                        df = self.depth_df.groupby(
                            level=0)['close'].apply(lambda x: macd(
                                x, slow=slow, fast=fast, n=n, return_type='df')
                                                    )
                    self.depth_df = pd.concat([self.depth_df, df],
                                              axis=1,
                                              sort=False)
Ejemplo n.º 4
0
    def run(self):
        """

        """
        if self.filter_factors:
            musts = []
            for factor in self.filter_factors:
                df = factor.get_result_df()

                if not df_is_not_null(df):
                    raise Exception('no data for factor:{},{}'.format(
                        factor.factor_name, factor))

                if len(df.columns) > 1:
                    s = df.agg("and", axis="columns")
                    s.name = 'score'
                    musts.append(s.to_frame(name='score'))
                else:
                    df.columns = ['score']
                    musts.append(df)

            self.filter_result = list(accumulate(musts,
                                                 func=operator.__and__))[-1]

        if self.score_factors:
            scores = []
            for factor in self.score_factors:
                df = factor.get_result_df()
                if not df_is_not_null(df):
                    raise Exception('no data for factor:{],{}'.format(
                        factor.factor_name, factor))

                if len(df.columns) > 1:
                    s = df.agg("mean", axis="columns")
                    s.name = 'score'
                    scores.append(s.to_frame(name='score'))
                else:
                    df.columns = ['score']
                    scores.append(df)
            self.score_result = list(accumulate(scores,
                                                func=operator.__add__))[-1]

        self.generate_targets()
Ejemplo n.º 5
0
    def record(self, entity, start, end, size, timestamps):
        # 不复权
        try:
            df = get_bars(to_jq_entity_id(entity),
                          count=size,
                          unit=self.jq_trading_level,
                          fields=[
                              'date', 'open', 'close', 'low', 'high', 'volume',
                              'money'
                          ],
                          include_now=False)
        except Exception as e:
            # just ignore the error,for some new stocks not in the index
            self.logger.exception(e)
            return None
        df['name'] = entity.name
        df.rename(columns={'money': 'turnover'}, inplace=True)

        df['timestamp'] = pd.to_datetime(df['date'])
        df['provider'] = 'joinquant'
        df['level'] = self.level.value

        # 前复权
        end_timestamp = to_time_str(now_pd_timestamp())
        qfq_df = get_bars(to_jq_entity_id(entity),
                          count=size,
                          unit=self.jq_trading_level,
                          fields=['date', 'open', 'close', 'low', 'high'],
                          fq_ref_date=end_timestamp,
                          include_now=False)
        # not need to update past
        df['qfq_close'] = qfq_df['close']
        df['qfq_open'] = qfq_df['open']
        df['qfq_high'] = qfq_df['high']
        df['qfq_low'] = qfq_df['low']

        check_df = qfq_df.head(1)
        check_date = check_df['date'][0]

        current_df = get_kdata(entity_id=entity.id,
                               provider=self.provider,
                               start_timestamp=check_date,
                               end_timestamp=check_date,
                               limit=1,
                               level=self.level)

        if df_is_not_null(current_df):
            old = current_df.iloc[0, :]['qfq_close']
            new = check_df['close'][0]
            # 相同时间的close不同,表明前复权需要重新计算
            if old != new:
                self.factor = new / old
                self.last_timestamp = pd.Timestamp(check_date)

        return df.to_dict(orient='records')
    def on_finish_entity(self, entity):
        # fill the timestamp for report published date
        the_data_list = get_data(
            data_schema=self.data_schema,
            provider=self.provider,
            entity_id=entity.id,
            order=self.data_schema.timestamp.asc(),
            return_type='domain',
            session=self.session,
            filters=[
                self.data_schema.timestamp == self.data_schema.report_date,
                self.data_schema.timestamp >= to_pd_timestamp('2005-01-01')
            ])
        if the_data_list:
            if self.data_schema == FinanceFactor:
                for the_data in the_data_list:
                    self.fill_timestamp_with_jq(entity, the_data)
            else:
                df = get_finance_factor(
                    entity_id=entity.id,
                    columns=[
                        FinanceFactor.timestamp, FinanceFactor.report_date,
                        FinanceFactor.id
                    ],
                    filters=[
                        FinanceFactor.timestamp != FinanceFactor.report_date,
                        FinanceFactor.timestamp >=
                        to_pd_timestamp('2005-01-01'),
                        FinanceFactor.report_date >=
                        the_data_list[0].report_date,
                        FinanceFactor.report_date <=
                        the_data_list[-1].report_date,
                    ])

                if df_is_not_null(df):
                    index_df(df, index='report_date')

                for the_data in the_data_list:
                    if (df is not None) and (
                            not df.empty) and the_data.report_date in df.index:
                        the_data.timestamp = df.at[the_data.report_date,
                                                   'timestamp']
                        self.logger.info(
                            'db fill {} {} timestamp:{} for report_date:{}'.
                            format(self.data_schema, entity.id,
                                   the_data.timestamp, the_data.report_date))
                        self.session.commit()
                    else:
                        # self.logger.info(
                        #     'waiting jq fill {} {} timestamp:{} for report_date:{}'.format(self.data_schema,
                        #                                                                    security_item.id,
                        #                                                                    the_data.timestamp,
                        #                                                                    the_data.report_date))

                        self.fill_timestamp_with_jq(entity, the_data)
Ejemplo n.º 7
0
def update_data_table(n_clicks, properties, codes: str, start_date, end_date):
    if n_clicks and properties:
        props = []
        for prop in properties:
            props.append(json.loads(prop))

        readers = properties_to_readers(properties=props,
                                        codes=codes,
                                        start_date=start_date,
                                        end_date=end_date)
        if readers:
            data_df = readers[0].data_df
            for reader in readers[1:]:
                if df_is_not_null(reader.data_df):
                    data_df = data_df.join(reader.data_df, how='outer')

            global current_df
            current_df = data_df

            normal_data = NormalData(data_df)
            data_table = Drawer(data=normal_data).draw_data_table(
                id='data-table-content')

            # generate col setting table
            properties = normal_data.data_df.columns.to_list()

            df = pd.DataFrame(
                OrderedDict([('property', properties),
                             ('y_axis', ['y1'] * len(properties)),
                             ('chart', ['line'] * len(properties))]))

            # generate intents
            intents = normal_data.get_intents()

            intent_options = [{
                'label': intent.value,
                'value': intent.value
            } for intent in intents]

            intent_value = intents[0].value

            return data_table, df.to_dict(
                'records'), normal_data.get_table_type(
                ), intent_options, intent_value

        else:
            return 'no data,please reselect!', None, '', [{
                'label':
                'compare_self',
                'value':
                'compare_self'
            }], 'compare_self'

    raise dash.exceptions.PreventUpdate()
Ejemplo n.º 8
0
    def record(self, entity, start, end, size, timestamps):
        if self.start_timestamp:
            start = max(self.start_timestamp, to_pd_timestamp(start))

        # if self.level < IntervalLevel.LEVEL_1HOUR:
        #     start = '2019-01-01'

        end = now_pd_timestamp()

        start_timestamp = to_time_str(start)

        # 聚宽get_price函数必须指定结束时间,否则会有未来数据
        end_timestamp = to_time_str(end, fmt=TIME_FORMAT_MINUTE2)
        # 不复权
        df = get_price(
            to_jq_entity_id(entity),
            start_date=to_time_str(start_timestamp),
            end_date=end_timestamp,
            frequency=self.jq_trading_level,
            fields=['open', 'close', 'low', 'high', 'volume', 'money'],
            skip_paused=True,
            fq=None)
        if df_is_not_null(df):
            df.index.name = 'timestamp'
            df.reset_index(inplace=True)
            df['name'] = entity.name
            df.rename(columns={'money': 'turnover'}, inplace=True)

            df['entity_id'] = entity.id
            df['timestamp'] = pd.to_datetime(df['timestamp'])
            df['provider'] = 'joinquant'
            df['level'] = self.level.value
            df['code'] = entity.code

            def generate_kdata_id(se):
                if self.level >= IntervalLevel.LEVEL_1DAY:
                    return "{}_{}".format(
                        se['entity_id'],
                        to_time_str(se['timestamp'], fmt=TIME_FORMAT_DAY))
                else:
                    return "{}_{}".format(
                        se['entity_id'],
                        to_time_str(se['timestamp'], fmt=TIME_FORMAT_ISO8601))

            df['id'] = df[['entity_id', 'timestamp']].apply(generate_kdata_id,
                                                            axis=1)

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

        return None
Ejemplo n.º 9
0
    def on_finish_entity(self, entity):
        kdatas = get_kdata(
            provider=self.provider,
            entity_id=entity.id,
            level=self.level.value,
            order=self.data_schema.timestamp.asc(),
            return_type='domain',
            session=self.session,
            filters=[
                self.data_schema.hfq_close.is_(None),
                self.data_schema.timestamp >= to_pd_timestamp('2005-01-01')
            ])
        if kdatas:
            start = kdatas[0].timestamp
            end = kdatas[-1].timestamp

            # get hfq from joinquant
            df = get_price(to_jq_entity_id(entity),
                           start_date=to_time_str(start),
                           end_date=now_time_str(),
                           frequency='daily',
                           fields=['factor', 'open', 'close', 'low', 'high'],
                           skip_paused=True,
                           fq='post')
            if df_is_not_null(df):
                # fill hfq data
                for kdata in kdatas:
                    time_str = to_time_str(kdata.timestamp)
                    if time_str in df.index:
                        kdata.hfq_open = df.loc[time_str, 'open']
                        kdata.hfq_close = df.loc[time_str, 'close']
                        kdata.hfq_high = df.loc[time_str, 'high']
                        kdata.hfq_low = df.loc[time_str, 'low']
                        kdata.factor = df.loc[time_str, 'factor']
                self.session.add_all(kdatas)
                self.session.commit()

                latest_factor = df.factor[-1]
                # factor not change yet, no need to reset the qfq past
                if latest_factor == self.current_factors.get(entity.id):
                    sql = 'UPDATE {} SET qfq_close=hfq_close/{},qfq_high=hfq_high/{}, qfq_open= hfq_open/{}, qfq_low= hfq_low/{} where ' \
                          'entity_id=\'{}\' and level=\'{}\' and (qfq_close isnull or qfq_high isnull or qfq_low isnull or qfq_open isnull)'.format(
                        self.data_schema.__table__, latest_factor, latest_factor, latest_factor, latest_factor,
                        entity.id, self.level.value)
                else:
                    sql = 'UPDATE {} SET qfq_close=hfq_close/{},qfq_high=hfq_high/{}, qfq_open= hfq_open/{}, qfq_low= hfq_low/{} where ' \
                          'entity_id=\'{}\' and level=\'{}\''.format(self.data_schema.__table__, latest_factor,
                                                                     latest_factor, latest_factor, latest_factor,
                                                                     entity.id,
                                                                     self.level.value)
                self.logger.info(sql)
                self.session.execute(sql)
                self.session.commit()
Ejemplo n.º 10
0
    def is_normalized(self):
        if df_is_not_null(self.data_df):
            names = self.data_df.index.names

            # it has been normalized
            if len(names) == 1 and names[0] == self.category_field:
                return True

            if len(names) == 2 and names[0] == self.category_field and names[
                    1] == self.index_field:
                return True

        return False
Ejemplo n.º 11
0
    def normalize(self):
        """
        normalize data_df to
                                    col1    col2    col3
        entity_id    index_field

        """
        if df_is_not_null(self.data_df):
            if not self.is_normalized():
                names = self.data_df.index.names
                for level, name in enumerate(names):
                    if name in self.data_df.columns.tolist():
                        self.data_df = self.data_df.reset_index(level=level,
                                                                drop=True)
                    else:
                        self.data_df = self.data_df.reset_index(level=level)

                self.data_df = index_df_with_category_xfield(
                    self.data_df,
                    category_field=self.category_field,
                    xfield=self.index_field,
                    is_timeseries=self.is_timeseries)

            if isinstance(self.data_df.index, pd.MultiIndex):
                self.entity_ids = list(
                    self.data_df.index.get_level_values(0).values)
            else:
                self.entity_ids = list(self.data_df.index.values)

            # remove the duplicates
            self.entity_ids = list(set(self.entity_ids))

            for entity_id, df_item in self.data_df.groupby(
                    self.category_field):
                df = df_item.copy()
                df.reset_index(inplace=True, level=self.category_field)
                self.df_list.append(df)

            if len(self.df_list) > 1:
                self.df_list = fill_with_same_index(df_list=self.df_list)

            for df in self.df_list:
                entity_id = df[df[self.category_field].notna()][
                    self.category_field][0]
                columns = list(df.columns)
                columns.remove(self.category_field)
                self.entity_map_df[entity_id] = df.loc[:, columns]

            self.entity_size = len(self.entity_ids)
            self.row_count = int(len(self.data_df) / self.entity_size)
            self.column_size = len(self.data_df.columns)
Ejemplo n.º 12
0
Archivo: api.py Proyecto: yezj0010/zvt
def df_to_db(df, data_schema, provider, force=False):
    if not df_is_not_null(df):
        return

    db_engine = get_db_engine(provider, data_schema=data_schema)

    if force:
        session = get_db_session(provider=provider, data_schema=data_schema)
        ids = df["id"].tolist()
        # count = len(ids)
        # start = 0
        # while True:
        #     end = min(count, start + 5000)
        #     sql = f'delete from {data_schema.__tablename__} where id in {tuple(ids[start:end])}'
        #     session.execute(sql)
        #     session.commit()
        #     if end == count:
        #         break
        #     start = end
        if len(ids) == 1:
            sql = f'delete from {data_schema.__tablename__} where id = "{ids[0]}"'
        else:
            sql = f'delete from {data_schema.__tablename__} where id in {tuple(ids)}'

        session.execute(sql)
        session.commit()

    else:
        current = get_data(data_schema=data_schema,
                           columns=[data_schema.id],
                           provider=provider)
        if df_is_not_null(current):
            df = df[~df['id'].isin(current['id'])]

    df.to_sql(data_schema.__tablename__,
              db_engine,
              index=False,
              if_exists='append')
Ejemplo n.º 13
0
    def get_targets(
            self,
            timestamp,
            target_type: TargetType = TargetType.open_long) -> pd.DataFrame:
        if target_type == TargetType.open_long:
            df = self.open_long_df
        if target_type == TargetType.open_short:
            df = self.open_short_df

        if df_is_not_null(df):
            if timestamp in df.index:
                target_df = df.loc[[to_pd_timestamp(timestamp)], :]
                return target_df['entity_id'].tolist()
        return []
Ejemplo n.º 14
0
def get_trading_signals_figure(order_reader: OrderReader,
                               entity_id: str,
                               provider: str,
                               level):
    entity_type, _, _ = decode_entity_id(entity_id)
    security_factor = TechnicalFactor(entity_type=entity_type, entity_ids=[entity_id],
                                      level=level, provider=provider)

    if df_is_not_null(security_factor.get_data_df()):
        print(security_factor.get_data_df().tail())

    # generate the annotation df
    order_reader.move_on(timeout=0)
    df = order_reader.get_data_df().copy()
    if df_is_not_null(df):
        df['value'] = df['order_price']
        df['flag'] = df['order_type'].apply(lambda x: order_type_flag(x))
        df['color'] = df['order_type'].apply(lambda x: order_type_color(x))
    print(df.tail())

    data, layout = security_factor.draw(render=None, figures=go.Candlestick, annotation_df=df)

    return go.Figure(data=data, layout=layout)
Ejemplo n.º 15
0
Archivo: api.py Proyecto: lunvs/zvt
def init_entities(df, entity_type='stock', provider='exchange'):
    df = df.drop_duplicates(subset=['id'])
    data_schema = get_entity_schema(entity_type)
    store_category = get_db_name(data_schema=data_schema)

    db_engine = get_db_engine(provider, db_name=store_category)
    security_schema = get_entity_schema(entity_type)

    current = get_entities(entity_type=entity_type, columns=[security_schema.id, security_schema.code],
                           provider=provider)

    if df_is_not_null(current):
        df = df[~df['id'].isin(current['id'])]

    df.to_sql(security_schema.__tablename__, db_engine, index=False, if_exists='append')
Ejemplo n.º 16
0
    def generate_targets(self):
        if df_is_not_null(self.filter_result) and df_is_not_null(
                self.score_result):
            # for long
            result1 = self.filter_result[self.filter_result.score]
            result2 = self.score_result[
                self.score_result.score >= self.long_threshold]
            long_result = result2.loc[result1.index, :]
            # for short
            result1 = self.filter_result[~self.filter_result.score]
            result2 = self.score_result[
                self.score_result.score <= self.short_threshold]
            short_result = result2.loc[result1.index, :]
        elif df_is_not_null(self.score_result):
            long_result = self.score_result[
                self.score_result.score >= self.long_threshold]
            short_result = self.score_result[
                self.score_result.score <= self.short_threshold]
        else:
            long_result = self.filter_result[self.filter_result.score]
            short_result = self.filter_result[~self.filter_result.score]

        self.open_long_df = self.normalize_result_df(long_result)
        self.open_short_df = self.normalize_result_df(short_result)
Ejemplo n.º 17
0
    def depth_computing(self):
        if df_is_not_null(self.data_df):
            self.depth_df = self.data_df.reset_index(level='timestamp')

            for idx, indicator in enumerate(self.indicators):
                if indicator == 'ma':
                    window = self.indicators_param[idx].get('window')

                    col = 'ma{}'.format(window)
                    self.indicator_cols.add(col)

                    for entity_id, df in self.depth_df.groupby('entity_id'):
                        if self.entity_type == 'stock':
                            self.depth_df.loc[entity_id,
                                              col] = ma(df['qfq_close'],
                                                        window=window)
                        else:
                            self.depth_df.loc[entity_id,
                                              col] = ma(df['close'],
                                                        window=window)
                if indicator == 'macd':
                    slow = self.indicators_param[idx].get('slow')
                    fast = self.indicators_param[idx].get('fast')
                    n = self.indicators_param[idx].get('n')

                    self.indicator_cols.add('diff')
                    self.indicator_cols.add('dea')
                    self.indicator_cols.add('macd')

                    for entity_id, df in self.depth_df.groupby('entity_id'):
                        if self.entity_type == 'stock' and self.fq == 'qfq':
                            diff, dea, m = macd(df['qfq_close'],
                                                slow=slow,
                                                fast=fast,
                                                n=n)
                        else:
                            diff, dea, m = macd(df['close'],
                                                slow=slow,
                                                fast=fast,
                                                n=n)

                        self.depth_df.loc[entity_id, 'diff'] = diff
                        self.depth_df.loc[entity_id, 'dea'] = dea
                        self.depth_df.loc[entity_id, 'macd'] = m

            self.depth_df = self.depth_df.set_index('timestamp', append=True)
Ejemplo n.º 18
0
    def load_data(self):
        if self.entity_ids:
            self.data_df = get_data(data_schema=self.data_schema,
                                    entity_ids=self.entity_ids,
                                    provider=self.provider,
                                    columns=self.columns,
                                    start_timestamp=self.start_timestamp,
                                    end_timestamp=self.end_timestamp,
                                    filters=self.filters,
                                    order=self.order,
                                    limit=self.limit,
                                    level=self.level,
                                    time_field=self.time_field,
                                    index=self.time_field)
        else:
            self.data_df = get_data(data_schema=self.data_schema,
                                    codes=self.codes,
                                    provider=self.provider,
                                    columns=self.columns,
                                    start_timestamp=self.start_timestamp,
                                    end_timestamp=self.end_timestamp,
                                    filters=self.filters,
                                    order=self.order,
                                    limit=self.limit,
                                    level=self.level,
                                    time_field=self.time_field,
                                    index=self.time_field)

        if self.trip_timestamp:
            if self.level == IntervalLevel.LEVEL_1DAY:
                self.data_df[self.time_field] = self.data_df[
                    self.time_field].apply(
                        lambda x: to_pd_timestamp(to_time_str(x)))

        if df_is_not_null(self.data_df):
            self.normal_data = NormalData(df=self.data_df,
                                          category_field=self.category_field,
                                          index_field=self.time_field,
                                          is_timeseries=True)
            self.data_df = self.normal_data.data_df

        for listener in self.data_listeners:
            listener.on_data_loaded(self.data_df)
Ejemplo n.º 19
0
    def on_finish(self):
        last_year = str(now_pd_timestamp().year)
        codes = [item.code for item in self.entities]
        need_filleds = get_dividend_financing(
            provider=self.provider,
            codes=codes,
            return_type='domain',
            session=self.session,
            filters=[DividendFinancing.spo_raising_fund.is_(None)],
            end_timestamp=last_year)

        for item in need_filleds:
            df = get_spo_detail(
                provider=self.provider,
                entity_id=item.entity_id,
                columns=[SpoDetail.timestamp, SpoDetail.spo_raising_fund],
                start_timestamp=item.timestamp,
                end_timestamp="{}-12-31".format(item.timestamp.year))
            if df_is_not_null(df):
                item.spo_raising_fund = df['spo_raising_fund'].sum()
                self.session.commit()
        super().on_finish()
Ejemplo n.º 20
0
    def move_on(self, to_timestamp: Union[str, pd.Timestamp] = None,
                timeout: int = 20) -> bool:
        """
        get the data happened before to_timestamp,if not set,get all the data which means to now

        Parameters
        ----------
        to_timestamp :
        timeout : the time waiting the data ready in seconds

        Returns
        -------
        whether got data
        """
        if not df_is_not_null(self.data_df):
            self.load_data()
            return False

        df = self.data_df.reset_index(level='timestamp')
        recorded_timestamps = df.groupby(level=0)['timestamp'].max()

        self.logger.info('level:{},current_timestamps:\n{}'.format(self.level, recorded_timestamps))

        changed = False
        # FIXME:we suppose history data should be there at first
        start_time = time.time()
        for category, recorded_timestamp in recorded_timestamps.iteritems():
            while True:
                category_filter = [self.category_column == category]
                if self.filters:
                    filters = self.filters + category_filter
                else:
                    filters = category_filter

                added = get_data(data_schema=self.data_schema, provider=self.provider, columns=self.columns,
                                 start_timestamp=recorded_timestamp,
                                 end_timestamp=to_timestamp, filters=filters, level=self.level)

                if df_is_not_null(added):
                    would_added = added[added['timestamp'] != recorded_timestamp].copy()
                    if not would_added.empty:
                        added = index_df_with_category_xfield(would_added, category_field=self.category_field,
                                                              xfield=self.time_field)
                        self.logger.info('category:{},added:\n{}'.format(category, added))

                        self.data_df = self.data_df.append(added)
                        self.data_df = self.data_df.sort_index(level=[0, 1])

                        for listener in self.data_listeners:
                            listener.on_category_data_added(category=category, added_data=added)
                        changed = True
                        # if got data,just move to another category
                        break

                cost_time = time.time() - start_time
                if cost_time > timeout:
                    self.logger.warning(
                        'category:{} level:{} getting data timeout,to_timestamp:{},now:{}'.format(category, self.level,
                                                                                                  to_timestamp,
                                                                                                  now_pd_timestamp()))
                    break

        if changed:
            for listener in self.data_listeners:
                listener.on_data_changed(self.data_df)

        return changed
Ejemplo n.º 21
0
def get_data(data_schema,
             entity_ids: List[str] = None,
             entity_id: str = None,
             codes: List[str] = None,
             level: Union[IntervalLevel, str] = None,
             provider: str = None,
             columns: List = None,
             return_type: str = 'df',
             start_timestamp: Union[pd.Timestamp, str] = None,
             end_timestamp: Union[pd.Timestamp, str] = None,
             filters: List = None,
             session: Session = None,
             order=None,
             limit: int = None,
             index: str = 'timestamp',
             index_is_time: bool = True,
             time_field: str = 'timestamp'):
    assert data_schema is not None
    assert provider is not None
    assert provider in global_providers

    local_session = False
    if not session:
        session = get_db_session(provider=provider, data_schema=data_schema)
        local_session = True

    try:
        time_col = eval('data_schema.{}'.format(time_field))

        if columns:
            # support str
            if type(columns[0]) == str:
                columns_ = []
                for col in columns:
                    columns_.append(eval('data_schema.{}'.format(col)))
                columns = columns_

            if time_col not in columns:
                columns.append(time_col)
            query = session.query(*columns)
        else:
            query = session.query(data_schema)

        if entity_id:
            query = query.filter(data_schema.entity_id == entity_id)
        if codes:
            query = query.filter(data_schema.code.in_(codes))
        if entity_ids:
            query = query.filter(data_schema.entity_id.in_(entity_ids))

        # we always store different level in different schema,the level param is not useful now
        if level:
            try:
                # some schema has no level,just ignore it
                data_schema.level
                if type(level) == IntervalLevel:
                    level = level.value
                query = query.filter(data_schema.level == level)
            except Exception as e:
                pass

        query = common_filter(query,
                              data_schema=data_schema,
                              start_timestamp=start_timestamp,
                              end_timestamp=end_timestamp,
                              filters=filters,
                              order=order,
                              limit=limit,
                              time_field=time_field)

        if return_type == 'df':
            df = pd.read_sql(query.statement, query.session.bind)
            if df_is_not_null(df):
                return index_df(df,
                                drop=False,
                                index=index,
                                index_is_time=index_is_time)
            return df
        elif return_type == 'domain':
            return query.all()
        elif return_type == 'dict':
            return [item.__dict__ for item in query.all()]
    except Exception:
        raise
    finally:
        if local_session:
            session.close()
Ejemplo n.º 22
0
 def is_empty(self):
     return not df_is_not_null(self.data_df)
Ejemplo n.º 23
0
 def normalize_result_df(self, df):
     if df_is_not_null(df):
         df = df.reset_index()
         df = index_df(df)
         df = df.sort_values(by=['score', 'entity_id'])
     return df
Ejemplo n.º 24
0
def get_trader_detail_figures(trader_domain: business.Trader,
                              account_reader: AccountReader,
                              order_reader: OrderReader):
    graph_list = []

    if account_reader:
        account_data, account_layout = account_reader.data_drawer().draw_line(render=None, keep_ui_state=False)

        for trader_name in account_reader.trader_names:
            graph_list.append(dcc.Graph(
                id='{}-account'.format(trader_name),
                figure={
                    'data': account_data,
                    'layout': account_layout
                }))

    order_reader.move_on(timeout=0)
    df_orders = order_reader.get_data_df().copy()

    if df_is_not_null(df_orders):
        grouped = df_orders.groupby('entity_id')

        for entity_id, order_df in grouped:
            entity_type, _, _ = decode_entity_id(entity_id)

            indicators = []
            indicators_param = []
            indicator_cols = []
            if trader_domain.technical_factors:
                tech_factors = simplejson.loads(trader_domain.technical_factors)
                print(tech_factors)
                for factor in tech_factors:
                    indicators += factor['indicators']
                    indicators_param += factor['indicators_param']
                    indicator_cols += factor['indicator_cols']

            security_factor = TechnicalFactor(entity_type=entity_type, entity_ids=[entity_id],
                                              start_timestamp=trader_domain.start_timestamp,
                                              end_timestamp=trader_domain.end_timestamp,
                                              level=trader_domain.level, provider=trader_domain.provider,
                                              indicators=indicators,
                                              indicators_param=indicators_param)

            # generate the annotation df
            df = order_df.copy()
            if df_is_not_null(df):
                df['value'] = df['order_price']
                df['flag'] = df['order_type'].apply(lambda x: order_type_flag(x))
                df['color'] = df['order_type'].apply(lambda x: order_type_color(x))
            print(df.tail())

            data, layout = security_factor.draw_depth(render=None, annotation_df=df, height=620)
            if trader_domain.real_time:
                result = get_current_price(entity_ids=[entity_id])
                bid_ask = result.get(entity_id)

                if bid_ask:
                    graph_list.append(daq.LEDDisplay(
                        id='ask',
                        label=f'ask price',
                        value=bid_ask[0],
                        color="#00da3c"
                    ))

                    graph_list.append(daq.LEDDisplay(
                        id='bid',
                        label=f'bid price',
                        value=bid_ask[1],
                        color="#FF5E5E"
                    ))

            graph_list.append(
                dcc.Graph(
                    id='{}-{}-signals'.format(trader_domain.trader_name, entity_id),
                    figure={
                        'data': data,
                        'layout': layout
                    }
                )
            )

    return graph_list