Quick Start
Fubon Market WebSocket API provides real-time stock market data services for Taiwan. Through the WebSocket API, you can fulfill your need for receiving real-time market data
Using SDK
Fubon Market WebSocket API provides Python 、 Node.js and C# SDKs. You can access the WebSocket API through the following methods:
And subscribe webSocket callback method to receive the callback messages below.
WebSocket api provides Low Latency Speed
Mode and Multi-Information Normal
Mode.
- Python
- Node.js
- C#
from fubon_neo.sdk import FubonSDK, Mode
def handle_message(message):
print(message)
sdk = FubonSDK()
accounts = sdk.login("Your ID", "Your password", "Your cert path", "Your cert password") # 需登入後,才能取得行情權限
sdk.init_realtime() # Establish market-data
# Offer MarketData Mode , Default : Speed
# sdk.init_realtime(Mode.Speed) or sdk.init_realtime(Mode.Normal)
stock = sdk.marketdata.websocket_client.stock
stock.on('message', handle_message)
stock.connect()
const { FubonSDK, Mode } = require('fubon-neo');
const sdk = new FubonSDK();
const accounts = sdk.login("Your ID", "Your password", "Your cert path","Your cert password");
sdk.initRealtime(); // Establish market-data
// Offer MarketData Mode , Default : Speed
// sdk.initRealtime(Mode.Speed) or sdk.initRealtime(Mode.Normal)
const stock = sdk.marketdata.webSocketClient.stock;
stock.on("message", (message) => {
const data = JSON.parse(message);
console.log(data);
});
(async () => {
await stock.connect();
stock.subscribe({
'channel': 'trades',
'symbol': '2881'
});
})()
using FubonNeo.Sdk;
using FugleMarketData.WebsocketModels;
var sdk = new FubonSDK();
var result = sdk.Login("Your ID", "Your password", "Your cert path", "Your cert password");
sdk.InitRealtime(); // Establish market-data
// Offer MarketData Mode , Default : Speed
// sdk.InitRealtime(Mode.Speed) or sdk.InitRealtime(Mode.Normal)
var stock = sdk.MarketData.WebSocketClient.Stock;
stock.OnMessage += (msg) => Console.WriteLine($"receive: { msg }");
await stock.Connect();
Authenticated
When the verification is successful, you will receive the following message:
{
"event": "authenticated",
"data": {
"message": "Authenticated successfully"
}
}
When the verification is failed, you will receive the following message:
{
"event": "error",
"data": {
"message": "Invalid authentication credentials"
}
}
Heartbeat
Every 30 seconds, the WebSocket server will send out a heartbeat message:
{
"event": "heartbeat",
"data": {
"time": "<Timestamp>"
}
}
Ping/Pong
Every 5 seconds, sdk will send the following JSON-formatted message to the WebSocket Server, or follow the below example (where state
is optional):
- Python
- Node.js
- C#
stock.ping({
'state' : '<ANY>'
})
stock.ping({state:'<ANY>'});
stock.ping("<ANY>");
The WebSocket Server will respond with the following message (if ping
was sent, the state
field will not be present):
{
"event": "pong",
"data": {
"time": "<TIMESTAMP>",
"state": "<ANY>"
}
}
Channels
The Fubon Market WebSocket API currently provides the following subscribable channels:
trades
- Subscribe to the latest stock trade informationbooks
- Subscribe to the latest top five bid and ask information for stocksindices
- Subscribe to the latest index market data
Subscribe Channel
Subscribe to a channel, send following example to the WebSocket Server:
- Python
- Node.js
- C#
stock.subscribe({
"channel" : "<CHANNEL_NAME>",
"symbol" : "<SYMBOL_ID>"
#"intradayOddLot": True Subscribe intradayOdd
})
stock.subscribe({
channel: '<CHANNEL_NAME>',
symbol: '<SYMBOL_ID>',
//intradayOddLot: true Subscribe intradayOdd
});
stock.Subscribe(StockChannel.<CHANNEL_NAME>,"<SYMBOL_ID>");
//stock.Subscribe(StockChannel.Trades,new StockSubscribeParams{Symbol="<SYMBOL_ID>", IntradayOddLot=true}); Subscribe intradayOdd
After a successful subscription, you will receive the following event response:
{
"event": "subscribed",
"data": {
"id": "<CHANNEL_ID>",
"channel": "<CHANNEL_NAME>",
"symbol": "<SYMBOL_ID>"
}
}
Supports the subscription of multiple stocks within the same channel:
- Python
- Node.js
- C#
stock.subscribe({
"channel" : "<CHANNEL_NAME>",
"symbols" : ["<SYMBOL_ID>","<SYMBOL_ID>"]
#"intradayOddLot": True Subscribe intradayOdd
})
stock.subscribe({
channel: '<CHANNEL_NAME>',
symbols: ['<SYMBOL_ID>','<SYMBOL_ID>']
//intradayOddLot: true Subscribe intradayOdd
});
stock.Subscribe(StockChannel.<CHANNEL_NAME>,"<SYMBOL_ID>","<SYMBOL_ID>");
//stock.Subscribe(StockChannel.Trades, new StockSubscribeParams{Symbols = new List<string>{"<SYMBOL_ID>", "<SYMBOL_ID>"}, IntradayOddLot=true}); Subscribe intradayOdd
After a successful subscription, you will receive the following event response:
{
"event": "subscribed",
"data": [
{
"id": "<CHANNEL_ID>",
"channel": "<CHANNEL_NAME>",
"symbol": "<SYMBOL_ID_1>"
},
{
"id": "<CHANNEL_ID>",
"channel": "<CHANNEL_NAME>",
"symbol": "<SYMBOL_ID_2>"
}
]
}
Unsubscribe
Unsubscribe from a subscribed channel, please send the following example to WebSocket Server:
- Python
- Node.js
- C#
stock.unsubscribe({
'id':'<CHANNEL_ID>'
})
stock.unsubscribe({
id : '<CHANNEL_ID>'
});
stock.Unsubscribe("<CHANNEL_ID>");
After a successful unsubscription, you will receive the following event response:
{
"event": "unsubscribed",
"data": {
"id": "<CHANNEL_ID>",
"channel" : "<CHANNEL_NAME>",
"symbol" : "<SYMBOL_ID>"
}
}
Supports the unsubscription from multiple channels:
- Python
- Node.js
- C#
stock.unsubscribe({
'ids':['<CHANNEL_ID>','<CHANNEL_ID>']
})
stock.unsubscribe({
ids : ['<CHANNEL_ID>','<CHANNEL_ID>']
});
stock.Unsubscribe("<CHANNEL_ID>","<CHANNEL_ID>");
After a successful unsubscription, you will receive the following event response:
{
"event": "unsubscribed",
"data": [
{
"id": "<CHANNEL_ID_1>",
"channel" : "<CHANNEL_NAME>",
"symbol" : "<SYMBOL_ID>"
},
{
"id": "<CHANNEL_ID_2>",
"channel" : "<CHANNEL_NAME>",
"symbol" : "<SYMBOL_ID>"
}
]
}
Error Handle
When there is an anomaly in the WebSocket callback you subscribed to or processed, you can handle error messages as follows:
- Python
- Node.js
- C#
def handle_connect():
print('market data connected')
def handle_disconnect(code, message):
print(f'market data disconnect: {code}, {message}')
def handle_error(error):
print(f'market data error: {error}')
stock.on("connect", handle_connect)
stock.on("disconnect", handle_disconnect)
stock.on("error", handle_error)
stock.on("connect", (message) => {
const connect_msg = JSON.parse(message);
console.log(connect_msg);
});
stock.on("disconnect", (message) => {
console.log(message);
});
stock.on("error", (message) => {
const err_msg = JSON.parse(message);
console.log(err_msg);
});
stock.OnError += (errmsg) => Console.WriteLine($"handle error: {errmsg}");
stock.OnConnected += (connmsg) => Console.WriteLine($"Connect: {connmsg}");
stock.OnDisconnected += (disconmsg) => Console.WriteLine($"Disconnect: {disconmsg}");
Reconnection
The following is a simple demonstration that automatically reconnects the websocket when a disconnection event is detected using a callback:
- Python
- Node.js
- C#
def handle_disconnect(code, message):
print(f'market data disconnect: {code}, {message}')
stock.connect()
print("Reconnected Succuess")
print("Resubscribe")
stock.subscribe({ # Resubscribe to the channels and symbols that you have previously subscribed
'channel': '<CHANNEL_NAME>',
'symbol': '<SYMBOL_ID>'
})
stock.on("disconnect", (message) => {
console.log(message);
stock.connect()
console.log("Reconnected Succuess");
stock.subscribe({ channel: '<CHANNEL_NAME>', symbol: '<SYMBOL_ID>' }); //Resubscribe to the channels and symbols that you have previously subscribed
});
stock.OnDisconnected += async (msg) =>
{
Console.WriteLine($"disconnected at {DateTime.Now}");
await Task.Delay(10);
Console.WriteLine("Try Reconnected");
await stock.Connect();
Console.WriteLine("Reconnected Success");
Console.WriteLine("Resubscribe...");
await stock.Subscribe(StockChannel.<CHANNEL_NAME>, "<SYMBOL_ID>"); //Resubscribe to the channels and symbols that you have previously subscribed
Console.WriteLine("Resubscribe Success");
};