对接印度股票数据源完整指南:使用Python实现
本文将介绍如何使用Python对接印度股票数据源,涵盖实时行情、历史K线、公司信息等核心功能。所有API都需要通过https://t.me/stocktvpaopao获取授权密钥。
准备工作
- 安装必要的Python库:
pip install requests websocket-client pandas
核心API对接代码
1. 获取印度股票市场列表
import requests
def get_indian_stocks(page=1, page_size=50):
url = "https://api.stocktv.top/stock/stocks"
params = {
"countryId": 14, # 印度国家ID
"pageSize": page_size,
"page": page,
"key": "YOUR_KEY" # 替换为实际密钥
}
response = requests.get(url, params=params)
return response.json()
# 示例调用
stocks = get_indian_stocks()
print(f"获取到{len(stocks['data']['records'])}只印度股票")
2. 查询单个股票详情
def get_stock_detail(stock_id):
url = "https://api.stocktv.top/stock/queryStocks"
params = {
"id": stock_id, # 股票ID
"key": "YOUR_KEY"
}
response = requests.get(url, params=params)
return response.json()
# 示例调用
infosys_data = get_stock_detail(7310) # INFY股票ID
3. 获取印度主要指数
def get_indices():
url = "https://api.stocktv.top/stock/indices"
params = {
"countryId": 14,
"key": "YOUR_KEY"
}
response = requests.get(url, params=params)
return response.json()
# 示例调用
indices = get_indices()
nifty50 = next(i for i in indices['data'] if i['name'] == 'Nifty 50')
print(f"Nifty 50当前点位: {nifty50['last']}")
4. 获取股票K线数据
def get_kline_data(stock_id, interval="PT15M"):
url = "https://api.stocktv.top/stock/kline"
params = {
"pid": stock_id,
"interval": interval, # 时间间隔
"key": "YOUR_KEY"
}
response = requests.get(url, params=params)
return response.json()
# 示例调用
reliance_kline = get_kline_data(12345, "PT1H") # 获取信实工业1小时K线
5. 实时行情WebSocket
import websocket
import json
def on_message(ws, message):
data = json.loads(message)
print(f"{data['symbol']} 最新价: {data['last_numeric']}")
def on_error(ws, error):
print(f"WebSocket错误: {error}")
def on_close(ws, close_status_code, close_msg):
print("WebSocket连接关闭")
def on_open(ws):
print("实时行情连接成功")
# 建立WebSocket连接
ws_url = "wss://ws-api.stocktv.top/connect?key=YOUR_KEY"
ws = websocket.WebSocketApp(
ws_url,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close
)
# 启动监听(新线程中运行)
ws.run_forever()
高级功能实现
监控涨跌排行榜
def monitor_top_movers(mover_type=1):
"""监控涨幅榜/跌幅榜
mover_type: 1=涨幅榜 2=跌幅榜 3=涨停榜 4=跌停榜
"""
url = "https://api.stocktv.top/stock/updownList"
params = {
"countryId": 14,
"type": mover_type,
"key": "YOUR_KEY"
}
response = requests.get(url, params=params)
return response.json()
# 示例:获取当日涨幅前十
top_gainers = monitor_top_movers(1)['data'][:10]
IPO新股日历
def get_ipo_calendar():
url = "https://api.stocktv.top/stock/getIpo"
params = {
"countryId": 14,
"key": "YOUR_KEY"
}
response = requests.get(url, params=params)
return response.json()
# 示例:获取近期IPO
upcoming_ipos = [ipo for ipo in get_ipo_calendar()['data']
if ipo['ipoListing'] > time.time()]
完整示例:印度股票监控系统
import time
import requests
from threading import Thread
class IndiaStockMonitor:
def __init__(self, api_key):
self.api_key = api_key
self.tracked_stocks = {}
def add_stock(self, stock_id, alert_price):
"""添加监控股票"""
self.tracked_stocks[stock_id] = {
'alert_price': alert_price,
'current_price': None
}
def start_monitoring(self):
"""启动监控线程"""
Thread(target=self._monitor_prices).start()
def _monitor_prices(self):
while True:
for stock_id, data in self.tracked_stocks.items():
stock_data = get_stock_detail(stock_id)
current_price = stock_data['data'][0]['last']
# 价格突破提醒
if current_price >= data['alert_price']:
print(f"警报! {stock_id} 突破目标价 {data['alert_price']}")
# 更新当前价格
data['current_price'] = current_price
# 每分钟更新一次
time.sleep(60)
# 使用示例
monitor = IndiaStockMonitor("YOUR_API_KEY")
monitor.add_stock(7310, 1800) # 监控INFY,目标价1800
monitor.add_stock(12345, 2800) # 监控RELIANCE
monitor.start_monitoring()
常见问题解决
-
API返回错误代码
- 401:无效API密钥,请重新获取
- 429:请求过于频繁,添加延时
- 500:服务器错误,稍后重试
-
数据更新频率
- 实时行情:WebSocket毫秒级更新
- K线数据:1分钟级更新
- 公司信息:每日更新
-
关键参数参考
# 常用国家ID COUNTRIES = { 'india': 14, 'china': 37, 'usa': 43, 'japan': 35 } # K线周期 INTERVALS = { '5m': 'PT5M', '15m': 'PT15M', '1h': 'PT1H', '1d': 'P1D' }
结语
本文详细介绍了如何通过Python对接印度股票市场数据接口,包含实时行情、历史数据、公司基本面等核心功能。建议:
- 使用WebSocket获取实时行情而非频繁轮询
- 本地缓存常用股票元数据减少API调用
- 添加请求重试机制应对网络波动
如需获取最新API文档或技术支持,请访问https://t.me/stocktvpaopao

浙公网安备 33010602011771号