{"id":184,"date":"2024-04-10T14:45:40","date_gmt":"2024-04-10T06:45:40","guid":{"rendered":"https:\/\/blog.alltick.co\/?p=184"},"modified":"2025-04-23T16:27:43","modified_gmt":"2025-04-23T08:27:43","slug":"get-stock-tick-data-with-alltick-api","status":"publish","type":"post","link":"https:\/\/blog.alltick.co\/zh-CN\/get-stock-tick-data-with-alltick-api\/","title":{"rendered":"\u5982\u4f55\u901a\u8fc7API\u63a5\u53e3\u67e5\u8be2\u80a1\u7968Tick\u6570\u636e"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001\u83b7\u53d6\u80a1\u7968K\u7ebf\u884c\u60c5\u6570\u636e<\/strong><\/h2>\n\n\n\n<p>\u4ee5\u67e5\u8be2\u82f9\u679c\u80a1\u7968\u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u67e5\u8be2\u82f9\u679c\u4e00\u5206\u949f\u7684K\u7ebf\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" 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>\u6ce8\u610f\uff0c\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<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u4e8c\u3001\u83b7\u53d6\u6700\u65b0\u6210\u4ea4\u62a5\u4ef7\u6570\u636e<\/strong><\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" 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<h2 class=\"wp-block-heading\"><strong>\u4e09\u3001\u901a\u8fc7websocket\u8ba2\u9605\u83b7\u53d6\u5b9e\u65f6\u80a1\u7968\u884c\u60c5\u6570\u636e<\/strong><\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" 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<h2 class=\"wp-block-heading\"><strong>\u56db\u3001\u83b7\u53d6\u6700\u65b0\u76d8\u53e3\u62a5\u4ef7\u6570\u636e<\/strong><\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" 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<h2 class=\"wp-block-heading\"><strong>AllTick\u91d1\u878d\u884c\u60c5API\u514d\u8d39\u8bd5\u7528<\/strong><\/h2>\n\n\n\n<p>AllTick\u63d0\u4f9b\u7a33\u5b9a\u53ef\u9760\u7684\u91d1\u878d\u884c\u60c5\u6570\u636e\uff0c\u4ea7\u54c1\u8986\u76d6\u9762\u5e7f\uff0c\u5305\u62ec\u6e2f\u80a1CFD\u3001\u7f8e\u80a1CFD\u3001\u5916\u6c47\u3001\u5546\u54c1\u3001\u52a0\u5bc6\u8d27\u5e01\u7b49\u3002\u76ee\u524d\u63d0\u4f9b\u514d\u8d39\u8bd5\u7528\uff0c\u6709\u9700\u8981\u53ef\u4ee5\u6ce8\u518c\u8d26\u6237\uff0c\u5e76\u4f7f\u7528\u4e0a\u9762\u6f14\u793a\u7684\u4ee3\u7801\u505a\u6d4b\u8bd5\u3002\u6709\u4efb\u4f55\u95ee\u9898\u53ef\u4ee5\u4e0e\u6211\u4eec\u7684\u5ba2\u670d\u8054\u7cfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u83b7\u53d6\u80a1\u7968K\u7ebf\u884c\u60c5\u6570\u636e \u4ee5\u67e5\u8be2\u82f9\u679c\u80a1\u7968\u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u67e5\u8be2\u82f9\u679c\u4e00\u5206\u949f\u7684K\u7ebf\uff1a \u6ce8\u610f\uff0c\u5982\u679c\u60f3\u67e5\u8be2\u5176\u5b83&#8230;<\/p>\n","protected":false},"author":1,"featured_media":199,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-184","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\/184","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=184"}],"version-history":[{"count":15,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/posts\/184\/revisions"}],"predecessor-version":[{"id":10403,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/posts\/184\/revisions\/10403"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/media\/199"}],"wp:attachment":[{"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/media?parent=184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/categories?post=184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alltick.co\/zh-CN\/wp-json\/wp\/v2\/tags?post=184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}