金融交易世界中,获取准确及时的tick数据至关重要。抓住交易良机的关键在于掌握实时tick数据。数据更新越快,可发现的赚钱机会就越多。这也是为何在高频数据交易领域,tick数据备受重视。与传统的行情数据相比,tick数据提供了更细致的市场变化记录,为交易者提供了更全面的视角。
首先,让我们简单了解一下什么是Tick数据。
我们日常看到的K线行情数据是基于时间单位的,而tick数据则记录了更细致的维度,即每次价格变化都被记录。举例来说:假设一个股票在一分钟内变动了30次价格,那么在一分钟的行情数据中,你只会看到4个价格:
-
开盘价
-
收盘价
-
最高价
-
最低价
因为只有这4项数据,就足够绘制出一个蜡烛图:
O=开盘价,L=最低价,H=最高价,C=收盘价
一、Tick数据的用途
1、量化交易
在量化交易中,Tick数据可以用于分析市场行情、制定交易策略和执行交易,Tick数据可以用于开发和测试量化交易策略。交易者可以根据Tick数据构建技术指标、价格模型和交易信号,以便自动化执行交易。通过分析历史的Tick数据,交易者可以评估策略的有效性和盈利潜力。
2、价格盯盘工具
Tick数据用于跟踪和监控特定交易品种的价格变动。交易者可以使用Tick数据来实时监测价格的波动和交易量的变化,以便及时做出决策。价格盯盘通常是基于实时的Tick数据进行操作,以帮助交易者捕捉到价格的波动和市场机会。
二、代码实例
以AllTick的API为例,这家的Tick数据经过封装,可随时方便调用。
官网:实时行情Tick数据 - 外汇、美股、港股、加密货币CFD数据API - 金融高频数据接口 - AllTick
import json
import 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()