一、什么是 tick 数据?
在当今金融市场的时代,美股交易数据的重要性愈发突出。其中,美股 tick 数据作为最实时的市场行情信息,扮演着不可或缺的角色。Tick 数据指的是美股金融市场中每次交易执行的最小单位的记录。在股票数据市场中,每次买入或卖出都会产生一个 tick 数据。与全球其他股票市场类似,美股 tick 数据也包含了交易的时间戳、交易价格、交易量以及买卖方向等关键信息。美股 tick 数据的准确性和实时性使其成为进行高频交易。所以,找到美股数据对接接口尤为重要,这些美股数据 tick 有助于他们制定交易策略、捕捉短期市场波动并获取利润。
二、高频 tick 数据可以用来做什么计算?
1、计算【现手】
volume 字段是累计成交量字段,现手数据很好计算,就是 volume 之差,即下一条数据 volume - 上一条数据 volume,图上黄色标注数据计算就是 1443112-1443011=101,和软件推送数据一致。
2、仓差
open_interest 字段是持仓数量,仓差是 open_interest 之差,图上是 1862177-1862180=-3,和软件推送数据一致。
3、开平
开平方向与现手、增仓、价格变动方向都有关系。
三、如何获取高频美股 tick 数据?
可以用 Alltick 的 tick 数据 api,先到网站申请 token:点击申请 token
使用方法超级简单,或者点击打开 github:github 上面有真实示例供参数,也可以直接参考下面的代码使用示例:
AllTick 以其全面的市场覆盖、高质量的数据、灵活的订阅方式、稳定可靠的服务和优质的客户服务。
`
import json
import websocket # pip install websocket-client
'''
# 特别注意:
# github: https://github.com/alltick/free-quote
# token申请:https://alltick.co
# 把下面url中的testtoken替换为您自己的token
# 外汇,数字币,贵金属的api址:
# wss://quote.tradeswitcher.com/quote-b-ws-api
# 港美股api地址:
# wss://quote.tradeswitcher.com/quote-stock-b-ws-api
'''
class Feed(object):
def __init__(self):
self.url = 'wss://quote.tradeswitcher.com/quote-stock-b-ws-api?token=testtoken' # 这里输入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,
}
]
}
}
#如果希望长时间运行,除了需要发送订阅之外,还需要修改代码,定时发送心跳,避免连接断开,具体查看接口文档
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()