Skip to content

Commit 9df12cc

Browse files
committed
feat: add trade data method (#6)
v1.4.1
1 parent dc9908b commit 9df12cc

File tree

15 files changed

+219
-6
lines changed

15 files changed

+219
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ console.log(`共获取 ${allQuotes.length} 只股票`);
165165
|------|------|
166166
| `getFundFlow` | 资金流向 |
167167
| `getPanelLargeOrder` | 盘口大单占比 |
168+
| `getTradingCalendar` | A 股交易日历 |
168169

169170
### 批量查询
170171

README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ console.log(`Fetched ${allQuotes.length} stocks`);
165165
|--------|-------------|
166166
| `getFundFlow` | Fund flow |
167167
| `getPanelLargeOrder` | Large order ratio |
168+
| `getTradingCalendar` | A-share trading calendar |
168169

169170
### Batch Query
170171

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "stock-sdk",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"type": "module",
55
"main": "./dist/index.cjs",
66
"module": "./dist/index.js",

src/core/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export const A_SHARE_LIST_URL = 'https://assets.linkdiary.cn/shares/zh_a_list.js
1111
export const US_LIST_URL = 'https://assets.linkdiary.cn/shares/us_list.json';
1212
export const HK_LIST_URL = 'https://assets.linkdiary.cn/shares/hk_list.json';
1313

14+
// A 股交易日历
15+
export const TRADE_CALENDAR_URL = 'https://assets.linkdiary.cn/shares/trade-data-list.txt';
16+
1417
/** @deprecated 使用 A_SHARE_LIST_URL 代替 */
1518
export const CODE_LIST_URL = A_SHARE_LIST_URL;
1619

src/index.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,3 +1902,30 @@ describe('概念板块', () => {
19021902
});
19031903
});
19041904
});
1905+
1906+
// ==================== 扩展数据 ====================
1907+
1908+
describe('扩展数据', () => {
1909+
describe('getTradingCalendar', () => {
1910+
it('should return A股交易日历', async () => {
1911+
const res = await sdk.getTradingCalendar();
1912+
expect(Array.isArray(res)).toBe(true);
1913+
expect(res.length).toBeGreaterThan(0);
1914+
// 验证日期格式 YYYY-MM-DD
1915+
expect(res[0]).toMatch(/^\d{4}-\d{2}-\d{2}$/);
1916+
// 验证包含已知的历史交易日
1917+
expect(res).toContain('1990-12-19'); // 上证所开业日
1918+
expect(res).toContain('2024-01-02'); // 2024年第一个交易日
1919+
});
1920+
1921+
it('should return dates in chronological order', async () => {
1922+
const res = await sdk.getTradingCalendar();
1923+
// 检查前几个日期是按时间顺序排列的
1924+
for (let i = 1; i < Math.min(10, res.length); i++) {
1925+
expect(new Date(res[i]).getTime()).toBeGreaterThan(
1926+
new Date(res[i - 1]).getTime()
1927+
);
1928+
}
1929+
});
1930+
});
1931+
});

src/providers/tencent/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ export {
3131
type GetAllAShareQuotesOptions,
3232
} from './batch';
3333

34+
// 交易日历
35+
export { getTradingCalendar } from './tradeCalendar';
36+
3437
// 解析器(供内部使用)
3538
export * from './parsers';
3639

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* A 股交易日历
3+
*/
4+
import type { RequestClient } from '../../core';
5+
import { TRADE_CALENDAR_URL } from '../../core/constants';
6+
7+
/**
8+
* 获取 A 股交易日历
9+
* @param client 请求客户端
10+
* @returns 交易日期字符串数组,格式如 ['1990-12-19', '1990-12-20', ...]
11+
*/
12+
export async function getTradingCalendar(client: RequestClient): Promise<string[]> {
13+
const text = await client.get<string>(TRADE_CALENDAR_URL);
14+
15+
if (!text || text.trim() === '') {
16+
return [];
17+
}
18+
19+
// 按逗号分割并过滤空字符串
20+
return text
21+
.trim()
22+
.split(',')
23+
.map((date) => date.trim())
24+
.filter((date) => date.length > 0);
25+
}
26+

src/sdk.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,16 @@ export class StockSDK {
345345
return this.client.getTencentQuote(params);
346346
}
347347

348+
// ==================== 扩展数据 ====================
349+
350+
/**
351+
* 获取 A 股交易日历
352+
* @returns 交易日期字符串数组,格式如 ['1990-12-19', '1990-12-20', ...]
353+
*/
354+
getTradingCalendar(): Promise<string[]> {
355+
return tencent.getTradingCalendar(this.client);
356+
}
357+
348358
// ==================== 技术指标 ====================
349359

350360
/**

website/.vitepress/theme/components/Playground.vue

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,16 @@ console.log(flows[0].mainNetRatio); // 主力净流入占比`
406406
console.log(orders[0].buyLargeRatio); // 买盘大单占比
407407
console.log(orders[0].sellLargeRatio); // 卖盘大单占比`
408408
},
409+
getTradingCalendar: {
410+
name: 'getTradingCalendar',
411+
desc: '获取 A 股交易日历',
412+
category: 'extended',
413+
params: [],
414+
code: `const calendar = await sdk.getTradingCalendar();
415+
console.log(calendar.length); // 交易日总数
416+
console.log(calendar[0]); // '1990-12-19' (第一个交易日)
417+
console.log(calendar.slice(-5)); // 最近 5 个交易日`
418+
},
409419
}
410420
411421
// 按分类分组方法
@@ -515,6 +525,10 @@ async function fetchData() {
515525
data = await sdk.value.getPanelLargeOrder(codes)
516526
break
517527
}
528+
case 'getTradingCalendar': {
529+
data = await sdk.value.getTradingCalendar()
530+
break
531+
}
518532
case 'getHKQuotes': {
519533
const codes = params.codes.split(',').map(c => c.trim()).filter(Boolean)
520534
data = await sdk.value.getHKQuotes(codes)

website/api/fund-flow.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,61 @@
1-
# 资金流向
1+
# 扩展数据
2+
3+
## getTradingCalendar
4+
5+
获取 A 股交易日历,返回从 1990 年至未来的所有交易日列表。
6+
7+
### 签名
8+
9+
```typescript
10+
getTradingCalendar(): Promise<string[]>
11+
```
12+
13+
### 返回类型
14+
15+
```typescript
16+
string[] // 交易日期数组,格式如 ['1990-12-19', '1990-12-20', ...]
17+
```
18+
19+
### 示例
20+
21+
```typescript
22+
const calendar = await sdk.getTradingCalendar();
23+
24+
console.log(`共有 ${calendar.length} 个交易日`);
25+
console.log(`第一个交易日: ${calendar[0]}`); // 1990-12-19
26+
console.log(`最后一个交易日: ${calendar[calendar.length - 1]}`);
27+
28+
// 判断某天是否为交易日
29+
function isTradingDay(date: string): boolean {
30+
return calendar.includes(date);
31+
}
32+
33+
console.log(isTradingDay('2024-01-02')); // true
34+
console.log(isTradingDay('2024-01-01')); // false (元旦)
35+
```
36+
37+
### 应用场景
38+
39+
```typescript
40+
// 获取最近 N 个交易日
41+
function getRecentTradingDays(n: number): string[] {
42+
const today = new Date().toISOString().slice(0, 10);
43+
const idx = calendar.findIndex(d => d >= today);
44+
return calendar.slice(Math.max(0, idx - n), idx);
45+
}
46+
47+
// 计算两个日期之间的交易日数量
48+
function countTradingDays(start: string, end: string): number {
49+
return calendar.filter(d => d >= start && d <= end).length;
50+
}
51+
52+
// 获取下一个交易日
53+
function getNextTradingDay(date: string): string | undefined {
54+
return calendar.find(d => d > date);
55+
}
56+
```
57+
58+
---
259

360
## getFundFlow
461

0 commit comments

Comments
 (0)