当前位置:   article > 正文

分享-实时行情数据源接口websocket接入方法_实时行情接口

实时行情接口

今天给大家带来一个技术干货分享,如何通过接口API订阅并接入实时行情数据源报价,它的方法与步骤

一、API地址及传参说明

支持以下产品品类:

  • 美股
  • 港股
  • A股
  • 外汇
  • 贵金属
  • 商品
  • 数字币
  1. github: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
  2. token申请:https://alltick.co
  3. 把下面url中的testtoken替换为您自己的token
  4. 外汇,数字币,贵金属的api址:
  5. wss://quote.tradeswitcher.com/quote-b-ws-api
  6. 港美股api地址:
  7. wss://quote.tradeswitcher.com/quote-stock-b-ws-api
  8. 建立连接:
  9. wss://quote.tradeswitcher.com/quote-stock-b-ws-api?token=testtoken

建立连接之后,就可以订阅具体的接口数据了,具体调用方式,请看下文介绍

二、免费申请Token

到它的官方网站使用邮箱直接注册即可:​​https://alltick.co​

顺便说一下它的github开源站点:​​https://github.com/alltick/free-quote​

三、请查看code产品列表

请到github或者官网上面查看并选择

四、开始真正的订阅实时行情数据源报价

当你选择好产品code列表后就可以开始真正的订阅实时行情数据源报价了。请看下面的实例代码:

  1. import json
  2. import websocket # pip install websocket-client
  3. '''
  4. # 特别注意:
  5. # github: https://github.com/alltick/free-quote
  6. # token申请:https://alltick.co
  7. # 把下面url中的testtoken替换为您自己的token
  8. # 外汇,数字币,贵金属的api址:
  9. # wss://quote.tradeswitcher.com/quote-b-ws-api
  10. # 港美股api地址:
  11. # wss://quote.tradeswitcher.com/quote-stock-b-ws-api
  12. '''
  13. class Feed(object):
  14. def __init__(self):
  15. self.url = 'wss://quote.tradeswitcher.com/quote-stock-b-ws-api?token=testtoken' # 这里输入websocket的url
  16. self.ws = None
  17. def on_open(self, ws):
  18. """
  19. Callback object which is called at opening websocket.
  20. 1 argument:
  21. @ ws: the WebSocketApp object
  22. """
  23. print('A new WebSocketApp is opened!')
  24. # 开始订阅(举个例子)
  25. sub_param = {
  26. "cmd_id": 22002,
  27. "seq_id": 123,
  28. "trace":"3baaa938-f92c-4a74-a228-fd49d5e2f8bc-1678419657806",
  29. "data":{
  30. "symbol_list":[
  31. {
  32. "code": "700.HK",
  33. "depth_level": 5,
  34. },
  35. {
  36. "code": "UNH.US",
  37. "depth_level": 5,
  38. }
  39. ]
  40. }
  41. }
  42. #如果希望长时间运行,除了需要发送订阅之外,还需要修改代码,定时发送心跳,避免连接断开,具体查看接口文档
  43. sub_str = json.dumps(sub_param)
  44. ws.send(sub_str)
  45. print("depth quote are subscribed!")
  46. def on_data(self, ws, string, type, continue_flag):
  47. """
  48. 4 argument.
  49. The 1st argument is this class object.
  50. The 2nd argument is utf-8 string which we get from the server.
  51. The 3rd argument is data type. ABNF.OPCODE_TEXT or ABNF.OPCODE_BINARY will be came.
  52. The 4th argument is continue flag. If 0, the data continue
  53. """
  54. def on_message(self, ws, message):
  55. """
  56. Callback object which is called when received data.
  57. 2 arguments:
  58. @ ws: the WebSocketApp object
  59. @ message: utf-8 data received from the server
  60. """
  61. # 对收到的message进行解析
  62. result = eval(message)
  63. print(result)
  64. def on_error(self, ws, error):
  65. """
  66. Callback object which is called when got an error.
  67. 2 arguments:
  68. @ ws: the WebSocketApp object
  69. @ error: exception object
  70. """
  71. print(error)
  72. def on_close(self, ws, close_status_code, close_msg):
  73. """
  74. Callback object which is called when the connection is closed.
  75. 2 arguments:
  76. @ ws: the WebSocketApp object
  77. @ close_status_code
  78. @ close_msg
  79. """
  80. print('The connection is closed!')
  81. def start(self):
  82. self.ws = websocket.WebSocketApp(
  83. self.url,
  84. on_open=self.on_open,
  85. on_message=self.on_message,
  86. on_data=self.on_data,
  87. on_error=self.on_error,
  88. on_close=self.on_close,
  89. )
  90. self.ws.run_forever()
  91. if __name__ == "__main__":
  92. feed = Feed()
  93. feed.start()

五、解析推送数据

5.1、最新成交报价解析
  1. {
  2. "cmd_id":22998,
  3. "data":{
  4. "code": "1288.HK",
  5. "seq": "1605509068000001",
  6. "tick_time": "1605509068",
  7. "price": "651.12",
  8. "volume": "300",
  9. "turnover": "12345.6",
  10. "trade_direction": 1,
  11. }
  12. }
5.2、最新5档深度数据解析
  1. {
  2. "cmd_id":22999,
  3. "data":{
  4. "code": "1288.HK",
  5. "seq": "1605509068000001",
  6. "tick_time": "1605509068",
  7. "bids": [
  8. {
  9. "pric": "9.12",
  10. "volume": "9.12",
  11. },
  12. {
  13. "pric": "9.12",
  14. "volume": "9.12",
  15. },
  16. {
  17. "pric": "9.12",
  18. "volume": "9.12",
  19. },
  20. {
  21. "pric": "9.12",
  22. "volume": "9.12",
  23. },
  24. {
  25. "pric": "9.12",
  26. "volume": "9.12",
  27. }
  28. ],
  29. "asks": [
  30. {
  31. "price": "147.12",
  32. "volume": "147.12",
  33. },
  34. {
  35. "price": "147.12",
  36. "volume": "147.12",
  37. },
  38. {
  39. "price": "147.12",
  40. "volume": "147.12",
  41. },
  42. {
  43. "price": "147.12",
  44. "volume": "147.12",
  45. },
  46. {
  47. "price": "147.12",
  48. "volume": "147.12",
  49. }
  50. ],
  51. }
  52. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/598037
推荐阅读
相关标签
  

闽ICP备14008679号