Go对接印度股票数据源指南:使用StockTV API


一、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/stockscountryId=14
指数数据/stock/indicescountryId=14
K线数据/stock/klinepid=股票ID
公司信息/stock/companiescountryId=14
涨跌排行榜/stock/updownListtype=1(涨)/2(跌)
IPO新股日历/stock/getIpocountryId=14

四、最佳实践建议

  1. 缓存机制:对低频数据(如公司信息)实施本地缓存
  2. 异常处理:使用指数退避重试策略
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("请求失败")
}
  1. 数据压缩:启用Gzip压缩减少70%流量消耗
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Accept-Encoding", "gzip")

五、注意事项

  1. 所有请求需携带key参数
  2. 印度市场交易时间(IST):
    • 早盘:9:15 AM - 3:30 PM
    • 盘前:8:00 AM - 9:00 AM
  3. 免费版限流:10请求/秒
  4. 历史数据获取需联系客服开通权限

完整代码示例:https://github.com/StockTvPP

通过本文指南,您可快速构建印度市场数据监控、量化交易或财经APP。遇到问题可通过https://t.me/stocktvpaopao获取技术支持。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值