@stocksharp/trading-controls 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 +30 -0
- package/NOTICE +21 -0
- package/README.md +240 -0
- package/dist/esm/active-orders-widget.js +382 -0
- package/dist/esm/active-orders-widget.js.map +1 -0
- package/dist/esm/control-types.js +23 -0
- package/dist/esm/control-types.js.map +1 -0
- package/dist/esm/dom.js +55 -0
- package/dist/esm/dom.js.map +1 -0
- package/dist/esm/formatters.js +69 -0
- package/dist/esm/formatters.js.map +1 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/positions-widget.js +258 -0
- package/dist/esm/positions-widget.js.map +1 -0
- package/dist/esm/trade-history-widget.js +197 -0
- package/dist/esm/trade-history-widget.js.map +1 -0
- package/dist/esm/trading-data.js +26 -0
- package/dist/esm/trading-data.js.map +1 -0
- package/dist/esm/trading-host.js +79 -0
- package/dist/esm/trading-host.js.map +1 -0
- package/dist/esm/watchlist-widget.js +481 -0
- package/dist/esm/watchlist-widget.js.map +1 -0
- package/dist/sstradingcontrols.js +1740 -0
- package/dist/sstradingcontrols.js.map +7 -0
- package/dist/types/active-orders-widget.d.ts +52 -0
- package/dist/types/active-orders-widget.d.ts.map +1 -0
- package/dist/types/control-types.d.ts +8 -0
- package/dist/types/control-types.d.ts.map +1 -0
- package/dist/types/dom.d.ts +6 -0
- package/dist/types/dom.d.ts.map +1 -0
- package/dist/types/formatters.d.ts +8 -0
- package/dist/types/formatters.d.ts.map +1 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/positions-widget.d.ts +37 -0
- package/dist/types/positions-widget.d.ts.map +1 -0
- package/dist/types/trade-history-widget.d.ts +25 -0
- package/dist/types/trade-history-widget.d.ts.map +1 -0
- package/dist/types/trading-data.d.ts +55 -0
- package/dist/types/trading-data.d.ts.map +1 -0
- package/dist/types/trading-host.d.ts +62 -0
- package/dist/types/trading-host.d.ts.map +1 -0
- package/dist/types/watchlist-widget.d.ts +60 -0
- package/dist/types/watchlist-widget.d.ts.map +1 -0
- package/package.json +129 -0
- package/src/active-orders-widget.ts +411 -0
- package/src/control-types.ts +24 -0
- package/src/dom.ts +68 -0
- package/src/formatters.ts +72 -0
- package/src/index.ts +52 -0
- package/src/positions-widget.ts +305 -0
- package/src/trade-history-widget.ts +221 -0
- package/src/trading-data.ts +121 -0
- package/src/trading-host.ts +300 -0
- package/src/watchlist-widget.ts +513 -0
- package/styles/theme.css +86 -0
- package/styles/trading-controls.css +469 -0
- package/translation-keys.json +58 -0
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
// Watchlist — multi-instance.
|
|
2
|
+
//
|
|
3
|
+
// Builds its own DOM (see `_buildRoot`) rather than cloning a <template> out of
|
|
4
|
+
// the page, so it can be constructed by any host that supplies a `TradingHost`.
|
|
5
|
+
// Each instance has its own search/filter/sort state; favourites are shared,
|
|
6
|
+
// held under one key in the host's preference store.
|
|
7
|
+
//
|
|
8
|
+
// The table is `DataGrid` from `@stocksharp/grids`. The grid holds the FILTERED
|
|
9
|
+
// instruments and does the sorting, which is what makes the two caps mean
|
|
10
|
+
// different things: `renderLimit` caps what gets painted while the export and
|
|
11
|
+
// the subscription sync read the whole filtered set. A live quote patches one
|
|
12
|
+
// cell through the grid's (rowKey, columnKey) lookup instead of repainting — a
|
|
13
|
+
// repaint would restart the flash animation it just triggered.
|
|
14
|
+
import { makeElement, makeIconButton, makePanelId, makePanelRoot } from './dom.js';
|
|
15
|
+
import { ControlTypes } from './control-types.js';
|
|
16
|
+
import { MarketDataClient, MarketDataLevels, TradingApi, TradingHost, assertHost } from './trading-host.js';
|
|
17
|
+
import type { InstrumentRow, QuoteStats } from './trading-data.js';
|
|
18
|
+
import { DataGrid, GridColumn } from '@stocksharp/grids/source/data-grid';
|
|
19
|
+
|
|
20
|
+
/// What the panel needs beyond the host port. Picking an instrument is the one
|
|
21
|
+
/// thing it reports that is not the ticker: the panel does not switch the
|
|
22
|
+
/// host's instrument itself, it says what the user chose and the host acts.
|
|
23
|
+
export interface WatchlistDeps {
|
|
24
|
+
host: TradingHost;
|
|
25
|
+
onSelect(symbol: string): void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class WatchlistWidget {
|
|
29
|
+
static FAVORITES_KEY = 'terminal_watchlist_favorites';
|
|
30
|
+
// Cache-key prefix for the per-session baseline price used to compute the
|
|
31
|
+
// change %. Day-scoped so the baseline resets every UTC day and the
|
|
32
|
+
// percent matches "since today open" intuitively. (Without a REST bars
|
|
33
|
+
// endpoint there is no broker-side day-open available, and the watchlist
|
|
34
|
+
// deliberately avoids REST — the live stream is its only source.) It goes
|
|
35
|
+
// in the host's cache rather than its preferences: one key per symbol per
|
|
36
|
+
// day is scratch, not a setting worth syncing.
|
|
37
|
+
static BASELINE_KEY_PREFIX = 'wlBaseline:';
|
|
38
|
+
static VISIBLE_CAP = 30;
|
|
39
|
+
static RENDER_CAP = 300;
|
|
40
|
+
static TYPE = ControlTypes.Watchlist;
|
|
41
|
+
|
|
42
|
+
rootEl: HTMLElement;
|
|
43
|
+
api: TradingApi;
|
|
44
|
+
instruments: InstrumentRow[];
|
|
45
|
+
stats: Map<string, QuoteStats>;
|
|
46
|
+
favorites: Set<string>;
|
|
47
|
+
filter: string;
|
|
48
|
+
search: string;
|
|
49
|
+
currentSymbol: string | null;
|
|
50
|
+
wsClient: MarketDataClient;
|
|
51
|
+
bodyEl: HTMLElement | null;
|
|
52
|
+
searchEl: HTMLInputElement | null;
|
|
53
|
+
tabsEl: HTMLElement | null;
|
|
54
|
+
sortHeaderEl: HTMLElement | null;
|
|
55
|
+
// `//` rather than `///` from here down — see the note in positions-widget.
|
|
56
|
+
_host: TradingHost;
|
|
57
|
+
_deps: WatchlistDeps;
|
|
58
|
+
// Symbols this widget currently holds a live-quote subscription for.
|
|
59
|
+
// Re-synced from the visible list on every render — see _syncSubs.
|
|
60
|
+
_subscribedSyms: Set<string>;
|
|
61
|
+
_closeBtn: HTMLElement | null;
|
|
62
|
+
_exportBtn: HTMLElement | null;
|
|
63
|
+
_grid: DataGrid<InstrumentRow> | null;
|
|
64
|
+
|
|
65
|
+
static create(hostEl: HTMLElement, state: Record<string, unknown>, deps: WatchlistDeps): WatchlistWidget {
|
|
66
|
+
// Assert before building: the markup below is localized through the
|
|
67
|
+
// host, so a missing host has to fail here rather than render a panel
|
|
68
|
+
// captioned with raw English keys.
|
|
69
|
+
const host = assertHost(deps?.host, 'WatchlistWidget');
|
|
70
|
+
const root = WatchlistWidget._buildRoot(host);
|
|
71
|
+
root.id = makePanelId(WatchlistWidget.TYPE);
|
|
72
|
+
hostEl.appendChild(root);
|
|
73
|
+
return new WatchlistWidget(root, state || {}, deps);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// The panel's markup. The host stylesheet reads this structure, and a
|
|
77
|
+
// docking host lifts `.panel-header`'s children — and, for this panel,
|
|
78
|
+
// `.watchlist-search-row` as well — into its tab strip, which is why the
|
|
79
|
+
// search row is a sibling of the header rather than a child of it.
|
|
80
|
+
//
|
|
81
|
+
// "All" and "Favorites" are the built-in tabs. The rest are appended at
|
|
82
|
+
// runtime from the distinct categories of the loaded instruments, so the
|
|
83
|
+
// tab set is configured through whatever assigns those categories rather
|
|
84
|
+
// than being spelled out here — see _renderCategoryTabs.
|
|
85
|
+
static _buildRoot(host: TradingHost): HTMLElement {
|
|
86
|
+
const search = host.t('SearchInstruments');
|
|
87
|
+
const favorites = host.t('Favorites');
|
|
88
|
+
return makePanelRoot('watchlist-panel', host.t('Watchlist'), [
|
|
89
|
+
makeElement('div', 'panel-header', {}, [
|
|
90
|
+
makeElement('span', '', {}, [host.t('Markets')]),
|
|
91
|
+
makeIconButton('bt-icon-btn panel-export-btn', host.t('ExportToExcel'), 'bi-file-earmark-spreadsheet', { type: 'button' }),
|
|
92
|
+
makeIconButton('bt-icon-btn bt-icon-cancel panel-close-btn', host.t('ClosePanel'), 'bi-x', { type: 'button' }),
|
|
93
|
+
]),
|
|
94
|
+
makeElement('div', 'watchlist-search-row', {}, [
|
|
95
|
+
makeElement('input', 'form-control form-control-sm watchlist-search-input watchlist-search',
|
|
96
|
+
{ type: 'text', placeholder: search, autocomplete: 'off', 'aria-label': search }, []),
|
|
97
|
+
]),
|
|
98
|
+
makeElement('div', 'watchlist-tabs', { role: 'tablist', 'aria-label': host.t('WatchlistFilter') }, [
|
|
99
|
+
makeElement('button', 'wl-tab active', { 'data-filter': 'all', role: 'tab', 'aria-selected': 'true' }, [host.t('All')]),
|
|
100
|
+
makeElement('button', 'wl-tab', { 'data-filter': 'favorites', role: 'tab', 'aria-selected': 'false', 'aria-label': favorites, title: favorites }, ['★']),
|
|
101
|
+
]),
|
|
102
|
+
makeElement('div', 'watchlist-pane', {}, [
|
|
103
|
+
makeElement('table', 'watchlist-table', {}, [
|
|
104
|
+
makeElement('thead', 'watchlist-headers', {}, []),
|
|
105
|
+
makeElement('tbody', 'watchlist-body', {}, []),
|
|
106
|
+
]),
|
|
107
|
+
]),
|
|
108
|
+
]);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
constructor(rootEl: HTMLElement, _state: Record<string, unknown>, deps: WatchlistDeps) {
|
|
112
|
+
this._host = assertHost(deps?.host, 'WatchlistWidget');
|
|
113
|
+
if (typeof deps?.onSelect !== 'function')
|
|
114
|
+
throw new Error('WatchlistWidget: dep "onSelect" is required');
|
|
115
|
+
|
|
116
|
+
this.rootEl = rootEl;
|
|
117
|
+
this._deps = deps;
|
|
118
|
+
this.api = this._host.trading.api;
|
|
119
|
+
|
|
120
|
+
this.instruments = [];
|
|
121
|
+
this.stats = new Map();
|
|
122
|
+
this.favorites = new Set(this._loadFavorites());
|
|
123
|
+
this.filter = 'all';
|
|
124
|
+
this.search = '';
|
|
125
|
+
this.currentSymbol = null;
|
|
126
|
+
this._subscribedSyms = new Set();
|
|
127
|
+
this.wsClient = this._host.trading.marketData;
|
|
128
|
+
|
|
129
|
+
this.bodyEl = this.rootEl.querySelector('.watchlist-body');
|
|
130
|
+
this.searchEl = this.rootEl.querySelector('.watchlist-search');
|
|
131
|
+
this.tabsEl = this.rootEl.querySelector('.watchlist-tabs');
|
|
132
|
+
this.sortHeaderEl = this.rootEl.querySelector('.watchlist-headers');
|
|
133
|
+
this._closeBtn = this.rootEl.querySelector('.panel-close-btn');
|
|
134
|
+
this._exportBtn = this.rootEl.querySelector('.panel-export-btn');
|
|
135
|
+
|
|
136
|
+
this._closeBtn?.addEventListener('click', (e) => {
|
|
137
|
+
e.preventDefault();
|
|
138
|
+
this.dispose();
|
|
139
|
+
this._host.close();
|
|
140
|
+
});
|
|
141
|
+
this._exportBtn?.addEventListener('click', (e) => {
|
|
142
|
+
e.preventDefault();
|
|
143
|
+
this._export();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
this._grid = this.sortHeaderEl && this.bodyEl
|
|
147
|
+
? new DataGrid<InstrumentRow>({
|
|
148
|
+
head: this.sortHeaderEl,
|
|
149
|
+
body: this.bodyEl,
|
|
150
|
+
columns: this._columns(),
|
|
151
|
+
// Symbol A→Z at rest — an instrument list has no natural "newest",
|
|
152
|
+
// and alphabetical is what the user scans by.
|
|
153
|
+
defaultSort: { col: 'symbol', dir: 'asc' },
|
|
154
|
+
rowKey: (i) => String(i.symbol),
|
|
155
|
+
emptyText: this._host.t('No instruments'),
|
|
156
|
+
rowClass: (i) => (i.symbol === this.currentSymbol ? 'wl-row wl-current' : 'wl-row'),
|
|
157
|
+
bindRow: (tr, i) => tr.addEventListener('click', () => this._deps.onSelect(i.symbol!)),
|
|
158
|
+
// Paint a screenful, export and subscribe over the whole filtered set.
|
|
159
|
+
renderLimit: WatchlistWidget.RENDER_CAP,
|
|
160
|
+
// A header click re-renders through the grid, so this is the only
|
|
161
|
+
// place that sees every change to the visible set — including a sort
|
|
162
|
+
// the widget was never told about.
|
|
163
|
+
afterRender: () => {
|
|
164
|
+
this._syncSubs();
|
|
165
|
+
this._publishVisible();
|
|
166
|
+
},
|
|
167
|
+
})
|
|
168
|
+
: null;
|
|
169
|
+
|
|
170
|
+
this._bind();
|
|
171
|
+
|
|
172
|
+
this._host.register(this);
|
|
173
|
+
|
|
174
|
+
// Auto-init data load: a caller that creates the panel expects a working
|
|
175
|
+
// widget without further ceremony. Unconditional — the host port
|
|
176
|
+
// guarantees an API client, so there is no "maybe we can load" state
|
|
177
|
+
// left to branch on.
|
|
178
|
+
void this.init();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
dispose(): void {
|
|
182
|
+
// Release every live subscription this widget held — the market-data
|
|
183
|
+
// client refcounts them, so another control's subscription survives if
|
|
184
|
+
// it happened to share a symbol with us.
|
|
185
|
+
for (const sym of this._subscribedSyms) {
|
|
186
|
+
try { void this.wsClient?.removeSymbol(sym); } catch { /* socket may be torn down */ }
|
|
187
|
+
}
|
|
188
|
+
this._subscribedSyms.clear();
|
|
189
|
+
this._host.unregister(this);
|
|
190
|
+
try { this.rootEl.remove(); } catch { /* already detached */ }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async init(): Promise<void> {
|
|
194
|
+
try {
|
|
195
|
+
this.instruments = await this.api.searchInstruments('');
|
|
196
|
+
} catch (err) {
|
|
197
|
+
// Through the port, not the console: this is the diagnostic the
|
|
198
|
+
// user cannot see and support has to, and routing it here is also
|
|
199
|
+
// what makes it assertable.
|
|
200
|
+
this._host.log(`WatchlistWidget: failed to load instruments: ${err}`);
|
|
201
|
+
this.instruments = [];
|
|
202
|
+
}
|
|
203
|
+
this._renderCategoryTabs();
|
|
204
|
+
this._render();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Build the filter tabs from the assigned instrument categories. "All" and
|
|
208
|
+
// "Favorites" are built into the markup; here we append one tab per distinct
|
|
209
|
+
// non-empty category found in the loaded instruments. Re-runnable: clears
|
|
210
|
+
// any category tabs a previous load added.
|
|
211
|
+
_renderCategoryTabs(): void {
|
|
212
|
+
if (!this.tabsEl) return;
|
|
213
|
+
this.tabsEl.querySelectorAll('.wl-tab[data-category]').forEach(el => el.remove());
|
|
214
|
+
|
|
215
|
+
const cats = Array.from(new Set(
|
|
216
|
+
this.instruments
|
|
217
|
+
.map(i => (i.category || '').trim())
|
|
218
|
+
.filter(c => c.length > 0)))
|
|
219
|
+
.sort((a, b) => a.localeCompare(b));
|
|
220
|
+
|
|
221
|
+
for (const cat of cats) {
|
|
222
|
+
const btn = document.createElement('button');
|
|
223
|
+
btn.className = 'wl-tab';
|
|
224
|
+
btn.setAttribute('role', 'tab');
|
|
225
|
+
btn.setAttribute('aria-selected', 'false');
|
|
226
|
+
btn.dataset.filter = cat;
|
|
227
|
+
btn.dataset.category = '1';
|
|
228
|
+
btn.textContent = cat;
|
|
229
|
+
this.tabsEl.appendChild(btn);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/// Highlight the instrument the rest of the page is showing. Patches the two
|
|
234
|
+
/// affected rows rather than repainting: a repaint here would restart any
|
|
235
|
+
/// flash animation currently running in a price cell.
|
|
236
|
+
setCurrentSymbol(symbol: string | null): void {
|
|
237
|
+
const previous = this.currentSymbol;
|
|
238
|
+
this.currentSymbol = symbol;
|
|
239
|
+
if (!this._grid) return;
|
|
240
|
+
if (previous) this._grid.rowElement(String(previous))?.classList.remove('wl-current');
|
|
241
|
+
if (symbol) this._grid.rowElement(String(symbol))?.classList.add('wl-current');
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
onPriceUpdate(symbol: string, price: number): void {
|
|
245
|
+
if (price == null || !isFinite(price)) return;
|
|
246
|
+
let full = symbol;
|
|
247
|
+
if (!full.includes('@')) {
|
|
248
|
+
const match = this.instruments.find(i => String(i.symbol).split('@')[0] === symbol);
|
|
249
|
+
if (match?.symbol) full = match.symbol;
|
|
250
|
+
}
|
|
251
|
+
const cur = this.stats.get(full);
|
|
252
|
+
const prev = cur?.lastPrice;
|
|
253
|
+
|
|
254
|
+
// Session-day baseline: first observed live price per (UTC-day, symbol).
|
|
255
|
+
// Kept in the host's cache so reloading the page doesn't jump the
|
|
256
|
+
// baseline mid-day. New UTC day = fresh baseline.
|
|
257
|
+
const baselineKey = WatchlistWidget._baselineKey(full);
|
|
258
|
+
let baseline = cur?.baseline;
|
|
259
|
+
if (baseline == null) {
|
|
260
|
+
const stored = this._host.cache.get(baselineKey, null);
|
|
261
|
+
baseline = stored ? Number(stored) : undefined;
|
|
262
|
+
}
|
|
263
|
+
if (baseline == null || !isFinite(baseline) || baseline === 0) {
|
|
264
|
+
baseline = price;
|
|
265
|
+
this._host.cache.set(baselineKey, String(price));
|
|
266
|
+
}
|
|
267
|
+
const chgPct = baseline ? ((price - baseline) / baseline) * 100 : 0;
|
|
268
|
+
|
|
269
|
+
this.stats.set(full, { ...(cur || {}), lastPrice: price, baseline, chgPct });
|
|
270
|
+
this._updateRow(full, prev);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
static _baselineKey(symbol: string): string {
|
|
274
|
+
const day = new Date().toISOString().slice(0, 10); // YYYY-MM-DD (UTC)
|
|
275
|
+
return WatchlistWidget.BASELINE_KEY_PREFIX + day + ':' + symbol;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
_bind(): void {
|
|
279
|
+
if (this.searchEl) {
|
|
280
|
+
this.searchEl.addEventListener('input', () => {
|
|
281
|
+
this.search = (this.searchEl!.value || '').trim().toLowerCase();
|
|
282
|
+
this._render();
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
if (this.tabsEl) {
|
|
286
|
+
this.tabsEl.addEventListener('click', (e) => {
|
|
287
|
+
const tgt = e.target as Element | null;
|
|
288
|
+
const btn = tgt?.closest('.wl-tab') as HTMLElement | null;
|
|
289
|
+
if (!btn) return;
|
|
290
|
+
this.filter = btn.dataset.filter || 'all';
|
|
291
|
+
this.tabsEl!.querySelectorAll('.wl-tab').forEach(b => b.classList.toggle('active', b === btn));
|
|
292
|
+
this._render();
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
// Sort header clicks are handled by the grid (wired in the constructor).
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// The instruments matching the search box and the active tab, unsorted —
|
|
299
|
+
// the grid owns the order.
|
|
300
|
+
_filtered(): InstrumentRow[] {
|
|
301
|
+
const q = this.search;
|
|
302
|
+
const rows: InstrumentRow[] = [];
|
|
303
|
+
for (const inst of this.instruments) {
|
|
304
|
+
const sym = (inst.symbol || '').toUpperCase();
|
|
305
|
+
// Match the "symbol/exchange" (and "symbol@exchange") display form too, so a
|
|
306
|
+
// full "BTC/IMEX" query resolves — not only the bare symbol "BTC".
|
|
307
|
+
const symL = sym.toLowerCase();
|
|
308
|
+
const shortSym = symL.split('@')[0];
|
|
309
|
+
const exch = (inst.exchange || '').toLowerCase();
|
|
310
|
+
const qualified = `${shortSym}/${exch}`;
|
|
311
|
+
const qualifiedAt = `${shortSym}@${exch}`;
|
|
312
|
+
if (q && !symL.includes(q) && !(inst.name || '').toLowerCase().includes(q) && !qualified.includes(q) && !qualifiedAt.includes(q)) continue;
|
|
313
|
+
if (this.filter === 'favorites') {
|
|
314
|
+
if (!this.favorites.has(sym)) continue;
|
|
315
|
+
} else if (this.filter !== 'all') {
|
|
316
|
+
// Any other filter value is an assigned instrument category,
|
|
317
|
+
// matched case-insensitively.
|
|
318
|
+
if ((inst.category || '').toLowerCase() !== this.filter.toLowerCase()) continue;
|
|
319
|
+
}
|
|
320
|
+
rows.push(inst);
|
|
321
|
+
}
|
|
322
|
+
return rows;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
_render(): void {
|
|
326
|
+
this._grid?.setRows(this._filtered());
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// The blotter's single column declaration. Prices and percentages come off
|
|
330
|
+
// the live stats map rather than the instrument row, so a quote arriving
|
|
331
|
+
// between two renders is picked up by the next one.
|
|
332
|
+
_columns(): GridColumn<InstrumentRow>[] {
|
|
333
|
+
const label = (key: string) => this._host.t(key);
|
|
334
|
+
return [
|
|
335
|
+
{
|
|
336
|
+
key: 'symbol',
|
|
337
|
+
header: label('Symbol'),
|
|
338
|
+
exportable: true,
|
|
339
|
+
value: (i) => i.symbol,
|
|
340
|
+
cellClass: () => 'wl-sym-cell',
|
|
341
|
+
render: (i) => this._symbolCell(i),
|
|
342
|
+
exportValue: (i) => String(i.symbol).split('@')[0],
|
|
343
|
+
},
|
|
344
|
+
{
|
|
345
|
+
key: 'last',
|
|
346
|
+
header: label('Last'),
|
|
347
|
+
headerClass: 'ta-right',
|
|
348
|
+
exportable: true,
|
|
349
|
+
value: (i) => this._statsOf(i).lastPrice,
|
|
350
|
+
cellClass: () => 'wl-last',
|
|
351
|
+
render: (i) => WatchlistWidget._priceText(this._statsOf(i).lastPrice),
|
|
352
|
+
exportValue: (i) => this._statsOf(i).lastPrice ?? '',
|
|
353
|
+
},
|
|
354
|
+
{
|
|
355
|
+
key: 'chgPct',
|
|
356
|
+
header: label('Change24hPct'),
|
|
357
|
+
headerClass: 'ta-right',
|
|
358
|
+
exportable: true,
|
|
359
|
+
value: (i) => this._statsOf(i).chgPct,
|
|
360
|
+
cellClass: (i) => ('wl-chg ' + WatchlistWidget._changeClass(this._statsOf(i).chgPct)).trim(),
|
|
361
|
+
render: (i) => WatchlistWidget._changeText(this._statsOf(i).chgPct),
|
|
362
|
+
exportValue: (i) => this._statsOf(i).chgPct ?? '',
|
|
363
|
+
},
|
|
364
|
+
];
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
_statsOf(instrument: InstrumentRow): QuoteStats {
|
|
368
|
+
return this.stats.get(String(instrument.symbol)) ?? {};
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// The favourite star plus the short symbol. The star stops the click from
|
|
372
|
+
// reaching the row, whose own listener switches the host's instrument.
|
|
373
|
+
_symbolCell(instrument: InstrumentRow): Node {
|
|
374
|
+
const cell = document.createDocumentFragment();
|
|
375
|
+
|
|
376
|
+
const star = document.createElement('button');
|
|
377
|
+
star.type = 'button';
|
|
378
|
+
star.className = this.favorites.has(instrument.symbol!) ? 'wl-fav active' : 'wl-fav';
|
|
379
|
+
star.title = this._host.t('Favorite');
|
|
380
|
+
star.textContent = '★';
|
|
381
|
+
star.addEventListener('click', (e) => {
|
|
382
|
+
e.stopPropagation();
|
|
383
|
+
this._toggleFavorite(instrument.symbol!);
|
|
384
|
+
});
|
|
385
|
+
cell.appendChild(star);
|
|
386
|
+
|
|
387
|
+
const label = document.createElement('span');
|
|
388
|
+
label.className = 'wl-sym';
|
|
389
|
+
label.textContent = String(instrument.symbol).split('@')[0];
|
|
390
|
+
cell.appendChild(label);
|
|
391
|
+
|
|
392
|
+
return cell;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Export the current filtered+sorted view to .xlsx — every matching
|
|
396
|
+
// instrument, without the RENDER_CAP truncation applied to the DOM.
|
|
397
|
+
_export(): void {
|
|
398
|
+
this._grid?.download('watchlist', this._host.t('Markets'));
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// Patch the price and change cells of one row in place. A full render would
|
|
402
|
+
// replace the cell element and so cancel the flash animation started here.
|
|
403
|
+
_updateRow(symbol: string, prevPrice: number | null | undefined): void {
|
|
404
|
+
if (!this._grid) return;
|
|
405
|
+
const st = this.stats.get(symbol) || {};
|
|
406
|
+
const lastEl = this._grid.cellElement(String(symbol), 'last');
|
|
407
|
+
const chgEl = this._grid.cellElement(String(symbol), 'chgPct');
|
|
408
|
+
if (lastEl && st.lastPrice != null) {
|
|
409
|
+
lastEl.textContent = WatchlistWidget._priceText(st.lastPrice);
|
|
410
|
+
if (prevPrice != null && st.lastPrice !== prevPrice) {
|
|
411
|
+
lastEl.classList.remove('flash-up', 'flash-down');
|
|
412
|
+
void lastEl.offsetWidth;
|
|
413
|
+
lastEl.classList.add(st.lastPrice > prevPrice ? 'flash-up' : 'flash-down');
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
if (chgEl && st.chgPct != null && isFinite(Number(st.chgPct))) {
|
|
417
|
+
chgEl.textContent = WatchlistWidget._changeText(st.chgPct);
|
|
418
|
+
chgEl.classList.remove('up', 'down');
|
|
419
|
+
chgEl.classList.add(WatchlistWidget._changeClass(st.chgPct));
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
_toggleFavorite(symbol: string): void {
|
|
424
|
+
if (!this._host.allow('add to favorites')) return;
|
|
425
|
+
if (this.favorites.has(symbol)) this.favorites.delete(symbol);
|
|
426
|
+
else this.favorites.add(symbol);
|
|
427
|
+
this._saveFavorites();
|
|
428
|
+
// Broadcast the change to every other watchlist instance so they reflect
|
|
429
|
+
// the new state without a fresh fetch.
|
|
430
|
+
this._host.broadcast<WatchlistWidget>(w => {
|
|
431
|
+
if (w !== this) { w.favorites = new Set(this.favorites); w._render(); }
|
|
432
|
+
});
|
|
433
|
+
this._render();
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
_loadFavorites(): string[] {
|
|
437
|
+
try {
|
|
438
|
+
const raw = this._host.preferences.get(WatchlistWidget.FAVORITES_KEY, null);
|
|
439
|
+
return raw ? JSON.parse(raw) : [];
|
|
440
|
+
} catch { return []; }
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
_saveFavorites(): void {
|
|
444
|
+
try { this._host.preferences.set(WatchlistWidget.FAVORITES_KEY, JSON.stringify(Array.from(this.favorites))); }
|
|
445
|
+
catch { /* store may be unavailable */ }
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// The symbols at the top of the current order — the ones a user is actually
|
|
449
|
+
// looking at, and the set both the subscriptions and the host's ticker are
|
|
450
|
+
// sized to.
|
|
451
|
+
_visible(): InstrumentRow[] {
|
|
452
|
+
return this._grid ? this._grid.sortedRows().slice(0, WatchlistWidget.VISIBLE_CAP) : [];
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Diff the desired set of visible symbols against currently-held live-price
|
|
456
|
+
// subscriptions, then add/remove to converge. Called after every render that
|
|
457
|
+
// may have changed the visible list (filter, sort, search). add/removeSymbol
|
|
458
|
+
// refcount internally, so another control's subscription on the same symbol
|
|
459
|
+
// is not affected.
|
|
460
|
+
_syncSubs(): void {
|
|
461
|
+
if (!this.wsClient) return;
|
|
462
|
+
const desired = new Set(this._visible().map(i => String(i.symbol).split('@')[0]));
|
|
463
|
+
|
|
464
|
+
for (const sym of this._subscribedSyms) {
|
|
465
|
+
if (!desired.has(sym)) {
|
|
466
|
+
try { void this.wsClient.removeSymbol(sym); } catch { /* socket race */ }
|
|
467
|
+
this._subscribedSyms.delete(sym);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
// Stagger the subscribe burst — firing every watchlist symbol
|
|
471
|
+
// simultaneously while a chart is fetching its history starves the
|
|
472
|
+
// adapter and stalls the chart's snapshot. 80ms between calls keeps the
|
|
473
|
+
// pipe quiet enough for that fetch to return on time, while still
|
|
474
|
+
// feeling instant to the user.
|
|
475
|
+
let i = 0;
|
|
476
|
+
for (const sym of desired) {
|
|
477
|
+
if (this._subscribedSyms.has(sym)) continue;
|
|
478
|
+
this._subscribedSyms.add(sym);
|
|
479
|
+
setTimeout(() => {
|
|
480
|
+
try { void this.wsClient.addSymbol(sym, MarketDataLevels.Quotes); } catch { /* socket race */ }
|
|
481
|
+
}, i * 80);
|
|
482
|
+
i++;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// Tell the host which symbols are on screen. Only the primary instance
|
|
487
|
+
// speaks for the page: duplicated watchlist panels have their own filters,
|
|
488
|
+
// and the page has one ticker for them to fight over.
|
|
489
|
+
_publishVisible(): void {
|
|
490
|
+
if (!this._host.isPrimary) return;
|
|
491
|
+
this._host.ticker.publish(this._visible().map(i => String(i.symbol)), this.stats);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
static _priceText(price: number | null | undefined): string {
|
|
495
|
+
const n = Number(price);
|
|
496
|
+
if (price == null || !isFinite(n) || n === 0) return '--';
|
|
497
|
+
if (n >= 1) return n.toFixed(2);
|
|
498
|
+
if (n >= 0.01) return n.toFixed(4);
|
|
499
|
+
return n.toFixed(6);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
static _changeText(chgPct: number | null | undefined): string {
|
|
503
|
+
const pct = Number(chgPct);
|
|
504
|
+
if (chgPct == null || !isFinite(pct)) return '--';
|
|
505
|
+
return `${pct >= 0 ? '+' : ''}${pct.toFixed(2)}%`;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
static _changeClass(chgPct: number | null | undefined): string {
|
|
509
|
+
const pct = Number(chgPct);
|
|
510
|
+
if (chgPct == null || !isFinite(pct)) return '';
|
|
511
|
+
return pct >= 0 ? 'up' : 'down';
|
|
512
|
+
}
|
|
513
|
+
}
|
package/styles/theme.css
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/* Default palette for @stocksharp/trading-controls.
|
|
2
|
+
*
|
|
3
|
+
* OPTIONAL. This file exists so the controls render on a page that has no
|
|
4
|
+
* design system of its own: drop it in and the blotters look like a dark
|
|
5
|
+
* trading terminal. A host with its own brand does NOT load this file — it
|
|
6
|
+
* declares the same custom properties itself, and `trading-controls.css` reads
|
|
7
|
+
* whatever is in scope.
|
|
8
|
+
*
|
|
9
|
+
* Every property below is part of the package's public contract and is listed
|
|
10
|
+
* in the README. `tools/check-style-contract.mjs` fails the build if
|
|
11
|
+
* `trading-controls.css` reads a property this file does not declare, so the
|
|
12
|
+
* contract cannot quietly grow a knob nobody documented.
|
|
13
|
+
*
|
|
14
|
+
* The values are a default, not a brand. A host's palette will differ, and that
|
|
15
|
+
* is the point — nothing here is duplicated from any application's stylesheet
|
|
16
|
+
* and the two are not expected to stay in step.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
:root {
|
|
20
|
+
/* Surfaces */
|
|
21
|
+
--t-bg: #0b0e11;
|
|
22
|
+
--t-panel: #181a20;
|
|
23
|
+
--t-header: #12151c;
|
|
24
|
+
--t-hover: #22262f;
|
|
25
|
+
--t-border: #2b3139;
|
|
26
|
+
|
|
27
|
+
/* Text */
|
|
28
|
+
--t-text: #eaecef;
|
|
29
|
+
--t-text-dim: #848e9c;
|
|
30
|
+
--t-text-bright: #ffffff;
|
|
31
|
+
|
|
32
|
+
/* Accent */
|
|
33
|
+
--t-accent: #fcd535;
|
|
34
|
+
--t-accent-hover: #f0c232;
|
|
35
|
+
--t-accent-text: #0b0e11;
|
|
36
|
+
--t-accent-glow-soft: rgba(252, 213, 53, 0.12);
|
|
37
|
+
|
|
38
|
+
/* Direction and outcome */
|
|
39
|
+
--t-green: #0ecb81;
|
|
40
|
+
--t-red: #f6465d;
|
|
41
|
+
--t-green-flash: rgba(14, 203, 129, 0.25);
|
|
42
|
+
--t-red-flash: rgba(246, 70, 93, 0.25);
|
|
43
|
+
|
|
44
|
+
/* Warnings. Two shades on purpose: --t-orange marks an action that is
|
|
45
|
+
destructive but not a cancel (reverse a position), --t-warning marks
|
|
46
|
+
information the user should read (a rejection reason). */
|
|
47
|
+
--t-orange: #f0b90b;
|
|
48
|
+
--t-warning: #e0a800;
|
|
49
|
+
|
|
50
|
+
/* Type and shape */
|
|
51
|
+
--t-font: 'DM Sans', -apple-system, sans-serif;
|
|
52
|
+
/* A monospace stack that covers Cyrillic: the blotters render localized
|
|
53
|
+
words ("Отменена", "Лимит") in the same cells as numbers, and a font
|
|
54
|
+
without those glyphs drops the whole cell to a sans-serif fallback, so
|
|
55
|
+
one table ends up looking unlike the next. */
|
|
56
|
+
--t-mono: 'JetBrains Mono', 'Cascadia Code', 'Cascadia Mono', 'Consolas', 'Liberation Mono', monospace;
|
|
57
|
+
--t-radius: 3px;
|
|
58
|
+
--t-transition: 180ms ease;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/* Light scheme. Keyed off the same attribute Bootstrap uses, so a host that
|
|
62
|
+
already flips `data-bs-theme` gets this for free. */
|
|
63
|
+
:root[data-bs-theme="light"] {
|
|
64
|
+
--t-bg: #f5f7fa;
|
|
65
|
+
--t-panel: #ffffff;
|
|
66
|
+
--t-header: #f8fafc;
|
|
67
|
+
--t-hover: #f1f5f9;
|
|
68
|
+
--t-border: #e2e8f0;
|
|
69
|
+
|
|
70
|
+
--t-text: #1e293b;
|
|
71
|
+
--t-text-dim: #64748b;
|
|
72
|
+
--t-text-bright: #0f172a;
|
|
73
|
+
|
|
74
|
+
--t-accent: #d97706;
|
|
75
|
+
--t-accent-hover: #b45309;
|
|
76
|
+
--t-accent-text: #ffffff;
|
|
77
|
+
--t-accent-glow-soft: rgba(217, 119, 6, 0.08);
|
|
78
|
+
|
|
79
|
+
--t-green: #16a34a;
|
|
80
|
+
--t-red: #dc2626;
|
|
81
|
+
--t-green-flash: rgba(22, 163, 74, 0.22);
|
|
82
|
+
--t-red-flash: rgba(220, 38, 38, 0.22);
|
|
83
|
+
|
|
84
|
+
--t-orange: #b45309;
|
|
85
|
+
--t-warning: #b45309;
|
|
86
|
+
}
|