Go SDK Documentation
The Go version of iTick API SDK provides REST API queries and WebSocket real-time data subscription for basic, stock, index, futures, fund, forex, and cryptocurrency data.
Features
- Supports REST API queries for basic, stock, index, futures, fund, forex, and cryptocurrency data
- Supports WebSocket real-time data subscription
- Automatic reconnection mechanism
- Heartbeat to maintain connection
- Callback-based event handling
Installation
go get io.github.itick/sdk
Quick Start
Initialize Client
package main
import (
"io.github.itick/sdk"
)
func main() {
token := "your_api_token"
client := sdk.NewClient(token)
}
REST API Usage
Forex Data Query
tick, err := client.GetForexTick("GB", "EURUSD")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Forex Tick: %+v\n", tick)
quote, err := client.GetForexQuote("GB", "EURUSD")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Forex Quote: %+v\n", quote)
depth, err := client.GetForexDepth("GB", "EURUSD")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Forex Depth: %+v\n", depth)
kline, err := client.GetForexKline("GB", "EURUSD", 2, 10, nil)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Forex Kline: %+v\n", kline)
Stock Data Query
tick, err := client.GetStockTick("US", "AAPL")
if err != nil {
log.Fatal(err)
}
quote, err := client.GetStockQuote("US", "AAPL")
if err != nil {
log.Fatal(err)
}
depth, err := client.GetStockDepth("US", "AAPL")
if err != nil {
log.Fatal(err)
}
kline, err := client.GetStockKline("US", "AAPL", 2, 10, nil)
if err != nil {
log.Fatal(err)
}
Cryptocurrency Data Query
tick, err := client.GetCryptoTick("BA", "BTCUSDT")
if err != nil {
log.Fatal(err)
}
quote, err := client.GetCryptoQuote("BA", "BTCUSDT")
if err != nil {
log.Fatal(err)
}
depth, err := client.GetCryptoDepth("BA", "BTCUSDT")
if err != nil {
log.Fatal(err)
}
kline, err := client.GetCryptoKline("BA", "BTCUSDT", 2, 10, nil)
if err != nil {
log.Fatal(err)
}
WebSocket Usage
The SDK provides enhanced WebSocket functionality including automatic reconnection and heartbeat maintenance, eliminating the need for manual connection management.
Set Callback Functions
client.SetMessageHandler(func(message []byte) {
fmt.Printf("Received WebSocket message: %s\n", message)
})
client.SetErrorHandler(func(err error) {
log.Printf("WebSocket error: %v\n", err)
})
Connect and Subscribe
err := client.ConnectForexWebSocket()
if err != nil {
log.Fatal(err)
}
defer client.CloseWebSocket()
subscribeMsg := []byte(`{"action": "subscribe", "codes": ["EURUSD"]}`)
err = client.SendWebSocketMessage(subscribeMsg)
if err != nil {
log.Fatal(err)
}
time.Sleep(10 * time.Second)
fmt.Printf("WebSocket connected: %v\n", client.IsWebSocketConnected())
Other WebSocket Connections
err := client.ConnectStockWebSocket()
err := client.ConnectCryptoWebSocket()
API Interface List
Basics
| Method | Description |
|---|
| GetSymbolList | Get symbol list |
| GetSymbolHolidays | Get holiday information |
Stock
| Method | Description |
|---|
| GetStockInfo | Get stock information |
| GetStockIPO | Get stock IPO information |
| GetStockSplit | Get stock split information |
| GetStockTick | Get stock real-time trade |
| GetStockQuote | Get stock real-time quote |
| GetStockDepth | Get stock real-time order book |
| GetStockKline | Get stock historical K-line |
| GetStockTicks | Get stock batch real-time trades |
| GetStockQuotes | Get stock batch real-time quotes |
| GetStockDepths | Get stock batch real-time order books |
| GetStockKlines | Get stock batch historical K-lines |
| ConnectStockWebSocket | Connect to stock WebSocket |
Indices
| Method | Description |
|---|
| GetIndicesTick | Get index real-time trade |
| GetIndicesQuote | Get index real-time quote |
| GetIndicesDepth | Get index real-time order book |
| GetIndicesKline | Get index historical K-line |
| GetIndicesTicks | Get index batch real-time trades |
| GetIndicesQuotes | Get index batch real-time quotes |
| GetIndicesDepths | Get index batch real-time order books |
| GetIndicesKlines | Get index batch historical K-lines |
| ConnectIndicesWebSocket | Connect to index WebSocket |
Futures
| Method | Description |
|---|
| GetFutureTick | Get futures real-time trade |
| GetFutureQuote | Get futures real-time quote |
| GetFutureDepth | Get futures real-time order book |
| GetFutureKline | Get futures historical K-line |
| GetFutureTicks | Get futures batch real-time trades |
| GetFutureQuotes | Get futures batch real-time quotes |
| GetFutureDepths | Get futures batch real-time order books |
| GetFutureKlines | Get futures batch historical K-lines |
| ConnectFutureWebSocket | Connect to futures WebSocket |
Funds
| Method | Description |
|---|
| GetFundTick | Get fund real-time trade |
| GetFundQuote | Get fund real-time quote |
| GetFundDepth | Get fund real-time order book |
| GetFundKline | Get fund historical K-line |
| GetFundTicks | Get fund batch real-time trades |
| GetFundQuotes | Get fund batch real-time quotes |
| GetFundDepths | Get fund batch real-time order books |
| GetFundKlines | Get fund batch historical K-lines |
| ConnectFundWebSocket | Connect to fund WebSocket |
Forex
| Method | Description |
|---|
| GetForexTick | Get forex real-time trade |
| GetForexQuote | Get forex real-time quote |
| GetForexDepth | Get forex real-time order book |
| GetForexKline | Get forex historical K-line |
| GetForexTicks | Get forex batch real-time trades |
| GetForexQuotes | Get forex batch real-time quotes |
| GetForexDepths | Get forex batch real-time order books |
| GetForexKlines | Get forex batch historical K-lines |
| ConnectForexWebSocket | Connect to forex WebSocket |
Crypto
| Method | Description |
|---|
| GetCryptoTick | Get cryptocurrency real-time trade |
| GetCryptoQuote | Get cryptocurrency real-time quote |
| GetCryptoDepth | Get cryptocurrency real-time order book |
| GetCryptoKline | Get cryptocurrency historical K-line |
| GetCryptoTicks | Get cryptocurrency batch real-time trades |
| GetCryptoQuotes | Get cryptocurrency batch real-time quotes |
| GetCryptoDepths | Get cryptocurrency batch real-time order books |
| GetCryptoKlines | Get cryptocurrency batch historical K-lines |
| ConnectCryptoWebSocket | Connect to cryptocurrency WebSocket |
WebSocket Functionality
Automatic Reconnection
The SDK has a built-in automatic reconnection mechanism that automatically attempts to reconnect when network exceptions or disconnections occur:
- Reconnection interval: 5 seconds
- Maximum reconnection attempts: 10 times
- Automatically restore subscriptions after successful reconnection
Heartbeat Maintenance
The SDK automatically maintains WebSocket connection heartbeats:
- Heartbeat interval: 30 seconds
- Automatically sends ping messages to keep the connection active
Connection Status Check
connected := client.IsWebSocketConnected()
Complete Example
package main
import (
"fmt"
"log"
"time"
"io.github.itick/sdk"
)
func main() {
token := "your_api_token"
client := sdk.NewClient(token)
client.SetMessageHandler(func(message []byte) {
fmt.Printf("Received WebSocket message: %s\n", message)
})
client.SetErrorHandler(func(err error) {
log.Printf("WebSocket error: %v\n", err)
})
tick, err := client.GetForexTick("GB", "EURUSD")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Forex Tick: %+v\n", tick)
err = client.ConnectForexWebSocket()
if err != nil {
log.Fatal(err)
}
defer client.CloseWebSocket()
subscribeMsg := []byte(`{"action": "subscribe", "codes": ["EURUSD"]}`)
err = client.SendWebSocketMessage(subscribeMsg)
if err != nil {
log.Fatal(err)
}
time.Sleep(10 * time.Second)
}