開始使用
富果行情 WebSocket API 提供台股即時行情服務。透過 WebSocket API 可以滿足您想要接收即時行情的需求。
建立連線
您可以透過以下位置向富果行情 WebSocket API server 建立連線:
wss://api.fugle.tw/marketdata/v1.0/stock/streaming
身份驗證
建立 WebSocket 連線後,請使用 API 金鑰進行身份驗證,以獲授權訂閱各項頻道:
{
"event": "auth",
"data": {
"apikey": "<API_KEY>"
}
}
當驗證成功後,會收到以下訊息:
{
"event": "authenticated",
"data": {
"message": "Authenticated successfully"
}
}
若驗證失敗,則收到以下訊息:
{
"event": "error",
"data": {
"message": "Invalid authentication credentials"
}
}
Heartbeat
每隔 30 秒 WebSocket server 會送出一個 heartbeat 訊息:
{
"event": "heartbeat",
"data": {
"time": "<Timestamp>"
}
}
Ping/Pong
將以下 JSON 格式訊息發送到 WebSocket Server (state
為可選):
{
"event": "ping",
"data": {
"state": "<ANY>"
}
}
WebSocket Server 會回應以下訊息 (若 ping 未送 state
則不會有該欄位):
{
"event": "pong",
"data": {
"time": "<TIMESTAMP>",
"state": "<ANY>"
}
}
Channels
富果行情 WebSocket API 目前提供以下可訂閱頻道:
trades
- 接收訂閱股票最新成交資訊candles
- 接收訂閱股票最新分鐘Kbooks
- 接收訂閱股票最新最佳五檔委買委賣資訊aggregates
- 接收訂閱股票聚合數據的行情資訊indices
- 接收訂閱股票最新指數行情資料
訂閱頻道
要訂閱一個頻道 JSON 格式訊息發送到 WebSocket Server:
{
"event": "subscribe",
"data": {
"channel": "<CHANNEL_NAME>",
"symbol": "<SYMBOL_ID>"
}
}
在
data
指定"intradayOddLot": true
可訂閱盤中零股行情。
訂閱成功後,會收到以下事件回應:
{
"event": "subscribed",
"data": {
"id": "<CHANNEL_ID>",
"channel": "<CHANNEL_NAME>",
"symbol": "<SYMBOL_ID>"
}
}
支援訂閱同頻道的多檔股票:
{
"event": "subscribe",
"data": {
"channel": "<CHANNEL_NAME>",
"symbols": ["<SYMBOL_ID_1>", "<SYMBOL_ID_2>"]
}
}
訂閱成功後,會收到以下事件回應:
{
"event": "subscribed",
"data": [
{
"id": "<CHANNEL_ID>",
"channel": "<CHANNEL_NAME>",
"symbol": "<SYMBOL_ID_1>"
},
{
"id": "<CHANNEL_ID>",
"channel": "<CHANNEL_NAME>",
"symbol": "<SYMBOL_ID_2>"
}
]
}
取消訂閱
要取消已訂閱頻道,請將以下 JSON 格式訊息發送到 WebSocket Server:
{
"event": "unsubscribe",
"data": {
"id": "<CHANNEL_ID>"
}
}
取消訂閱成功後,會收到以下事件回應:
{
"event": "unsubscribed",
"data": {
"id": "<CHANNEL_ID>"
}
}
支援取消訂閱多個頻道:
{
"event": "unsubscribe",
"data": {
"ids": ["<CHANNEL_ID_1>", "<CHANNEL_ID_2>"]
}
}
取消訂閱成功後,會收到以下事件回應:
{
"event": "unsubscribed",
"data": [
{
"id": "<CHANNEL_ID_1>"
},
{
"id": "<CHANNEL_ID_2>"
}
]
}
訂閱資訊
要取得已訂閱的頻道,請將以下 JSON 格式訊息發送到 WebSocket Server:
{
"event": "subscriptions"
}
然後會收到以下事件回應:
{
"event": "subscriptions",
"data": [
{
"id": "<CHANNEL_ID>",
"channel": "<CHANNEL_NAME>",
"symbol": "<SYMBOL_ID>"
}
]
}
快速開始
以下是一個在瀏覽器環境下,建立 WebSocket 連線、身份驗證以及訂閱頻道接收即時行情的範例:
const socket = new WebSocket('wss://api.fugle.tw/marketdata/v1.0/stock/streaming');
socket.onopen = () => {
socket.send(
JSON.stringify({
event: 'auth',
data: {
apikey: '<API_KEY>',
},
}),
);
socket.onmessage = (message) => {
const data = JSON.parse(message.data);
if (data.event === 'authenticated') {
socket.send(
JSON.stringify({
event: 'subscribe',
data: {
channel: 'trades',
symbol: '2330',
},
}),
);
}
console.log(data);
};
};
使用 SDK
富果行情 WebSocket API 提供 Python 與 Node.js SDK。您可以透過以下範例存取 WebSocket API:
- Python
- Node.js
from fugle_marketdata import WebSocketClient
def handle_message(message):
print(f'message: {message}')
def handle_connect():
print('connected')
def handle_disconnect(code, message):
print(f'disconnect: {code}, {message}')
def handle_error(error):
print(f'error: {error}')
def main():
client = WebSocketClient(api_key='YOUR_API_KEY')
stock = client.stock
stock.on("connect", handle_connect)
stock.on("message", handle_message)
stock.on("disconnect", handle_disconnect)
stock.on("error", handle_error)
stock.connect()
stock.subscribe({
"channel": 'trades',
"symbol": '2330'
})
if __name__ == "__main__":
main()
const { WebSocketClient } = require('@fugle/marketdata');
const client = new WebSocketClient({ apiKey: 'YOUR_API_KEY' });
const stock = client.stock;
// open the WebSocket connection and authenticate
stock.connect().then(() => {
// subscribe the channel to receive streaming data
stock.subscribe({ channel: 'trades', symbol: '2330' });
});
stock.on('message', (message) => {
const data = JSON.parse(message);
console.log(data);
});