Beispiel #1
0
    def draw_chart(cls, data):
        kline = (
            Kline().add_xaxis(xaxis_data=data["times"]).add_yaxis(
                series_name="",
                y_axis=data["datas"],
                itemstyle_opts=opts.ItemStyleOpts(
                    color="#ef232a",
                    color0="#14b143",
                    border_color="#ef232a",
                    border_color0="#14b143",
                ),
                markpoint_opts=opts.MarkPointOpts(data=[
                    opts.MarkPointItem(type_="max", name="最大值"),
                    opts.MarkPointItem(type_="min", name="最小值"),
                ]),
                markline_opts=opts.MarkLineOpts(
                    label_opts=opts.LabelOpts(position="middle",
                                              color="blue",
                                              font_size=15),
                    data=cls.split_data_part(data),
                    symbol=["circle", "none"],
                ),
            ).set_series_opts(markarea_opts=opts.MarkAreaOpts(
                is_silent=True, data=cls.split_data_part(data))).
            set_global_opts(
                title_opts=opts.TitleOpts(title="K线周期图表", pos_left="0"),
                xaxis_opts=opts.AxisOpts(
                    type_="category",
                    is_scale=True,
                    boundary_gap=False,
                    axisline_opts=opts.AxisLineOpts(is_on_zero=False),
                    splitline_opts=opts.SplitLineOpts(is_show=False),
                    split_number=20,
                    min_="dataMin",
                    max_="dataMax",
                ),
                yaxis_opts=opts.AxisOpts(
                    is_scale=True,
                    splitline_opts=opts.SplitLineOpts(is_show=True)),
                tooltip_opts=opts.TooltipOpts(trigger="axis",
                                              axis_pointer_type="line"),
                datazoom_opts=[
                    opts.DataZoomOpts(is_show=False,
                                      type_="inside",
                                      xaxis_index=[0,
                                                   0],
                                      range_end=100),
                    opts.DataZoomOpts(is_show=True,
                                      xaxis_index=[0,
                                                   1],
                                      pos_top="97%",
                                      range_end=100),
                    opts.DataZoomOpts(is_show=False,
                                      xaxis_index=[0,
                                                   2],
                                      range_end=100),
                    opts.DataZoomOpts(is_show=False,
                                      xaxis_index=[0,
                                                   3],
                                      range_end=100),
                ],
                # 三个图的 axis 连在一块
                # axispointer_opts=opts.AxisPointerOpts(
                #     is_show=True,
                #     link=[{"xAxisIndex": "all"}],
                #     label=opts.LabelOpts(background_color="#777"),
                # ),
            ))

        kline_line = (
            Line().add_xaxis(xaxis_data=data["times"])
            # # EMA10
            # .add_yaxis(
            #     series_name="EMA10",
            #     y_axis=data["ema10"],
            #     xaxis_index=2,
            #     yaxis_index=2,
            #     label_opts=opts.LabelOpts(is_show=False),
            # )

            # EMA144
            # .add_yaxis(
            #     series_name="EMA144",
            #     y_axis=data["ema144"],
            #     xaxis_index=2,
            #     yaxis_index=2,
            #     label_opts=opts.LabelOpts(is_show=False),
            # )
            # boll_median
            .add_yaxis(
                series_name="median",
                y_axis=data["boll_median"],
                xaxis_index=2,
                yaxis_index=2,
                label_opts=opts.LabelOpts(is_show=False),
            )
            # boll_upper
            .add_yaxis(
                series_name="upper",
                y_axis=data["boll_upper"],
                xaxis_index=2,
                yaxis_index=2,
                label_opts=opts.LabelOpts(is_show=False),
            )
            # boll_lower
            .add_yaxis(
                series_name="lower",
                y_axis=data["boll_lower"],
                xaxis_index=2,
                yaxis_index=2,
                label_opts=opts.LabelOpts(is_show=False),
            ).set_global_opts(
                xaxis_opts=opts.AxisOpts(
                    type_="category",
                    grid_index=1,
                    axislabel_opts=opts.LabelOpts(is_show=False),
                ),
                yaxis_opts=opts.AxisOpts(
                    grid_index=1,
                    split_number=3,
                    axisline_opts=opts.AxisLineOpts(is_on_zero=False),
                    axistick_opts=opts.AxisTickOpts(is_show=False),
                    splitline_opts=opts.SplitLineOpts(is_show=False),
                    axislabel_opts=opts.LabelOpts(is_show=True),
                ),
            ))
        # Overlap Kline + Line
        overlap_kline_line = kline.overlap(kline_line)

        # Bar-1
        bar_1 = (
            Bar().add_xaxis(xaxis_data=data["times"]).add_yaxis(
                series_name="Volumn",
                yaxis_data=data["vols"],
                xaxis_index=1,
                yaxis_index=1,
                label_opts=opts.LabelOpts(is_show=False),
                # 根据 echarts demo 的原版是这么写的
                # itemstyle_opts=opts.ItemStyleOpts(
                #     color=JsCode("""
                #     function(params) {
                #         var colorList;
                #         if (data.datas[params.dataIndex][1]>data.datas[params.dataIndex][0]) {
                #           colorList = '#ef232a';
                #         } else {
                #           colorList = '#14b143';
                #         }
                #         return colorList;
                #     }
                #     """)
                # )
                # 改进后在 grid 中 add_js_funcs 后变成如下
                itemstyle_opts=opts.ItemStyleOpts(color=JsCode("""
                    function(params) {
                        var colorList;
                        if (barData[params.dataIndex][1] > barData[params.dataIndex][0]) {
                            colorList = '#ef232a';
                        } else {
                            colorList = '#14b143';
                        }
                        return colorList;
                    }
                    """)),
            ).set_global_opts(
                xaxis_opts=opts.AxisOpts(
                    type_="category",
                    grid_index=1,
                    axislabel_opts=opts.LabelOpts(is_show=False),
                ),
                legend_opts=opts.LegendOpts(is_show=False),
            ))

        # Bar-2 (Overlap Bar + Line)
        bar_2 = (Bar().add_xaxis(xaxis_data=data["times"]).add_yaxis(
            series_name="MACD",
            yaxis_data=data["macds"],
            xaxis_index=2,
            yaxis_index=2,
            label_opts=opts.LabelOpts(is_show=False),
            itemstyle_opts=opts.ItemStyleOpts(color=JsCode("""
                            function(params) {
                                var colorList;
                                if (params.data >= 0) {
                                  colorList = '#ef232a';
                                } else {
                                  colorList = '#14b143';
                                }
                                return colorList;
                            }
                            """)),
        ).set_global_opts(
            xaxis_opts=opts.AxisOpts(
                type_="category",
                grid_index=2,
                axislabel_opts=opts.LabelOpts(is_show=False),
            ),
            yaxis_opts=opts.AxisOpts(
                grid_index=2,
                split_number=4,
                axisline_opts=opts.AxisLineOpts(is_on_zero=False),
                axistick_opts=opts.AxisTickOpts(is_show=False),
                splitline_opts=opts.SplitLineOpts(is_show=False),
                axislabel_opts=opts.LabelOpts(is_show=True),
            ),
            legend_opts=opts.LegendOpts(is_show=False),
        ))

        line_2 = (Line().add_xaxis(xaxis_data=data["times"]).add_yaxis(
            series_name="DIF",
            y_axis=data["difs"],
            xaxis_index=2,
            yaxis_index=2,
            label_opts=opts.LabelOpts(is_show=False),
        ).add_yaxis(
            series_name="DEA",
            y_axis=data["deas"],
            xaxis_index=2,
            yaxis_index=2,
            label_opts=opts.LabelOpts(is_show=False),
        ).set_global_opts(legend_opts=opts.LegendOpts(is_show=False)))

        rsi_line = (Line().add_xaxis(xaxis_data=data["times"]).add_yaxis(
            series_name="rsi6",
            y_axis=data["rsi6"],
            xaxis_index=2,
            yaxis_index=2,
            label_opts=opts.LabelOpts(is_show=False),
        ).add_yaxis(
            series_name="rsi12",
            y_axis=data["rsi12"],
            xaxis_index=2,
            yaxis_index=2,
            label_opts=opts.LabelOpts(is_show=False),
        ).add_yaxis(
            series_name="rsi24",
            y_axis=data["rsi24"],
            xaxis_index=2,
            yaxis_index=2,
            label_opts=opts.LabelOpts(is_show=False),
        ).set_global_opts(legend_opts=opts.LegendOpts(is_show=False)))

        # MACD
        overlap_bar_line = bar_2.overlap(line_2)

        # 最后的 Grid
        grid_chart = Grid(
            init_opts=opts.InitOpts(width="1400px", height="800px"))

        # 这个是为了把 data.datas 这个数据写入到 html 中,还没想到怎么跨 series 传值
        # demo 中的代码也是用全局变量传的
        grid_chart.add_js_funcs("var barData = {}".format(data["datas"]))

        # K线图和 MA5 的折线图
        grid_chart.add(
            overlap_kline_line,
            grid_opts=opts.GridOpts(pos_left="4%",
                                    pos_right="1%",
                                    height="45%"),
        )
        # Volumn 柱状图
        grid_chart.add(
            bar_1,
            grid_opts=opts.GridOpts(pos_left="4%",
                                    pos_right="1%",
                                    pos_top="56%",
                                    height="10%"),
        )
        # MACD DIFS DEAS
        grid_chart.add(
            overlap_bar_line,
            grid_opts=opts.GridOpts(pos_left="4%",
                                    pos_right="1%",
                                    pos_top="68%",
                                    height="14%"),
        )
        # rsi6
        grid_chart.add(
            rsi_line,
            grid_opts=opts.GridOpts(pos_left="4%",
                                    pos_right="1%",
                                    pos_top="84%",
                                    height="10%"),
        )
        return grid_chart.render_embed()
Beispiel #2
0
def draw_kline(chart_data):
    # [open, close, lowest, highest]
    kline_data = [data[1:-1] for data in chart_data["values"]]
    markpoint_item = [
                         opts.MarkPointItem(name="AI", coord=coord, value=value, symbol='pin', symbol_size=[35, 20],
                                            itemstyle_opts=opts.ItemStyleOpts(color='rgb(255,0,0)'))
                         for coord, value in zip(chart_data["ai_markpoint_coord"], chart_data["ai_markpoint_value"])
                     # ]
                     ] + \
                     [
                         opts.MarkPointItem(name="每日周線談整突破", coord=coord, value=value, symbol='pin',
                                            symbol_size=[35, 20],
                                            itemstyle_opts=opts.ItemStyleOpts(color='rgb(0,255,0)'))
                         for coord, value in zip(chart_data["other1_markpoint_coord"], chart_data["other1_markpoint_value"])
                     ] + \
                     [
                         opts.MarkPointItem(name="每日日線", coord=coord, value=value, symbol='pin',
                                            symbol_size=[35, 20],
                                            itemstyle_opts=opts.ItemStyleOpts(color='rgb(0,0,255)'))
                         for coord, value in zip(chart_data["other2_markpoint_coord"], chart_data["other2_markpoint_value"])
                     ] + \
                     [
                         opts.MarkPointItem(name="每日日線盤整突破", coord=coord, value=value, symbol='pin',
                                            symbol_size=[35, 20],
                                            itemstyle_opts=opts.ItemStyleOpts(color='rgb(255,255,0)'))
                         for coord, value in zip(chart_data["other3_markpoint_coord"], chart_data["other3_markpoint_value"])
                     ] + \
                     [
                         opts.MarkPointItem(name="每日日線布林條件", coord=coord, value=value, symbol='pin',
                                            symbol_size=[35, 20],
                                            itemstyle_opts=opts.ItemStyleOpts(color='rgb(0,255,255)'))
                         for coord, value in zip(chart_data["other4_markpoint_coord"], chart_data["other4_markpoint_value"])
                     ]
    kline = (Kline().add_xaxis(
        xaxis_data=chart_data["categoryData"]).add_yaxis(
            series_name="Dow-Jones index",
            y_axis=kline_data,
            itemstyle_opts=opts.ItemStyleOpts(color="#ec0000",
                                              color0="#00da3c"),
            markpoint_opts=opts.MarkPointOpts(data=markpoint_item),
        ).set_global_opts(
            legend_opts=opts.LegendOpts(is_show=False,
                                        pos_bottom=10,
                                        pos_left="center"),
            datazoom_opts=[
                opts.DataZoomOpts(
                    is_show=False,
                    type_="inside",
                    xaxis_index=[0, 1],
                    range_start=98,
                    range_end=100,
                ),
                opts.DataZoomOpts(
                    is_show=True,
                    xaxis_index=[0, 1],
                    type_="slider",
                    pos_top="85%",
                    range_start=98,
                    range_end=100,
                ),
            ],
            yaxis_opts=opts.AxisOpts(
                is_scale=True,
                splitarea_opts=opts.SplitAreaOpts(
                    is_show=True,
                    areastyle_opts=opts.AreaStyleOpts(opacity=1)),
            ),
            tooltip_opts=opts.TooltipOpts(
                trigger="axis",
                axis_pointer_type="cross",
                background_color="rgba(245, 245, 245, 0.8)",
                border_width=1,
                border_color="#ccc",
                textstyle_opts=opts.TextStyleOpts(color="#000"),
            ),
            visualmap_opts=opts.VisualMapOpts(
                is_show=False,
                dimension=2,
                series_index=5,
                is_piecewise=True,
                pieces=[
                    {
                        "value": 1,
                        "color": "#00da3c"
                    },
                    {
                        "value": -1,
                        "color": "#ec0000"
                    },
                ],
            ),
            axispointer_opts=opts.AxisPointerOpts(
                is_show=True,
                link=[{
                    "xAxisIndex": "all"
                }],
                label=opts.LabelOpts(background_color="#777"),
            ),
            brush_opts=opts.BrushOpts(
                x_axis_index="all",
                brush_link="all",
                out_of_brush={"colorAlpha": 0.1},
                brush_type="lineX",
            ),
        ))

    line = (Line().add_xaxis(xaxis_data=chart_data["categoryData"]).add_yaxis(
        series_name="MA5",
        y_axis=calculate_ma(day_count=5, data=chart_data),
        is_smooth=True,
        is_hover_animation=False,
        linestyle_opts=opts.LineStyleOpts(width=3, opacity=0.5),
        label_opts=opts.LabelOpts(is_show=False),
    ).add_yaxis(
        series_name="MA10",
        y_axis=calculate_ma(day_count=10, data=chart_data),
        is_smooth=True,
        is_hover_animation=False,
        linestyle_opts=opts.LineStyleOpts(width=3, opacity=0.5),
        label_opts=opts.LabelOpts(is_show=False),
    ).add_yaxis(
        series_name="MA20",
        y_axis=calculate_ma(day_count=20, data=chart_data),
        is_smooth=True,
        is_hover_animation=False,
        linestyle_opts=opts.LineStyleOpts(width=3, opacity=0.5),
        label_opts=opts.LabelOpts(is_show=False),
    ).add_yaxis(
        series_name="MA30",
        y_axis=calculate_ma(day_count=30, data=chart_data),
        is_smooth=True,
        is_hover_animation=False,
        linestyle_opts=opts.LineStyleOpts(width=3, opacity=0.5),
        label_opts=opts.LabelOpts(is_show=False),
    ).set_global_opts(xaxis_opts=opts.AxisOpts(type_="category")))

    bar = (Bar().add_xaxis(xaxis_data=chart_data["categoryData"]).add_yaxis(
        series_name="Volume",
        y_axis=chart_data["volumes"],
        xaxis_index=1,
        yaxis_index=1,
        label_opts=opts.LabelOpts(is_show=False),
    ).set_global_opts(
        xaxis_opts=opts.AxisOpts(
            type_="category",
            is_scale=True,
            grid_index=1,
            boundary_gap=False,
            axisline_opts=opts.AxisLineOpts(is_on_zero=False),
            axistick_opts=opts.AxisTickOpts(is_show=False),
            splitline_opts=opts.SplitLineOpts(is_show=False),
            axislabel_opts=opts.LabelOpts(is_show=False),
            split_number=20,
            min_="dataMin",
            max_="dataMax",
        ),
        yaxis_opts=opts.AxisOpts(
            grid_index=1,
            is_scale=True,
            split_number=2,
            axislabel_opts=opts.LabelOpts(is_show=False),
            axisline_opts=opts.AxisLineOpts(is_show=False),
            axistick_opts=opts.AxisTickOpts(is_show=False),
            splitline_opts=opts.SplitLineOpts(is_show=False),
        ),
        legend_opts=opts.LegendOpts(is_show=False),
    ))

    # Kline And Line
    overlap_kline_line = kline.overlap(line)

    # Grid Overlap + Bar
    grid_chart = Grid(init_opts=opts.InitOpts(
        width="1000px",
        height="800px",
        animation_opts=opts.AnimationOpts(animation=False),
    ))
    grid_chart.add(
        overlap_kline_line,
        grid_opts=opts.GridOpts(pos_left="10%", pos_right="8%", height="50%"),
    )
    grid_chart.add(
        bar,
        grid_opts=opts.GridOpts(pos_left="10%",
                                pos_right="8%",
                                pos_top="63%",
                                height="16%"),
    )

    return grid_chart.render_embed()