一、StockTV API简介
StockTV提供全球200+国家的实时金融数据,覆盖股票、外汇、期货和加密货币市场。针对印度市场(国家ID=14),其主要优势包括:
- 毫秒级低延迟响应
- 7x24小时稳定服务
- 日均处理亿级数据
- 免费技术支持
官方资源:
- https://documenter.getpostman.com/view/42914868/2sB2ixkEZR
- https://github.com/StockTvPP/stock-exchange
二、Go对接实战
1. 准备工作
import (
"encoding/json"
"fmt"
"net/http"
)
const (
API_URL = "https://api.stocktv.top"
API_KEY = "YOUR_API_KEY" // 联系客服获取
COUNTRY_ID = 14 // 印度国家ID
)
2. 获取实时股票数据
func GetRealTimeStocks() {
url := fmt.Sprintf("%s/stock/stocks?countryId=%d&key=%s", API_URL, COUNTRY_ID, API_KEY)
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
// 解析数据示例
data := result["data"].(map[string]interface{})
for _, stock := range data["records"].([]interface{}) {
s := stock.(map[string]interface{})
fmt.Printf("股票代码: %s 最新价: %.2f 涨跌幅: %.2f%%\n",
s["symbol"], s["last"], s["chgPct"])
}
}
3. 获取指数数据(Nifty 50)
func GetNifty50() {
url := fmt.Sprintf("%s/stock/indices?countryId=%d&key=%s", API_URL, COUNTRY_ID, API_KEY)
resp, err := http.Get(url)
// ...(同上)
for _, index := range data.([]interface{}) {
idx := index.(map[string]interface{})
if idx["name"] == "Nifty 50" {
fmt.Printf("Nifty50: %.2f (%.2f%%)",
idx["last"], idx["chgPct"])
}
}
}
4. 获取K线数据(15分钟线)
func GetKLine(pid int) {
url := fmt.Sprintf("%s/stock/kline?pid=%d&interval=PT15M&key=%s",
API_URL, pid, API_KEY)
// ...(同上)
for _, k := range data.([]interface{}) {
kline := k.(map[string]interface{})
fmt.Printf("时间: %d 开:%.2f 高:%.2f 低:%.2f 收:%.2f\n",
kline["time"], kline["open"], kline["high"],
kline["low"], kline["close"])
}
}
5. WebSocket实时数据
import "github.com/gorilla/websocket"
func ConnectWS() {
conn, _, err := websocket.DefaultDialer.Dial(
fmt.Sprintf("wss://ws-api.stocktv.top/connect?key=%s", API_KEY), nil)
go func() {
for {
_, msg, _ := conn.ReadMessage()
var data map[string]interface{}
json.Unmarshal(msg, &data)
fmt.Printf("实时报价: %s %.2f\n", data["symbol"], data["last_numeric"])
}
}()
}
三、核心API说明(印度市场)
| 功能 | 端点 | 关键参数 |
|---|---|---|
| 实时股票列表 | /stock/stocks | countryId=14 |
| 指数数据 | /stock/indices | countryId=14 |
| K线数据 | /stock/kline | pid=股票ID |
| 公司信息 | /stock/companies | countryId=14 |
| 涨跌排行榜 | /stock/updownList | type=1(涨)/2(跌) |
| IPO新股日历 | /stock/getIpo | countryId=14 |
四、最佳实践建议
- 缓存机制:对低频数据(如公司信息)实施本地缓存
- 异常处理:使用指数退避重试策略
func GetWithRetry(url string, retries int) (*http.Response, error) {
for i := 0; i < retries; i++ {
resp, err := http.Get(url)
if err == nil && resp.StatusCode == 200 {
return resp, nil
}
time.Sleep(time.Second * time.Duration(math.Pow(2, float64(i))))
}
return nil, fmt.Errorf("请求失败")
}
- 数据压缩:启用Gzip压缩减少70%流量消耗
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Accept-Encoding", "gzip")
五、注意事项
- 所有请求需携带
key参数 - 印度市场交易时间(IST):
- 早盘:9:15 AM - 3:30 PM
- 盘前:8:00 AM - 9:00 AM
- 免费版限流:10请求/秒
- 历史数据获取需联系客服开通权限
完整代码示例:https://github.com/StockTvPP
通过本文指南,您可快速构建印度市场数据监控、量化交易或财经APP。遇到问题可通过https://t.me/stocktvpaopao获取技术支持。

被折叠的 条评论
为什么被折叠?



