@dsh-ft/dsh-fft-paper-trading 0.7.2 → 0.8.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/README.md +4 -3
- package/README.zh.md +4 -3
- package/lib/agent/account-tools.d.ts +18 -0
- package/lib/agent/account-tools.d.ts.map +1 -0
- package/lib/agent/account-tools.js +127 -0
- package/lib/agent/account-tools.js.map +1 -0
- package/lib/agent/futures-tools.d.ts.map +1 -1
- package/lib/agent/futures-tools.js +1 -6
- package/lib/agent/futures-tools.js.map +1 -1
- package/lib/agent/ledger-tools.d.ts +15 -3
- package/lib/agent/ledger-tools.d.ts.map +1 -1
- package/lib/agent/ledger-tools.js +11 -15
- package/lib/agent/ledger-tools.js.map +1 -1
- package/lib/agent/tool-result.d.ts +19 -0
- package/lib/agent/tool-result.d.ts.map +1 -0
- package/lib/agent/tool-result.js +22 -0
- package/lib/agent/tool-result.js.map +1 -0
- package/lib/agent/tools.d.ts +10 -2
- package/lib/agent/tools.d.ts.map +1 -1
- package/lib/agent/tools.js +26 -11
- package/lib/agent/tools.js.map +1 -1
- package/lib/client.js +1448 -282
- package/lib/contract/accounts.d.ts +76 -0
- package/lib/contract/accounts.d.ts.map +1 -0
- package/lib/contract/accounts.js +6 -0
- package/lib/contract/accounts.js.map +1 -0
- package/lib/contract/common.d.ts +144 -0
- package/lib/contract/common.d.ts.map +1 -0
- package/lib/contract/common.js +33 -0
- package/lib/contract/common.js.map +1 -0
- package/lib/contract/domain.d.ts +196 -0
- package/lib/contract/domain.d.ts.map +1 -0
- package/lib/contract/domain.js +14 -0
- package/lib/contract/domain.js.map +1 -0
- package/lib/contract/errors.d.ts +6 -0
- package/lib/contract/errors.d.ts.map +1 -0
- package/lib/contract/errors.js +6 -0
- package/lib/contract/errors.js.map +1 -0
- package/lib/contract/index.d.ts +15 -0
- package/lib/contract/index.d.ts.map +1 -0
- package/lib/contract/index.js +15 -0
- package/lib/contract/index.js.map +1 -0
- package/lib/contract/ledger.d.ts +40 -0
- package/lib/contract/ledger.d.ts.map +1 -0
- package/lib/contract/ledger.js +6 -0
- package/lib/contract/ledger.js.map +1 -0
- package/lib/contract/market.d.ts +30 -0
- package/lib/contract/market.d.ts.map +1 -0
- package/lib/contract/market.js +6 -0
- package/lib/contract/market.js.map +1 -0
- package/lib/contract/rpc.d.ts +24 -0
- package/lib/contract/rpc.d.ts.map +1 -0
- package/lib/contract/rpc.js +6 -0
- package/lib/contract/rpc.js.map +1 -0
- package/lib/contract.d.ts +6 -403
- package/lib/contract.d.ts.map +1 -1
- package/lib/contract.js +6 -24
- package/lib/contract.js.map +1 -1
- package/lib/core/session.d.ts +1 -1
- package/lib/core/session.d.ts.map +1 -1
- package/lib/core/session.js +1 -2
- package/lib/core/session.js.map +1 -1
- package/lib/data/cached-market-data.d.ts +61 -0
- package/lib/data/cached-market-data.d.ts.map +1 -0
- package/lib/data/cached-market-data.js +174 -0
- package/lib/data/cached-market-data.js.map +1 -0
- package/lib/data/futures-market-data.d.ts.map +1 -1
- package/lib/data/futures-market-data.js +9 -10
- package/lib/data/futures-market-data.js.map +1 -1
- package/lib/index.d.ts +25 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +156 -18
- package/lib/index.js.map +1 -1
- package/lib/service/account-moves.d.ts +64 -0
- package/lib/service/account-moves.d.ts.map +1 -0
- package/lib/service/account-moves.js +49 -0
- package/lib/service/account-moves.js.map +1 -0
- package/lib/service/accounts-rpc.d.ts +17 -0
- package/lib/service/accounts-rpc.d.ts.map +1 -0
- package/lib/service/accounts-rpc.js +77 -0
- package/lib/service/accounts-rpc.js.map +1 -0
- package/lib/service/compare-rpc.d.ts +22 -0
- package/lib/service/compare-rpc.d.ts.map +1 -0
- package/lib/service/compare-rpc.js +70 -0
- package/lib/service/compare-rpc.js.map +1 -0
- package/lib/service/errors.d.ts +20 -4
- package/lib/service/errors.d.ts.map +1 -1
- package/lib/service/errors.js +12 -5
- package/lib/service/errors.js.map +1 -1
- package/lib/service/futures-rpc.d.ts.map +1 -1
- package/lib/service/futures-rpc.js +7 -25
- package/lib/service/futures-rpc.js.map +1 -1
- package/lib/service/futures-service.d.ts +28 -8
- package/lib/service/futures-service.d.ts.map +1 -1
- package/lib/service/futures-service.js +144 -146
- package/lib/service/futures-service.js.map +1 -1
- package/lib/service/ledger-admin.d.ts +67 -0
- package/lib/service/ledger-admin.d.ts.map +1 -0
- package/lib/service/ledger-admin.js +102 -0
- package/lib/service/ledger-admin.js.map +1 -0
- package/lib/service/paper-service.d.ts +39 -12
- package/lib/service/paper-service.d.ts.map +1 -1
- package/lib/service/paper-service.js +177 -156
- package/lib/service/paper-service.js.map +1 -1
- package/lib/service/route.d.ts.map +1 -1
- package/lib/service/route.js +5 -2
- package/lib/service/route.js.map +1 -1
- package/lib/service/rpc-support.d.ts +34 -0
- package/lib/service/rpc-support.d.ts.map +1 -0
- package/lib/service/rpc-support.js +57 -0
- package/lib/service/rpc-support.js.map +1 -0
- package/lib/service/rpc.d.ts +20 -3
- package/lib/service/rpc.d.ts.map +1 -1
- package/lib/service/rpc.js +44 -25
- package/lib/service/rpc.js.map +1 -1
- package/lib/store/accounts.d.ts +110 -0
- package/lib/store/accounts.d.ts.map +1 -0
- package/lib/store/accounts.js +352 -0
- package/lib/store/accounts.js.map +1 -0
- package/lib/store/atomic-file.d.ts +16 -0
- package/lib/store/atomic-file.d.ts.map +1 -0
- package/lib/store/atomic-file.js +34 -0
- package/lib/store/atomic-file.js.map +1 -0
- package/lib/store/futures-repo.d.ts +8 -49
- package/lib/store/futures-repo.d.ts.map +1 -1
- package/lib/store/futures-repo.js +34 -246
- package/lib/store/futures-repo.js.map +1 -1
- package/lib/store/ledger-repo-base.d.ts +120 -0
- package/lib/store/ledger-repo-base.d.ts.map +1 -0
- package/lib/store/ledger-repo-base.js +275 -0
- package/lib/store/ledger-repo-base.js.map +1 -0
- package/lib/store/notes.d.ts +29 -0
- package/lib/store/notes.d.ts.map +1 -0
- package/lib/store/notes.js +78 -0
- package/lib/store/notes.js.map +1 -0
- package/lib/store/paths.d.ts +2 -0
- package/lib/store/paths.d.ts.map +1 -1
- package/lib/store/paths.js +4 -0
- package/lib/store/paths.js.map +1 -1
- package/lib/store/repo.d.ts +15 -55
- package/lib/store/repo.d.ts.map +1 -1
- package/lib/store/repo.js +41 -206
- package/lib/store/repo.js.map +1 -1
- package/lib/store/shortlist.d.ts.map +1 -1
- package/lib/store/shortlist.js +4 -6
- package/lib/store/shortlist.js.map +1 -1
- package/lib/store/snapshots.d.ts +0 -3
- package/lib/store/snapshots.d.ts.map +1 -1
- package/lib/store/snapshots.js +0 -3
- package/lib/store/snapshots.js.map +1 -1
- package/package.json +1 -1
- package/src/agent/account-tools.ts +136 -0
- package/src/agent/futures-tools.ts +1 -6
- package/src/agent/ledger-tools.ts +29 -16
- package/src/agent/tool-result.ts +27 -0
- package/src/agent/tools.ts +30 -10
- package/src/client/entry.js +14 -0
- package/src/client/parts/10-core.js +535 -0
- package/src/client/parts/20-styles.js +336 -0
- package/src/client/parts/30-atoms.js +769 -0
- package/src/client/parts/35-hooks.js +447 -0
- package/src/client/parts/40-stock-tabs.js +641 -0
- package/src/client/parts/50-futures.js +911 -0
- package/src/client/parts/60-shell.js +706 -0
- package/src/client/parts/70-plugin.js +67 -0
- package/src/contract/accounts.ts +82 -0
- package/src/contract/common.ts +173 -0
- package/src/contract/domain.ts +217 -0
- package/src/contract/errors.ts +34 -0
- package/src/contract/index.ts +15 -0
- package/src/contract/ledger.ts +42 -0
- package/src/contract/market.ts +39 -0
- package/src/contract/rpc.ts +15 -0
- package/src/contract.ts +6 -472
- package/src/core/session.ts +1 -2
- package/src/data/cached-market-data.ts +193 -0
- package/src/data/futures-market-data.ts +9 -10
- package/src/index.ts +168 -20
- package/src/service/account-moves.ts +72 -0
- package/src/service/accounts-rpc.ts +80 -0
- package/src/service/compare-rpc.ts +77 -0
- package/src/service/errors.ts +23 -4
- package/src/service/futures-rpc.ts +7 -24
- package/src/service/futures-service.ts +161 -159
- package/src/service/ledger-admin.ts +140 -0
- package/src/service/paper-service.ts +183 -153
- package/src/service/route.ts +5 -2
- package/src/service/rpc-support.ts +61 -0
- package/src/service/rpc.ts +59 -23
- package/src/store/accounts.ts +373 -0
- package/src/store/atomic-file.ts +37 -0
- package/src/store/futures-repo.ts +35 -260
- package/src/store/ledger-repo-base.ts +337 -0
- package/src/store/notes.ts +86 -0
- package/src/store/paths.ts +5 -0
- package/src/store/repo.ts +47 -229
- package/src/store/shortlist.ts +3 -5
- package/src/store/snapshots.ts +0 -3
- package/src/client/index.js +0 -3275
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
/* 10 · runtime — endpoint, formatting, vocabularies, storage keys, banner model. */
|
|
2
|
+
|
|
3
|
+
const ROUTE = '/api/ft-paper-trading'
|
|
4
|
+
/** Release number stamped into the Feedback link title at build time. */
|
|
5
|
+
const PAPER_VERSION = '__PAPER_VERSION__'
|
|
6
|
+
/** Public repository; the panel links out and collects nothing itself. */
|
|
7
|
+
const ISSUE_NEW = 'https://github.com/gigaflw/dsh-fft-paper-trading/issues/new'
|
|
8
|
+
/**
|
|
9
|
+
* Feedback opens the Bug form with the release already in the title: a
|
|
10
|
+
* report without a version costs triage a round trip. GitHub honours only
|
|
11
|
+
* `title`/`labels` on a form URL — a `body` param is ignored (and a blank
|
|
12
|
+
* issue can 400 on repos that disable it) — so the body stays user-written.
|
|
13
|
+
*/
|
|
14
|
+
const FEEDBACK_URL = `${ISSUE_NEW}?template=bug_report.yml&title=${encodeURIComponent(`[Bug] v${PAPER_VERSION} `)}`
|
|
15
|
+
const PERIODS = [
|
|
16
|
+
['7d', '7 天'],
|
|
17
|
+
['30d', '30 天'],
|
|
18
|
+
['all', '全部'],
|
|
19
|
+
]
|
|
20
|
+
// The futures account has no market page and no K-line: search lives inside
|
|
21
|
+
// the ticket, so the wallet pages match the stock ones one for one.
|
|
22
|
+
const FUTURES_TABS = ['trade', 'positions', 'orders', 'review']
|
|
23
|
+
const FUTURES_TAB_LABEL = { trade: '交易', positions: '持仓', orders: '订单', review: '复盘' }
|
|
24
|
+
|
|
25
|
+
/* ---------------------------------------------------------------- text --
|
|
26
|
+
Pure formatting and vocabularies. Times are always zh-CN /
|
|
27
|
+
Asia/Shanghai; numbers that are not finite render as `—` instead of a
|
|
28
|
+
blank or `NaN`, and backend codes never leak into the main copy. */
|
|
29
|
+
|
|
30
|
+
const TZ = 'Asia/Shanghai'
|
|
31
|
+
// sv-SE formats as YYYY-MM-DD, which matches the trade-date column and the
|
|
32
|
+
// header; zh-CN would print 2026/10/22 slashes instead.
|
|
33
|
+
const dateTimeFormat = new Intl.DateTimeFormat('sv-SE', {
|
|
34
|
+
timeZone: TZ, year: 'numeric', month: '2-digit', day: '2-digit',
|
|
35
|
+
hour: '2-digit', minute: '2-digit', second: '2-digit', hourCycle: 'h23',
|
|
36
|
+
})
|
|
37
|
+
const timeFormat = new Intl.DateTimeFormat('zh-CN', {
|
|
38
|
+
timeZone: TZ, hour: '2-digit', minute: '2-digit', second: '2-digit', hourCycle: 'h23',
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const isFiniteNumber = value => typeof value === 'number' && Number.isFinite(value)
|
|
42
|
+
// Amounts read as money: group thousands and always show two decimals.
|
|
43
|
+
const amountFormat = new Intl.NumberFormat('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })
|
|
44
|
+
const money = value => isFiniteNumber(value) ? amountFormat.format(value) : '—'
|
|
45
|
+
const cents = value => isFiniteNumber(value) ? amountFormat.format(value / 100) : '—'
|
|
46
|
+
const pct = value => isFiniteNumber(value) ? `${(value * 100).toFixed(2)}%` : '—'
|
|
47
|
+
const signed = value => {
|
|
48
|
+
if (!isFiniteNumber(value)) return '—'
|
|
49
|
+
// Zero is neither a gain nor a loss: no misleading "+0.00".
|
|
50
|
+
return `${value > 0 ? '+' : ''}${amountFormat.format(value)}`
|
|
51
|
+
}
|
|
52
|
+
const tone = value => !isFiniteNumber(value) ? '' : value > 0 ? 'pt-up' : value < 0 ? 'pt-down' : ''
|
|
53
|
+
const count = value => isFiniteNumber(value) ? String(Math.round(value)) : '—'
|
|
54
|
+
const toDate = value => {
|
|
55
|
+
if (value === undefined || value === null || value === '') return null
|
|
56
|
+
const time = Date.parse(value)
|
|
57
|
+
return Number.isNaN(time) ? null : new Date(time)
|
|
58
|
+
}
|
|
59
|
+
const dateTime = value => {
|
|
60
|
+
const date = toDate(value)
|
|
61
|
+
return date === null ? '—' : dateTimeFormat.format(date)
|
|
62
|
+
}
|
|
63
|
+
const clockTime = value => {
|
|
64
|
+
const date = toDate(value)
|
|
65
|
+
return date === null ? '—' : timeFormat.format(date)
|
|
66
|
+
}
|
|
67
|
+
const dateFormat = new Intl.DateTimeFormat('sv-SE', {
|
|
68
|
+
timeZone: TZ, year: 'numeric', month: '2-digit', day: '2-digit',
|
|
69
|
+
})
|
|
70
|
+
const shanghaiToday = () => dateFormat.format(new Date())
|
|
71
|
+
/** After the close the ledger rolls to the next trading day; say so. */
|
|
72
|
+
const tradeDateText = (value, estimated = false) => {
|
|
73
|
+
if (!value) return '—'
|
|
74
|
+
return value > shanghaiToday()
|
|
75
|
+
? `下一交易日${estimated ? '(按工作日估算)' : ''} ${value}`
|
|
76
|
+
: value
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Trading-day source note for the header subtitle. Only rendered while the
|
|
80
|
+
* weekday fallback is in use, so the normal state stays quiet.
|
|
81
|
+
*/
|
|
82
|
+
const calendarNote = calendar => {
|
|
83
|
+
if (calendar === null || calendar === undefined || calendar.source !== 'fallback') return null
|
|
84
|
+
const detail = calendar.reason === undefined ? '' : `(${calendar.reason})`
|
|
85
|
+
return h('span', {
|
|
86
|
+
className: 'pt-note',
|
|
87
|
+
title: `交易日历降级:按周一至周五估算,节假日会被当作交易日,影响 T+1 可卖、挂单过期、期货结算与下一交易日${detail}`,
|
|
88
|
+
}, ' · 交易日历降级(按工作日估算)')
|
|
89
|
+
}
|
|
90
|
+
/** Canonical ids arrive from the host; the raw input keeps whatever was typed. */
|
|
91
|
+
const halfWidthDigits = value => String(value ?? '')
|
|
92
|
+
.replace(/[0-9]/g, character => String.fromCharCode(character.charCodeAt(0) - 0xFEE0))
|
|
93
|
+
.replace(/[A-Z]/g, character => String.fromCharCode(character.charCodeAt(0) - 0xFEE0))
|
|
94
|
+
// dsh_kline hands over its own suffixes (`600519.XSHG`, `000001.XSHE`); the
|
|
95
|
+
// ticket shows the canonical `.SH` / `.SZ` form and the host accepts both.
|
|
96
|
+
const HOST_EXCHANGES = { XSHG: 'SH', XSHE: 'SZ', BJSE: 'BJ' }
|
|
97
|
+
const foldHostSymbol = value => {
|
|
98
|
+
const text = halfWidthDigits(value).toUpperCase().trim()
|
|
99
|
+
const suffix = /^(\d{6})\.([A-Z]{2,4})$/.exec(text)
|
|
100
|
+
if (suffix === null) return text
|
|
101
|
+
const exchange = HOST_EXCHANGES[suffix[2]]
|
|
102
|
+
return exchange === undefined ? text : `${suffix[1]}.${exchange}`
|
|
103
|
+
}
|
|
104
|
+
// The ticket accepts `600519`, `600519.SH`, `600519.XSHG` and `SH600519`;
|
|
105
|
+
// the host folds them into one canonical id, so the panel keeps letters.
|
|
106
|
+
const symbolInput = value => {
|
|
107
|
+
const text = foldHostSymbol(value).replace(/[^0-9A-Z.]/g, '').slice(0, 12)
|
|
108
|
+
const prefix = /^(SH|SZ|BJ)(\d{6})$/.exec(text)
|
|
109
|
+
return prefix === null ? text : `${prefix[2]}.${prefix[1]}`
|
|
110
|
+
}
|
|
111
|
+
const budgetInput = value => halfWidthDigits(value).replace(/[,,\s]/g, '')
|
|
112
|
+
const symbolText = quote => [quote?.symbol, quote?.name].filter(text => text).join(' ')
|
|
113
|
+
/**
|
|
114
|
+
* Quote origin in human terms; the raw interface name is too technical.
|
|
115
|
+
* A daily-bar quote is the free-tier fallback (last close), never a live
|
|
116
|
+
* tick, so the label has to say which one it is.
|
|
117
|
+
*/
|
|
118
|
+
const quoteSourceText = quote => {
|
|
119
|
+
if (!quote || !quote.source) return 'FTShare'
|
|
120
|
+
if (quote.source === 'manual') return '参考价'
|
|
121
|
+
// Provenance, not tier: a keyed account can also end up on the daily close.
|
|
122
|
+
if (quote.source === 'daily-bar') return '日K收盘'
|
|
123
|
+
return 'FTShare 实时'
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Quote line time. Same-day data shows the clock only; a daily-close quote
|
|
127
|
+
* (free tier) or any older timestamp carries its date, so yesterday's
|
|
128
|
+
* close cannot read as today's live price.
|
|
129
|
+
*/
|
|
130
|
+
const quoteUpdateText = quote => {
|
|
131
|
+
if (!quote) return ''
|
|
132
|
+
if (quote.source === 'manual') return '手动价'
|
|
133
|
+
const ts = quote.marketTs
|
|
134
|
+
if (ts === undefined || ts === null) return '更新 —'
|
|
135
|
+
const date = dateFormat.format(new Date(ts))
|
|
136
|
+
return date === shanghaiToday() ? `更新 ${clockTime(ts)}` : `更新 ${date.slice(5)} ${clockTime(ts)}`
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* How often the open ticket should re-price itself. A live quote deserves
|
|
140
|
+
* the 5 s beat; a daily-bar (free tier) or missing one does not, and on the
|
|
141
|
+
* anonymous quota the 5 s re-preview alone would spend the day's budget.
|
|
142
|
+
*/
|
|
143
|
+
const ticketRefreshInterval = quote =>
|
|
144
|
+
quote === null || quote === undefined || quote.source === 'daily-bar' ? 300_000 : 5_000
|
|
145
|
+
/**
|
|
146
|
+
* Header badge copy for the four data-source states. The badge answers
|
|
147
|
+
* "is an FTShare key configured" without ever showing the key itself; the
|
|
148
|
+
* title carries the consequence (realtime vs daily-close pricing).
|
|
149
|
+
*/
|
|
150
|
+
const DATA_SOURCE_STATE = {
|
|
151
|
+
configured: { text: '数据源正常', title: 'FTShare API Key 已配置,可看实时行情' },
|
|
152
|
+
free: {
|
|
153
|
+
text: '免费数据源',
|
|
154
|
+
title: '未配置 FTShare Key:免费档报价取日K收盘,可手填参考价交易',
|
|
155
|
+
},
|
|
156
|
+
none: { text: '无数据源', title: '未安装 FTShare 数据服务,行情不可用;可手填参考价交易' },
|
|
157
|
+
invalid: { text: '数据源无效', title: 'FTShare Key 无法使用;请在「设置」中检查,可手填参考价交易' },
|
|
158
|
+
}
|
|
159
|
+
/** Badge suffix per recorded failure, so the header cannot read 正常 while quotes fail. */
|
|
160
|
+
const DATA_PROBLEM_STATE = {
|
|
161
|
+
'no-key': '未配置 Key',
|
|
162
|
+
'invalid-key': 'Key 无效',
|
|
163
|
+
'plan-denied': '套餐未开通',
|
|
164
|
+
quota: '额度用尽',
|
|
165
|
+
network: '上游超时',
|
|
166
|
+
upstream: '上游异常',
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Where a missing or rejected key gets fixed.
|
|
170
|
+
*
|
|
171
|
+
* The banner does not link out: the settings section already carries the
|
|
172
|
+
* signup link in its 访问凭据 card, and the host exposes no client API that
|
|
173
|
+
* opens settings from here, so the copy names the section instead of sending
|
|
174
|
+
* the user to a second, differently-worded URL.
|
|
175
|
+
*/
|
|
176
|
+
const KEY_SETTINGS = '设置 → 金融终端'
|
|
177
|
+
/** One reason per stored value, so a new problem shows again after a dismissal. */
|
|
178
|
+
const DATA_BANNER_DISMISS_KEY = 'paper-trading:data-banner-dismissed'
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Model for the data banner.
|
|
182
|
+
*
|
|
183
|
+
* Order matters: a missing service beats any recorded failure (nothing can
|
|
184
|
+
* work), a spent quota beats the plain "no key" hint (it explains why行情
|
|
185
|
+
* stopped), and a runtime key rejection beats the configured badge.
|
|
186
|
+
*/
|
|
187
|
+
function bannerModel(status) {
|
|
188
|
+
if (status === null || status === undefined) return null
|
|
189
|
+
if (status.state === 'none') {
|
|
190
|
+
return {
|
|
191
|
+
id: 'no-service', tone: 'info',
|
|
192
|
+
title: '行情数据服务未安装',
|
|
193
|
+
detail: '现在只能用参考价交易;安装 FTShare 数据服务后自动恢复。',
|
|
194
|
+
actions: [],
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const noKeyBanner = {
|
|
198
|
+
id: 'no-key', tone: 'info',
|
|
199
|
+
// Quotes still work on the free tier (last daily close), so the banner
|
|
200
|
+
// cannot say 行情未启用; what a key adds is the realtime feed.
|
|
201
|
+
title: '免费档:报价取日K收盘',
|
|
202
|
+
detail: `在 ${KEY_SETTINGS} 填入 API Key 可看实时行情。`,
|
|
203
|
+
actions: [],
|
|
204
|
+
}
|
|
205
|
+
const problem = status.problem
|
|
206
|
+
if (problem !== undefined && problem !== null) {
|
|
207
|
+
switch (problem.reason) {
|
|
208
|
+
case 'quota':
|
|
209
|
+
return {
|
|
210
|
+
id: 'quota', tone: 'warn',
|
|
211
|
+
// 匿名档是「最近 24 小时 500 次」的滚动额度,不是一个自然日。
|
|
212
|
+
title: '免费额度已用尽',
|
|
213
|
+
detail: `请在 ${KEY_SETTINGS} 获取并填入 API Key;或等约 24 小时额度恢复。`,
|
|
214
|
+
actions: ['retry'],
|
|
215
|
+
}
|
|
216
|
+
case 'plan-denied':
|
|
217
|
+
// Without a key the realtime interface is simply not enabled; a
|
|
218
|
+
// "套餐未开通" banner would send a keyless user to the wrong fix.
|
|
219
|
+
return status.state === 'free'
|
|
220
|
+
? noKeyBanner
|
|
221
|
+
: {
|
|
222
|
+
id: 'plan-denied', tone: 'warn',
|
|
223
|
+
title: '当前套餐未开通实时行情',
|
|
224
|
+
detail: '可用日K收盘价模拟;套餐开通后刷新即可。',
|
|
225
|
+
actions: ['retry'],
|
|
226
|
+
}
|
|
227
|
+
case 'invalid-key':
|
|
228
|
+
// The anonymous channel also answers 401 for paid interfaces, so a
|
|
229
|
+
// keyless user must see the "configure a key" hint, not "key bad".
|
|
230
|
+
return status.state === 'free'
|
|
231
|
+
? noKeyBanner
|
|
232
|
+
: {
|
|
233
|
+
id: 'invalid-key', tone: 'error',
|
|
234
|
+
title: 'FTShare 数据源无法使用',
|
|
235
|
+
detail: `在 ${KEY_SETTINGS} 检查 API Key。`,
|
|
236
|
+
actions: ['retry'],
|
|
237
|
+
}
|
|
238
|
+
case 'network':
|
|
239
|
+
return {
|
|
240
|
+
id: 'network', tone: 'warn',
|
|
241
|
+
title: '行情服务暂时不可用',
|
|
242
|
+
detail: '网络或上游超时,稍后重试;账本不受影响。',
|
|
243
|
+
actions: ['retry'],
|
|
244
|
+
}
|
|
245
|
+
default:
|
|
246
|
+
return {
|
|
247
|
+
id: 'upstream', tone: 'warn',
|
|
248
|
+
title: '行情服务上游异常',
|
|
249
|
+
// 手填参考价只覆盖市价单(期货仅市价平仓),先说清最确定的一件事。
|
|
250
|
+
detail: '稍后重试;账本不受影响。',
|
|
251
|
+
actions: ['retry'],
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (status.state === 'invalid') {
|
|
256
|
+
return {
|
|
257
|
+
id: 'invalid-key', tone: 'error',
|
|
258
|
+
title: 'FTShare 数据源无法使用',
|
|
259
|
+
detail: `${status.message === undefined || status.message === '' ? 'Key 未通过验证' : status.message}。在 ${KEY_SETTINGS} 检查 API Key。`,
|
|
260
|
+
actions: [],
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (status.state === 'free') return noKeyBanner
|
|
264
|
+
return null
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function readBannerDismissal() {
|
|
268
|
+
try {
|
|
269
|
+
return window.localStorage.getItem(DATA_BANNER_DISMISS_KEY) ?? ''
|
|
270
|
+
} catch {
|
|
271
|
+
return ''
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/* ------------------------------------------------------ rail layout --
|
|
276
|
+
The rail's size is a user preference, never the layout authority: a drag
|
|
277
|
+
stores a px hint, CSS clamps it against the workspace (the trading column
|
|
278
|
+
keeps room for its ticket before the rail takes space), and the narrow
|
|
279
|
+
container query drops to one column no matter what was stored. The hint
|
|
280
|
+
is written on release only — a drag reports dozens of widths a second,
|
|
281
|
+
and every write is synchronous local storage. */
|
|
282
|
+
|
|
283
|
+
const LAYOUT_KEY = 'ft-paper-trading.layout.v1'
|
|
284
|
+
const RAIL_LIMITS = {
|
|
285
|
+
stock: { min: 320, max: 760 },
|
|
286
|
+
futures: { min: 240, max: 400 },
|
|
287
|
+
}
|
|
288
|
+
// `pad`: how much of the workspace the trading column must keep for its
|
|
289
|
+
// ticket before the rail may grow (560px form+preview + the 10px gap).
|
|
290
|
+
const RAIL_MAIN_RESERVE = 570
|
|
291
|
+
const KLINE_HEIGHTS = ['auto', 'tall', 'full']
|
|
292
|
+
|
|
293
|
+
/** Defaults for both accounts; a missing or malformed entry falls back here. */
|
|
294
|
+
function emptyLayout() {
|
|
295
|
+
return {
|
|
296
|
+
stock: { rail: null, collapsed: false, klineHeight: 'auto' },
|
|
297
|
+
futures: { rail: null, collapsed: false },
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Clamp a rail width into its account's contract range. */
|
|
302
|
+
function clampRail(value, mode) {
|
|
303
|
+
const limits = RAIL_LIMITS[mode] ?? RAIL_LIMITS.stock
|
|
304
|
+
return Math.min(limits.max, Math.max(limits.min, Math.round(value)))
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** Read the persisted layout; anything unreadable or malformed is ignored. */
|
|
308
|
+
function readLayout() {
|
|
309
|
+
const layout = emptyLayout()
|
|
310
|
+
try {
|
|
311
|
+
const raw = window.localStorage.getItem(LAYOUT_KEY)
|
|
312
|
+
if (raw === null) return layout
|
|
313
|
+
const saved = JSON.parse(raw)
|
|
314
|
+
for (const mode of ['stock', 'futures']) {
|
|
315
|
+
const slice = saved === null || typeof saved !== 'object' ? undefined : saved[mode]
|
|
316
|
+
if (slice === null || slice === undefined || typeof slice !== 'object') continue
|
|
317
|
+
if (typeof slice.rail === 'number' && Number.isFinite(slice.rail)) layout[mode].rail = clampRail(slice.rail, mode)
|
|
318
|
+
layout[mode].collapsed = slice.collapsed === true
|
|
319
|
+
if (mode === 'stock' && KLINE_HEIGHTS.includes(slice.klineHeight)) layout.stock.klineHeight = slice.klineHeight
|
|
320
|
+
}
|
|
321
|
+
} catch {
|
|
322
|
+
// A private window or a truncated write: the panel starts at its defaults.
|
|
323
|
+
}
|
|
324
|
+
return layout
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** Persist one account's preference without clobbering the other's. */
|
|
328
|
+
function writeLayout(mode, patch) {
|
|
329
|
+
try {
|
|
330
|
+
const next = readLayout()
|
|
331
|
+
next[mode] = { ...next[mode], ...patch }
|
|
332
|
+
window.localStorage.setItem(LAYOUT_KEY, JSON.stringify(next))
|
|
333
|
+
} catch {
|
|
334
|
+
// Losing a size preference is acceptable; losing the panel is not.
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** Remember the account the human looked at last; the panel re-selects it. */
|
|
339
|
+
const CURRENT_ACCOUNT_KEY = 'ft-paper-trading.current-account.v1'
|
|
340
|
+
|
|
341
|
+
function readCurrentAccount() {
|
|
342
|
+
try {
|
|
343
|
+
return window.localStorage.getItem(CURRENT_ACCOUNT_KEY) ?? ''
|
|
344
|
+
} catch {
|
|
345
|
+
return ''
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function writeCurrentAccount(id) {
|
|
350
|
+
try {
|
|
351
|
+
window.localStorage.setItem(CURRENT_ACCOUNT_KEY, id)
|
|
352
|
+
} catch {
|
|
353
|
+
// A private window loses the preference, not the panel.
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** Both sides are canonical ids (`000001.SZ`), so compare them verbatim. */
|
|
358
|
+
const shortlisted = (shortlist, symbol) => shortlist.some(item => item === symbol)
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Why the ticket cannot be previewed yet. An incomplete draft would
|
|
362
|
+
* otherwise come back priced at 0.00 — a card that looks like a real
|
|
363
|
+
* order for nothing — so the panel asks for the missing field instead.
|
|
364
|
+
*/
|
|
365
|
+
const previewBlockReason = draft => {
|
|
366
|
+
if (String(draft.symbol ?? '').trim().length < 6) return '输入代码后自动计算'
|
|
367
|
+
const qty = Number(draft.qty)
|
|
368
|
+
if (!Number.isInteger(qty) || qty <= 0) return '填写数量后自动计算'
|
|
369
|
+
if (draft.type === 'limit' && !(Number(draft.limitPrice) > 0)) return '填写限价后自动计算'
|
|
370
|
+
return ''
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const STATUS_TEXT = {
|
|
374
|
+
queued: '待开盘',
|
|
375
|
+
pending: '挂单',
|
|
376
|
+
partially_filled: '部分成交',
|
|
377
|
+
filled: '成交',
|
|
378
|
+
canceled: '已撤',
|
|
379
|
+
rejected: '拒绝',
|
|
380
|
+
expired: '过期',
|
|
381
|
+
}
|
|
382
|
+
/** Why the ticket is limited outside continuous trading. */
|
|
383
|
+
const sessionNote = session => {
|
|
384
|
+
if (!session || session.canMatch) return ''
|
|
385
|
+
if (session.phase === 'auction_close') return '(收盘竞价暂不支持下单)'
|
|
386
|
+
if (session.phase === 'pre_open') return '(集合竞价,09:30 开盘撮合)'
|
|
387
|
+
if (session.phase === 'lunch') return '(午休,13:00 开盘撮合)'
|
|
388
|
+
return '(盘后委托顺延至下一交易时段开盘)'
|
|
389
|
+
}
|
|
390
|
+
const PHASE_TEXT = {
|
|
391
|
+
pre_open: '集合竞价',
|
|
392
|
+
continuous_am: '上午连续竞价',
|
|
393
|
+
lunch: '午休',
|
|
394
|
+
continuous_pm: '下午连续竞价',
|
|
395
|
+
auction_close: '收盘竞价',
|
|
396
|
+
closed: '已收盘',
|
|
397
|
+
}
|
|
398
|
+
const CASH_TYPE_TEXT = {
|
|
399
|
+
open: '初始资金',
|
|
400
|
+
reset: '重置',
|
|
401
|
+
deposit: '注资',
|
|
402
|
+
withdraw: '提款',
|
|
403
|
+
buy: '买入',
|
|
404
|
+
sell: '卖出',
|
|
405
|
+
fee: '费用',
|
|
406
|
+
}
|
|
407
|
+
const FUTURES_CASH_TYPE_TEXT = {
|
|
408
|
+
open: '初始资金',
|
|
409
|
+
deposit: '注资',
|
|
410
|
+
withdraw: '提款',
|
|
411
|
+
open_position: '开仓占用',
|
|
412
|
+
close_position: '平仓释放',
|
|
413
|
+
}
|
|
414
|
+
const CANCEL_REASON_TEXT = {
|
|
415
|
+
user: '手动撤单',
|
|
416
|
+
expired: '当日有效期满',
|
|
417
|
+
insufficient_cash: '资金不足',
|
|
418
|
+
insufficient_position: '可卖不足',
|
|
419
|
+
margin_unavailable: '可用资金不足',
|
|
420
|
+
insufficient_margin: '保证金不足',
|
|
421
|
+
risk: '强制平仓',
|
|
422
|
+
expiry: '到期平仓',
|
|
423
|
+
}
|
|
424
|
+
/** Basket row states; the raw codes never reach the table. */
|
|
425
|
+
const BASKET_STATUS_TEXT = {
|
|
426
|
+
ok: '可买',
|
|
427
|
+
zero_lot: '不足 1 手',
|
|
428
|
+
no_quote: '无行情',
|
|
429
|
+
suspended: '停牌',
|
|
430
|
+
}
|
|
431
|
+
const FUTURES_PHASE_TEXT = {
|
|
432
|
+
pre_open: '待开盘',
|
|
433
|
+
trading: '交易中',
|
|
434
|
+
lunch: '休市',
|
|
435
|
+
closed: '已收盘',
|
|
436
|
+
}
|
|
437
|
+
const FUTURES_STATUS_TEXT = {
|
|
438
|
+
pending: '挂单',
|
|
439
|
+
filled: '成交',
|
|
440
|
+
canceled: '已撤',
|
|
441
|
+
rejected: '拒绝',
|
|
442
|
+
expired: '过期',
|
|
443
|
+
}
|
|
444
|
+
/** Position direction; orders use the open/close form below. */
|
|
445
|
+
const FUTURES_SIDE_TEXT = { long: '多', short: '空' }
|
|
446
|
+
const FUTURES_DIRECTION_TEXT = {
|
|
447
|
+
long_open: '开多',
|
|
448
|
+
short_open: '开空',
|
|
449
|
+
long_close: '平多',
|
|
450
|
+
short_close: '平空',
|
|
451
|
+
}
|
|
452
|
+
const directionText = (side, offset) => FUTURES_DIRECTION_TEXT[`${side}_${offset}`] ?? '—'
|
|
453
|
+
const orderTypeText = type => type === 'limit' ? '限价' : '市价'
|
|
454
|
+
const ERROR_TEXT = {
|
|
455
|
+
ACCOUNT_BLOCKED: '账本校验失败,账户已冻结:可让 AI 助手从备份恢复,或重置/导出账本',
|
|
456
|
+
ACCOUNT_LOCKED: '账户已被人工锁定:在账户菜单里解锁后才能下单',
|
|
457
|
+
ACCOUNT_ARCHIVED: '账户已归档:在账户菜单里恢复归档后才能交易',
|
|
458
|
+
INVALID_ARGUMENT: '参数不合法',
|
|
459
|
+
INVALID_SYMBOL: '证券代码不合法',
|
|
460
|
+
INVALID_QTY: '数量不合法',
|
|
461
|
+
INVALID_LOT: '买入数量必须是 100 股的整数倍',
|
|
462
|
+
INSUFFICIENT_CASH: '可用资金不足',
|
|
463
|
+
INSUFFICIENT_POSITION: '持仓不足',
|
|
464
|
+
T1_LOCKED: '当日买入的股份不可卖出(T+1)',
|
|
465
|
+
MAX_NOTIONAL: '超过单笔金额上限',
|
|
466
|
+
ORDER_NOT_FOUND: '订单不存在',
|
|
467
|
+
ORDER_NOT_PENDING: '订单不是挂单状态',
|
|
468
|
+
QUOTE_UNAVAILABLE: '没有可用行情',
|
|
469
|
+
SESSION_CLOSED: '当前时段不接受委托',
|
|
470
|
+
AUCTION_ONLY: '当前时段不接受市价单',
|
|
471
|
+
PRICE_LIMIT: '价格超出涨跌停范围',
|
|
472
|
+
SUSPENDED: '标的停牌',
|
|
473
|
+
UNSUPPORTED_INSTRUMENT: '该标的不支持交易',
|
|
474
|
+
INSUFFICIENT_MARGIN: '保证金不足',
|
|
475
|
+
ACCOUNT_NOT_FOUND: '账户不存在',
|
|
476
|
+
ACCOUNT_LIMIT: '账户数量已达上限',
|
|
477
|
+
LEDGER_RESTORE_FAILED: '账本恢复失败',
|
|
478
|
+
CONTRACT_NOT_FOUND: '合约不存在',
|
|
479
|
+
CONTRACT_EXPIRED: '合约临近交割或已到期',
|
|
480
|
+
NO_VOLUME: '当日无成交',
|
|
481
|
+
LIMIT_UNAVAILABLE: '暂无涨跌停数据',
|
|
482
|
+
MARGIN_UNAVAILABLE: '暂无保证金率数据',
|
|
483
|
+
INTERNAL: '内部错误',
|
|
484
|
+
}
|
|
485
|
+
const CODE_PREFIX = /^([A-Z_]+):\s*(.*)$/
|
|
486
|
+
/** `T1_LOCKED: 可卖数量不足(可卖 0 股)` → the Chinese detail only. */
|
|
487
|
+
const reasonText = (reason, scope = 'stock') => {
|
|
488
|
+
if (!reason) return ''
|
|
489
|
+
const matched = CODE_PREFIX.exec(reason)
|
|
490
|
+
if (matched) return matched[2] || ERROR_TEXT[matched[1]] || reason
|
|
491
|
+
if (reason === 'insufficient_position') return scope === 'futures' ? '可平不足' : '可卖不足'
|
|
492
|
+
return CANCEL_REASON_TEXT[reason] ?? reason
|
|
493
|
+
}
|
|
494
|
+
// The host message is already a readable Chinese sentence and carries
|
|
495
|
+
// the specifics ("最多 20 只,本次 21 只"); the code label is only a fallback.
|
|
496
|
+
const errorText = (code, message) => (typeof message === 'string' && message.trim() !== '')
|
|
497
|
+
? message
|
|
498
|
+
: (ERROR_TEXT[code] ?? '操作失败')
|
|
499
|
+
const errorOf = cause => ({
|
|
500
|
+
text: cause && cause.message ? cause.message : '操作失败',
|
|
501
|
+
detail: cause && cause.detail ? cause.detail : '',
|
|
502
|
+
})
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Send one RPC. `account` names the stock ledger for account-scoped
|
|
506
|
+
* endpoints; the caller decides it (the stock dashboard keeps the selection
|
|
507
|
+
* in a ref owned by its mount), so nothing here depends on module state.
|
|
508
|
+
* Futures ledgers and the account registry have no account dimension of
|
|
509
|
+
* their own and never receive the field.
|
|
510
|
+
*/
|
|
511
|
+
async function rpc(endpoint, payload, account) {
|
|
512
|
+
const requestBody = { endpoint, payload: payload ?? {} }
|
|
513
|
+
if (account !== null && account !== undefined && !endpoint.startsWith('futures/') && !endpoint.startsWith('accounts/')) {
|
|
514
|
+
requestBody.account = account
|
|
515
|
+
}
|
|
516
|
+
const response = await fetch(ROUTE, {
|
|
517
|
+
method: 'POST',
|
|
518
|
+
headers: { 'content-type': 'application/json' },
|
|
519
|
+
body: JSON.stringify(requestBody),
|
|
520
|
+
})
|
|
521
|
+
let body = null
|
|
522
|
+
try {
|
|
523
|
+
body = await response.json()
|
|
524
|
+
} catch {
|
|
525
|
+
body = null
|
|
526
|
+
}
|
|
527
|
+
if (!response.ok || body === null || body.ok !== true) {
|
|
528
|
+
const failureBody = body && body.error ? body.error : { code: 'INTERNAL', message: `HTTP ${response.status}` }
|
|
529
|
+
const failure = new Error(errorText(failureBody.code, failureBody.message))
|
|
530
|
+
failure.code = failureBody.code
|
|
531
|
+
failure.detail = `${failureBody.code}: ${failureBody.message}`
|
|
532
|
+
throw failure
|
|
533
|
+
}
|
|
534
|
+
return body.value
|
|
535
|
+
}
|