@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,641 @@
|
|
|
1
|
+
/* 40 · stock tabs — ticket, book, market, positions, orders, review, basket. */
|
|
2
|
+
|
|
3
|
+
/* ----------------------------------------------------------- tabs ----- */
|
|
4
|
+
|
|
5
|
+
function TradeTab({
|
|
6
|
+
account, form, setForm, quote, quoteError, quoteNotice, preview, previewState, previewError, previewHint,
|
|
7
|
+
busy, onSubmit, submitReason, receipt, positions, pending, onCancel, onSell,
|
|
8
|
+
}) {
|
|
9
|
+
// Default to whichever book has content; the toggle keeps both one click away.
|
|
10
|
+
const [book, setBook] = useState(() => (positions.length === 0 && pending.length > 0 ? 'orders' : 'positions'))
|
|
11
|
+
const maxBuy = preview ? count(preview.maxBuyQty) : '—'
|
|
12
|
+
const priceLabel = form.type === 'limit' ? '限价' : '参考价(可选)'
|
|
13
|
+
const quoteHint = quote
|
|
14
|
+
? null
|
|
15
|
+
: form.symbol.trim().length < 6
|
|
16
|
+
? '输入代码后自动取行情'
|
|
17
|
+
: previewState === 'loading'
|
|
18
|
+
? '取行情…'
|
|
19
|
+
: form.type === 'limit' ? '未取到行情,可填限价' : '未取到行情,可填参考价'
|
|
20
|
+
const emptyPreview = previewHint !== ''
|
|
21
|
+
? previewHint
|
|
22
|
+
: previewState === 'loading' ? '计算中…' : previewError !== '' ? previewError : '计算中…'
|
|
23
|
+
return h('div', { className: 'pt-trade' },
|
|
24
|
+
h('div', { className: 'pt-card pt-ticket-card' },
|
|
25
|
+
h('div', { className: 'pt-ticket' },
|
|
26
|
+
h('div', { className: 'pt-ticket-form' },
|
|
27
|
+
h(Field, { label: '代码', span: true }, h('input', {
|
|
28
|
+
className: 'pt-input', value: form.symbol, maxLength: 12, spellCheck: false,
|
|
29
|
+
placeholder: '600519 / 000001.SZ',
|
|
30
|
+
onChange: event => setForm(prev => ({ ...prev, symbol: symbolInput(event.target.value) })),
|
|
31
|
+
})),
|
|
32
|
+
h('div', { className: 'pt-span-2 pt-field-pair' },
|
|
33
|
+
h(Field, { label: '方向' }, h('div', { className: 'pt-seg' },
|
|
34
|
+
[['buy', '买入'], ['sell', '卖出']].map(([key, label]) =>
|
|
35
|
+
h('button', { key, 'data-active': form.side === key, onClick: () => setForm(prev => ({ ...prev, side: key })) }, label),
|
|
36
|
+
),
|
|
37
|
+
)),
|
|
38
|
+
h(Field, { label: '数量(100 股整数倍)' }, h(DebouncedNumber, {
|
|
39
|
+
value: form.qty, min: 0, step: 100,
|
|
40
|
+
onChange: value => setForm(prev => ({ ...prev, qty: value })),
|
|
41
|
+
})),
|
|
42
|
+
),
|
|
43
|
+
h(Field, { label: '类型' }, h('div', { className: 'pt-seg' },
|
|
44
|
+
[['market', '市价'], ['limit', '限价']].map(([key, label]) =>
|
|
45
|
+
h('button', { key, 'data-active': form.type === key, onClick: () => setForm(prev => ({ ...prev, type: key })) }, label),
|
|
46
|
+
),
|
|
47
|
+
)),
|
|
48
|
+
h(Field, { label: priceLabel }, h(DebouncedNumber, {
|
|
49
|
+
value: form.type === 'limit' ? form.limitPrice : form.price, min: 0, step: 0.01,
|
|
50
|
+
onChange: value => setForm(prev => prev.type === 'limit'
|
|
51
|
+
? { ...prev, limitPrice: value }
|
|
52
|
+
: { ...prev, price: value }),
|
|
53
|
+
})),
|
|
54
|
+
h('div', { className: 'pt-span-2' },
|
|
55
|
+
quote
|
|
56
|
+
? h('div', { className: 'pt-quote' },
|
|
57
|
+
h('span', null, symbolText(quote)),
|
|
58
|
+
h('span', null, `${quote.source === 'daily-bar' ? '收盘' : '现价'} ${money(quote.last)}`),
|
|
59
|
+
h('span', { className: 'pt-note' }, quoteUpdateText(quote)),
|
|
60
|
+
h('span', { className: 'pt-note' }, `来源 ${quoteSourceText(quote)}`),
|
|
61
|
+
quoteNotice !== '' && preview === null ? h('span', { className: 'pt-note' }, quoteNotice) : null,
|
|
62
|
+
)
|
|
63
|
+
: h('div', { className: 'pt-note' }, quoteHint),
|
|
64
|
+
),
|
|
65
|
+
h(Field, { label: '备注(复盘用)', span: true }, h('input', {
|
|
66
|
+
className: 'pt-input', value: form.note, maxLength: 120,
|
|
67
|
+
onChange: event => setForm(prev => ({ ...prev, note: event.target.value })),
|
|
68
|
+
})),
|
|
69
|
+
quoteError !== '' ? h('div', { className: 'pt-error pt-span-2' }, quoteError) : null,
|
|
70
|
+
),
|
|
71
|
+
h('div', { className: 'pt-preview-pane' },
|
|
72
|
+
h('div', { className: 'pt-card-title', style: { marginBottom: 6 } }, '预览'),
|
|
73
|
+
preview ? (preview.price > 0 ? h('div', { className: 'pt-preview' },
|
|
74
|
+
h('div', null, h('span', null, '预估成交价'), h('span', null, money(preview.price))),
|
|
75
|
+
h('div', null, h('span', null, '金额'), h('span', null, cents(preview.amountCents))),
|
|
76
|
+
h('div', null, h('span', null, '佣金'), h('span', null, cents(preview.commissionCents))),
|
|
77
|
+
h('div', null, h('span', null, '印花税'), h('span', null, cents(preview.stampCents))),
|
|
78
|
+
h('div', null, h('span', null, '过户费'), h('span', null, cents(preview.transferCents))),
|
|
79
|
+
h('div', null, h('span', null, '成交后现金'),
|
|
80
|
+
h('span', { className: preview.cashAfterCents < 0 ? 'pt-risk-high' : undefined }, cents(preview.cashAfterCents)),
|
|
81
|
+
),
|
|
82
|
+
h('div', null, h('span', null, '最多可买'), h('span', null, `${count(preview.maxBuyQty)} 股`)),
|
|
83
|
+
previewState === 'loading' ? h('div', { className: 'pt-note' }, '计算中…') : null,
|
|
84
|
+
preview.warnings.length > 0 ? h('div', { className: 'pt-warn' }, preview.warnings.join(';')) : null,
|
|
85
|
+
previewState === 'error' && previewError !== '' ? h('div', { className: 'pt-warn' }, previewError) : null,
|
|
86
|
+
) : h('div', { className: 'pt-empty' }, '没有可用行情,请填写参考价或改用限价单')) : h('div', { className: 'pt-empty' }, emptyPreview),
|
|
87
|
+
),
|
|
88
|
+
),
|
|
89
|
+
h('div', { className: 'pt-ticket-foot' },
|
|
90
|
+
submitReason !== '' ? h('div', { className: 'pt-note' }, `不可提交:${submitReason}`) : null,
|
|
91
|
+
h('button', {
|
|
92
|
+
className: 'pt-btn pt-btn-primary pt-ticket-submit', onClick: onSubmit, title: submitReason || undefined,
|
|
93
|
+
disabled: busy || submitReason !== '',
|
|
94
|
+
}, form.side === 'buy' ? '买入' : '卖出'),
|
|
95
|
+
h('div', { className: 'pt-ticket-meta' },
|
|
96
|
+
h('span', { className: 'pt-note' },
|
|
97
|
+
form.side === 'sell'
|
|
98
|
+
? `可用 ${money(account.cash)}${preview ? ` · 可卖 ${count(preview.availableQty)} 股` : ''}`
|
|
99
|
+
: `可用 ${money(account.cash)}${preview ? ` · 最多可买 ${maxBuy} 股` : ''}`,
|
|
100
|
+
),
|
|
101
|
+
),
|
|
102
|
+
receipt ? h(ReceiptLine, { receipt }) : null,
|
|
103
|
+
),
|
|
104
|
+
),
|
|
105
|
+
h(TradeBook, { positions, pending, book, setBook, busy, onCancel, onSell }),
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Compact holdings/orders book, pinned under the ticket so the left column
|
|
110
|
+
always reaches the K-line's bottom edge. */
|
|
111
|
+
function TradeBook({ positions, pending, book, setBook, busy, onCancel, onSell }) {
|
|
112
|
+
const showPositions = book === 'positions'
|
|
113
|
+
return h('div', { className: 'pt-card pt-trade-book' },
|
|
114
|
+
h('div', { className: 'pt-book-head' },
|
|
115
|
+
h('div', { className: 'pt-seg' },
|
|
116
|
+
h('button', { 'data-active': showPositions, onClick: () => setBook('positions') }, `持仓 ${positions.length}`),
|
|
117
|
+
h('button', { 'data-active': !showPositions, onClick: () => setBook('orders') }, `挂单 ${pending.length}`),
|
|
118
|
+
),
|
|
119
|
+
h('div', { className: 'pt-note' }, showPositions ? '成交后自动更新' : '未成交委托,可随时撤单'),
|
|
120
|
+
),
|
|
121
|
+
h('div', { className: 'pt-book-scroll' },
|
|
122
|
+
showPositions
|
|
123
|
+
? (positions.length === 0
|
|
124
|
+
? h('div', { className: 'pt-empty pt-book-empty' }, '无持仓 · 成交后自动显示')
|
|
125
|
+
: h(Table, null,
|
|
126
|
+
h('thead', null, h('tr', null,
|
|
127
|
+
['代码', '名称', '数量', '可卖', '成本', '现价', '浮盈亏'].map(label => h('th', { key: label }, label)),
|
|
128
|
+
h('th', null, '操作'),
|
|
129
|
+
)),
|
|
130
|
+
h('tbody', null, positions.map(position => h('tr', { key: position.symbol },
|
|
131
|
+
h('td', null, position.symbol),
|
|
132
|
+
h('td', null, position.name ?? '—'),
|
|
133
|
+
h('td', null, count(position.qty)),
|
|
134
|
+
h('td', null, count(position.available)),
|
|
135
|
+
h('td', null, money(position.avgCost)),
|
|
136
|
+
h('td', null, money(position.lastPrice)),
|
|
137
|
+
h('td', { className: tone(position.unrealizedPnlCents ?? 0) }, cents(position.unrealizedPnlCents)),
|
|
138
|
+
h('td', null, h('button', {
|
|
139
|
+
className: 'pt-btn', onClick: () => onSell(position), disabled: !(position.available > 0),
|
|
140
|
+
title: position.available > 0 ? undefined : 'T+1:今日买入的股份明日可卖',
|
|
141
|
+
}, '卖出')),
|
|
142
|
+
))),
|
|
143
|
+
))
|
|
144
|
+
: (pending.length === 0
|
|
145
|
+
? h('div', { className: 'pt-empty pt-book-empty' }, '无挂单 · 未成交的限价委托会停在这里')
|
|
146
|
+
: h(Table, null,
|
|
147
|
+
h('thead', null, h('tr', null,
|
|
148
|
+
['代码', '方向', '类型', '数量', '限价', '状态'].map(label => h('th', { key: label }, label)),
|
|
149
|
+
h('th', null, '操作'),
|
|
150
|
+
)),
|
|
151
|
+
h('tbody', null, pending.map(order => h('tr', { key: order.id },
|
|
152
|
+
h('td', null, order.symbol),
|
|
153
|
+
h('td', null, order.side === 'buy' ? '买' : '卖'),
|
|
154
|
+
h('td', null, order.type === 'limit' ? '限价' : '市价'),
|
|
155
|
+
h('td', null, count(order.qty)),
|
|
156
|
+
h('td', null, order.limitPrice === undefined ? '—' : money(order.limitPrice)),
|
|
157
|
+
h('td', null, STATUS_TEXT[order.status] ?? order.status),
|
|
158
|
+
h('td', null, h('button', { className: 'pt-btn', onClick: () => onCancel(order.id), disabled: busy }, '撤单')),
|
|
159
|
+
))),
|
|
160
|
+
)),
|
|
161
|
+
),
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Post-submit receipt, built from the ledger's fill record, not the draft. */
|
|
166
|
+
function ReceiptLine({ receipt }) {
|
|
167
|
+
if (receipt.failure !== undefined) {
|
|
168
|
+
return h('div', { className: 'pt-receipt', 'data-state': 'rejected', title: receipt.failure.detail || undefined },
|
|
169
|
+
`拒绝:${receipt.failure.text}`,
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
const { order, fill, cash } = receipt
|
|
173
|
+
const symbol = order.name ? `${order.symbol} ${order.name}` : order.symbol
|
|
174
|
+
const side = order.side === 'buy' ? '买入' : '卖出'
|
|
175
|
+
if (order.status === 'rejected') {
|
|
176
|
+
return h('div', { className: 'pt-receipt', 'data-state': 'rejected', title: order.reason || undefined },
|
|
177
|
+
`拒绝:${side} ${symbol} ${count(order.qty)} 股 · ${reasonText(order.reason) || '未通过风控'}`,
|
|
178
|
+
)
|
|
179
|
+
}
|
|
180
|
+
if (order.status === 'filled') {
|
|
181
|
+
const price = fill ? fill.price : order.fillPrice
|
|
182
|
+
const qty = fill ? fill.qty : order.filledQty
|
|
183
|
+
const amount = fill ? fill.amountCents : order.amountCents
|
|
184
|
+
const fees = fill ? fill.feesCents : order.feesCents
|
|
185
|
+
return h('div', { className: 'pt-receipt', 'data-state': 'filled', title: order.id },
|
|
186
|
+
`成交:${side} ${symbol} ${count(qty)} 股 · 价 ${money(price)} · 金额 ${cents(amount)} · 费用 ${cents(fees)} · 成交后现金 ${money(cash)} · 单 ${order.id}`,
|
|
187
|
+
)
|
|
188
|
+
}
|
|
189
|
+
const pendingText = order.status === 'queued'
|
|
190
|
+
? '当前时段不撮合,已受理为下一交易时段开盘委托'
|
|
191
|
+
: '现价未穿越,成交前一直挂单'
|
|
192
|
+
return h('div', { className: 'pt-receipt', 'data-state': 'resting', title: order.id },
|
|
193
|
+
`挂单:${side} ${symbol} ${count(order.qty)} 股${order.limitPrice === undefined ? '' : ` @ ${money(order.limitPrice)}`} · ${pendingText} · 单 ${order.id}`,
|
|
194
|
+
)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const marketSessionNote = session => {
|
|
198
|
+
if (!session || session.canMatch) return ''
|
|
199
|
+
if (session.phase === 'pre_open') return '集合竞价中,现价为最近一次行情'
|
|
200
|
+
if (session.phase === 'auction_close') return '收盘竞价中,现价为最近一次行情'
|
|
201
|
+
return '非交易时段,现价为最近一次行情'
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function MarketTab({ searchQuery, setSearchQuery, runSearch, searchHits, searchAttempted, shortlist, isShortlisted, toggleShortlist, marketList, marketBoard, setMarketBoard, shortlistOnly, setShortlistOnly, loadMarket, busy, adoptQuote, session }) {
|
|
205
|
+
const visibleQuotes = marketList === null
|
|
206
|
+
? []
|
|
207
|
+
: marketList.quotes.filter(item => !shortlistOnly || isShortlisted(item.symbol)).slice(0, 50)
|
|
208
|
+
return h('div', { style: { display: 'flex', flexDirection: 'column', gap: 10 } },
|
|
209
|
+
h('div', { className: 'pt-card' },
|
|
210
|
+
h('div', { className: 'pt-card-title' }, '搜索标的'),
|
|
211
|
+
h('div', { className: 'pt-row' },
|
|
212
|
+
h('input', {
|
|
213
|
+
className: 'pt-input', style: { flex: 1, minWidth: 200 }, value: searchQuery,
|
|
214
|
+
placeholder: '代码或名称,例如 600519.SH / 贵州茅台',
|
|
215
|
+
onChange: event => setSearchQuery(event.target.value),
|
|
216
|
+
onKeyDown: event => { if (event.key === 'Enter') void runSearch() },
|
|
217
|
+
}),
|
|
218
|
+
h('button', { className: 'pt-btn', onClick: runSearch, disabled: busy }, '搜索'),
|
|
219
|
+
),
|
|
220
|
+
searchHits.length > 0 ? h('div', { style: { marginTop: 8, display: 'flex', flexDirection: 'column', gap: 4 } },
|
|
221
|
+
searchHits.map(hit => h('div', { key: hit.symbol, className: 'pt-row' },
|
|
222
|
+
h('button', {
|
|
223
|
+
className: 'pt-btn', style: { flex: 1, justifyContent: 'flex-start' },
|
|
224
|
+
onClick: () => adoptQuote(hit.symbol, null),
|
|
225
|
+
}, `${hit.symbol} ${hit.name ?? ''}`),
|
|
226
|
+
h('button', { className: 'pt-btn', onClick: () => toggleShortlist(hit.symbol) }, isShortlisted(hit.symbol) ? '已备选' : '加备选'),
|
|
227
|
+
)),
|
|
228
|
+
) : searchAttempted ? h('div', { className: 'pt-empty', style: { marginTop: 8 } }, '没有找到匹配标的') : null,
|
|
229
|
+
),
|
|
230
|
+
h('div', { className: 'pt-card' },
|
|
231
|
+
h('div', { className: 'pt-row' },
|
|
232
|
+
h('div', { className: 'pt-card-title', style: { margin: 0 } }, '全市场'),
|
|
233
|
+
[['main', '主板'], ['star', '科创板'], ['chi_next', '创业板'], ['bjse', '北交所'], ['xshg', '沪市'], ['xshe', '深市']].map(([key, label]) =>
|
|
234
|
+
h('button', { key, className: 'pt-btn', 'data-active': marketBoard === key, onClick: () => setMarketBoard(key) }, label),
|
|
235
|
+
),
|
|
236
|
+
h('button', { className: 'pt-btn', 'data-active': shortlistOnly, onClick: () => setShortlistOnly(value => !value) }, '只看备选'),
|
|
237
|
+
h('span', { className: 'pt-note' }, `第 ${marketList?.page ?? 1} 页`),
|
|
238
|
+
h('button', {
|
|
239
|
+
className: 'pt-btn', onClick: () => loadMarket(Math.max(1, (marketList?.page ?? 1) - 1)),
|
|
240
|
+
disabled: marketList === null || (marketList.page ?? 1) <= 1,
|
|
241
|
+
}, '上一页'),
|
|
242
|
+
h('button', {
|
|
243
|
+
className: 'pt-btn', onClick: () => loadMarket((marketList?.page ?? 1) + 1),
|
|
244
|
+
disabled: marketList !== null && marketList.quotes.length < 50,
|
|
245
|
+
}, '下一页'),
|
|
246
|
+
),
|
|
247
|
+
marketSessionNote(session) !== ''
|
|
248
|
+
? h('div', { className: 'pt-note' }, marketSessionNote(session))
|
|
249
|
+
: null,
|
|
250
|
+
marketList !== null && marketList.quotes.length > 0 && marketList.notice
|
|
251
|
+
? h('div', { className: 'pt-note' }, marketList.notice)
|
|
252
|
+
: null,
|
|
253
|
+
marketList === null ? h('div', { className: 'pt-empty' }, '加载行情…')
|
|
254
|
+
: marketList.quotes.length === 0 ? h('div', { className: 'pt-empty' }, marketList.notice ?? '没有行情')
|
|
255
|
+
: visibleQuotes.length === 0
|
|
256
|
+
? h('div', { className: 'pt-empty' }, shortlistOnly
|
|
257
|
+
? (shortlist.length === 0 ? '还没有备选,点行情行的 ☆ 添加' : '本页没有备选,可翻页或关闭「只看备选」')
|
|
258
|
+
: '没有行情')
|
|
259
|
+
: h(Table, null,
|
|
260
|
+
h('thead', null, h('tr', null, ['代码', '名称', '现价', '涨跌幅', '备选'].map(label => h('th', { key: label }, label)))),
|
|
261
|
+
h('tbody', null, visibleQuotes.map(item => {
|
|
262
|
+
const change = item.prevClose && item.prevClose > 0 ? (item.last - item.prevClose) / item.prevClose : null
|
|
263
|
+
return h('tr', {
|
|
264
|
+
key: `${item.symbol}-${item.last}`, style: { cursor: 'pointer' },
|
|
265
|
+
onClick: () => adoptQuote(item.symbol, item),
|
|
266
|
+
},
|
|
267
|
+
h('td', null, item.symbol),
|
|
268
|
+
h('td', null, item.name ?? '—'),
|
|
269
|
+
h('td', null, money(item.last)),
|
|
270
|
+
h('td', { className: tone(change ?? 0) }, change === null ? '—' : pct(change)),
|
|
271
|
+
h('td', null, h('button', { className: 'pt-btn', onClick: event => { event.stopPropagation(); void toggleShortlist(item.symbol) } }, isShortlisted(item.symbol) ? '★' : '☆')),
|
|
272
|
+
)
|
|
273
|
+
})),
|
|
274
|
+
),
|
|
275
|
+
),
|
|
276
|
+
)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function PositionsTab({ positions, onSell }) {
|
|
280
|
+
if (positions.length === 0) return h('div', { className: 'pt-empty' }, '无持仓 · 成交后自动显示,可在交易页下单')
|
|
281
|
+
return h(Table, null,
|
|
282
|
+
h('thead', null, h('tr', null,
|
|
283
|
+
['代码', '名称', '数量', '可卖', '成本', '现价', '浮盈亏'].map(label => h('th', { key: label }, label)),
|
|
284
|
+
h('th', null, '操作'),
|
|
285
|
+
)),
|
|
286
|
+
h('tbody', null, positions.map(position => h('tr', { key: position.symbol },
|
|
287
|
+
h('td', null, position.symbol),
|
|
288
|
+
h('td', null, position.name ?? '—'),
|
|
289
|
+
h('td', null, count(position.qty)),
|
|
290
|
+
h('td', null, count(position.available)),
|
|
291
|
+
h('td', null, money(position.avgCost)),
|
|
292
|
+
h('td', null, money(position.lastPrice)),
|
|
293
|
+
h('td', { className: tone(position.unrealizedPnlCents ?? 0) }, cents(position.unrealizedPnlCents)),
|
|
294
|
+
h('td', null, h('button', {
|
|
295
|
+
className: 'pt-btn',
|
|
296
|
+
onClick: () => onSell(position),
|
|
297
|
+
disabled: !(position.available > 0),
|
|
298
|
+
title: position.available > 0 ? undefined : 'T+1:今日买入的股份明日可卖',
|
|
299
|
+
}, '卖出')),
|
|
300
|
+
))),
|
|
301
|
+
)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function OrdersTab({ pending, history, busy, onCancel }) {
|
|
305
|
+
return h('div', { style: { display: 'flex', flexDirection: 'column', gap: 12 } },
|
|
306
|
+
h('div', null,
|
|
307
|
+
h('div', { className: 'pt-sub' }, '挂单'),
|
|
308
|
+
pending.length === 0 ? h('div', { className: 'pt-empty' }, '无挂单') : h(Table, null,
|
|
309
|
+
h('thead', null, h('tr', null, ['代码', '方向', '类型', '数量', '限价', '状态', '备注', '操作'].map(label => h('th', { key: label }, label)))),
|
|
310
|
+
h('tbody', null, pending.map(order => h('tr', { key: order.id },
|
|
311
|
+
h('td', null, order.symbol),
|
|
312
|
+
h('td', null, order.side === 'buy' ? '买' : '卖'),
|
|
313
|
+
h('td', null, order.type === 'limit' ? '限价' : '市价'),
|
|
314
|
+
h('td', null, count(order.qty)),
|
|
315
|
+
h('td', null, order.limitPrice === undefined ? '—' : money(order.limitPrice)),
|
|
316
|
+
h('td', null, STATUS_TEXT[order.status] ?? order.status),
|
|
317
|
+
h('td', { title: order.note ?? undefined }, order.note === undefined ? '' : h('span', { className: 'pt-note-cell' }, order.note)),
|
|
318
|
+
h('td', null, h('button', { className: 'pt-btn', onClick: () => onCancel(order.id), disabled: busy }, '撤单')),
|
|
319
|
+
))),
|
|
320
|
+
),
|
|
321
|
+
pending.some(order => order.status === 'queued')
|
|
322
|
+
? h('div', { className: 'pt-note' }, '「待开盘」:当前时段不撮合,下一交易时段开盘后按当时价格尝试成交,可随时撤单。')
|
|
323
|
+
: null,
|
|
324
|
+
),
|
|
325
|
+
h('div', null,
|
|
326
|
+
h('div', { className: 'pt-sub' }, '历史'),
|
|
327
|
+
history.length === 0 ? h('div', { className: 'pt-empty' }, '无历史订单') : h(Table, null,
|
|
328
|
+
h('thead', null, h('tr', null, ['时间', '代码', '方向', '数量', '限价', '状态', '成交价', '原因', '备注'].map(label => h('th', { key: label }, label)))),
|
|
329
|
+
h('tbody', null, history.slice(0, 30).map(order => h('tr', { key: order.id },
|
|
330
|
+
h('td', null, dateTime(order.createdAt)),
|
|
331
|
+
h('td', null, order.symbol),
|
|
332
|
+
h('td', null, order.side === 'buy' ? '买' : '卖'),
|
|
333
|
+
h('td', null, count(order.qty)),
|
|
334
|
+
h('td', null, order.limitPrice === undefined ? '—' : money(order.limitPrice)),
|
|
335
|
+
h('td', null, STATUS_TEXT[order.status] ?? order.status),
|
|
336
|
+
h('td', null, order.fillPrice === undefined ? '—' : money(order.fillPrice)),
|
|
337
|
+
h('td', { title: order.reason ?? undefined }, reasonText(order.reason)),
|
|
338
|
+
h('td', { title: order.note ?? undefined }, order.note === undefined ? '' : h('span', { className: 'pt-note-cell' }, order.note)),
|
|
339
|
+
))),
|
|
340
|
+
),
|
|
341
|
+
history.length > 30 ? h('div', { className: 'pt-note' }, `仅显示最近 30 条,共 ${history.length} 条`) : null,
|
|
342
|
+
),
|
|
343
|
+
)
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function ReviewTab({ review, period, setPeriod, trades, daily, cashFlow, trips }) {
|
|
347
|
+
return h('div', { style: { display: 'flex', flexDirection: 'column', gap: 10 } },
|
|
348
|
+
h('div', { className: 'pt-row' }, PERIODS.map(([key, label]) =>
|
|
349
|
+
h('button', {
|
|
350
|
+
key, className: 'pt-btn', 'data-active': period === key,
|
|
351
|
+
onClick: () => setPeriod(key),
|
|
352
|
+
}, label),
|
|
353
|
+
)),
|
|
354
|
+
review ? h('div', { style: { display: 'flex', flexDirection: 'column', gap: 10 } },
|
|
355
|
+
h('div', { className: 'pt-stats' },
|
|
356
|
+
h('div', { className: 'pt-stat' }, h('div', { className: 'pt-stat-label' }, '成交笔数'), h('div', { className: 'pt-stat-value' }, count(review.tradeCount))),
|
|
357
|
+
h('div', { className: 'pt-stat' }, h('div', { className: 'pt-stat-label' }, '已实现盈亏'), h('div', { className: `pt-stat-value ${tone(review.realizedPnl)}` }, signed(review.realizedPnl))),
|
|
358
|
+
h('div', { className: 'pt-stat' }, h('div', { className: 'pt-stat-label' }, '费用合计'), h('div', { className: 'pt-stat-value' }, money(review.totalFees))),
|
|
359
|
+
h('div', { className: 'pt-stat' }, h('div', { className: 'pt-stat-label' }, '最大回撤'), h('div', { className: 'pt-stat-value' }, pct(review.maxDrawdownPct))),
|
|
360
|
+
),
|
|
361
|
+
h('div', { className: 'pt-chart-wrap' }, h(EquityChart, { points: review.equityCurve })),
|
|
362
|
+
h('div', null,
|
|
363
|
+
h('div', { className: 'pt-sub' }, '成交记录'),
|
|
364
|
+
trades.length === 0 ? h('div', { className: 'pt-empty' }, '无成交') : h(Table, null,
|
|
365
|
+
h('thead', null, h('tr', null, ['时间', '代码', '方向', '数量', '价格', '金额', '费用'].map(label => h('th', { key: label }, label)))),
|
|
366
|
+
h('tbody', null, trades.slice(0, 30).map(trade => h('tr', { key: trade.id },
|
|
367
|
+
h('td', null, dateTime(trade.ts)),
|
|
368
|
+
h('td', null, trade.symbol),
|
|
369
|
+
h('td', null, trade.side === 'buy' ? '买' : '卖'),
|
|
370
|
+
h('td', null, count(trade.qty)),
|
|
371
|
+
h('td', null, money(trade.price)),
|
|
372
|
+
h('td', null, cents(trade.amountCents)),
|
|
373
|
+
h('td', null, cents(trade.feesCents)),
|
|
374
|
+
))),
|
|
375
|
+
),
|
|
376
|
+
trades.length > 30 ? h('div', { className: 'pt-note' }, `仅显示最近 30 条,共 ${trades.length} 条`) : null,
|
|
377
|
+
),
|
|
378
|
+
h('div', null,
|
|
379
|
+
h('div', { className: 'pt-sub' }, '日终报告'),
|
|
380
|
+
daily.length === 0 ? h('div', { className: 'pt-empty' }, '暂无日终快照') : h(Table, null,
|
|
381
|
+
h('thead', null, h('tr', null, ['日期', '权益', '当日盈亏', '费用', '收盘价来源'].map(label => h('th', { key: label }, label)))),
|
|
382
|
+
h('tbody', null, daily.slice(-14).reverse().map(row => h('tr', { key: row.date },
|
|
383
|
+
h('td', null, row.date),
|
|
384
|
+
h('td', null, money(row.equity)),
|
|
385
|
+
h('td', { className: tone(row.dayPnl) }, signed(row.dayPnl)),
|
|
386
|
+
h('td', null, money(row.fees)),
|
|
387
|
+
h('td', null, row.closeSource === 'daily' ? '日K收盘' : row.closeSource === 'last' ? '最后报价' : '缺失'),
|
|
388
|
+
))),
|
|
389
|
+
),
|
|
390
|
+
daily.length > 14 ? h('div', { className: 'pt-note' }, `仅显示最近 14 个交易日,共 ${daily.length} 个`) : null,
|
|
391
|
+
),
|
|
392
|
+
h('div', null,
|
|
393
|
+
h('div', { className: 'pt-sub' }, '资金流水'),
|
|
394
|
+
cashFlow.length === 0 ? h('div', { className: 'pt-empty' }, '暂无流水') : h(Table, null,
|
|
395
|
+
h('thead', null, h('tr', null, ['时间', '类型', '金额', '余额'].map(label => h('th', { key: label }, label)))),
|
|
396
|
+
h('tbody', null, cashFlow.slice(-30).reverse().map((row, index) => h('tr', { key: `${row.ts}-${index}` },
|
|
397
|
+
h('td', null, dateTime(row.ts)),
|
|
398
|
+
h('td', null, CASH_TYPE_TEXT[row.type] ?? row.type),
|
|
399
|
+
h('td', { className: tone(row.amount) }, signed(row.amount)),
|
|
400
|
+
h('td', null, money(row.balance)),
|
|
401
|
+
))),
|
|
402
|
+
),
|
|
403
|
+
cashFlow.length > 30 ? h('div', { className: 'pt-note' }, `仅显示最近 30 条,共 ${cashFlow.length} 条`) : null,
|
|
404
|
+
),
|
|
405
|
+
h('div', null,
|
|
406
|
+
h('div', { className: 'pt-sub' }, '回合统计(按标的)'),
|
|
407
|
+
trips.length === 0 ? h('div', { className: 'pt-empty' }, '暂无已平仓记录') : h(Table, null,
|
|
408
|
+
h('thead', null, h('tr', null, ['代码', '数量', '成本', '收入', '盈亏', '持有天数'].map(label => h('th', { key: label }, label)))),
|
|
409
|
+
h('tbody', null, trips.slice(-30).reverse().map(trip => h('tr', { key: trip.symbol },
|
|
410
|
+
h('td', null, trip.symbol),
|
|
411
|
+
h('td', null, count(trip.qty)),
|
|
412
|
+
h('td', null, money(trip.cost)),
|
|
413
|
+
h('td', null, money(trip.proceeds)),
|
|
414
|
+
h('td', { className: tone(trip.pnl) }, signed(trip.pnl)),
|
|
415
|
+
h('td', null, count(trip.holdingDays)),
|
|
416
|
+
))),
|
|
417
|
+
),
|
|
418
|
+
trips.length > 30 ? h('div', { className: 'pt-note' }, `仅显示最近 30 条,共 ${trips.length} 条`) : null,
|
|
419
|
+
),
|
|
420
|
+
) : h('div', { className: 'pt-empty' }, '加载复盘…'),
|
|
421
|
+
)
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/* --------------------------------------------------------- basket ----- */
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Basket import: CSV in, host-computed plan out. Confirming submits the
|
|
428
|
+
* plan's `{symbol, qty}` rows verbatim, so the quantities the user sees are
|
|
429
|
+
* the quantities that reach the ledger. All state is local: leaving the tab
|
|
430
|
+
* unmounts the panel and clears the import.
|
|
431
|
+
*/
|
|
432
|
+
function BasketTab({ account, onDone, resetNonce }) {
|
|
433
|
+
const [csv, setCsv] = useState('')
|
|
434
|
+
const [fileName, setFileName] = useState('')
|
|
435
|
+
const [budget, setBudget] = useState('')
|
|
436
|
+
const [preview, setPreview] = useState(null)
|
|
437
|
+
const [result, setResult] = useState(null)
|
|
438
|
+
const [busy, setBusy] = useState(false)
|
|
439
|
+
const [error, setError] = useState('')
|
|
440
|
+
// Frozen when a preview succeeds: retrying a lost response must resend
|
|
441
|
+
// the same batchId, otherwise the host would buy the basket twice.
|
|
442
|
+
const [batchId, setBatchId] = useState('')
|
|
443
|
+
const placing = useRef(false)
|
|
444
|
+
// A preview is only valid for the CSV/budget it was computed from.
|
|
445
|
+
const previewSeq = useRef(0)
|
|
446
|
+
// A reset changes the cash the preview was based on: drop it.
|
|
447
|
+
useEffect(() => {
|
|
448
|
+
previewSeq.current += 1
|
|
449
|
+
setBusy(false)
|
|
450
|
+
setPreview(null)
|
|
451
|
+
setResult(null)
|
|
452
|
+
setBatchId('')
|
|
453
|
+
setError('')
|
|
454
|
+
}, [resetNonce])
|
|
455
|
+
const nextBatchId = () => typeof crypto !== 'undefined' && crypto.randomUUID
|
|
456
|
+
? crypto.randomUUID()
|
|
457
|
+
: `basket-${Date.now()}-${Math.random().toString(36).slice(2)}`
|
|
458
|
+
|
|
459
|
+
// Empty means "host uses the available cash"; the input is yuan, cents go out.
|
|
460
|
+
const budgetCents = useMemo(() => {
|
|
461
|
+
const text = budget.trim()
|
|
462
|
+
if (text === '') return undefined
|
|
463
|
+
const value = Number(text)
|
|
464
|
+
return Number.isFinite(value) && value > 0 ? Math.round(value * 100) : NaN
|
|
465
|
+
}, [budget])
|
|
466
|
+
|
|
467
|
+
const budgetInvalid = budget.trim() !== '' && Number.isNaN(budgetCents)
|
|
468
|
+
|
|
469
|
+
const readFile = useCallback(async event => {
|
|
470
|
+
const file = event.target.files && event.target.files[0]
|
|
471
|
+
// Clear the value so picking the same file again still fires a change.
|
|
472
|
+
event.target.value = ''
|
|
473
|
+
if (!file) return
|
|
474
|
+
previewSeq.current += 1
|
|
475
|
+
setBusy(false)
|
|
476
|
+
setError('')
|
|
477
|
+
setPreview(null)
|
|
478
|
+
setResult(null)
|
|
479
|
+
setBatchId('')
|
|
480
|
+
setFileName(file.name)
|
|
481
|
+
// The RPC has the same cap; catching it here gives instant feedback.
|
|
482
|
+
if (file.size > 32 * 1024) {
|
|
483
|
+
setCsv('')
|
|
484
|
+
setError('CSV 文件不能超过 32 KiB')
|
|
485
|
+
return
|
|
486
|
+
}
|
|
487
|
+
try {
|
|
488
|
+
setCsv(await file.text())
|
|
489
|
+
} catch {
|
|
490
|
+
setError('读取文件失败')
|
|
491
|
+
}
|
|
492
|
+
}, [])
|
|
493
|
+
|
|
494
|
+
const runPreview = useCallback(async () => {
|
|
495
|
+
if (csv.trim() === '') return
|
|
496
|
+
if (Number.isNaN(budgetCents)) {
|
|
497
|
+
setError('预算需为正数')
|
|
498
|
+
return
|
|
499
|
+
}
|
|
500
|
+
const seq = ++previewSeq.current
|
|
501
|
+
const sourceCsv = csv
|
|
502
|
+
setBusy(true)
|
|
503
|
+
setError('')
|
|
504
|
+
setResult(null)
|
|
505
|
+
try {
|
|
506
|
+
const payload = { csv: sourceCsv }
|
|
507
|
+
if (budgetCents !== undefined) payload.budgetCents = budgetCents
|
|
508
|
+
const next = await rpc('basket/preview', payload)
|
|
509
|
+
// A file/budget change while the request was in flight wins.
|
|
510
|
+
if (seq !== previewSeq.current) return
|
|
511
|
+
setPreview(next)
|
|
512
|
+
setBatchId(nextBatchId())
|
|
513
|
+
} catch (cause) {
|
|
514
|
+
if (seq === previewSeq.current) {
|
|
515
|
+
setError(errorOf(cause).text)
|
|
516
|
+
setPreview(null)
|
|
517
|
+
}
|
|
518
|
+
} finally {
|
|
519
|
+
if (seq === previewSeq.current) setBusy(false)
|
|
520
|
+
}
|
|
521
|
+
}, [csv, budgetCents])
|
|
522
|
+
|
|
523
|
+
const rows = useMemo(
|
|
524
|
+
() => preview === null ? [] : preview.rows.filter(row => row.qty > 0).map(row => ({ symbol: row.symbol, qty: row.qty })),
|
|
525
|
+
[preview],
|
|
526
|
+
)
|
|
527
|
+
const placeReason = useMemo(() => {
|
|
528
|
+
if (account !== null && account.blocked === true) return '账户已锁定'
|
|
529
|
+
if (preview === null) return '先批量预览'
|
|
530
|
+
if (rows.length === 0) return '没有可下单的行'
|
|
531
|
+
if (preview.session && preview.session.phase === 'auction_close') return '收盘竞价暂不支持模拟撮合'
|
|
532
|
+
return ''
|
|
533
|
+
}, [account, preview, rows])
|
|
534
|
+
|
|
535
|
+
const place = useCallback(async () => {
|
|
536
|
+
if (busy || placeReason !== '' || placing.current) return
|
|
537
|
+
placing.current = true
|
|
538
|
+
setBusy(true)
|
|
539
|
+
setError('')
|
|
540
|
+
try {
|
|
541
|
+
// Reuse the preview's id: a retry after a lost response must be
|
|
542
|
+
// idempotent on the host, not a second purchase.
|
|
543
|
+
const id = batchId !== '' ? batchId : nextBatchId()
|
|
544
|
+
setResult(await rpc('basket/place', { batchId: id, rows }))
|
|
545
|
+
if (typeof onDone === 'function') await onDone()
|
|
546
|
+
} catch (cause) {
|
|
547
|
+
setError(errorOf(cause).text)
|
|
548
|
+
} finally {
|
|
549
|
+
placing.current = false
|
|
550
|
+
setBusy(false)
|
|
551
|
+
}
|
|
552
|
+
}, [busy, placeReason, rows, onDone, batchId])
|
|
553
|
+
|
|
554
|
+
return h('div', { className: 'pt-trade' },
|
|
555
|
+
h('div', { className: 'pt-card pt-ticket-card' },
|
|
556
|
+
h('div', { className: 'pt-card-title' }, '批量买入'),
|
|
557
|
+
h('div', { className: 'pt-row' },
|
|
558
|
+
h(Field, { label: 'CSV 文件' }, h('input', {
|
|
559
|
+
className: 'pt-input', type: 'file', accept: '.csv,text/csv', onChange: readFile,
|
|
560
|
+
})),
|
|
561
|
+
fileName !== '' ? h('div', { className: 'pt-note' }, fileName) : null,
|
|
562
|
+
h(Field, { label: '预算(元)' }, h('input', {
|
|
563
|
+
className: 'pt-input', style: { width: 140 }, value: budget, inputMode: 'decimal',
|
|
564
|
+
placeholder: account === null ? '默认可用现金' : `默认可用 ${money(account.cash)}`,
|
|
565
|
+
onChange: event => {
|
|
566
|
+
previewSeq.current += 1
|
|
567
|
+
setBusy(false)
|
|
568
|
+
setBudget(budgetInput(event.target.value))
|
|
569
|
+
setPreview(null)
|
|
570
|
+
setResult(null)
|
|
571
|
+
setBatchId('')
|
|
572
|
+
setError('')
|
|
573
|
+
},
|
|
574
|
+
})),
|
|
575
|
+
h('button', {
|
|
576
|
+
className: 'pt-btn pt-btn-primary', onClick: runPreview, disabled: busy || csv.trim() === '' || budgetInvalid,
|
|
577
|
+
title: budgetInvalid ? '预算需为正数' : csv.trim() === '' ? '请先选择 CSV 文件' : undefined,
|
|
578
|
+
}, busy ? '预览中…' : '批量预览'),
|
|
579
|
+
budgetInvalid ? h('div', { className: 'pt-note pt-risk-high' }, '预算需为正数') : null,
|
|
580
|
+
),
|
|
581
|
+
h('div', { className: 'pt-note' }, csv.trim() === ''
|
|
582
|
+
? '先选择 CSV 文件;两列「代码,权重」,最多 20 只'
|
|
583
|
+
: '两列「代码,权重」,最多 20 只,代码写 600519.SH 或 000001.SZ;仅连续竞价时段可下单'),
|
|
584
|
+
error !== '' ? h('div', { className: 'pt-error', style: { marginTop: 8 } }, error) : null,
|
|
585
|
+
),
|
|
586
|
+
preview !== null ? h('div', { className: 'pt-card' },
|
|
587
|
+
h('div', { className: 'pt-card-title' }, '预览'),
|
|
588
|
+
h(Table, null,
|
|
589
|
+
h('thead', null, h('tr', null,
|
|
590
|
+
['代码', '名称', '目标权重', '目标金额', '股数', '金额', '费用', '实际权重', '状态'].map(label => h('th', { key: label }, label)),
|
|
591
|
+
)),
|
|
592
|
+
h('tbody', null, preview.rows.map(row => h('tr', { key: row.symbol },
|
|
593
|
+
h('td', null, row.symbol),
|
|
594
|
+
h('td', null, row.name ?? '—'),
|
|
595
|
+
h('td', null, pct(row.weight)),
|
|
596
|
+
h('td', null, cents(row.targetCents)),
|
|
597
|
+
h('td', null, count(row.qty)),
|
|
598
|
+
h('td', null, cents(row.amountCents)),
|
|
599
|
+
h('td', null, cents(row.feesCents)),
|
|
600
|
+
h('td', null, pct(row.actualWeight)),
|
|
601
|
+
h('td', {
|
|
602
|
+
title: row.warning || undefined,
|
|
603
|
+
className: row.status === 'ok' ? undefined : 'pt-note',
|
|
604
|
+
}, BASKET_STATUS_TEXT[row.status] ?? row.status),
|
|
605
|
+
))),
|
|
606
|
+
),
|
|
607
|
+
h('div', { className: 'pt-quote', style: { marginTop: 8 } },
|
|
608
|
+
[['合计金额', preview.totalAmountCents], ['费用', preview.totalFeesCents],
|
|
609
|
+
['剩余现金', preview.residualCents], ['成交后现金', preview.cashAfterCents]].map(([label, value]) =>
|
|
610
|
+
h('span', { key: label }, `${label} ${cents(value)}`)),
|
|
611
|
+
),
|
|
612
|
+
preview.issues.length > 0 ? h('div', { className: 'pt-error', style: { marginTop: 8 } },
|
|
613
|
+
preview.issues.map(issue => `${issue.line > 0 ? `第 ${issue.line} 行:` : ''}${issue.message}`).join(';'),
|
|
614
|
+
) : null,
|
|
615
|
+
preview.warnings.length > 0 ? h('div', { className: 'pt-note', style: { marginTop: 8 } }, preview.warnings.join(';')) : null,
|
|
616
|
+
h('div', { className: 'pt-row', style: { marginTop: 8 } },
|
|
617
|
+
placeReason !== '' ? h('div', { className: 'pt-note', style: { marginRight: 'auto' } }, placeReason) : null,
|
|
618
|
+
h('button', {
|
|
619
|
+
className: 'pt-btn pt-btn-primary', onClick: place, disabled: busy || placeReason !== '',
|
|
620
|
+
title: placeReason || undefined,
|
|
621
|
+
}, busy ? '提交中…' : '确认下单'),
|
|
622
|
+
),
|
|
623
|
+
) : null,
|
|
624
|
+
result !== null ? h('div', { className: 'pt-card' },
|
|
625
|
+
h('div', { className: 'pt-card-title' }, '下单结果'),
|
|
626
|
+
h(Table, null,
|
|
627
|
+
h('thead', null, h('tr', null,
|
|
628
|
+
['代码', '状态', '数量', '成交价', '原因'].map(label => h('th', { key: label }, label)),
|
|
629
|
+
)),
|
|
630
|
+
h('tbody', null, result.rows.map(row => h('tr', { key: row.symbol },
|
|
631
|
+
h('td', null, row.symbol),
|
|
632
|
+
h('td', null, row.order ? STATUS_TEXT[row.order.status] ?? row.order.status : '拒绝'),
|
|
633
|
+
h('td', null, count(row.order ? row.order.qty : undefined)),
|
|
634
|
+
h('td', null, row.order && row.order.fillPrice !== undefined ? money(row.order.fillPrice) : '—'),
|
|
635
|
+
h('td', { title: row.error ?? row.order?.reason ?? undefined }, reasonText(row.error ?? row.order?.reason)),
|
|
636
|
+
))),
|
|
637
|
+
),
|
|
638
|
+
h('div', { className: 'pt-note', style: { marginTop: 8 } }, `成交后现金 ${cents(result.cashAfterCents)}`),
|
|
639
|
+
) : null,
|
|
640
|
+
)
|
|
641
|
+
}
|