npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

stock-sdk

v1.2.2

Published

Tencent qt.gtimg.cn stock quote SDK - supports both browser and Node.js

Readme

Stock SDK

npm version npm downloads license Test Coverage

English | 中文

前端和 Node.js 设计的股票行情 SDK

无需 Python、无需后端服务,直接在 浏览器或 Node.js 中获取 A 股 / 港股 / 美股 / 公募基金 的实时行情与 K 线数据。

✨ 零依赖 | 🌐 Browser + Node.js | 📦 <10KB | 🧠 完整 TypeScript 类型

📦 NPM | 📖 GitHub | 🎮 在线演示

Why stock-sdk?

如果你是前端工程师,可能遇到过这些问题:

  • 股票行情工具大多是 Python 生态,前端难以直接使用
  • 想做行情看板 / Demo,不想额外维护后端服务
  • 财经接口返回格式混乱、编码复杂(GBK / 并发 / 批量)
  • AkShare 很强,但并不适合浏览器或 Node.js 项目

stock-sdk 的目标很简单:

让前端工程师,用最熟悉的 JavaScript / TypeScript,优雅地获取股票行情数据。


使用场景

  • 📊 股票行情看板(Web / Admin)
  • 📈 数据可视化(ECharts / TradingView)
  • 🎓 股票 / 金融课程 Demo
  • 🧪 量化策略原型验证(JS / Node)
  • 🕒 Node.js 定时抓取行情数据

特性

  • 零依赖,轻量级(压缩后 < 10KB)
  • ✅ 支持 浏览器Node.js 18+ 双端运行
  • ✅ 同时提供 ESMCommonJS 两种模块格式
  • ✅ 完整的 TypeScript 类型定义的单元测试覆盖
  • A 股、港股、美股、公募基金实时行情
  • 历史 K 线(日/周/月)、分钟 K 线(1/5/15/30/60 分钟)和当日分时走势数据
  • 资金流向盘口大单等扩展数据
  • ✅ 获取全部 A 股代码列表(5000+ 只股票)和批量获取全市场行情(内置并发控制)

安装

npm install stock-sdk
# 或
yarn add stock-sdk
# 或
pnpm add stock-sdk

快速开始(10 行 Demo)

import { StockSDK } from 'stock-sdk';

const sdk = new StockSDK();

const quotes = await sdk.getSimpleQuotes([
  'sh000001',
  'sz000858',
  'sh600519',
]);

quotes.forEach(q => {
  console.log(`${q.name}: ${q.price} (${q.changePercent}%)`);
});

示例:全市场 A 股行情

前端直接一次性获取全市场 A 股行情(5000+股票),无需 Python 或后端服务。

const allQuotes = await sdk.getAllAShareQuotes({
  batchSize: 300,
  concurrency: 5,
  onProgress: (completed, total) => {
    console.log(`进度: ${completed}/${total}`);
  },
});

console.log(`共获取 ${allQuotes.length} 只股票`);

API 文档

实时行情

| 方法 | 说明 | |------|------| | getFullQuotes | A 股/指数全量行情 | | getSimpleQuotes | A 股/指数简要行情 | | getHKQuotes | 港股行情 | | getUSQuotes | 美股行情 | | getFundQuotes | 公募基金行情 |

K 线数据

| 方法 | 说明 | |------|------| | getHistoryKline | 历史 K 线(日/周/月) | | getMinuteKline | 分钟 K 线(1/5/15/30/60 分钟) | | getTodayTimeline | 当日分时走势 |

扩展数据

| 方法 | 说明 | |------|------| | getFundFlow | 资金流向 | | getPanelLargeOrder | 盘口大单占比 |

批量查询

| 方法 | 说明 | |------|------| | getAShareCodeList | 获取全部 A 股代码 | | getAllAShareQuotes | 获取全市场 A 股行情 | | getAllQuotesByCodes | 批量获取指定股票行情 |


getFullQuotes(codes): Promise<FullQuote[]>

获取 A 股/指数全量行情数据。

参数

| 参数 | 类型 | 说明 | |------|------|------| | codes | string[] | 股票代码数组,如 ['sz000858', 'sh600519'] |

返回类型

interface FullQuote {
  marketId: string;       // 市场标识
  name: string;           // 名称
  code: string;           // 股票代码
  price: number;          // 最新价
  prevClose: number;      // 昨收
  open: number;           // 今开
  high: number;           // 最高
  low: number;            // 最低
  volume: number;         // 成交量(手)
  amount: number;         // 成交额(万)
  change: number;         // 涨跌额
  changePercent: number;  // 涨跌幅 %
  bid: { price: number; volume: number }[];  // 买一~买五
  ask: { price: number; volume: number }[];  // 卖一~卖五
  turnoverRate: number | null;   // 换手率 %
  pe: number | null;             // 市盈率(TTM)
  pb: number | null;             // 市净率
  totalMarketCap: number | null; // 总市值(亿)
  circulatingMarketCap: number | null; // 流通市值(亿)
  volumeRatio: number | null;    // 量比
  limitUp: number | null;        // 涨停价
  limitDown: number | null;      // 跌停价
  // ... 更多字段见类型定义
}

示例

const quotes = await sdk.getFullQuotes(['sz000858']);
console.log(quotes[0].name);   // 五 粮 液
console.log(quotes[0].price);  // 111.70
console.log(quotes[0].changePercent);  // 2.35

getSimpleQuotes(codes): Promise<SimpleQuote[]>

获取简要行情(股票/指数)。

参数

| 参数 | 类型 | 说明 | |------|------|------| | codes | string[] | 代码数组,如 ['sz000858', 'sh000001'] |

返回类型

interface SimpleQuote {
  marketId: string;
  name: string;
  code: string;
  price: number;
  change: number;
  changePercent: number;
  volume: number;
  amount: number;
  marketCap: number | null;
  marketType: string;
}

示例

const quotes = await sdk.getSimpleQuotes(['sh000001']);
console.log(quotes[0].name);  // 上证指数

getHistoryKline(symbol, options?): Promise<HistoryKline[]>

获取 A 股历史 K 线(日/周/月),数据来源:东方财富。

参数

| 参数 | 类型 | 说明 | |------|------|------| | symbol | string | 股票代码,如 '000001''sz000001' | | options.period | 'daily' \| 'weekly' \| 'monthly' | K 线周期,默认 'daily' | | options.adjust | '' \| 'qfq' \| 'hfq' | 复权类型,默认 'hfq'(后复权) | | options.startDate | string | 开始日期 YYYYMMDD | | options.endDate | string | 结束日期 YYYYMMDD |

返回类型

interface HistoryKline {
  date: string;               // 日期 YYYY-MM-DD
  code: string;               // 股票代码
  open: number | null;        // 开盘价
  close: number | null;       // 收盘价
  high: number | null;        // 最高价
  low: number | null;         // 最低价
  volume: number | null;      // 成交量
  amount: number | null;      // 成交额
  changePercent: number | null;  // 涨跌幅 %
  change: number | null;         // 涨跌额
  amplitude: number | null;      // 振幅 %
  turnoverRate: number | null;   // 换手率 %
}

示例

// 获取日线(默认后复权)
const dailyKlines = await sdk.getHistoryKline('000001');

// 获取周线,前复权,指定日期范围
const weeklyKlines = await sdk.getHistoryKline('sz000858', {
  period: 'weekly',
  adjust: 'qfq',
  startDate: '20240101',
  endDate: '20241231',
});

getMinuteKline(symbol, options?): Promise<MinuteTimeline[] | MinuteKline[]>

获取 A 股分钟 K 线或分时数据,数据来源:东方财富。

注意period='1' 分时数据仅返回近 5 个交易日数据。

参数

| 参数 | 类型 | 说明 | |------|------|------| | symbol | string | 股票代码,如 '000001''sz000001' | | options.period | '1' \| '5' \| '15' \| '30' \| '60' | K 线周期,默认 '1'(分时) | | options.adjust | '' \| 'qfq' \| 'hfq' | 复权类型(仅 5/15/30/60 有效),默认 'hfq' | | options.startDate | string | 开始时间 YYYY-MM-DD HH:mm:ss | | options.endDate | string | 结束时间 YYYY-MM-DD HH:mm:ss |

示例

// 获取分时数据
const timeline = await sdk.getMinuteKline('000001');

// 获取 5 分钟 K 线
const kline5m = await sdk.getMinuteKline('sz000858', { period: '5' });

getTodayTimeline(code): Promise<TodayTimelineResponse>

获取当日分时走势数据,数据来源:腾讯财经。

注意:仅返回当日交易时段数据,成交量和成交额为累计值。

参数

| 参数 | 类型 | 说明 | |------|------|------| | code | string | 股票代码,如 'sz000001' |

返回类型

interface TodayTimelineResponse {
  code: string;             // 股票代码
  date: string;             // 交易日期 YYYYMMDD
  data: TodayTimeline[];    // 分时数据列表
}

interface TodayTimeline {
  time: string;      // 时间 HH:mm
  price: number;     // 成交价
  volume: number;    // 累计成交量(股)
  amount: number;    // 累计成交额(元)
  avgPrice: number;  // 当日均价
}

示例

const timeline = await sdk.getTodayTimeline('sz000001');
console.log(timeline.date);              // '20241218'
console.log(timeline.data[0].avgPrice);  // 当日均价

getAShareCodeList(includeExchange?): Promise<string[]>

获取全部 A 股代码列表(沪市、深市、北交所 5000+ 只股票)。

参数

| 参数 | 类型 | 说明 | |------|------|------| | includeExchange | boolean | 是否包含交易所前缀,默认 true |

示例

// 包含交易所前缀
const codes = await sdk.getAShareCodeList();
// ['sh600000', 'sz000001', 'bj430047', ...]

// 不包含交易所前缀
const pureCodes = await sdk.getAShareCodeList(false);
// ['600000', '000001', '430047', ...]

getAllAShareQuotes(options?): Promise<FullQuote[]>

获取全市场 A 股实时行情(5000+ 只股票),返回格式同 getFullQuotes

⚠️ 如遇超时或报错,可尝试减小 batchSize(如设为 100)。

参数

| 参数 | 类型 | 说明 | |------|------|------| | options.batchSize | number | 单次请求股票数量,默认 500 | | options.concurrency | number | 最大并发数,默认 7 | | options.onProgress | (completed, total) => void | 进度回调 |

示例

const allQuotes = await sdk.getAllAShareQuotes({
  batchSize: 300,
  concurrency: 3,
  onProgress: (completed, total) => {
    console.log(`进度: ${completed}/${total}`);
  },
});
console.log(`共获取 ${allQuotes.length} 只股票`);

getAllQuotesByCodes(codes, options?): Promise<FullQuote[]>

批量获取指定股票的全量行情,参数同 getAllAShareQuotes

const quotes = await sdk.getAllQuotesByCodes(
  ['sz000858', 'sh600519', 'sh600000'],
  { batchSize: 100, concurrency: 2 }
);

getFundFlow(codes): Promise<FundFlow[]>

获取资金流向数据。

参数

| 参数 | 类型 | 说明 | |------|------|------| | codes | string[] | 股票代码数组,如 ['sz000858'] |

返回类型

interface FundFlow {
  code: string;
  name: string;
  mainInflow: number;     // 主力流入
  mainOutflow: number;    // 主力流出
  mainNet: number;        // 主力净流入
  mainNetRatio: number;   // 主力净流入占比
  retailInflow: number;   // 散户流入
  retailOutflow: number;  // 散户流出
  retailNet: number;      // 散户净流入
  retailNetRatio: number; // 散户净流入占比
  totalFlow: number;      // 总资金流
  date: string;
}

getPanelLargeOrder(codes): Promise<PanelLargeOrder[]>

获取盘口大单占比。

参数

| 参数 | 类型 | 说明 | |------|------|------| | codes | string[] | 股票代码数组,如 ['sz000858'] |

返回类型

interface PanelLargeOrder {
  buyLargeRatio: number;   // 买盘大单占比
  buySmallRatio: number;   // 买盘小单占比
  sellLargeRatio: number;  // 卖盘大单占比
  sellSmallRatio: number;  // 卖盘小单占比
}

getHKQuotes(codes): Promise<HKQuote[]>

获取港股行情。

参数

| 参数 | 类型 | 说明 | |------|------|------| | codes | string[] | 港股代码数组,如 ['09988', '00700'] |

示例

const quotes = await sdk.getHKQuotes(['09988']);
console.log(quotes[0].name);  // 阿里巴巴-W

getUSQuotes(codes): Promise<USQuote[]>

获取美股行情。

参数

| 参数 | 类型 | 说明 | |------|------|------| | codes | string[] | 美股代码数组,如 ['BABA', 'AAPL'] |

示例

const quotes = await sdk.getUSQuotes(['BABA', 'AAPL']);
console.log(quotes[0].code);  // BABA.N

getFundQuotes(codes): Promise<FundQuote[]>

获取公募基金行情。

参数

| 参数 | 类型 | 说明 | |------|------|------| | codes | string[] | 基金代码数组,如 ['000001', '110011'] |

返回类型

interface FundQuote {
  code: string;
  name: string;
  nav: number;      // 最新单位净值
  accNav: number;   // 累计净值
  change: number;   // 当日涨跌额
  navDate: string;  // 净值日期
}

batchRaw(params): Promise<{ key: string; fields: string[] }[]>

批量混合查询,返回原始解析结果。

参数

| 参数 | 类型 | 说明 | |------|------|------| | params | string | 逗号分隔的查询参数,如 'sz000858,s_sh000001' |

示例

const raw = await sdk.batchRaw('sz000858,s_sh000001');
console.log(raw[0].key);     // sz000858
console.log(raw[0].fields);  // ['51', '五 粮 液', '000858', ...]

浏览器直接使用

SDK 使用原生 TextDecoder 解码 GBK 编码数据,无需额外 polyfill。

<script type="module">
  import { StockSDK } from 'https://unpkg.com/stock-sdk/dist/index.js';

  const sdk = new StockSDK();
  const quotes = await sdk.getFullQuotes(['sz000858']);
  console.log(quotes[0].name, quotes[0].price);
</script>

开发

# 安装依赖
yarn install

# 运行测试
yarn test

# 查看覆盖率
yarn test --coverage

# 构建
yarn build

# 启动调试页面
yarn dev

许可证

ISC


📦 NPM | 📖 GitHub | 🎮 在线演示 | 🐛 Issues


如果这个项目对你有帮助,欢迎 Star ⭐ 或提出 Issue 反馈。