Example #1
0
def get_kdata(entity_id=None,
              level=IntervalLevel.LEVEL_1DAY.value,
              provider='joinquant',
              columns=None,
              return_type='df',
              start_timestamp=None,
              end_timestamp=None,
              filters=None,
              session=None,
              order=None,
              limit=None,
              index='timestamp'):
    entity_type, exchange, code = decode_entity_id(entity_id)
    data_schema: Mixin = get_kdata_schema(entity_type, level=level)

    return data_schema.query_data(entity_id=entity_id,
                                  level=level,
                                  provider=provider,
                                  columns=columns,
                                  return_type=return_type,
                                  start_timestamp=start_timestamp,
                                  end_timestamp=end_timestamp,
                                  filters=filters,
                                  session=session,
                                  order=order,
                                  limit=limit,
                                  index=index)
Example #2
0
def get_one_day_trading_minutes(entity_id: str = None, entity_type: str = None):
    if entity_type is None:
        entity_type, _, _ = decode_entity_id(entity_id)
    if entity_type == 'coin':
        return 24 * 60
    if entity_type == 'stock':
        return 4 * 60
Example #3
0
    def draw_kline(self, plotly_layout=None, annotation_df=None, render='html', file_name=None, width=None, height=None,
                   title=None, keep_ui_state=True, indicators=[], property_map=None, **kwargs):
        data = []
        for entity_id, df in self.normal_data.entity_map_df.items():
            entity_type, _, code = decode_entity_id(entity_id)

            trace_name = '{}_kdata'.format(code)

            if entity_type == 'stock':
                open = df.loc[:, 'qfq_open']
                close = df.loc[:, 'qfq_close']
                high = df.loc[:, 'qfq_high']
                low = df.loc[:, 'qfq_low']
            else:
                open = df.loc[:, 'open']
                close = df.loc[:, 'close']
                high = df.loc[:, 'high']
                low = df.loc[:, 'low']

            data.append(
                go.Candlestick(x=df.index, open=open, close=close, low=low, high=high, name=trace_name, **kwargs))

            # append indicators
            for indicator in indicators:
                if indicator in df.columns:
                    trace_name = '{}_{}'.format(code, indicator)
                    ydata = df.loc[:, indicator].values.tolist()
                    data.append(go.Scatter(x=df.index, y=ydata, mode='lines', name=trace_name))

        return self.show(plotly_data=data, plotly_layout=plotly_layout, annotation_df=annotation_df, render=render,
                         file_name=file_name, width=width,
                         height=height, title=title, keep_ui_state=keep_ui_state)
Example #4
0
    def draw_compare(self, chart: str, plotly_layout=None, annotation_df=None, render='html', file_name=None,
                     width=None, height=None, title=None, keep_ui_state=True, property_map=None, **kwargs):
        data = []
        layout_params = {}
        for entity_id, df in self.normal_data.entity_map_df.items():
            _, _, code = decode_entity_id(entity_id)
            for col in df.columns:
                trace_name = '{}_{}'.format(code, col)
                ydata = df.loc[:, col].values.tolist()

                # set y axis
                yaxis, layout, col_chart = self.get_yaxis_layout_chart(col, property_map)
                if yaxis and layout:
                    layout_params[f'yaxis{yaxis[-1]}'] = layout

                if not col_chart:
                    col_chart = chart

                if col_chart == 'line':
                    trace = go.Scatter(x=df.index, y=ydata, mode='lines', name=trace_name, yaxis=yaxis, **kwargs)
                elif col_chart == 'scatter':
                    trace = go.Scatter(x=df.index, y=ydata, mode='markers', name=trace_name, yaxis=yaxis, **kwargs)
                elif col_chart == 'area':
                    trace = go.Scatter(x=df.index, y=ydata, mode='none', fill='tonexty', name=trace_name, yaxis=yaxis,
                                       **kwargs)
                elif col_chart == 'bar':
                    trace = go.Bar(x=df.index, y=ydata, name=trace_name, yaxis=yaxis, **kwargs)

                data.append(trace)

        return self.show(plotly_data=data, plotly_layout=plotly_layout, annotation_df=annotation_df, render=render,
                         file_name=file_name, width=width,
                         height=height, title=title, keep_ui_state=keep_ui_state, **layout_params)
Example #5
0
def get_kdata(entity_id,
              level=IntervalLevel.LEVEL_1DAY.value,
              provider='eastmoney',
              columns=None,
              return_type='df',
              start_timestamp=None,
              end_timestamp=None,
              filters=None,
              session=None,
              order=None,
              limit=None):
    entity_type, exchange, code = decode_entity_id(entity_id)
    data_schema = get_kdata_schema(entity_type, level=level)

    return get_data(data_schema=data_schema,
                    entity_id=entity_id,
                    level=level,
                    provider=provider,
                    columns=columns,
                    return_type=return_type,
                    start_timestamp=start_timestamp,
                    end_timestamp=end_timestamp,
                    filters=filters,
                    session=session,
                    order=order,
                    limit=limit)
Example #6
0
    def draw_histogram(self, plotly_layout=None, annotation_df=None, render='html', file_name=None, width=None,
                       height=None,
                       title=None, keep_ui_state=True, property_map=None, **kwargs):
        data = []
        annotations = []
        for entity_id, df in self.normal_data.entity_map_df.items():
            _, _, code = decode_entity_id(entity_id)

            for col in df.columns:
                trace_name = '{}_{}'.format(code, col)
                x = df[col].tolist()
                trace = go.Histogram(
                    x=x,
                    name=trace_name,
                    histnorm='probability',
                    **kwargs
                )
                annotation = dict(
                    x=x[-1],
                    y=0,
                    xref='x',
                    yref='y',
                    text=f'current:{x[-1]}',
                    showarrow=True,
                    align='center',
                    arrowhead=2,
                    arrowsize=1,
                    arrowwidth=2,
                    # arrowcolor='#030813',
                    # ax=-0.02,
                    # ay=-0.02,
                    bordercolor='#c7c7c7',
                    borderwidth=1,
                    opacity=0.8
                )
                data.append(trace)
                annotations.append(annotation)
            # just support one entity
            break

        if keep_ui_state:
            uirevision = True
        else:
            uirevision = None

        layout = go.Layout(showlegend=True,
                           uirevision=uirevision,
                           height=height,
                           width=width,
                           title=title,
                           annotations=annotations,
                           barmode='overlay')

        return self.show(plotly_data=data, plotly_layout=layout, render=render, file_name=file_name, width=width,
                         height=height, title=title, keep_ui_state=keep_ui_state)
Example #7
0
def to_high_level_kdata(kdata_df: pd.DataFrame, to_level: IntervalLevel):
    def to_close(s):
        if pd_is_not_null(s):
            return s[-1]

    def to_open(s):
        if pd_is_not_null(s):
            return s[0]

    def to_high(s):
        return np.max(s)

    def to_low(s):
        return np.min(s)

    def to_sum(s):
        return np.sum(s)

    original_level = kdata_df['level'][0]
    entity_id = kdata_df['entity_id'][0]
    provider = kdata_df['provider'][0]
    name = kdata_df['name'][0]
    code = kdata_df['code'][0]

    entity_type, _, _ = decode_entity_id(entity_id=entity_id)

    assert IntervalLevel(original_level) <= IntervalLevel.LEVEL_1DAY
    assert IntervalLevel(original_level) < IntervalLevel(to_level)

    df: pd.DataFrame = None
    if to_level == IntervalLevel.LEVEL_1WEEK:
        # loffset='-2' 用周五作为时间标签
        if entity_type == 'stock':
            df = kdata_df.resample('W', loffset=pd.DateOffset(days=-2)).apply({'close': to_close,
                                                                               'open': to_open,
                                                                               'high': to_high,
                                                                               'low': to_low,
                                                                               'volume': to_sum,
                                                                               'turnover': to_sum})
        else:
            df = kdata_df.resample('W', loffset=pd.DateOffset(days=-2)).apply({'close': to_close,
                                                                               'open': to_open,
                                                                               'high': to_high,
                                                                               'low': to_low,
                                                                               'volume': to_sum,
                                                                               'turnover': to_sum})
    df = df.dropna()
    # id        entity_id  timestamp   provider    code  name level
    df['entity_id'] = entity_id
    df['provider'] = provider
    df['code'] = code
    df['name'] = name

    return df
Example #8
0
def get_close_time(entity_id: str):
    """

    :param entity_id:
    :type entity_id: str
    :return:0,0 means never stop
    :rtype: Tuple[int, int]
    """
    entity_type, _, _ = decode_entity_id(entity_id)
    if entity_type == 'coin':
        return 0, 0
    if entity_type == 'stock':
        return 15, 0
Example #9
0
    def draw_bar(self, x='columns', plotly_layout=None, annotation_df=None, render='html', file_name=None, width=None,
                 height=None,
                 title=None, keep_ui_state=True, **kwargs):
        data = []
        for entity_id, df in self.normal_data.entity_map_df.items():
            _, _, code = decode_entity_id(entity_id)
            for col in df.columns:
                trace_name = '{}_{}'.format(code, col)
                ydata = df.loc[:, col].values.tolist()
                data.append(go.Bar(x=df.index, y=ydata, name=trace_name, **kwargs))

        return self.show(plotly_data=data, plotly_layout=plotly_layout, annotation_df=annotation_df, render=render,
                         file_name=file_name, width=width,
                         height=height, title=title, keep_ui_state=keep_ui_state)
Example #10
0
    def draw_bar(self,
                 x='columns',
                 plotly_layout=None,
                 annotation_df=None,
                 render='html',
                 file_name=None,
                 width=None,
                 height=None,
                 title=None,
                 keep_ui_state=True,
                 property_map=None,
                 **kwargs):
        data = []
        layout_params = {}
        for entity_id, df in self.normal_data.entity_map_df.items():
            _, _, code = decode_entity_id(entity_id)
            for col in df.columns:
                trace_name = '{}_{}'.format(code, col)
                ydata = df.loc[:, col].values.tolist()

                # set y axis
                yaxis, layout, _ = self.get_yaxis_layout_chart(
                    col, property_map)
                if yaxis and layout:
                    layout_params[f'yaxis{yaxis[-1]}'] = layout

                data.append(
                    go.Bar(x=df.index,
                           y=ydata,
                           name=trace_name,
                           yaxis=yaxis,
                           **kwargs))

        return self.show(plotly_data=data,
                         plotly_layout=plotly_layout,
                         annotation_df=annotation_df,
                         render=render,
                         file_name=file_name,
                         width=width,
                         height=height,
                         title=title,
                         keep_ui_state=keep_ui_state,
                         **layout_params)
Example #11
0
def get_current_price(entity_ids=None, entity_type='coin'):
    result = {}
    if entity_type == 'coin':
        if entity_ids:
            for entity_id in entity_ids:
                a, exchange, code = decode_entity_id(entity_id)
                assert a == entity_type
                ccxt_exchange = CCXTAccount.get_ccxt_exchange(exchange_str=exchange)

                if not ccxt_exchange:
                    raise Exception('{} not support'.format(exchange))

                orderbook = ccxt_exchange.fetch_order_book(code)

                bid = orderbook['bids'][0][0] if len(orderbook['bids']) > 0 else None
                ask = orderbook['asks'][0][0] if len(orderbook['asks']) > 0 else None
                entity_id = f'coin_{exchange}_{code}'
                result[entity_id] = (bid, ask)

    return result
Example #12
0
def get_open_time(entity_id: str):
    entity_type, _, _ = decode_entity_id(entity_id)
    if entity_type == 'coin':
        return 0, 0
    if entity_type == 'stock':
        return 9, 30
Example #13
0
    def _draw(self,
              main_chart='kline',
              sub_chart='bar',
              mode='markers',
              width=None,
              height=None,
              title=None,
              keep_ui_state=True,
              **kwargs):
        if self.sub_data is not None and not self.sub_data.empty():
            subplot = True
            fig = make_subplots(rows=2,
                                cols=1,
                                row_heights=[0.8, 0.2],
                                vertical_spacing=0.08,
                                shared_xaxes=True)
        else:
            subplot = False
            fig = go.Figure()

        traces = []
        sub_traces = []

        for entity_id, df in self.main_data.entity_map_df.items():
            entity_type, _, code = decode_entity_id(entity_id)

            if main_chart == 'kline':
                trace_name = '{}_kdata'.format(code)
                trace = go.Candlestick(x=df.index,
                                       open=df['open'],
                                       close=df['close'],
                                       low=df['low'],
                                       high=df['high'],
                                       name=trace_name,
                                       **kwargs)
                traces.append(trace)
            elif main_chart == 'scatter':
                for col in df.columns:
                    trace_name = '{}_{}'.format(code, col)
                    ydata = df[col].values.tolist()
                    traces.append(
                        go.Scatter(x=df.index,
                                   y=ydata,
                                   mode=mode,
                                   name=trace_name,
                                   **kwargs))

            if subplot:
                # 绘制幅图
                sub_df = self.sub_data.entity_map_df.get(entity_id)
                if pd_is_not_null(sub_df):
                    for col in sub_df.columns:
                        trace_name = '{}_{}'.format(code, col)
                        ydata = sub_df[col].values.tolist()

                        def color(i):
                            if i > 0:
                                return 'red'
                            else:
                                return 'green'

                        colors = [color(i) for i in ydata]
                        bar = go.Bar(x=sub_df.index,
                                     y=ydata,
                                     name=trace_name,
                                     yaxis='y2',
                                     marker_color=colors)
                        sub_traces.append(bar)

        if subplot:
            fig.add_traces(traces,
                           rows=[1] * len(traces),
                           cols=[1] * len(traces))
            fig.add_traces(sub_traces,
                           rows=[2] * len(sub_traces),
                           cols=[1] * len(sub_traces))
        else:
            fig.add_traces(traces)

        fig.update_layout(
            self.gen_plotly_layout(width=width,
                                   height=height,
                                   title=title,
                                   keep_ui_state=keep_ui_state,
                                   subplot=subplot))

        fig.show()
Example #14
0
    def draw_histogram(
            self,
            entity_in_subplot: bool = False,
            plotly_layout=None,
            render='html',
            file_name=None,
            width=None,
            height=None,
            title=None,
            keep_ui_state=True,
            # one of ( "" | "percent" | "probability" | "density" | "probability density" )
            histnorm='',
            **kwargs):
        annotations = []

        fig, total_rows = self.generate_fig(entity_in_subplot)

        row = 1
        for entity_id, df in self.main_data.entity_map_df.items():
            _, _, code = decode_entity_id(entity_id)

            traces = []
            rows = []
            for col in df.columns:
                trace_name = '{}_{}'.format(code, col)
                x = df[col].tolist()
                trace = go.Histogram(x=x,
                                     name=trace_name,
                                     histnorm=histnorm,
                                     **kwargs)

                if row > 1:
                    yref = f'y{row}'
                else:
                    yref = 'y'

                annotation = dict(x=x[-1],
                                  y=0,
                                  xref='x',
                                  yref=yref,
                                  text=f'current:{x[-1]}',
                                  showarrow=True,
                                  align='center',
                                  arrowhead=2,
                                  arrowsize=1,
                                  arrowwidth=2,
                                  bordercolor='#c7c7c7',
                                  borderwidth=1,
                                  opacity=0.8)

                traces.append(trace)
                rows.append(row)
                annotations.append(annotation)

            # add the traces for row
            if entity_in_subplot and (total_rows > 1):
                fig.add_traces(traces, rows=rows, cols=[1] * len(rows))
                row = row + 1
            else:
                fig.add_traces(traces)

        if keep_ui_state:
            uirevision = True
        else:
            uirevision = None

        layout = go.Layout(showlegend=True,
                           uirevision=uirevision,
                           height=height,
                           width=width,
                           title=title,
                           annotations=annotations,
                           barmode='overlay')

        fig.update_layout(layout)

        fig.show()
Example #15
0
    def draw_scatter(self,
                     mode='markers',
                     plotly_layout=None,
                     render='html',
                     file_name=None,
                     width=None,
                     height=None,
                     title=None,
                     keep_ui_state=True,
                     **kwargs):
        if self.sub_data is not None and not self.sub_data.empty():
            subplot = True
            fig = make_subplots(rows=2,
                                cols=1,
                                row_heights=[0.8, 0.2],
                                vertical_spacing=0.08,
                                shared_xaxes=True)
        else:
            subplot = False
            fig = go.Figure()

        traces = []
        sub_traces = []

        for entity_id, df in self.main_data.entity_map_df.items():
            entity_type, _, code = decode_entity_id(entity_id)

            for col in df.columns:
                trace_name = '{}_{}'.format(code, col)
                ydata = df[col].values.tolist()
                traces.append(
                    go.Scatter(x=df.index,
                               y=ydata,
                               mode=mode,
                               name=trace_name,
                               **kwargs))

            if subplot:
                # 绘制幅图
                sub_df = self.sub_data.entity_map_df.get(entity_id)
                if pd_is_not_null(sub_df):
                    for col in sub_df.columns:
                        trace_name = '{}_{}'.format(code, col)
                        ydata = sub_df[col].values.tolist()
                        sub_traces.append(
                            go.Bar(x=sub_df.index,
                                   y=ydata,
                                   name=trace_name,
                                   yaxis='y2'))

        if subplot:
            fig.add_traces(traces,
                           rows=[1] * len(traces),
                           cols=[1] * len(traces))
            fig.add_traces(sub_traces,
                           rows=[2] * len(sub_traces),
                           cols=[1] * len(sub_traces))
        else:
            fig.add_traces(traces)

        fig.update_layout(
            self.get_plotly_layout(width=width,
                                   height=height,
                                   title=title,
                                   keep_ui_state=keep_ui_state,
                                   subplot=subplot))

        fig.show()