pyecharts draw K line

        Recently I want to expand it vnpy, optimize the performance of some of the features and code. Looking backtesting part of the code, I found, vnpy actually quite weak back-tested function, you can expand it yourself. And it was followed by a back-tested results visualization problems. Backtesting results vnpy native k-line is not drawn, so there is no visual indicators of visual and closing the position, and only then visualize the results of the transaction. In fact, I own a little bit not used to visualize the results of backtesting strategies did not see, a little bit unhappy, so I plan to do it yourself. First is the choice of visualization tools, pyecharts should be a choice, and now the development of better and better.

        Well, first of all to try k line pyechats official part of the code.

        The following code from the official website oh

http://pyecharts.org

from pyecharts import Kline

v1 = [[2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38],
      [2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8],
      [2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82],
      [2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38],
      [2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73],
      [2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03],
      [2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07],
      [2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82],
      [2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78],
      [2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63],
      [2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65],
      [2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14],
      [2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02],
      [2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96],
      [2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33],
      [2255.77, 2270.28, 2253.31, 2276.22]]
kline = Kline("K 线图示例")
kline.add("日K", ["2017/7/{}".format(i + 1) for i in range(31)], v1)
kline.render()

        First, k is defined as a line which pyechats Kline class. Here, first written data k lines, v1. We note that the structure of the data is a list of lists, each list which is open close low high, which is oclh format. We do very much in line quantify style.

        Once you have the data line is an example of an object k, a pass name of the image on it. The key then is to add method, in fact, to the image data is inserted inside, we can add a look of realization:

    def __add(self, name, x_axis, y_axis, **kwargs):
        """

        :param name:
            系列名称,用于 tooltip 的显示,legend 的图例筛选。
        :param x_axis:
            x 坐标轴数据。
        :param y_axis:
            y 坐标轴数据。数据中,每一行是一个『数据项』,每一列属于一个『维度』。
            数据项具体为 [open, close, lowest, highest] (即:[开盘值, 收盘值,
             最低值, 最高值])。
        :param kwargs:
        """

        In Kline inside the class, add in fact called __add, which, name parameter is actually a legend, and then the data x coordinate, y coordinate data.

        Then you can get the render method is the result of a html format, open it with a browser.

Something like the following:

Pictures from pyecharts official website.

        In addition you can also add some other settings that are set by add some keywords to complete the process, such as:

kline.add(
    "日K",
    ["2017/7/{}".format(i + 1) for i in range(31)],
    v1,
    mark_point=["max"],
    is_datazoom_show=True,
)

        Here, we have found in a set mark_point and is_datazoom_show, wherein, mark_point is used to mark a maximum value, and is used to mark whether is_datazoom_show has a telescopic function of the coordinate axes.

        同样的,坐标轴伸缩方向可以通过datazoom_orient来设置:

kline.add(
    "日K",
    ["2017/7/{}".format(i + 1) for i in range(31)],
    v1,
    mark_point=["max"],
    is_datazoom_show=True,
    datazoom_orient="vertical",
)

        此外,还可以在图上画一些别的线,比如close价的最大值。

kline.add(
    "日K",
    ["2017/7/{}".format(i + 1) for i in range(31)],
    v1,
    mark_line=["max"],
    mark_line_symbolsize=0,
    datazoom_orient="vertical",
    mark_line_valuedim="close",
)

        那么,接下来我们来看一下如何改进这个k先的绘制方法吧,我们从一个pandas开始。后面的代码就是笔者自己写的哦。

        我们现在有一个pandas, 里面的数据如下:

        还有一列ma10没有放上去,其实就是5日均线和10日均线。

def backtesting_plot(table_name, indicator_name_list):
    # data preparation
    da = pd.DataFrame(data=table_name)
    da['volume'] = da['volume'].apply(lambda vol: vol if vol > 0 else 0)
    date = da["datetime"].apply(lambda x: str(x)).tolist()
    k_plot_value = da.apply(lambda record: [record['open'], record['close'], record['low'], record['high']], axis=1).tolist()
    
    # K chart
    kline = Kline()
    kline.add("Backtesting Result", date, k_plot_value)
    
    indicator_lines = Line()
    for indicator_name in indicator_name_list:
        indicator_lines.add(indicator_name, date, da[indicator_name].tolist())
    # trading volume bar chart
    bar = Bar()
    bar.add("volume", date, da["volume"],
            tooltip_tragger="axis", is_legend_show=False, is_yaxis_show=False, yaxis_max=5*max(da["volume"]))
    # buy and sell
    v1 = date[10]
    v2 = da['high'].iloc[10]
    es = EffectScatter("buy")
    es.add("buy", [v1], [v2])
    v1 = date[18]
    v2 = da['high'].iloc[18]
    es.add( "sell",  [v1],  [v2], symbol="pin",)
    
    overlap = Overlap()
    overlap.add(kline)
    overlap.add(indicator_lines,)
    overlap.add(bar,yaxis_index=1, is_add_yaxis=True)
    overlap.add(es)
    overlap.render(path='tt.html')

        我们看一下上面这个函数,首先我们从pandas中拿出数据,转换成pyecharts能接受的list格式。要提醒大家的是,这里的datetime也要转化成字符串格式。

        然后就是实例化Kline和技术指标的Line

# K chart
    kline = Kline()
    kline.add("Backtesting Result", date, k_plot_value)
    
    indicator_lines = Line()
    for indicator_name in indicator_name_list:
        indicator_lines.add(indicator_name, date, da[indicator_name].tolist())

        然后用bar来制作成交量。这样的话基本就形成了。

        但是我们进一步希望能够在k线图上绘制出买卖信号发生的信息,也就是交易发出的时间点,那么我们用es来添加,这里随便使用了10和18天作为一个买卖时间点。

        我们做了这么多的图怎么让他们一起显示出来呢?这里就要用到overlap了,也就是叠加的类。

    overlap = Overlap()
    overlap.add(kline)
    overlap.add(indicator_lines,)
    overlap.add(bar,yaxis_index=1, is_add_yaxis=True)
    overlap.add(es)
    overlap.render(path='tt.html')

        我们最后来看一下结果怎么样:

 

        可以说相当漂亮了。笔者圈起来的就是我们绘制的时候设置的buy和sell两个点。

发布了205 篇原创文章 · 获赞 236 · 访问量 98万+

Guess you like

Origin blog.csdn.net/qtlyx/article/details/85221503