ITPub博客

首页 > 大数据 > 数据挖掘 > 金融API | 获取股票tick数据的方法

金融API | 获取股票tick数据的方法

原创 数据挖掘 作者:量化集结者 时间:2024-04-10 22:51:04 0 删除 编辑

通过金融API,我们可以轻松获取股票tick数据,其中包括实时价格、成交量和市值等关键信息。这些数据对于市场推广至关重要,能够帮助我们准确了解股票的动态变化,及时调整推广策略,抓住投资机会。利用金融API的数据接口,我们可以快速获取股票的tick数据,为推广工作提供有力支持。

tick逐笔成交数据是金融API中的重要数据类型之一。通过分析tick数据,我们能够深入了解股票的交易量、价格变动情况,以及交易的执行细节。这些详细的tick逐笔成交数据能够帮助我们更准确地判断市场趋势,优化推广策略,并做出明智的投资决策。利用金融API提供的tick逐笔成交数据,我们能够更加全面地了解股票市场,实现更精准的市场推广。


下面是亲测有效的股票行情数据接口获取方式:

一、获取各类K线数据

AllTick的 API,该API已经封装好,可以直接调用。下面是相关的代码示例:

申请touken地址: 网页链接

github地址: 网页链接

import timeimport requestsimport json
 
# Extra headers
test_headers = {
    'Content-Type':'application/json'}
 '''
github:https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
申请免费token:https://alltick.co/register
官网:https://alltick.co
将如下JSON进行url的encode,复制到http的查询字符串的query字段里
{"trace":"python_http_test1","data":{"code":"AAPL.US","kline_type":1,"kline_timestamp_end":0,"query_kline_num":2,"adjust_type":0}}
'''test_url1 = 'https://quote.tradeswitcher.com/quote-stock-b-api/kline?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806&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'
 resp1 = requests.get(url=test_url1, headers=test_headers)
 # Decoded text returned by the request
text1 = resp1.textprint(text1)


二、获取最新成交报价数据

import timeimport requestsimport json
 
# Extra headers
test_headers = {
    'Content-Type':'application/json'}'''
github:https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
申请免费token:https://alltick.co/register
官网:https://alltick.co
将如下JSON进行url的encode,复制到http的查询字符串的query字段里
{"trace":"python_http_test2","data":{"symbol_list":[{"code": "700.HK"},{"code": "UNH.US"},{"code": "600416.SH"}]}}
'''test_url1 = 'https://quote.tradeswitcher.com/quote-stock-b-api/trade-tick?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806&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'
 resp1 = requests.get(url=test_url1, headers=test_headers)
 # Decoded text returned by the request
text1 = resp1.textprint(text1)


三、通过websockety订阅获取实时股票行情数据

import jsonimport websocket    # pip install websocket-client
 '''
github:https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
申请免费token:https://alltick.co/register
官网:https://alltick.co
'''
 class Feed(object):
 
    def __init__(self):
        self.url = 'wss://quote.tradeswitcher.com/quote-stock-b-ws-api?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806'  # 这里输入websocket的url
        self.ws = None
 
    def on_open(self, ws):
        """
        Callback object which is called at opening websocket.
        1 argument:
        @ ws: the WebSocketApp object
        """
        print('A new WebSocketApp is opened!')
 
        # 开始订阅(举个例子)
        sub_param = {
            "cmd_id": 22002, 
            "seq_id": 123,
            "trace":"3baaa938-f92c-4a74-a228-fd49d5e2f8bc-1678419657806",
            "data":{
                "symbol_list":[
                    {
                        "code": "700.HK",
                        "depth_level": 5,
                    },
                    {
                        "code": "UNH.US",
                        "depth_level": 5,
                    },
                    {
                        "code": "600416.SH",
                        "depth_level": 5,
                    }
                ]
            }
        }
        
        #如果希望长时间运行,除了需要发送订阅之外,还需要修改代码,定时发送心跳,避免连接断开,具体查看接口文档
        sub_str = json.dumps(sub_param)
        ws.send(sub_str)
        print("depth quote are subscribed!")
 
    def on_data(self, ws, string, type, continue_flag):
        """
        4 argument.
        The 1st argument is this class object.
        The 2nd argument is utf-8 string which we get from the server.
        The 3rd argument is data type. ABNF.OPCODE_TEXT or ABNF.OPCODE_BINARY will be came.
        The 4th argument is continue flag. If 0, the data continue
        """
 
    def on_message(self, ws, message):
        """
        Callback object which is called when received data.
        2 arguments:
        @ ws: the WebSocketApp object
        @ message: utf-8 data received from the server
        """
        # 对收到的message进行解析
        result = eval(message)
        print(result)
 
    def on_error(self, ws, error):
        """
        Callback object which is called when got an error.
        2 arguments:
        @ ws: the WebSocketApp object
        @ error: exception object
        """
        print(error)
 
    def on_close(self, ws, close_status_code, close_msg):
        """
        Callback object which is called when the connection is closed.
        2 arguments:
        @ ws: the WebSocketApp object
        @ close_status_code
        @ close_msg
        """
        print('The connection is closed!')
 
    def start(self):
        self.ws = websocket.WebSocketApp(
            self.url,
            on_open=self.on_open,
            on_message=self.on_message,
            on_data=self.on_data,
            on_error=self.on_error,
            on_close=self.on_close,
        )
        self.ws.run_forever()
 
 if __name__ == "__main__":
    feed = Feed()
    feed.start()


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70037950/viewspace-3012133/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2024-04-08

  • 博文量
    6
  • 访问量
    7678