AllTick
How to Query Stock Tick Data via API Interface
Blog

How to Query Stock Tick Data via API Interface

AllTick Real-Time Financial Market Data API – Free Stock, Forex, Crypto Tick Data

AllTick2 min read

1. Getting Stock K-Line Data

Example: Querying Apple stock 1-minute K-line data.

import time
import requests
import json

# Extra headers
test_headers = {
'Content-Type':'application/json'
}

'''
GitHub: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
Apply for free token: https://alltick.co/register
Official website: https://alltick.co
URL-encode the following JSON and copy it into the `query` field of the HTTP request string:
{"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.alltick.io/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.text
print(text1)

Note: To query other types of K-line data, set kline_type as follows:
1 – 1-minute, 2 – 5-minute, 3 – 15-minute, 4 – 30-minute, 5 – hourly, 6 – 2-hour, 7 – 4-hour, 8 – daily, 9 – weekly, 10 – monthly.

2. Getting Latest Trade (Tick) Data

import time
import requests
import json

# Extra headers
test_headers = {
'Content-Type':'application/json'
}

'''
GitHub: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
Apply for free token: https://alltick.co/register
Official website: https://alltick.co
URL-encode the following JSON and copy it into the `query` field:
{"trace":"python_http_test2","data":{"symbol_list":[{"code": "700.HK"},{"code": "UNH.US"},{"code": "600416.SH"}]}}
'''
test_url1 = 'https://quote.alltick.io/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.text
print(text1)

3. Subscribing to Real-time Stock Data via WebSocket

import json
import websocket # pip install websocket-client

'''
GitHub: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
Apply for free token: https://alltick.co/register
Official website: https://alltick.co
'''

class Feed(object):

def __init__(self):
self.url = 'wss://quote.alltick.io/quote-stock-b-ws-api?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806' # WebSocket URL
self.ws = None

def on_open(self, ws):
"""
Callback called when the WebSocket connection is opened.
Argument:
@ ws: the WebSocketApp object
"""
print('A new WebSocketApp is opened!')

# Subscribe (example)
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,
}
]
}
}

# For long-running sessions, besides sending subscription, you also need to send periodic heartbeats to avoid disconnection. See the API documentation for details.
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 arguments.
1st: this class object.
2nd: UTF-8 string received from the server.
3rd: data type (ABNF.OPCODE_TEXT or ABNF.OPCODE_BINARY).
4th: continue flag (0 means data continues).
"""

def on_message(self, ws, message):
"""
Callback called when a message is received.
2 arguments:
@ ws: the WebSocketApp object
@ message: UTF-8 data received from the server
"""
# Parse the received message
result = eval(message)
print(result)

def on_error(self, ws, error):
"""
Callback called when an error occurs.
2 arguments:
@ ws: the WebSocketApp object
@ error: exception object
"""
print(error)

def on_close(self, ws, close_status_code, close_msg):
"""
Callback called when the connection is closed.
3 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()

4. Getting Latest Depth (Order Book) Data

import time
import requests
import json

# Extra headers
test_headers = {
'Content-Type':'application/json'
}

'''
GitHub: https://github.com/alltick/realtime-forex-crypto-stock-tick-finance-websocket-api
Apply for free token: https://alltick.co/register
Official website: https://alltick.co
URL-encode the following JSON and copy it into the `query` field:
{"trace":"python_http_test2","data":{"symbol_list":[{"code": "700.HK"},{"code": "UNH.US"},{"code": "600416.SH"}]}}
'''
test_url1 = 'https://quote.alltick.io/quote-stock-b-api/depth-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.text
print(text1)

AllTick Financial Market Data API Free Trial

AllTick provides stable and reliable financial market data, covering a wide range of products including Hong Kong stocks CFD, US stocks CFD, forex, commodities, cryptocurrencies, etc. A free trial is currently available. You can register an account and test using the sample codes above. If you have any questions, please contact our customer service.


Start streaming market data today

Generate a free API key in seconds and connect to every market from one endpoint.