@stocksharp/trading-controls 1.2.0 → 1.3.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 +95 -16
- package/dist/esm/black-scholes.js +147 -0
- package/dist/esm/black-scholes.js.map +1 -0
- package/dist/esm/control-types.js +4 -0
- package/dist/esm/control-types.js.map +1 -1
- package/dist/esm/index.js +18 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/log-monitor-widget.js +265 -0
- package/dist/esm/log-monitor-widget.js.map +1 -0
- package/dist/esm/log-tree.js +96 -0
- package/dist/esm/log-tree.js.map +1 -0
- package/dist/esm/option-desk-widget.js +322 -0
- package/dist/esm/option-desk-widget.js.map +1 -0
- package/dist/esm/pnl-curve.js +129 -0
- package/dist/esm/pnl-curve.js.map +1 -0
- package/dist/esm/statistics-widget.js +194 -0
- package/dist/esm/statistics-widget.js.map +1 -0
- package/dist/esm/strategies-widget.js +348 -0
- package/dist/esm/strategies-widget.js.map +1 -0
- package/dist/sstradingcontrols.js +1307 -47
- package/dist/sstradingcontrols.js.map +4 -4
- package/dist/types/black-scholes.d.ts +28 -0
- package/dist/types/black-scholes.d.ts.map +1 -0
- package/dist/types/control-types.d.ts +4 -0
- package/dist/types/control-types.d.ts.map +1 -1
- package/dist/types/index.d.ts +16 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/log-monitor-widget.d.ts +42 -0
- package/dist/types/log-monitor-widget.d.ts.map +1 -0
- package/dist/types/log-tree.d.ts +34 -0
- package/dist/types/log-tree.d.ts.map +1 -0
- package/dist/types/option-desk-widget.d.ts +68 -0
- package/dist/types/option-desk-widget.d.ts.map +1 -0
- package/dist/types/pnl-curve.d.ts +43 -0
- package/dist/types/pnl-curve.d.ts.map +1 -0
- package/dist/types/statistics-widget.d.ts +29 -0
- package/dist/types/statistics-widget.d.ts.map +1 -0
- package/dist/types/strategies-widget.d.ts +63 -0
- package/dist/types/strategies-widget.d.ts.map +1 -0
- package/dist/types/trading-data.d.ts +9 -0
- package/dist/types/trading-data.d.ts.map +1 -1
- package/package.json +27 -2
- package/screenshots/log-monitor.png +0 -0
- package/screenshots/option-desk.png +0 -0
- package/screenshots/panels.jpg +0 -0
- package/screenshots/statistics.png +0 -0
- package/screenshots/strategies.png +0 -0
- package/src/black-scholes.ts +199 -0
- package/src/control-types.ts +4 -0
- package/src/index.ts +41 -0
- package/src/log-monitor-widget.ts +312 -0
- package/src/log-tree.ts +131 -0
- package/src/option-desk-widget.ts +422 -0
- package/src/pnl-curve.ts +204 -0
- package/src/statistics-widget.ts +226 -0
- package/src/strategies-widget.ts +435 -0
- package/src/trading-data.ts +22 -0
- package/styles/trading-controls.css +356 -0
- package/translation-keys.json +70 -1
package/src/pnl-curve.ts
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// The geometry of a cumulative P&L curve, as pure functions over numbers.
|
|
2
|
+
//
|
|
3
|
+
// The same curve is read in three places and drawn at three sizes: a strategy row's sparkline,
|
|
4
|
+
// a statistics card, and a backtest's own chart. Only the last of those is a price chart - it
|
|
5
|
+
// shares the price axis and belongs to the chart engine. The other two are a curve in a box,
|
|
6
|
+
// and this is that curve: where each point lands, where the baseline sits, and which way the
|
|
7
|
+
// run ended. No canvas, so it can be checked against arithmetic rather than against pixels.
|
|
8
|
+
|
|
9
|
+
/// One sample of a run's cumulative P&L. Time is whatever the caller counts in - seconds, unix
|
|
10
|
+
/// milliseconds - because only the spacing between samples is used, never the absolute value.
|
|
11
|
+
export interface PnlPoint {
|
|
12
|
+
time: number;
|
|
13
|
+
value: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/// The box one curve fills, in device pixels. The padding keeps a curve at its extreme off the
|
|
17
|
+
/// edge, where a stroke would be clipped in half.
|
|
18
|
+
export interface PnlBox {
|
|
19
|
+
width: number;
|
|
20
|
+
height: number;
|
|
21
|
+
padX: number;
|
|
22
|
+
padY: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface PnlCurve {
|
|
26
|
+
/// The curve, in draw order.
|
|
27
|
+
points: [number, number][];
|
|
28
|
+
/// The curve closed down to the baseline, ready to fill.
|
|
29
|
+
area: [number, number][];
|
|
30
|
+
/// Y of zero P&L. Always inside the box: the range is widened to include it.
|
|
31
|
+
zeroY: number;
|
|
32
|
+
/// The value range the box covers, zero included.
|
|
33
|
+
min: number;
|
|
34
|
+
max: number;
|
|
35
|
+
/// Whether the run ended at or above where it started. What the curve is coloured by - and
|
|
36
|
+
/// the last value, not the highest: a run that peaked and gave it all back is a loss.
|
|
37
|
+
positive: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/// A run reduced to at most `count` samples, spanning the same stretch of time.
|
|
41
|
+
///
|
|
42
|
+
/// A sparkline that keeps only the newest N samples marches sideways: each new sample pushes the
|
|
43
|
+
/// oldest off the left edge, so the shape slides and the run's beginning is lost. Compressing
|
|
44
|
+
/// instead keeps the whole run in the box and re-buckets it, so a new sample changes the curve's
|
|
45
|
+
/// shape rather than its position - which is what makes two readings of the same strategy
|
|
46
|
+
/// comparable.
|
|
47
|
+
///
|
|
48
|
+
/// A bucket is summarised by its LAST sample, not by an average. This is a cumulative figure:
|
|
49
|
+
/// the last value in a bucket is where the run actually stood at that moment, while an average
|
|
50
|
+
/// would draw a rising run below the line it was on. Every point returned is therefore a real
|
|
51
|
+
/// sample, never a computed one, and the first and last are kept exactly - they are where the
|
|
52
|
+
/// run started and where it stands now.
|
|
53
|
+
export function compressPnl(points: readonly PnlPoint[], count: number): PnlPoint[] {
|
|
54
|
+
if (points.length <= count || count < 2) return points.slice();
|
|
55
|
+
|
|
56
|
+
const first = points[0];
|
|
57
|
+
const last = points[points.length - 1];
|
|
58
|
+
const span = last.time - first.time;
|
|
59
|
+
|
|
60
|
+
// Every sample at the same instant: there is no time to bucket by, so fall back to position.
|
|
61
|
+
if (!(span > 0)) {
|
|
62
|
+
const step = (points.length - 1) / (count - 1);
|
|
63
|
+
return Array.from({ length: count }, (_, i) => points[Math.round(i * step)]);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// One bucket per output point, the last sample in each standing for it. Buckets that caught
|
|
67
|
+
// nothing are skipped rather than repeated, so a quiet stretch reads as a long flat segment
|
|
68
|
+
// instead of as a row of identical points.
|
|
69
|
+
// `count - 1` buckets, not `count`: the first sample is seeded and the last is appended, so
|
|
70
|
+
// the buckets between them may contribute at most `count - 2` points. Sized any wider the
|
|
71
|
+
// result can overrun `count`, and trimming it afterwards would drop the run's start - the
|
|
72
|
+
// one point this function exists to keep.
|
|
73
|
+
const buckets = count - 1;
|
|
74
|
+
const out: PnlPoint[] = [first];
|
|
75
|
+
let bucket = 0;
|
|
76
|
+
for (let i = 1; i < points.length; i++) {
|
|
77
|
+
const index = Math.min(buckets - 1, Math.floor(((points[i].time - first.time) / span) * buckets));
|
|
78
|
+
if (index > bucket) {
|
|
79
|
+
out.push(points[i - 1]);
|
|
80
|
+
bucket = index;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (out[out.length - 1] !== last) out.push(last);
|
|
84
|
+
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/// Lay a run out in a box, or null when there is no curve to draw.
|
|
89
|
+
///
|
|
90
|
+
/// Two rules that are not arbitrary. Zero is always in the range, so a run that only ever won
|
|
91
|
+
/// still shows the line it started from and the height of the curve means something. And the
|
|
92
|
+
/// horizontal axis is time, not sample index: a strategy that traded twice in the morning and
|
|
93
|
+
/// forty times after lunch should look like that, not like a curve with evenly spaced steps.
|
|
94
|
+
export function pnlCurve(points: readonly PnlPoint[], box: PnlBox): PnlCurve | null {
|
|
95
|
+
const clean = points
|
|
96
|
+
.filter(p => p !== null && p !== undefined && Number.isFinite(p.time) && Number.isFinite(p.value))
|
|
97
|
+
.slice()
|
|
98
|
+
.sort((a, b) => a.time - b.time);
|
|
99
|
+
|
|
100
|
+
if (clean.length < 2) return null;
|
|
101
|
+
|
|
102
|
+
// One sample per drawable pixel is as much as a box can show; past that the extra points are
|
|
103
|
+
// strokes on top of strokes. Compressing here rather than asking the caller to do it is what
|
|
104
|
+
// lets a host keep the whole run and still get a curve that does not slide.
|
|
105
|
+
const drawable = Math.max(2, Math.round(box.width - box.padX * 2));
|
|
106
|
+
const shown = compressPnl(clean, drawable);
|
|
107
|
+
|
|
108
|
+
const values = shown.map(p => p.value);
|
|
109
|
+
const min = Math.min(0, ...values);
|
|
110
|
+
const max = Math.max(0, ...values);
|
|
111
|
+
|
|
112
|
+
const left = box.padX;
|
|
113
|
+
const right = box.width - box.padX;
|
|
114
|
+
const top = box.padY;
|
|
115
|
+
const bottom = box.height - box.padY;
|
|
116
|
+
|
|
117
|
+
const firstTime = shown[0].time;
|
|
118
|
+
const lastTime = shown[shown.length - 1].time;
|
|
119
|
+
const timeSpan = lastTime - firstTime;
|
|
120
|
+
const valueSpan = max - min;
|
|
121
|
+
|
|
122
|
+
// A run with no spread in one axis collapses onto a line rather than dividing by nothing:
|
|
123
|
+
// every sample at the same instant stacks at the left edge, every sample at the same value
|
|
124
|
+
// rests on the top edge, which for a flat run is also its baseline.
|
|
125
|
+
const x = (time: number): number => (timeSpan === 0 ? left : left + ((time - firstTime) / timeSpan) * (right - left));
|
|
126
|
+
const y = (value: number): number => (valueSpan === 0 ? top : bottom - ((value - min) / valueSpan) * (bottom - top));
|
|
127
|
+
|
|
128
|
+
const curve = shown.map(p => [x(p.time), y(p.value)] as [number, number]);
|
|
129
|
+
const zeroY = y(0);
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
points: curve,
|
|
133
|
+
// Closed along the baseline rather than along the foot of the box: a run entirely above
|
|
134
|
+
// water fills the gap between itself and zero, and nothing below it.
|
|
135
|
+
area: [...curve, [curve[curve.length - 1][0], zeroY], [curve[0][0], zeroY]],
|
|
136
|
+
zeroY,
|
|
137
|
+
min,
|
|
138
|
+
max,
|
|
139
|
+
positive: shown[shown.length - 1].value >= 0,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/// What a curve is drawn with. Colours come from the caller because the page owns its palette;
|
|
144
|
+
/// the two directions are the same pair every other control uses for up and down.
|
|
145
|
+
export interface PnlCurveStyle {
|
|
146
|
+
up: string;
|
|
147
|
+
down: string;
|
|
148
|
+
/// The zero line. A curve is read against it, so it is drawn even when nothing crosses it.
|
|
149
|
+
baseline: string;
|
|
150
|
+
lineWidth: number;
|
|
151
|
+
/// How much of the direction colour the filled area keeps, 0 to 1.
|
|
152
|
+
fillOpacity: number;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/// The 2D calls one paint makes. Narrow on purpose: a test supplies a recorder, and a control
|
|
156
|
+
/// that started reaching for something else shows up here rather than in a screenshot.
|
|
157
|
+
export interface PnlCurveContext {
|
|
158
|
+
clearRect(x: number, y: number, w: number, h: number): void;
|
|
159
|
+
beginPath(): void;
|
|
160
|
+
moveTo(x: number, y: number): void;
|
|
161
|
+
lineTo(x: number, y: number): void;
|
|
162
|
+
closePath(): void;
|
|
163
|
+
stroke(): void;
|
|
164
|
+
fill(): void;
|
|
165
|
+
setLineDash(segments: number[]): void;
|
|
166
|
+
globalAlpha: number;
|
|
167
|
+
// The browser's own type for these, so a real 2D context satisfies this contract as
|
|
168
|
+
// it stands: it accepts a gradient or a pattern where this only ever writes a colour.
|
|
169
|
+
strokeStyle: string | CanvasGradient | CanvasPattern;
|
|
170
|
+
fillStyle: string | CanvasGradient | CanvasPattern;
|
|
171
|
+
lineWidth: number;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/// Paint a laid-out curve: the baseline, the filled area under it, then the line itself.
|
|
175
|
+
export function drawPnlCurve(ctx: PnlCurveContext, curve: PnlCurve, box: PnlBox, style: PnlCurveStyle): void {
|
|
176
|
+
const colour = curve.positive ? style.up : style.down;
|
|
177
|
+
|
|
178
|
+
ctx.clearRect(0, 0, box.width, box.height);
|
|
179
|
+
|
|
180
|
+
ctx.setLineDash([2, 3]);
|
|
181
|
+
ctx.strokeStyle = style.baseline;
|
|
182
|
+
ctx.lineWidth = 1;
|
|
183
|
+
ctx.beginPath();
|
|
184
|
+
ctx.moveTo(box.padX, curve.zeroY);
|
|
185
|
+
ctx.lineTo(box.width - box.padX, curve.zeroY);
|
|
186
|
+
ctx.stroke();
|
|
187
|
+
ctx.setLineDash([]);
|
|
188
|
+
|
|
189
|
+
ctx.globalAlpha = style.fillOpacity;
|
|
190
|
+
ctx.fillStyle = colour;
|
|
191
|
+
ctx.beginPath();
|
|
192
|
+
ctx.moveTo(curve.area[0][0], curve.area[0][1]);
|
|
193
|
+
for (const [px, py] of curve.area.slice(1)) ctx.lineTo(px, py);
|
|
194
|
+
ctx.closePath();
|
|
195
|
+
ctx.fill();
|
|
196
|
+
ctx.globalAlpha = 1;
|
|
197
|
+
|
|
198
|
+
ctx.strokeStyle = colour;
|
|
199
|
+
ctx.lineWidth = style.lineWidth;
|
|
200
|
+
ctx.beginPath();
|
|
201
|
+
ctx.moveTo(curve.points[0][0], curve.points[0][1]);
|
|
202
|
+
for (const [px, py] of curve.points.slice(1)) ctx.lineTo(px, py);
|
|
203
|
+
ctx.stroke();
|
|
204
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
// Strategy statistics — multi-instance.
|
|
2
|
+
//
|
|
3
|
+
// A read-only table of everything a strategy reports about itself: net profit, drawdown, trade
|
|
4
|
+
// counts, latencies. One row per parameter, grouped by the area it belongs to.
|
|
5
|
+
//
|
|
6
|
+
// The desktop grid derives its rows by reflecting over the strategy's statistic parameters, and
|
|
7
|
+
// a browser has no equivalent - so the rows arrive from the consumer already resolved, name and
|
|
8
|
+
// description translated, with the two things that decide where a row sits: a stable category
|
|
9
|
+
// key and the registry order. Both matter. Grouping on the translated category would regroup
|
|
10
|
+
// the table when the language changes, and sorting by name or by value would scatter parameters
|
|
11
|
+
// that are read together.
|
|
12
|
+
import { makeElement, makeIconButton, makePanelId, makePanelRoot } from './dom.js';
|
|
13
|
+
import { ControlTypes } from './control-types.js';
|
|
14
|
+
import { makeGridMenu } from './grid-menu.js';
|
|
15
|
+
import { TradingHost, assertHost } from './trading-host.js';
|
|
16
|
+
import type { StatisticRow } from './trading-data.js';
|
|
17
|
+
import { DataGrid, GridColumn } from '@stocksharp/grids/source/data-grid';
|
|
18
|
+
|
|
19
|
+
/// What the table needs beyond the host port. Nothing: there is nothing here to act on.
|
|
20
|
+
export interface StatisticsDeps {
|
|
21
|
+
host: TradingHost;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/// A row with its category's place worked out.
|
|
25
|
+
interface RankedRow extends StatisticRow {
|
|
26
|
+
categoryRank: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/// Give every category the place of its first parameter.
|
|
30
|
+
///
|
|
31
|
+
/// Taken from the rows rather than assumed: the registry bands its orders by category today
|
|
32
|
+
/// - profit below a hundred, trades from a hundred - but a consumer numbering them some
|
|
33
|
+
/// other way still gets its groups in the order it asked for.
|
|
34
|
+
function rank(rows: readonly StatisticRow[]): RankedRow[] {
|
|
35
|
+
const first = new Map<string, number>();
|
|
36
|
+
for (const row of rows) {
|
|
37
|
+
const seen = first.get(row.category);
|
|
38
|
+
if (seen === undefined || row.order < seen) first.set(row.category, row.order);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return rows.map(row => ({ ...row, categoryRank: first.get(row.category) ?? row.order }));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class StatisticsWidget {
|
|
45
|
+
static TYPE = ControlTypes.Statistics;
|
|
46
|
+
|
|
47
|
+
rootEl: HTMLElement;
|
|
48
|
+
el: HTMLElement | null;
|
|
49
|
+
// `//` rather than `///` from here down — see the note in positions-widget.
|
|
50
|
+
_host: TradingHost;
|
|
51
|
+
_closeBtn: HTMLElement | null;
|
|
52
|
+
_exportBtn: HTMLElement | null;
|
|
53
|
+
_rows: RankedRow[];
|
|
54
|
+
_grid: DataGrid<RankedRow> | null;
|
|
55
|
+
|
|
56
|
+
static create(hostEl: HTMLElement, state: Record<string, unknown>, deps: StatisticsDeps): StatisticsWidget {
|
|
57
|
+
const host = assertHost(deps?.host, 'StatisticsWidget');
|
|
58
|
+
const root = StatisticsWidget._buildRoot(host);
|
|
59
|
+
root.id = makePanelId(StatisticsWidget.TYPE);
|
|
60
|
+
hostEl.appendChild(root);
|
|
61
|
+
return new StatisticsWidget(root, state || {}, deps);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// The panel's markup. No rail action but the export: a statistic is produced by the
|
|
65
|
+
// strategy and there is nothing here to cancel, reload or edit.
|
|
66
|
+
static _buildRoot(host: TradingHost): HTMLElement {
|
|
67
|
+
const title = host.t('Statistics');
|
|
68
|
+
return makePanelRoot('statistics-panel', title, [
|
|
69
|
+
makeElement('div', 'panel-header', {}, [
|
|
70
|
+
makeElement('span', '', {}, [title]),
|
|
71
|
+
makeIconButton('bt-icon-btn bt-icon-cancel panel-close-btn', host.t('ClosePanel'), 'bi-x', { type: 'button' }),
|
|
72
|
+
]),
|
|
73
|
+
makeElement('div', 'panel-body panel-body-with-rail', {}, [
|
|
74
|
+
makeElement('div', 'panel-body-content', {}, [
|
|
75
|
+
makeElement('table', 'terminal-table statistics-table', { role: 'table', 'aria-label': host.t('StatisticsList') }, [
|
|
76
|
+
makeElement('thead', '', {}, []),
|
|
77
|
+
makeElement('tbody', 'statistics-body', {}, []),
|
|
78
|
+
]),
|
|
79
|
+
]),
|
|
80
|
+
makeElement('div', 'panel-rail', { role: 'toolbar', 'aria-label': host.t('StatisticsActions') }, [
|
|
81
|
+
makeIconButton('bt-icon-btn panel-export-btn', host.t('ExportToExcel'), 'bi-file-earmark-spreadsheet', {}),
|
|
82
|
+
]),
|
|
83
|
+
]),
|
|
84
|
+
]);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
constructor(rootEl: HTMLElement, _state: Record<string, unknown>, deps: StatisticsDeps) {
|
|
88
|
+
this._host = assertHost(deps?.host, 'StatisticsWidget');
|
|
89
|
+
|
|
90
|
+
this.rootEl = rootEl;
|
|
91
|
+
this.el = this.rootEl.querySelector('.statistics-body');
|
|
92
|
+
this._closeBtn = this.rootEl.querySelector('.panel-close-btn');
|
|
93
|
+
this._exportBtn = this.rootEl.querySelector('.panel-export-btn');
|
|
94
|
+
this._rows = [];
|
|
95
|
+
|
|
96
|
+
this._closeBtn?.addEventListener('click', (e) => {
|
|
97
|
+
e.preventDefault();
|
|
98
|
+
this._host.close();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
this._exportBtn?.addEventListener('click', (e) => {
|
|
102
|
+
e.preventDefault();
|
|
103
|
+
this._export();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const head = this.rootEl.querySelector('.statistics-table thead');
|
|
107
|
+
this._grid = head && this.el
|
|
108
|
+
? new DataGrid<RankedRow>({
|
|
109
|
+
head: head as HTMLElement,
|
|
110
|
+
body: this.el,
|
|
111
|
+
columns: this._columns(),
|
|
112
|
+
// Not a sort the reader chose: the registry hands every parameter an order, and
|
|
113
|
+
// the bands it assigns are what puts profit above drawdown above trade counts.
|
|
114
|
+
defaultSort: { col: 'order', dir: 'asc' },
|
|
115
|
+
rowKey: (r) => String(r.key),
|
|
116
|
+
emptyText: this._host.t('NoStatistics'),
|
|
117
|
+
contextMenu: makeGridMenu(this._host),
|
|
118
|
+
// The groups sit where the registry order puts them, not where the
|
|
119
|
+
// alphabet would: profit, then trades, then positions, then orders.
|
|
120
|
+
groupOrder: 'rows',
|
|
121
|
+
selection: 'multi',
|
|
122
|
+
})
|
|
123
|
+
: null;
|
|
124
|
+
|
|
125
|
+
// Grouped on the key, never on the caption: the caption is translated and would
|
|
126
|
+
// regroup the table under the reader's language. Neither the key nor the order is a
|
|
127
|
+
// column anyone reads, so both are hidden - declared, so the grid can group and sort
|
|
128
|
+
// on them, and out of the way.
|
|
129
|
+
this._grid?.setState({ group: 'category', hidden: ['category', 'order'] });
|
|
130
|
+
|
|
131
|
+
this._host.register(this);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
dispose(): void {
|
|
135
|
+
this._grid?.destroy();
|
|
136
|
+
this._host.unregister(this);
|
|
137
|
+
try { this.rootEl.remove(); } catch { /* already detached */ }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/// Show these statistics. The whole set at once: a strategy publishes its parameters as one
|
|
141
|
+
/// table and a row that vanished from it has stopped existing, not stopped changing.
|
|
142
|
+
update(rows: StatisticRow[]): void {
|
|
143
|
+
this._rows = rank(rows || []);
|
|
144
|
+
this._grid?.setRows(this._rows);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
_export(): void {
|
|
148
|
+
this._grid?.download('statistics', this._host.t('Statistics'));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Two visible columns and one that only groups. The order column is hidden as well: it
|
|
152
|
+
// decides the sort and means nothing to a reader.
|
|
153
|
+
_columns(): GridColumn<RankedRow>[] {
|
|
154
|
+
const label = (key: string) => this._host.t(key);
|
|
155
|
+
return [
|
|
156
|
+
{
|
|
157
|
+
key: 'category',
|
|
158
|
+
header: label('Category'),
|
|
159
|
+
exportable: false,
|
|
160
|
+
// The group's place, not its name: groups are laid out in the order the
|
|
161
|
+
// registry gives their parameters, so profit comes before trade counts.
|
|
162
|
+
// Grouping on the name would order them alphabetically, and on the
|
|
163
|
+
// translated name would reorder them with the reader's language.
|
|
164
|
+
value: (r) => r.categoryRank,
|
|
165
|
+
text: (r) => r.categoryText || r.category,
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
key: 'order',
|
|
169
|
+
header: label('Order'),
|
|
170
|
+
exportable: false,
|
|
171
|
+
value: (r) => r.order,
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
key: 'name',
|
|
175
|
+
header: label('Name'),
|
|
176
|
+
exportable: true,
|
|
177
|
+
value: (r) => r.name,
|
|
178
|
+
bindCell: (td, r) => {
|
|
179
|
+
if (r.description) td.setAttribute('title', r.description);
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
key: 'value',
|
|
184
|
+
header: label('Value'),
|
|
185
|
+
exportable: true,
|
|
186
|
+
cellClass: () => 'statistic-value',
|
|
187
|
+
// Sorts on the raw value so a number sorts as a number, reads as the text below.
|
|
188
|
+
value: (r) => r.value as string | number | null,
|
|
189
|
+
render: (r) => formatStatistic(r.value),
|
|
190
|
+
exportValue: (r) => r.value ?? '',
|
|
191
|
+
},
|
|
192
|
+
];
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/// A statistic as text.
|
|
197
|
+
///
|
|
198
|
+
/// Three shapes reach this: a number, a moment, and everything else. A number is rounded to two
|
|
199
|
+
/// places and shown without trailing zeros - a profit of 11055.75 and a count of 1340 both read
|
|
200
|
+
/// the way they are meant to. A moment is a date: these are run-level facts (the day of the
|
|
201
|
+
/// maximum drawdown), and the hour would be noise. Anything with no value yet is a blank cell
|
|
202
|
+
/// rather than a zero, which would read as a measured nothing.
|
|
203
|
+
export function formatStatistic(value: unknown): string {
|
|
204
|
+
if (value === null || value === undefined || value === '') return '';
|
|
205
|
+
|
|
206
|
+
if (typeof value === 'number') {
|
|
207
|
+
if (!isFinite(value)) return '';
|
|
208
|
+
return String(Math.round(value * 100) / 100);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (value instanceof Date) return isoDate(value);
|
|
212
|
+
|
|
213
|
+
if (typeof value === 'string') {
|
|
214
|
+
// A date only when it is unambiguously one: a bare number in a string stays a number,
|
|
215
|
+
// and a name stays a name.
|
|
216
|
+
const at = /^\d{4}-\d{2}-\d{2}([T ]|$)/.test(value) ? new Date(value) : null;
|
|
217
|
+
if (at !== null && !isNaN(at.getTime())) return isoDate(at);
|
|
218
|
+
return value;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return String(value);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function isoDate(at: Date): string {
|
|
225
|
+
return at.toISOString().slice(0, 10);
|
|
226
|
+
}
|