@dshtrading/connector-akshare 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +75 -0
- package/README.md +34 -0
- package/lib/dataplane.d.ts +7 -0
- package/lib/dataplane.js +22 -0
- package/lib/index.d.ts +31 -0
- package/lib/index.js +124 -0
- package/lib/rest.d.ts +30 -0
- package/lib/rest.js +97 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
Copyright (c) 2026 zhu1090093659 (dsh-trading)
|
|
2
|
+
|
|
3
|
+
# PolyForm Noncommercial License 1.0.0
|
|
4
|
+
|
|
5
|
+
<https://polyformproject.org/licenses/noncommercial/1.0.0>
|
|
6
|
+
|
|
7
|
+
## Acceptance
|
|
8
|
+
|
|
9
|
+
In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
|
|
10
|
+
|
|
11
|
+
## Copyright License
|
|
12
|
+
|
|
13
|
+
The licensor grants you a copyright license for the software to do everything you might do with the software that would otherwise infringe the licensor's copyright in it for any permitted purpose. However, you may only distribute the software according to [Distribution License](#distribution-license) and make changes or new works based on the software according to [Changes and New Works License](#changes-and-new-works-license).
|
|
14
|
+
|
|
15
|
+
## Distribution License
|
|
16
|
+
|
|
17
|
+
The licensor grants you an additional copyright license to distribute copies of the software. Your license to distribute covers distributing the software with changes and new works permitted by [Changes and New Works License](#changes-and-new-works-license).
|
|
18
|
+
|
|
19
|
+
## Notices
|
|
20
|
+
|
|
21
|
+
You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms or the URL for them above, as well as copies of any plain-text lines beginning with `Required Notice:` that the licensor provided with the software. For example:
|
|
22
|
+
|
|
23
|
+
> Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
|
|
24
|
+
|
|
25
|
+
## Changes and New Works License
|
|
26
|
+
|
|
27
|
+
The licensor grants you an additional copyright license to make changes and new works based on the software for any permitted purpose.
|
|
28
|
+
|
|
29
|
+
## Patent License
|
|
30
|
+
|
|
31
|
+
The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
|
|
32
|
+
|
|
33
|
+
## Noncommercial Purposes
|
|
34
|
+
|
|
35
|
+
Any noncommercial purpose is a permitted purpose.
|
|
36
|
+
|
|
37
|
+
## Personal Uses
|
|
38
|
+
|
|
39
|
+
Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, is use for a permitted purpose.
|
|
40
|
+
|
|
41
|
+
## Noncommercial Organizations
|
|
42
|
+
|
|
43
|
+
Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution is use for a permitted purpose regardless of the source of funding or obligations resulting from the funding.
|
|
44
|
+
|
|
45
|
+
## Fair Use
|
|
46
|
+
|
|
47
|
+
You may have "fair use" rights for the software under the law. These terms do not limit them.
|
|
48
|
+
|
|
49
|
+
## No Other Rights
|
|
50
|
+
|
|
51
|
+
These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
|
|
52
|
+
|
|
53
|
+
## Patent Defense
|
|
54
|
+
|
|
55
|
+
If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
56
|
+
|
|
57
|
+
## Violations
|
|
58
|
+
|
|
59
|
+
The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
|
|
60
|
+
|
|
61
|
+
## No Liability
|
|
62
|
+
|
|
63
|
+
***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
|
|
64
|
+
|
|
65
|
+
## Definitions
|
|
66
|
+
|
|
67
|
+
The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
|
|
68
|
+
|
|
69
|
+
**You** refers to the individual or entity agreeing to these terms.
|
|
70
|
+
|
|
71
|
+
**Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. **Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
72
|
+
|
|
73
|
+
**Your licenses** are all the licenses granted to you for the software under these terms.
|
|
74
|
+
|
|
75
|
+
**Use** means anything you do with the software requiring one of your licenses.
|
package/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# @dshtrading/connector-template
|
|
2
|
+
|
|
3
|
+
**本包是脚手架源,不是可安装的连接器**:不入任何 bundle 依赖,仅在 `pnpm -r build/test`
|
|
4
|
+
基线内保证可编译、冒烟测试绿。真实接入用生成器:
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
node scripts/new-connector.mjs --slug bybit --title Bybit --market crypto
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
生成后按 [docs/connector-playbook.md](../../docs/connector-playbook.md) 的填写顺序
|
|
11
|
+
(src/rest.ts 头部清单 7 项)实现 TODO。每个 TODO 的参照段都在 connector-okx
|
|
12
|
+
(packages/connector-okx/),它是本仓第一个真实 TradeService 连接器。
|
|
13
|
+
|
|
14
|
+
## 结构
|
|
15
|
+
|
|
16
|
+
- `src/rest.ts` —— 错误载体(TradingServiceError)+ 通用请求管线(超时/错误映射/
|
|
17
|
+
模拟盘头/注入 fetch)直接可用;签名与端点 TODO。
|
|
18
|
+
- `src/index.ts` —— Config(enabled/env/dryRun/liveTrading + 三 ref 凭证组)、
|
|
19
|
+
闸门三态判定、凭证解析(BYOK + 环境变量回落)、MarketDataService/TradeService 骨架、
|
|
20
|
+
7 个市场前缀工具注册(duplicate-safe)、apply(enabled=false 静默退出)。
|
|
21
|
+
- `test/template.test.ts` —— 结构冒烟 7 用例。
|
|
22
|
+
|
|
23
|
+
## Token
|
|
24
|
+
|
|
25
|
+
`akshare`(slug)/ `Akshare`(title)/ `AKSHARE`(ref 前缀)/
|
|
26
|
+
`crypto`(工具前缀)/ `Crypto`(服务键 infix)。token 未展开也可编译;
|
|
27
|
+
生成器保证全部替换。
|
|
28
|
+
|
|
29
|
+
## 注意(勿改的坑)
|
|
30
|
+
|
|
31
|
+
- 服务类用 TS 编译期 `private`(禁 ECMAScript `#`,realm 代理会炸)。
|
|
32
|
+
- 工具名 = 市场前缀 + 语义词(不带交易所名)——base 闸门正则只认市场前缀。
|
|
33
|
+
- 行 id = `dsh-trading-<market>-connector-<slug>`,全仓唯一(insert-only 铁律 #1)。
|
|
34
|
+
- 凭证只进 ref,绝不内置/落日志(铁律 #3/#5)。
|
package/lib/dataplane.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { AkshareMarketDataService, TRADING_CN_MARKET_DATA_KEY } from "./index.js";
|
|
2
|
+
//#region src/dataplane.ts
|
|
3
|
+
const inject = [];
|
|
4
|
+
function resolveMarketDataRegistry(ctx) {
|
|
5
|
+
const candidate = ctx.get?.("tradingMarketDataRegistry", false);
|
|
6
|
+
return candidate !== void 0 ? candidate : void 0;
|
|
7
|
+
}
|
|
8
|
+
const ROUTER_PROVIDER = "akshare";
|
|
9
|
+
const MARKET = "cn";
|
|
10
|
+
function apply(ctx, config) {
|
|
11
|
+
if (!config.enabled) return;
|
|
12
|
+
const registry = resolveMarketDataRegistry(ctx);
|
|
13
|
+
if (registry === void 0) {
|
|
14
|
+
new AkshareMarketDataService(ctx, { apiUrl: config.apiUrl });
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const inner = ctx.isolate(TRADING_CN_MARKET_DATA_KEY);
|
|
18
|
+
const service = new AkshareMarketDataService(inner, { apiUrl: config.apiUrl });
|
|
19
|
+
ctx.effect(() => registry.register(MARKET, ROUTER_PROVIDER, service));
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
export { apply, inject };
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { AkshareRestClient, AkshareRestOptions, INTERVAL_VOCABULARY, Interval, Kline, Ticker, TradingServiceError, toEastmoneySecid } from "./rest.js";
|
|
2
|
+
import { Context, Service } from "@deepseek-ai/cordis";
|
|
3
|
+
import Schema from "@deepseek-ai/schemastery";
|
|
4
|
+
import { Disposable, Interval as Interval$1, Kline as Kline$1, MarketDataService, Ticker as Ticker$1 } from "@dshtrading/api";
|
|
5
|
+
//#region src/index.d.ts
|
|
6
|
+
declare const name = "dsh-trading-cn-connector-akshare";
|
|
7
|
+
interface Config {
|
|
8
|
+
enabled: boolean;
|
|
9
|
+
apiUrl?: string;
|
|
10
|
+
}
|
|
11
|
+
declare const Config: Schema<Config>;
|
|
12
|
+
declare const TRADING_CN_MARKET_DATA_KEY = "tradingCnMarketData";
|
|
13
|
+
declare class AkshareMarketDataService extends Service implements MarketDataService {
|
|
14
|
+
private readonly client;
|
|
15
|
+
constructor(ctx: Context, options?: AkshareRestOptions, serviceName?: string);
|
|
16
|
+
getTicker(symbol: string): Promise<Ticker$1>;
|
|
17
|
+
getKlines(symbol: string, interval?: Interval$1, limit?: number): Promise<Kline$1[]>;
|
|
18
|
+
getSectorFundFlow(): Promise<Array<{
|
|
19
|
+
name: string;
|
|
20
|
+
changePercent: number;
|
|
21
|
+
mainNetInflow: number;
|
|
22
|
+
}>>;
|
|
23
|
+
subscribeTicker(symbol: string, cb: (ticker: Ticker$1) => void, options?: {
|
|
24
|
+
intervalMs?: number;
|
|
25
|
+
}): Disposable;
|
|
26
|
+
}
|
|
27
|
+
declare const ROUTER_PROVIDER = "akshare";
|
|
28
|
+
declare function routeAllows(ctx: Context, config: Config, market: string): boolean;
|
|
29
|
+
declare function apply(ctx: Context, config: Config): void;
|
|
30
|
+
//#endregion
|
|
31
|
+
export { AkshareMarketDataService, AkshareRestClient, AkshareRestOptions, Config, INTERVAL_VOCABULARY, type Interval, type Kline, ROUTER_PROVIDER, TRADING_CN_MARKET_DATA_KEY, type Ticker, TradingServiceError, apply, name, routeAllows, toEastmoneySecid };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { AkshareRestClient, INTERVAL_VOCABULARY, TradingServiceError, toEastmoneySecid } from "./rest.js";
|
|
2
|
+
import { Service } from "@deepseek-ai/cordis";
|
|
3
|
+
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
4
|
+
import Schema from "@deepseek-ai/schemastery";
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
const name = "dsh-trading-cn-connector-akshare";
|
|
7
|
+
const Config = Schema.object({
|
|
8
|
+
enabled: Schema.boolean().default(true).description("是否激活 AkShare 连接器"),
|
|
9
|
+
apiUrl: Schema.string().default("http://127.0.0.1:8080").description("AkShare 本地 RPC/HTTP 服务地址")
|
|
10
|
+
});
|
|
11
|
+
const TRADING_CN_MARKET_DATA_KEY = "tradingCnMarketData";
|
|
12
|
+
var AkshareMarketDataService = class extends Service {
|
|
13
|
+
client;
|
|
14
|
+
constructor(ctx, options = {}, serviceName = TRADING_CN_MARKET_DATA_KEY) {
|
|
15
|
+
super(ctx, serviceName);
|
|
16
|
+
this.client = new AkshareRestClient(options);
|
|
17
|
+
}
|
|
18
|
+
async getTicker(symbol) {
|
|
19
|
+
return this.client.getTicker(symbol);
|
|
20
|
+
}
|
|
21
|
+
async getKlines(symbol, interval = "1d", limit = 100) {
|
|
22
|
+
return this.client.getKlines(symbol, interval, limit);
|
|
23
|
+
}
|
|
24
|
+
async getSectorFundFlow() {
|
|
25
|
+
return this.client.getSectorFundFlow();
|
|
26
|
+
}
|
|
27
|
+
subscribeTicker(symbol, cb, options) {
|
|
28
|
+
const ms = Math.max(options?.intervalMs ?? 5e3, 1e3);
|
|
29
|
+
const tick = () => {
|
|
30
|
+
this.getTicker(symbol).then(cb, () => {});
|
|
31
|
+
};
|
|
32
|
+
tick();
|
|
33
|
+
const timer = setInterval(tick, ms);
|
|
34
|
+
return { dispose: () => clearInterval(timer) };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const ROUTER_PROVIDER = "akshare";
|
|
38
|
+
function routeAllows(ctx, config, market) {
|
|
39
|
+
if (!config.enabled) return false;
|
|
40
|
+
const router = ctx.get?.("tradingMarketRouter", false);
|
|
41
|
+
if (router === void 0) return true;
|
|
42
|
+
return router.activeProvider(market) === ROUTER_PROVIDER;
|
|
43
|
+
}
|
|
44
|
+
function apply(ctx, config) {
|
|
45
|
+
if (!config.enabled) return;
|
|
46
|
+
if (!routeAllows(ctx, config, "cn")) return;
|
|
47
|
+
const marketData = new AkshareMarketDataService(ctx, { apiUrl: config.apiUrl });
|
|
48
|
+
ctx.inject(["tools"], (ctx) => {
|
|
49
|
+
const tools = ctx.tools;
|
|
50
|
+
const register = (t) => {
|
|
51
|
+
if (tools.get(t.name) === void 0) tools.register(t);
|
|
52
|
+
};
|
|
53
|
+
register(defineTool({
|
|
54
|
+
name: "cn_get_ticker",
|
|
55
|
+
description: "Get the latest trade price and quote for an A-share stock via AkShare/Eastmoney API.",
|
|
56
|
+
parameters: { symbol: {
|
|
57
|
+
type: "string",
|
|
58
|
+
required: true,
|
|
59
|
+
description: "A-share stock symbol, e.g. 600519.SH"
|
|
60
|
+
} },
|
|
61
|
+
output: {
|
|
62
|
+
schema: { type: "string" },
|
|
63
|
+
render: (_a, v) => [{
|
|
64
|
+
type: "text",
|
|
65
|
+
text: v
|
|
66
|
+
}]
|
|
67
|
+
},
|
|
68
|
+
async execute(args) {
|
|
69
|
+
return JSON.stringify(await marketData.getTicker(args.symbol));
|
|
70
|
+
}
|
|
71
|
+
}));
|
|
72
|
+
register(defineTool({
|
|
73
|
+
name: "cn_get_klines",
|
|
74
|
+
description: "Get recent public klines for an A-share stock via AkShare/Eastmoney API.",
|
|
75
|
+
parameters: {
|
|
76
|
+
symbol: {
|
|
77
|
+
type: "string",
|
|
78
|
+
required: true,
|
|
79
|
+
description: "A-share stock symbol, e.g. 600519.SH"
|
|
80
|
+
},
|
|
81
|
+
interval: {
|
|
82
|
+
type: "string",
|
|
83
|
+
enum: INTERVAL_VOCABULARY,
|
|
84
|
+
default: "1d",
|
|
85
|
+
description: "Interval"
|
|
86
|
+
},
|
|
87
|
+
limit: {
|
|
88
|
+
type: "integer",
|
|
89
|
+
default: 100,
|
|
90
|
+
description: "Limit"
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
output: {
|
|
94
|
+
schema: { type: "string" },
|
|
95
|
+
render: (_a, v) => [{
|
|
96
|
+
type: "text",
|
|
97
|
+
text: v
|
|
98
|
+
}]
|
|
99
|
+
},
|
|
100
|
+
async execute(args) {
|
|
101
|
+
const klines = await marketData.getKlines(args.symbol, args.interval ?? "1d", args.limit);
|
|
102
|
+
return JSON.stringify(klines);
|
|
103
|
+
}
|
|
104
|
+
}));
|
|
105
|
+
register(defineTool({
|
|
106
|
+
name: "cn_get_sector_fund_flow",
|
|
107
|
+
description: "Get sector/industry fund flow and main capital net inflow ranking via AkShare.",
|
|
108
|
+
parameters: {},
|
|
109
|
+
output: {
|
|
110
|
+
schema: { type: "string" },
|
|
111
|
+
render: (_a, v) => [{
|
|
112
|
+
type: "text",
|
|
113
|
+
text: v
|
|
114
|
+
}]
|
|
115
|
+
},
|
|
116
|
+
async execute() {
|
|
117
|
+
const list = await marketData.getSectorFundFlow();
|
|
118
|
+
return JSON.stringify(list);
|
|
119
|
+
}
|
|
120
|
+
}));
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
//#endregion
|
|
124
|
+
export { AkshareMarketDataService, AkshareRestClient, Config, INTERVAL_VOCABULARY, ROUTER_PROVIDER, TRADING_CN_MARKET_DATA_KEY, TradingServiceError, apply, name, routeAllows, toEastmoneySecid };
|
package/lib/rest.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Interval as Interval$1, Kline as Kline$1, Ticker as Ticker$1, TradingErrorCode } from "@dshtrading/api";
|
|
2
|
+
//#region src/rest.d.ts
|
|
3
|
+
declare class TradingServiceError extends Error {
|
|
4
|
+
readonly code: TradingErrorCode;
|
|
5
|
+
constructor(code: TradingErrorCode, message: string, cause?: unknown);
|
|
6
|
+
}
|
|
7
|
+
declare const INTERVAL_VOCABULARY: readonly ["1m", "5m", "15m", "30m", "1h", "1d", "1w", "1M"];
|
|
8
|
+
declare function toEastmoneySecid(symbol: string): {
|
|
9
|
+
secid: string;
|
|
10
|
+
canonical: string;
|
|
11
|
+
};
|
|
12
|
+
interface AkshareRestOptions {
|
|
13
|
+
apiUrl?: string;
|
|
14
|
+
fetchImpl?: typeof fetch;
|
|
15
|
+
}
|
|
16
|
+
declare class AkshareRestClient {
|
|
17
|
+
readonly apiUrl: string;
|
|
18
|
+
private readonly fetchImpl;
|
|
19
|
+
constructor(options?: AkshareRestOptions);
|
|
20
|
+
private requestJson;
|
|
21
|
+
getTicker(symbol: string): Promise<Ticker$1>;
|
|
22
|
+
getKlines(symbol: string, interval?: Interval$1, limit?: number): Promise<Kline$1[]>;
|
|
23
|
+
getSectorFundFlow(): Promise<Array<{
|
|
24
|
+
name: string;
|
|
25
|
+
changePercent: number;
|
|
26
|
+
mainNetInflow: number;
|
|
27
|
+
}>>;
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
export { AkshareRestClient, AkshareRestOptions, INTERVAL_VOCABULARY, type Interval$1 as Interval, type Kline$1 as Kline, type Ticker$1 as Ticker, TradingServiceError, toEastmoneySecid };
|
package/lib/rest.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
//#region src/rest.ts
|
|
2
|
+
var TradingServiceError = class extends Error {
|
|
3
|
+
code;
|
|
4
|
+
constructor(code, message, cause) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "TradingServiceError";
|
|
7
|
+
this.code = code;
|
|
8
|
+
if (cause !== void 0) this.cause = cause;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
const INTERVAL_VOCABULARY = [
|
|
12
|
+
"1m",
|
|
13
|
+
"5m",
|
|
14
|
+
"15m",
|
|
15
|
+
"30m",
|
|
16
|
+
"1h",
|
|
17
|
+
"1d",
|
|
18
|
+
"1w",
|
|
19
|
+
"1M"
|
|
20
|
+
];
|
|
21
|
+
function toEastmoneySecid(symbol) {
|
|
22
|
+
const clean = symbol.trim().toUpperCase();
|
|
23
|
+
let code = clean;
|
|
24
|
+
let market = "SH";
|
|
25
|
+
if (clean.includes(".")) {
|
|
26
|
+
const parts = clean.split(".");
|
|
27
|
+
code = parts[0];
|
|
28
|
+
market = parts[1];
|
|
29
|
+
} else if (code.startsWith("0") || code.startsWith("3")) market = "SZ";
|
|
30
|
+
return {
|
|
31
|
+
secid: `${market === "SH" ? "1" : "0"}.${code}`,
|
|
32
|
+
canonical: `${code}.${market}`
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
var AkshareRestClient = class {
|
|
36
|
+
apiUrl;
|
|
37
|
+
fetchImpl;
|
|
38
|
+
constructor(options = {}) {
|
|
39
|
+
this.apiUrl = options.apiUrl ?? (process.env.AKSHARE_API_URL || "http://127.0.0.1:8080");
|
|
40
|
+
this.fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
41
|
+
}
|
|
42
|
+
async requestJson(url) {
|
|
43
|
+
try {
|
|
44
|
+
const res = await this.fetchImpl(url, { headers: { "Accept": "application/json" } });
|
|
45
|
+
if (!res.ok) throw new TradingServiceError("TRADING_UPSTREAM_ERROR", `Akshare HTTP ${res.status}: ${res.statusText}`);
|
|
46
|
+
return await res.json();
|
|
47
|
+
} catch (err) {
|
|
48
|
+
if (err instanceof TradingServiceError) throw err;
|
|
49
|
+
throw new TradingServiceError("TRADING_NETWORK", `Akshare network error: ${err instanceof Error ? err.message : String(err)}`, err);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async getTicker(symbol) {
|
|
53
|
+
const { secid, canonical } = toEastmoneySecid(symbol);
|
|
54
|
+
const url = `https://push2.eastmoney.com/api/qt/stock/get?secid=${secid}&fields=f43,f47,f60,f86,f170`;
|
|
55
|
+
const res = await this.requestJson(url);
|
|
56
|
+
if (!res.data) throw new TradingServiceError("TRADING_SYMBOL_NOT_FOUND", `AkShare/Eastmoney quote not found: ${symbol}`);
|
|
57
|
+
const rawPrice = typeof res.data.f43 === "number" ? res.data.f43 / 100 : 0;
|
|
58
|
+
const prevClose = typeof res.data.f60 === "number" && res.data.f60 > 0 ? res.data.f60 / 100 : void 0;
|
|
59
|
+
const changePercent = typeof res.data.f170 === "number" && Number.isFinite(res.data.f170) ? res.data.f170 / 100 : void 0;
|
|
60
|
+
return {
|
|
61
|
+
symbol: canonical,
|
|
62
|
+
price: rawPrice > 0 ? rawPrice : 0,
|
|
63
|
+
volume: res.data.f47 ?? 0,
|
|
64
|
+
timestamp: res.data.f86 ? res.data.f86 * 1e3 : Date.now(),
|
|
65
|
+
...prevClose !== void 0 ? { prevClose } : {},
|
|
66
|
+
...changePercent !== void 0 ? { changePercent } : {}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
async getKlines(symbol, interval = "1d", limit = 100) {
|
|
70
|
+
const { secid } = toEastmoneySecid(symbol);
|
|
71
|
+
const url = `https://push2his.eastmoney.com/api/qt/stock/kline/get?secid=${secid}&klt=${interval === "5m" ? "5" : interval === "15m" ? "15" : interval === "30m" ? "30" : interval === "1h" ? "60" : "101"}&fqt=1&lmt=${limit}&fields2=f51,f52,f53,f54,f55,f56`;
|
|
72
|
+
const res = await this.requestJson(url);
|
|
73
|
+
if (!res.data?.klines) return [];
|
|
74
|
+
return res.data.klines.map((line) => {
|
|
75
|
+
const parts = line.split(",");
|
|
76
|
+
const openTime = new Date(parts[0].replace(/-/g, "/")).getTime();
|
|
77
|
+
return {
|
|
78
|
+
openTime,
|
|
79
|
+
open: parseFloat(parts[1]),
|
|
80
|
+
close: parseFloat(parts[2]),
|
|
81
|
+
high: parseFloat(parts[3]),
|
|
82
|
+
low: parseFloat(parts[4]),
|
|
83
|
+
volume: parseFloat(parts[5]),
|
|
84
|
+
closeTime: openTime + 864e5 - 1
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
async getSectorFundFlow() {
|
|
89
|
+
return ((await this.requestJson("https://push2.eastmoney.com/api/qt/clist/get?pn=1&pz=10&po=1&np=1&fields=f12,f14,f3,f62&fs=m:90+t:2")).data?.diff ?? []).map((item) => ({
|
|
90
|
+
name: item.f14,
|
|
91
|
+
changePercent: typeof item.f3 === "number" ? item.f3 / 100 : 0,
|
|
92
|
+
mainNetInflow: item.f62
|
|
93
|
+
}));
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
//#endregion
|
|
97
|
+
export { AkshareRestClient, INTERVAL_VOCABULARY, TradingServiceError, toEastmoneySecid };
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dshtrading/connector-akshare",
|
|
3
|
+
"description": "【模板】交易所连接器插件脚手架:Akshare(akshare)v2 插件骨架 —— 市场数据服务 + 交易服务 + 三态闸门 + BYOK 凭证 ref。生成器:node scripts/new-connector.mjs --slug <exchange>",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"types": "./lib/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/index.d.ts",
|
|
11
|
+
"default": "./lib/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./dataplane": {
|
|
14
|
+
"types": "./lib/dataplane.d.ts",
|
|
15
|
+
"default": "./lib/dataplane.js"
|
|
16
|
+
},
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"lib"
|
|
21
|
+
],
|
|
22
|
+
"license": "PolyForm-Noncommercial-1.0.0",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@dshtrading/api": "^0.1.0"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"@deepseek-ai/cordis": ">=4.0.0",
|
|
28
|
+
"@deepseek-ai/dsh-tools": ">=0.1.2-alpha.1",
|
|
29
|
+
"@deepseek-ai/schemastery": ">=3.18.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"tsdown": "^0.22.0",
|
|
33
|
+
"vitest": "^3.0.0"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsdown",
|
|
37
|
+
"test": "vitest run"
|
|
38
|
+
}
|
|
39
|
+
}
|