{"id":306,"date":"2024-04-28T10:38:52","date_gmt":"2024-04-28T02:38:52","guid":{"rendered":"https:\/\/blog.alltick.co\/?p=306"},"modified":"2025-04-23T16:16:28","modified_gmt":"2025-04-23T08:16:28","slug":"python-for-trading-how-to-import-tick-data","status":"publish","type":"post","link":"https:\/\/blog.alltick.co\/zh-CN\/python-for-trading-how-to-import-tick-data\/","title":{"rendered":"Python\u91cf\u5316\u4ea4\u6613\uff1a\u5982\u4f55\u63a5\u5165\u91d1\u878d\u884c\u60c5\u6570\u636e\uff1f"},"content":{"rendered":"\n<p>\u672c\u6587\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u8c03\u7528\u5df2\u7ecf\u5c01\u88c5\u597d\u7684\u9ad8\u9891\u6570\u636eAPI\u3002\u8fd9\u91cc\u4ee5Alltick\u7684tick\u6570\u636e\u63a5\u53e3\u4f5c\u4e3a\u6f14\u793a\u3002\u4e0b\u9762\u662f\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u8bf7\u6c42K\u7ebf\u6570\u636e<\/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=\"\">import time\nimport requests\nimport json\n \n# Extra headers\ntest_headers = {\n    'Content-Type':'application\/json'\n}\n \n'''\ngithub\uff1ahttps:\/\/github.com\/alltick\/realtime-forex-crypto-stock-tick-finance-websocket-api\n\u7533\u8bf7\u514d\u8d39token\uff1ahttps:\/\/alltick.co\/register\n\u5b98\u7f51\uff1ahttps:\/\/alltick.co\n\u5c06\u5982\u4e0bJSON\u8fdb\u884curl\u7684encode\uff0c\u590d\u5236\u5230http\u7684\u67e5\u8be2\u5b57\u7b26\u4e32\u7684query\u5b57\u6bb5\u91cc\n{\"trace\":\"python_http_test1\",\"data\":{\"code\":\"AAPL.US\",\"kline_type\":1,\"kline_timestamp_end\":0,\"query_kline_num\":2,\"adjust_type\":0}}\n'''\ntest_url1 = 'https:\/\/quote.alltick.io\/quote-stock-b-api\/kline?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806&amp;query=%7B%22trace%22%3A%22python_http_test1%22%2C%22data%22%3A%7B%22code%22%3A%22AAPL.US%22%2C%22kline_type%22%3A1%2C%22kline_timestamp_end%22%3A0%2C%22query_kline_num%22%3A2%2C%22adjust_type%22%3A0%7D%7D'\n \nresp1 = requests.get(url=test_url1, headers=test_headers)\n \n# Decoded text returned by the request\ntext1 = resp1.text\nprint(text1)<\/pre>\n\n\n\n<p>\u4e0a\u9762\u4ee3\u7801\u4e2d\u662f\u4ee5\u67e5\u8be2\u82f9\u679c\u80a1\u7968(AAPL.US)\u5206\u949fK\u7ebf\u4e3a\u4f8b\u5b50\u7684,\u5982\u679c\u60f3\u67e5\u8be2\u5176\u5b83\u7c7b\u578b\u7684K\u7ebf\u6570\u636e\u5219kline_type\u4f20\u5165\u4ee5\u4e0b\u503c\uff1a1-\u5206\u949fK\uff0c2-\u4e3a5\u5206\u949fK\uff0c3-\u4e3a15\u5206\u949fK\uff0c4-\u4e3a30\u5206\u949fK\uff0c5-\u4e3a\u5c0f\u65f6K\uff0c6-\u4e3a2\u5c0f\u65f6K\uff0c7-\u4e3a4\u5c0f\u65f6K\uff0c8-\u4e3a\u65e5K\uff0c9-\u4e3a\u5468K\uff0c10-\u4e3a\u6708K\u3002&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u8bf7\u6c42\u6700\u65b0\u62a5\u4ef7\u6210\u4ea4\u6570\u636e<\/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=\"\">import time\nimport requests\nimport json\n \n# Extra headers\ntest_headers = {\n    'Content-Type':'application\/json'\n}\n \n'''\ngithub\uff1ahttps:\/\/github.com\/alltick\/realtime-forex-crypto-stock-tick-finance-websocket-api\n\u7533\u8bf7\u514d\u8d39token\uff1ahttps:\/\/alltick.co\/register\n\u5b98\u7f51\uff1ahttps:\/\/alltick.co\n\u5c06\u5982\u4e0bJSON\u8fdb\u884curl\u7684encode\uff0c\u590d\u5236\u5230http\u7684\u67e5\u8be2\u5b57\u7b26\u4e32\u7684query\u5b57\u6bb5\u91cc\n{\"trace\":\"python_http_test2\",\"data\":{\"symbol_list\":[{\"code\": \"700.HK\"},{\"code\": \"UNH.US\"},{\"code\": \"600416.SH\"}]}}\n'''\ntest_url1 = 'https:\/\/quote.alltick.io\/quote-stock-b-api\/trade-tick?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806&amp;query=%7B%22trace%22%3A%22python_http_test2%22%2C%22data%22%3A%7B%22symbol_list%22%3A%5B%7B%22code%22%3A%20%22700.HK%22%7D%2C%7B%22code%22%3A%20%22UNH.US%22%7D%2C%7B%22code%22%3A%20%22600416.SH%22%7D%5D%7D%7D'\n \nresp1 = requests.get(url=test_url1, headers=test_headers)\n \n# Decoded text returned by the request\ntext1 = resp1.text\nprint(text1)<\/pre>\n\n\n\n<p>\u4e0a\u9762\u4ee3\u7801\u4e2dsymbol_list\u662f\u53ef\u4ee5\u540c\u65f6\u4f20\u5165\u591a\u4e2a\u7684,\u5206\u522b\u4f20\u5165\u4e0d\u540c\u7684\u5e02\u573a\u7684\u4ea7\u54c1\u4e5f\u662f\u53ef\u4ee5\u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u83b7\u53d6\u6700\u65b0\u76d8\u53e3\u62a5\u4ef7\u6570\u636e<\/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=\"\">import time\nimport requests\nimport json\n \n# Extra headers\ntest_headers = {\n    'Content-Type':'application\/json'\n}\n \n'''\ngithub\uff1ahttps:\/\/github.com\/alltick\/realtime-forex-crypto-stock-tick-finance-websocket-api\n\u7533\u8bf7\u514d\u8d39token\uff1ahttps:\/\/alltick.co\/register\n\u5b98\u7f51\uff1ahttps:\/\/alltick.co\n\u5c06\u5982\u4e0bJSON\u8fdb\u884curl\u7684encode\uff0c\u590d\u5236\u5230http\u7684\u67e5\u8be2\u5b57\u7b26\u4e32\u7684query\u5b57\u6bb5\u91cc\n{\"trace\":\"python_http_test2\",\"data\":{\"symbol_list\":[{\"code\": \"700.HK\"},{\"code\": \"UNH.US\"},{\"code\": \"600416.SH\"}]}}\n'''\ntest_url1 = 'https:\/\/quote.alltick.io\/quote-stock-b-api\/depth-tick?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806&amp;query=%7B%22trace%22%3A%22python_http_test2%22%2C%22data%22%3A%7B%22symbol_list%22%3A%5B%7B%22code%22%3A%20%22700.HK%22%7D%2C%7B%22code%22%3A%20%22UNH.US%22%7D%2C%7B%22code%22%3A%20%22600416.SH%22%7D%5D%7D%7D'\n \nresp1 = requests.get(url=test_url1, headers=test_headers)\n \n# Decoded text returned by the request\ntext1 = resp1.text\nprint(text1)<\/pre>\n\n\n\n<p>\u540c\u4e0a\uff0csymbol_list\u652f\u6301\u4f20\u5165\u591a\u4e2a\u503c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u901a\u8fc7Websocket\u8ba2\u9605\u5b9e\u65f6\u884c\u60c5\u6570\u636e<\/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=\"\">import json\nimport websocket    # pip install websocket-client\n \n'''\ngithub\uff1ahttps:\/\/github.com\/alltick\/realtime-forex-crypto-stock-tick-finance-websocket-api\n\u7533\u8bf7\u514d\u8d39token\uff1ahttps:\/\/alltick.co\/register\n\u5b98\u7f51\uff1ahttps:\/\/alltick.co\n'''\n \nclass Feed(object):\n \n    def __init__(self):\n        self.url = 'wss:\/\/quote.alltick.io\/quote-stock-b-ws-api?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806'  # \u8fd9\u91cc\u8f93\u5165websocket\u7684url\n        self.ws = None\n \n    def on_open(self, ws):\n        \"\"\"\n        Callback object which is called at opening websocket.\n        1 argument:\n        @ ws: the WebSocketApp object\n        \"\"\"\n        print('A new WebSocketApp is opened!')\n \n        # \u5f00\u59cb\u8ba2\u9605\uff08\u4e3e\u4e2a\u4f8b\u5b50\uff09\n        sub_param = {\n            \"cmd_id\": 22002, \n            \"seq_id\": 123,\n            \"trace\":\"3baaa938-f92c-4a74-a228-fd49d5e2f8bc-1678419657806\",\n            \"data\":{\n                \"symbol_list\":[\n                    {\n                        \"code\": \"700.HK\",\n                        \"depth_level\": 5,\n                    },\n                    {\n                        \"code\": \"UNH.US\",\n                        \"depth_level\": 5,\n                    },\n                    {\n                        \"code\": \"600416.SH\",\n                        \"depth_level\": 5,\n                    }\n                ]\n            }\n        }\n        \n        #\u5982\u679c\u5e0c\u671b\u957f\u65f6\u95f4\u8fd0\u884c\uff0c\u9664\u4e86\u9700\u8981\u53d1\u9001\u8ba2\u9605\u4e4b\u5916\uff0c\u8fd8\u9700\u8981\u4fee\u6539\u4ee3\u7801\uff0c\u5b9a\u65f6\u53d1\u9001\u5fc3\u8df3\uff0c\u907f\u514d\u8fde\u63a5\u65ad\u5f00\uff0c\u5177\u4f53\u67e5\u770b\u63a5\u53e3\u6587\u6863\n        sub_str = json.dumps(sub_param)\n        ws.send(sub_str)\n        print(\"depth quote are subscribed!\")\n \n    def on_data(self, ws, string, type, continue_flag):\n        \"\"\"\n        4 argument.\n        The 1st argument is this class object.\n        The 2nd argument is utf-8 string which we get from the server.\n        The 3rd argument is data type. ABNF.OPCODE_TEXT or ABNF.OPCODE_BINARY will be came.\n        The 4th argument is continue flag. If 0, the data continue\n        \"\"\"\n \n    def on_message(self, ws, message):\n        \"\"\"\n        Callback object which is called when received data.\n        2 arguments:\n        @ ws: the WebSocketApp object\n        @ message: utf-8 data received from the server\n        \"\"\"\n        # \u5bf9\u6536\u5230\u7684message\u8fdb\u884c\u89e3\u6790\n        result = eval(message)\n        print(result)\n \n    def on_error(self, ws, error):\n        \"\"\"\n        Callback object which is called when got an error.\n        2 arguments:\n        @ ws: the WebSocketApp object\n        @ error: exception object\n        \"\"\"\n        print(error)\n \n    def on_close(self, ws, close_status_code, close_msg):\n        \"\"\"\n        Callback object which is called when the connection is closed.\n        2 arguments:\n        @ ws: the WebSocketApp object\n        @ close_status_code\n        @ close_msg\n        \"\"\"\n        print('The connection is closed!')\n \n    def start(self):\n        self.ws = websocket.WebSocketApp(\n            self.url,\n            on_open=self.on_open,\n            on_message=self.on_message,\n            on_data=self.on_data,\n            on_error=self.on_error,\n            on_close=self.on_close,\n        )\n        self.ws.run_forever()\n \n \nif __name__ == \"__main__\":\n    feed = Feed()\n    feed.start()<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2dsymbol_list\u4ee3\u8868\u4f60\u8981\u8ba2\u9605\u7684\u4ea7\u54c1\u5217\u8868\uff0c\u53ef\u4ee5\u540c\u65f6\u4f20\u5165\u591a\u4e2a\u5e02\u573a\u7684\u591a\u4e2a\u4ea7\u54c1\uff0c&nbsp;cmd_id=22002\u662f\u8ba2\u9605\u76d8\u53e3\u6570\u636e\uff0c\u5f53cmd_id=22004\u65f6\u8ba2\u9605\u7684\u662f\u6210\u4ea4\u62a5\u4ef7\uff0c\u4e00\u65e6\u8ba2\u9605\u6210\u529f\uff0c\u5b9e\u65f6\u80a1\u7968\u884c\u60c5\u6570\u636e\u5c31\u4f1a\u6e90\u6e90\u4e0d\u65ad\u7684\u63a8\u9001\u8fc7\u6765\uff0c\u5e76\u4e14\u662f\u53ca\u65f6\u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Alltick\u5b9e\u65f6\u884c\u60c5\u6570\u636e\u63a5\u53e3<\/strong><\/h2>\n\n\n\n<p>Alltick\u63d0\u4f9b\u7684\u6570\u636e\u63a5\u53e3\u6db5\u76d6\u5404\u7c7b\u8d44\u4ea7\uff0c\u6e2f\u80a1\u3001\u7f8e\u80a1\u4ee5\u53ca\u52a0\u5bc6\u8d27\u5e01\u7684\u6570\u636e\u90fd\u6709\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>\u5e02\u573a<\/strong><\/td><td><strong>API<\/strong><\/td><\/tr><tr><td rowspan=\"3\">\u6e2f\u80a1<\/td><td>\u5b9e\u65f6\u884c\u60c5\u6570\u636e<\/td><\/tr><tr><td>\u5b9e\u65f6\u5341\u6863\u76d8\u53e3\u6570\u636e<\/td><\/tr><tr><td>\u5b9e\u65f6K\u7ebf\u6570\u636e<\/td><\/tr><tr><td rowspan=\"3\">\u7f8e\u80a1<\/td><td>\u5b9e\u65f6\u884c\u60c5\u6570\u636e<\/td><\/tr><tr><td>\u5b9e\u65f6\u4e00\u6863\u76d8\u53e3\u6570<\/td><\/tr><tr><td>\u5b9e\u65f6K\u7ebf\u6570\u636e<\/td><\/tr><tr><td rowspan=\"3\">A\u80a1<\/td><td>\u6caa\u6df1A\u80a1\u5b9e\u65f6\u884c\u60c5\u6570\u636e<\/td><\/tr><tr><td>\u6caa\u6df1\u5b9e\u65f6\u4e94\u6863\u76d8\u53e3\u6570\u636e<\/td><\/tr><tr><td>\u6caa\u6df1\u5b9e\u65f6K\u7ebf\u6570\u636e<\/td><\/tr><tr><td rowspan=\"3\">\u52a0\u5bc6\u8d27\u5e01<\/td><td>\u5b9e\u65f6\u884c\u60c5\u6570\u636e<\/td><\/tr><tr><td>\u5b9e\u65f6\u591a\u6863\u76d8\u53e3\u6570\u636e<\/td><\/tr><tr><td>\u5b9e\u65f6K\u7ebf\u6570\u636e<\/td><\/tr><tr><td rowspan=\"3\">\u5916\u6c47<\/td><td>\u5b9e\u65f6\u884c\u60c5\u6570\u636e<\/td><\/tr><tr><td>\u5b9e\u65f6\u4e94\u6863\u76d8\u53e3\u6570\u636e<\/td><\/tr><tr><td>\u5b9e\u65f6K\u7ebf\u6570\u636e<\/td><\/tr><tr><td rowspan=\"3\">\u8d35\u91d1\u5c5e<\/td><td>\u5b9e\u65f6\u884c\u60c5\u6570\u636e<\/td><\/tr><tr><td>\u5b9e\u65f6\u4e94\u6863\u76d8\u53e3\u6570\u636e<\/td><\/tr><tr><td>\u5b9e\u65f6K\u7ebf\u6570\u636e<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\u3010Github\u3011<a href=\"https:\/\/github.com\/alltick\/realtime-forex-crypto-stock-tick-finance-websocket-api\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/alltick\/realtime-forex-crypto-stock-tick-finance-websocket-api<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u8c03\u7528\u5df2\u7ecf\u5c01\u88c5\u597d\u7684\u9ad8\u9891\u6570\u636eAPI\u3002\u8fd9\u91cc\u4ee5Alltick\u7684tick\u6570\u636e\u63a5\u53e3\u4f5c\u4e3a\u6f14&#8230;<\/p>\n","protected":false},"author":1,"featured_media":310,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-306","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\/306","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=306"}],"version-history":[{"count":3,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/posts\/306\/revisions"}],"predecessor-version":[{"id":10402,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/posts\/306\/revisions\/10402"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/media\/310"}],"wp:attachment":[{"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/media?parent=306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/categories?post=306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/tags?post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}