{"id":251,"date":"2024-04-23T14:46:26","date_gmt":"2024-04-23T06:46:26","guid":{"rendered":"https:\/\/blog.alltick.co\/?p=251"},"modified":"2025-04-28T15:21:21","modified_gmt":"2025-04-28T07:21:21","slug":"from-tick-data-to-kline-chart","status":"publish","type":"post","link":"https:\/\/blog.alltick.co\/zh-CN\/from-tick-data-to-kline-chart\/","title":{"rendered":"\u4eceTick\u5230K\u7ebf\uff1a\u6570\u636e\u8f6c\u6362\u548c\u8ba1\u7b97\u65b9\u6cd5"},"content":{"rendered":"\n<p>K\u7ebf\u56fe\u662f\u80a1\u7968\u5e02\u573a\u548c\u91d1\u878d\u4ea4\u6613\u4e2d\u5e38\u7528\u7684\u56fe\u8868\u7c7b\u578b\uff0c\u7528\u4e8e\u5c55\u793a\u4e00\u6bb5\u65f6\u95f4\u5185\u7684\u5f00\u76d8\u4ef7\u3001\u6700\u9ad8\u4ef7\u3001\u6700\u4f4e\u4ef7\u548c\u6536\u76d8\u4ef7\u7b49\u4fe1\u606f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5c06\u5b9e\u65f6Tick\u6570\u636e\u8f6c\u6362\u4e3a\u4e0d\u540c\u5468\u671f\u7684K\u7ebf\u6570\u636e\uff0c\u5e76\u63d0\u4f9b\u5fc5\u8981\u7684\u516c\u5f0f\u548c\u8ba1\u7b97\u65b9\u6cd5\uff0c\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u5206\u6790\u548c\u7406\u89e3\u5e02\u573a\u8d70\u52bf\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001K\u7ebf\u56fe\u7b80\u4ecb<\/strong><\/h2>\n\n\n\n<p>K\u7ebf\u56fe\u7531\u4e00\u7cfb\u5217\u8fde\u7eed\u7684\u77e9\u5f62\u6846\u7ec4\u6210\uff0c\u6bcf\u4e2a\u77e9\u5f62\u6846\u4ee3\u8868\u4e00\u6bb5\u65f6\u95f4\u5185\u7684\u4ef7\u683c\u8d70\u52bf\u3002\u6bcf\u4e2a\u77e9\u5f62\u6846\u901a\u5e38\u5305\u542b\u56db\u4e2a\u5173\u952e\u4ef7\u683c\u70b9\uff1a\u5f00\u76d8\u4ef7\uff08Open\uff09\u3001\u6700\u9ad8\u4ef7\uff08High\uff09\u3001\u6700\u4f4e\u4ef7\uff08Low\uff09\u548c\u6536\u76d8\u4ef7\uff08Close\uff09\u3002K\u7ebf\u56fe\u7684\u5f62\u6001\u548c\u7ec4\u5408\u53ef\u4ee5\u5e2e\u52a9\u5206\u6790\u5e08\u548c\u4ea4\u6613\u8005\u8bc6\u522b\u5e02\u573a\u8d8b\u52bf\u548c\u4ef7\u683c\u6a21\u5f0f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e8c\u3001\u5b9e\u65f6Tick\u6570\u636e<\/strong><\/h2>\n\n\n\n<p>\u5b9e\u65f6Tick\u6570\u636e\u662f\u6307\u5e02\u573a\u4e2d\u6bcf\u6b21\u4ea4\u6613\u7684\u4ef7\u683c\u548c\u6210\u4ea4\u91cf\u4fe1\u606f\u3002\u5b83\u63d0\u4f9b\u4e86\u5173\u4e8e\u5e02\u573a\u6d41\u52a8\u6027\u548c\u4ef7\u683c\u53d8\u52a8\u7684\u5373\u65f6\u53cd\u9988\u3002\u5b9e\u65f6Tick\u6570\u636e\u901a\u5e38\u5305\u542b\u65f6\u95f4\u6233\u3001\u6210\u4ea4\u4ef7\u3001\u6210\u4ea4\u91cf\u7b49\u5b57\u6bb5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e09\u3001K\u7ebf\u6570\u636e\u8ba1\u7b97\u65b9\u6cd5<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u65f6\u95f4\u7a97\u53e3\u8bbe\u5b9a \u9996\u5148\uff0c\u9700\u8981\u786e\u5b9aK\u7ebf\u56fe\u7684\u5468\u671f\uff0c\u5373\u6bcf\u4e2aK\u7ebf\u4ee3\u8868\u7684\u65f6\u95f4\u6bb5\uff0c\u4f8b\u59821\u5206\u949f\u300115\u5206\u949f\u30011\u5c0f\u65f6\u7b49\u3002\u8fd9\u51b3\u5b9a\u4e86\u6211\u4eec\u5728\u5b9e\u65f6Tick\u6570\u636e\u4e2d\u53d6\u6837\u7684\u65f6\u95f4\u95f4\u9694\u3002<\/li>\n\n\n\n<li>K\u7ebf\u6570\u636e\u8ba1\u7b97 \u5728\u6bcf\u4e2a\u65f6\u95f4\u7a97\u53e3\u5185\uff0c\u6839\u636e\u5b9e\u65f6Tick\u6570\u636e\u8ba1\u7b97K\u7ebf\u6570\u636e\u7684\u5404\u4e2a\u4ef7\u683c\u70b9\u3002<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f00\u76d8\u4ef7\uff08Open\uff09\uff1a\u65f6\u95f4\u7a97\u53e3\u7684\u7b2c\u4e00\u4e2aTick\u7684\u4ef7\u683c\u3002<\/li>\n\n\n\n<li>\u6700\u9ad8\u4ef7\uff08High\uff09\uff1a\u65f6\u95f4\u7a97\u53e3\u5185\u7684\u6700\u9ad8\u4ef7\u683c\u3002<\/li>\n\n\n\n<li>\u6700\u4f4e\u4ef7\uff08Low\uff09\uff1a\u65f6\u95f4\u7a97\u53e3\u5185\u7684\u6700\u4f4e\u4ef7\u683c\u3002<\/li>\n\n\n\n<li>\u6536\u76d8\u4ef7\uff08Close\uff09\uff1a\u65f6\u95f4\u7a97\u53e3\u7684\u6700\u540e\u4e00\u4e2aTick\u7684\u4ef7\u683c\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u66f4\u65b0K\u7ebf\u6570\u636e \u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\uff0c\u6bcf\u4e2a\u65f6\u95f4\u7a97\u53e3\u90fd\u4f1a\u6ed1\u52a8\uff0c\u65b0\u7684Tick\u6570\u636e\u8fdb\u5165\u8ba1\u7b97\u8303\u56f4\u3002\u5728\u6bcf\u4e2a\u65f6\u95f4\u7a97\u53e3\u7ed3\u675f\u65f6\uff0c\u6839\u636e\u65b0\u7684Tick\u6570\u636e\u66f4\u65b0K\u7ebf\u6570\u636e\u3002<\/li>\n\n\n\n<li>\u5176\u4ed6\u6307\u6807\u8ba1\u7b97 \u9664\u4e86\u5f00\u76d8\u4ef7\u3001\u6700\u9ad8\u4ef7\u3001\u6700\u4f4e\u4ef7\u548c\u6536\u76d8\u4ef7\uff0c\u60a8\u8fd8\u53ef\u4ee5\u6839\u636eK\u7ebf\u6570\u636e\u8ba1\u7b97\u5176\u4ed6\u6307\u6807\uff0c\u5982\u6210\u4ea4\u91cf\u3001\u5747\u7ebf\u7b49\u3002\u8fd9\u4e9b\u6307\u6807\u53ef\u4ee5\u63d0\u4f9b\u66f4\u6df1\u5165\u7684\u5e02\u573a\u5206\u6790\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u56db\u3001\u793a\u4f8b\u516c\u5f0f<\/strong><\/h2>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5c06\u5b9e\u65f6Tick\u6570\u636e\u8f6c\u6362\u4e3a1\u5206\u949fK\u7ebf\u6570\u636e\u7684\u8ba1\u7b97\u516c\u5f0f\uff1a<\/p>\n\n\n\n<p>1. \u521d\u59cb\u5316K\u7ebf\u6570\u636e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u521d\u59cb\u5f00\u76d8\u4ef7\uff08Initial Open\uff09\uff1a\u7b2c\u4e00\u4e2aTick\u7684\u4ef7\u683c<\/li>\n\n\n\n<li>\u521d\u59cb\u6700\u9ad8\u4ef7\uff08Initial High\uff09\uff1a\u7b2c\u4e00\u4e2aTick\u7684\u4ef7\u683c<\/li>\n\n\n\n<li>\u521d\u59cb\u6700\u4f4e\u4ef7\uff08Initial Low\uff09\uff1a\u7b2c\u4e00\u4e2aTick\u7684\u4ef7\u683c<\/li>\n\n\n\n<li>\u521d\u59cb\u6536\u76d8\u4ef7\uff08Initial Close\uff09\uff1a\u7b2c\u4e00\u4e2aTick\u7684\u4ef7\u683c<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<p>2. \u66f4\u65b0K\u7ebf\u6570\u636e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u5f53\u524dTick\u7684\u65f6\u95f4\u6233\u4e0d\u5728\u5f53\u524d\u65f6\u95f4\u7a97\u53e3\u5185\uff0c\u8868\u793a\u65f6\u95f4\u7a97\u53e3\u7ed3\u675f\uff0c\u9700\u8981\u66f4\u65b0K\u7ebf\u6570\u636e\u3002<\/li>\n\n\n\n<li>\u66f4\u65b0K\u7ebf\u6570\u636e\u7684\u5f00\u76d8\u4ef7\uff08Open\uff09\uff1a\u4e0a\u4e00\u4e2a\u65f6\u95f4\u7a97\u53e3\u7684\u6536\u76d8\u4ef7\uff08Previous Close\uff09<\/li>\n\n\n\n<li>\u66f4\u65b0K\u7ebf\u6570\u636e\u7684\u6700\u9ad8\u4ef7\uff08High\uff09\uff1a\u65f6\u95f4\u7a97\u53e3\u5185\u7684\u6700\u9ad8\u4ef7\u683c<\/li>\n\n\n\n<li>\u66f4\u65b0K\u7ebf\u6570\u636e\u7684\u6700\u4f4e\u4ef7\uff08Low\uff09\uff1a\u65f6\u95f4\u7a97\u53e3\u5185\u7684\u6700\u4f4e\u4ef7\u683c<\/li>\n\n\n\n<li>\u66f4\u65b0K\u7ebf\u6570\u636e\u7684\u6536\u76d8\u4ef7\uff08Close\uff09\uff1a\u5f53\u524dTick\u7684\u4ef7\u683c<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><\/li>\n<\/ol>\n\n\n\n<p>\u4ee5\u4e0a\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff0c\u5b9e\u9645\u5e94\u7528\u4e2d\u53ef\u80fd\u9700\u8981\u8003\u8651\u66f4\u591a\u7684\u7ec6\u8282\u548c\u6570\u636e\u5904\u7406\u65b9\u6cd5\uff0c\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>JAVA\u4ee3\u7801\u793a\u4f8b<\/strong><\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.ArrayList;\nimport java.util.List;\n\npublic class KLineGenerator {\n    public static List&lt;KLineData> generateKLineData(List&lt;TickData> tickDataList, int interval) {\n        List&lt;KLineData> kLineDataList = new ArrayList&lt;>();\n        long startTime = tickDataList.get(0).getTime(); \/\/ \u83b7\u53d6\u7b2c\u4e00\u4e2aTick\u7684\u65f6\u95f4\u4f5c\u4e3a\u8d77\u59cb\u65f6\u95f4\n\n        \/\/ \u521d\u59cb\u5316K\u7ebf\u6570\u636e\n        long currentKLineStartTime = startTime - (startTime % interval);\n        double openPrice = tickDataList.get(0).getPrice();\n        double highPrice = openPrice;\n        double lowPrice = openPrice;\n        double closePrice = 0;\n        long volume = 0;\n\n        \/\/ \u904d\u5386Tick\u6570\u636e\n        for (TickData tickData : tickDataList) {\n            long currentTime = tickData.getTime();\n            double currentPrice = tickData.getPrice();\n            long currentVolume = tickData.getVolume();\n\n            \/\/ \u5982\u679c\u8d85\u8fc7\u5f53\u524dK\u7ebf\u7684\u65f6\u95f4\u5468\u671f\uff0c\u751f\u6210\u65b0\u7684K\u7ebf\u6570\u636e\n            if (currentTime >= currentKLineStartTime + interval) {\n                closePrice = tickDataList.get(tickDataList.indexOf(tickData) - 1).getPrice(); \/\/ \u4e0a\u4e00\u4e2aTick\u7684\u4ef7\u683c\u4f5c\u4e3a\u6536\u76d8\u4ef7\n\n                KLineData kLineData = new KLineData(currentKLineStartTime, openPrice, highPrice, lowPrice, closePrice, volume);\n                kLineDataList.add(kLineData);\n\n                \/\/ \u91cd\u7f6eK\u7ebf\u6570\u636e\u4e3a\u65b0\u7684\u65f6\u95f4\u5468\u671f\n                currentKLineStartTime += interval;\n                openPrice = currentPrice;\n                highPrice = currentPrice;\n                lowPrice = currentPrice;\n                volume = currentVolume;\n            } else {\n                \/\/ \u66f4\u65b0K\u7ebf\u6570\u636e\n                highPrice = Math.max(highPrice, currentPrice);\n                lowPrice = Math.min(lowPrice, currentPrice);\n                volume += currentVolume;\n            }\n        }\n\n        \/\/ \u6700\u540e\u4e00\u4e2aTick\u4f5c\u4e3aK\u7ebf\u6570\u636e\u7684\u6536\u76d8\u4ef7\n        closePrice = tickDataList.get(tickDataList.size() - 1).getPrice();\n\n        \/\/ \u751f\u6210\u6700\u540e\u4e00\u4e2aK\u7ebf\u6570\u636e\n        KLineData lastKLineData = new KLineData(currentKLineStartTime, openPrice, highPrice, lowPrice, closePrice, volume);\n        kLineDataList.add(lastKLineData);\n\n        return kLineDataList;\n    }\n}\n\n\/\/ Tick\u6570\u636e\u7c7b\nclass TickData {\n    private long time;\n    private double price;\n    private long volume;\n\n    public TickData(long time, double price, long volume) {\n        this.time = time;\n        this.price = price;\n        this.volume = volume;\n    }\n\n    public long getTime() {\n        return time;\n    }\n\n    public double getPrice() {\n        return price;\n    }\n\n    public long getVolume() {\n        return volume;\n    }\n}\n\n\/\/ K\u7ebf\u6570\u636e\u7c7b\nclass KLineData {\n    private long time;\n    private double open;\n    private double high;\n    private double low;\n    private double close;\n    private long volume;\n\n    public KLineData(long time, double open, double high, double low, double close, long volume) {\n        this.time = time;\n        this.open = open;\n        this.high = high;\n        this.low = low;\n        this.close = close;\n        this.volume = volume;\n    }\n\n    \/\/ \u7701\u7565\u4e86getter\u548csetter\u65b9\u6cd5\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Python\u4ee3\u7801\u793a\u4f8b<\/strong><\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from datetime import datetime, timedelta\n\nclass TickData:\n    def __init__(self, time, price, volume):\n        self.time = time\n        self.price = price\n        self.volume = volume\n\nclass KLineData:\n    def __init__(self, time, open_price, high_price, low_price, close_price, volume):\n        self.time = time\n        self.open_price = open_price\n        self.high_price = high_price\n        self.low_price = low_price\n        self.close_price = close_price\n        self.volume = volume\n\ndef generate_kline_data(tick_data_list, interval):\n    kline_data_list = []\n    start_time = tick_data_list[0].time\n    current_kline_start_time = start_time - (start_time % interval)\n    open_price = tick_data_list[0].price\n    high_price = open_price\n    low_price = open_price\n    close_price = 0\n    volume = 0\n\n    for tick_data in tick_data_list:\n        current_time = tick_data.time\n        current_price = tick_data.price\n        current_volume = tick_data.volume\n\n        if current_time >= current_kline_start_time + interval:\n            close_price = tick_data_list[tick_data_list.index(tick_data) - 1].price\n            kline_data = KLineData(current_kline_start_time, open_price, high_price, low_price, close_price, volume)\n            kline_data_list.append(kline_data)\n\n            current_kline_start_time += interval\n            open_price = current_price\n            high_price = current_price\n            low_price = current_price\n            volume = current_volume\n        else:\n            high_price = max(high_price, current_price)\n            low_price = min(low_price, current_price)\n            volume += current_volume\n\n    close_price = tick_data_list[-1].price\n    last_kline_data = KLineData(current_kline_start_time, open_price, high_price, low_price, close_price, volume)\n    kline_data_list.append(last_kline_data)\n\n    return kline_data_list<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>K\u7ebf\u56fe\u662f\u80a1\u7968\u5e02\u573a\u548c\u91d1\u878d\u4ea4\u6613\u4e2d\u5e38\u7528\u7684\u56fe\u8868\u7c7b\u578b\uff0c\u7528\u4e8e\u5c55\u793a\u4e00\u6bb5\u65f6\u95f4\u5185\u7684\u5f00\u76d8\u4ef7\u3001\u6700\u9ad8\u4ef7\u3001\u6700\u4f4e\u4ef7\u548c\u6536\u76d8\u4ef7\u7b49\u4fe1\u606f\u3002\u672c\u6587\u5c06\u4ecb&#8230;<\/p>\n","protected":false},"author":1,"featured_media":254,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-251","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tick-data-wiki"],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/posts\/251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/comments?post=251"}],"version-history":[{"count":3,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/posts\/251\/revisions"}],"predecessor-version":[{"id":10426,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/posts\/251\/revisions\/10426"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/media\/254"}],"wp:attachment":[{"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/media?parent=251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/categories?post=251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/tags?post=251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}