@tradecanvas/chart 0.8.1 → 0.8.2

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.
Files changed (2) hide show
  1. package/README.md +140 -47
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -8,12 +8,16 @@ High-performance canvas trading chart with built-in indicators, drawing tools, a
8
8
 
9
9
  Most chart libraries make you choose: pretty charts with no trading features, or trading features with an ugly API. TradeCanvas gives you both.
10
10
 
11
- - **33+ built-in indicators** — MA, EMA, Hull MA, RSI, MACD, Bollinger, Ichimoku, Pivot Points, Anchored VWAP, ZigZag, Linear Regression Channel, Awesome / Chaikin Oscillator, and more. No separate calculation library needed.
12
- - **10+ drawing tools** — Trendlines, Fibonacci retracement, horizontal/vertical lines, rectangles, channels, Elliott waves, Gann fans. With undo/redo.
13
- - **Trading overlay** — Render open positions with entry line, P&L zone, and SL/TP markers. Orders as dashed lines. Users can drag SL/TP to modify.
11
+ - **33 built-in indicators** — SMA, EMA, Hull MA, RSI, MACD, Bollinger, Ichimoku, Pivot Points, Anchored VWAP, ZigZag, Linear Regression Channel, Awesome / Chaikin Oscillator, and more. No separate calculation library needed.
12
+ - **24 drawing tools** — Trendlines, Fibonacci (retracement, extension, time zones), horizontal/vertical lines, channels, Elliott waves, Gann fans / boxes, Pitchfork, Volume Profile range. With undo/redo and full serialization.
13
+ - **17 chart types** — Candlestick, line, area, bar, hollow candle, baseline, Heikin-Ashi, Renko, Kagi, Line Break, Point & Figure, Range Bars, Volume Candles, **Equivolume**, HLC Area, Step Line, Line+Markers.
14
+ - **Trading overlay** — Render open positions with entry line, P&L zone, and SL/TP markers. Orders as dashed lines. Drag SL/TP to modify. Cleanly opt-out via `features.trading: false` for non-trading projects.
14
15
  - **Real-time streaming** — Built-in Binance adapter. Plug in your own data source with the adapter interface.
16
+ - **Strategy backtester** *(new in 0.8)* — `@tradecanvas/analytics` ships a bar-by-bar `Backtester` with virtual fills, commission/slippage models, portfolio tracking, and risk metrics (Sharpe, Sortino, Calmar, max drawdown).
17
+ - **Replay mode** *(new in 0.8)* — `ReplayController` drives historical bars forward at controlled speed with start / pause / step / seek / setSpeed; decoupled from `Chart` so it can power both UI playback and headless backtests.
18
+ - **Multi-chart grid** — `ChartGrid` for synchronized 2×2 / 2×3 layouts with linked crosshairs and shared time axis.
19
+ - **Signal markers & trade zones** — render bot/algorithm output (directional arrows, entry→exit rectangles) as a first-class chart layer.
15
20
  - **Save/load chart state** — Persist drawings, indicators, theme, and chart type to JSON. Restore with one call.
16
- - **Replay mode** — Step through historical data bar-by-bar for backtesting visualization.
17
21
  - **Zero dependencies** — The entire library is self-contained. No `d3`, no `chart.js`, no `fancy-canvas`.
18
22
 
19
23
  ## Install
@@ -28,50 +32,45 @@ yarn add @tradecanvas/chart
28
32
 
29
33
  ## Quick Start
30
34
 
31
- ```typescript
32
- import { Chart, BinanceAdapter } from '@tradecanvas/chart'
35
+ The fastest path is `ChartWidget` — drop-in component with a full TradingView-like UI (toolbar, drawing sidebar, settings dialog, status bar). Zero framework dependency.
33
36
 
34
- // Create a chart
35
- const chart = new Chart(document.getElementById('chart')!, {
36
- theme: 'dark',
37
- autoScale: true,
38
- features: {
39
- drawings: true,
40
- indicators: true,
41
- trading: true,
42
- volume: true,
43
- },
44
- })
37
+ ```typescript
38
+ import { ChartWidget } from '@tradecanvas/chart/widget'
39
+ import { BinanceAdapter } from '@tradecanvas/chart'
45
40
 
46
- // Connect to live Binance data
47
- const adapter = new BinanceAdapter()
48
- chart.connect({
49
- adapter,
41
+ const widget = new ChartWidget(document.getElementById('chart')!, {
50
42
  symbol: 'BTCUSDT',
51
43
  timeframe: '5m',
52
- historyLimit: 300,
44
+ theme: 'dark',
45
+ adapter: new BinanceAdapter(),
46
+ trading: true,
53
47
  })
54
48
  ```
55
49
 
56
- That's it. A full-featured trading chart with live data in 15 lines.
50
+ That's it. Live data, all 33 indicators, all 24 drawing tools, command palette (`Ctrl+K`).
57
51
 
58
- ## Widget (Complete UI)
52
+ ## Headless Chart
59
53
 
60
- For a complete TradingView-like experience with built-in toolbar, drawing tools, and settings — no UI code needed:
54
+ For projects that want to own the surrounding UI (custom toolbar, framework-specific controls), use the lower-level `Chart` class directly:
61
55
 
62
56
  ```typescript
63
- import { ChartWidget } from '@tradecanvas/chart/widget'
64
- import { BinanceAdapter } from '@tradecanvas/chart'
57
+ import { Chart, BinanceAdapter } from '@tradecanvas/chart'
65
58
 
66
- const widget = new ChartWidget(document.getElementById('chart')!, {
67
- symbol: 'BTCUSDT',
68
- timeframe: '5m',
69
- adapter: new BinanceAdapter(),
59
+ const chart = new Chart(document.getElementById('chart')!, {
70
60
  theme: 'dark',
61
+ autoScale: true,
62
+ features: {
63
+ drawings: true,
64
+ indicators: true,
65
+ trading: true, // set false to disable orders/positions entirely
66
+ tradingContextMenu: true, // set false to keep overlay but drop right-click menu
67
+ volume: true,
68
+ },
71
69
  })
72
- ```
73
70
 
74
- That's it. Full toolbar, drawing sidebar, settings modal, and status bar — all included.
71
+ const adapter = new BinanceAdapter()
72
+ chart.connect({ adapter, symbol: 'BTCUSDT', timeframe: '5m', historyLimit: 300 })
73
+ ```
75
74
 
76
75
  ### Widget Options
77
76
 
@@ -123,12 +122,15 @@ That's it. Full toolbar, drawing sidebar, settings modal, and status bar — all
123
122
  | Line Break | Three-line break charts |
124
123
  | Range Bars | Fixed price-range bars — each bar's high − low equals a configured range |
125
124
  | Volume Candles | Candlesticks with width proportional to volume |
125
+ | Equivolume | Full-range boxes with width proportional to volume share (Richard Arms style) |
126
126
  | HLC Area | High-low-close area band with close line |
127
127
  | Step Line | Staircase/step pattern from close prices |
128
128
  | Line with Markers | Close line with circular markers at each data point |
129
129
 
130
130
  ### Multi-Chart Grid
131
131
 
132
+ Display multiple synchronized charts side-by-side with linked crosshairs and time axis:
133
+
132
134
  ```typescript
133
135
  import { ChartGrid, BinanceAdapter } from '@tradecanvas/chart'
134
136
 
@@ -142,11 +144,11 @@ const adapter = new BinanceAdapter()
142
144
  grid.connectAll(adapter, ['BTCUSDT', 'ETHUSDT', 'SOLUSDT', 'BNBUSDT'], '5m')
143
145
  ```
144
146
 
145
- Layouts: `'1x1'`, `'1x2'`, `'2x1'`, `'2x2'`, `'1x3'`, `'3x1'`, `'2x3'`, `'3x2'`.
147
+ Supported layouts: `'1x1'`, `'1x2'`, `'2x1'`, `'2x2'`, `'1x3'`, `'3x1'`, `'2x3'`, `'3x2'`.
146
148
 
147
149
  ### Command Palette
148
150
 
149
- Press `Ctrl+K` / `Cmd+K` inside ChartWidget to open the command palette. Search indicators, chart types, drawing tools, timeframes, and actions.
151
+ Press `Ctrl+K` (or `Cmd+K`) inside ChartWidget to open a searchable command palette. Quickly find and toggle indicators, change chart types, activate drawing tools, switch timeframes, or trigger actions (screenshot, theme toggle, settings).
150
152
 
151
153
  ### Finance Charts
152
154
 
@@ -210,7 +212,7 @@ All indicator parameters are validated at runtime — invalid values (NaN, Infin
210
212
 
211
213
  ### Drawing Tools
212
214
 
213
- Trendline, Horizontal Line, Vertical Line, Ray, Extended Line, Parallel Channel, Fibonacci Retracement, Fibonacci Extension, Rectangle, Ellipse, Triangle, Arrow, Pitchfork, Gann Fan, Gann Box, Elliott Wave, Regression Channel, Date Range, Price Range, Measure, Anchored VWAP, Volume Profile Range, Text Annotation
215
+ Trendline, Horizontal Line, Vertical Line, Ray, Extended Line, Parallel Channel, Fibonacci Retracement, Fibonacci Extension, **Fibonacci Time Zones**, Rectangle, Ellipse, Triangle, Arrow, Pitchfork, Gann Fan, Gann Box, Elliott Wave, Regression Channel, Date Range, Price Range, Measure, Anchored VWAP, Volume Profile Range, Text Annotation
214
216
 
215
217
  All drawing tools support:
216
218
  - Click-to-place with magnet snapping to OHLC values
@@ -262,6 +264,53 @@ chart.on('positionModify', (e) => console.log('SL/TP moved:', e.payload))
262
264
  chart.on('orderModify', (e) => console.log('Order moved:', e.payload))
263
265
  ```
264
266
 
267
+ ### Signal Markers
268
+
269
+ Visualize buy/sell signals from bots, indicators, or manual analysis.
270
+
271
+ ```typescript
272
+ chart.addSignalMarker({
273
+ time: 1715692800000,
274
+ price: 62500,
275
+ direction: 'long',
276
+ confidence: 0.85,
277
+ source: 'ema-crossover',
278
+ label: 'EMA Cross',
279
+ })
280
+
281
+ // Color-code by source
282
+ chart.setSignalMarkerStyle({
283
+ sourceColors: {
284
+ 'ema-crossover': '#2196F3',
285
+ 'rsi-divergence': '#FF9800',
286
+ 'whale-flow': '#9C27B0',
287
+ },
288
+ })
289
+ ```
290
+
291
+ ### Trade Zones
292
+
293
+ Render entry→exit rectangles with P&L coloring for executed trades.
294
+
295
+ ```typescript
296
+ const zoneId = chart.addTradeZone({
297
+ entryTime: 1715692800000,
298
+ entryPrice: 62500,
299
+ exitTime: 1715700000000,
300
+ exitPrice: 63200,
301
+ direction: 'long',
302
+ pnl: 140,
303
+ pnlPercent: 1.12,
304
+ })
305
+
306
+ // Update a live trade when it closes
307
+ chart.updateTradeZone(zoneId, {
308
+ exitTime: Date.now(),
309
+ exitPrice: 63500,
310
+ pnl: 200,
311
+ })
312
+ ```
313
+
265
314
  ### Real-Time Streaming
266
315
 
267
316
  ```typescript
@@ -360,28 +409,72 @@ chart.on('positionModify', (e) => { /* ... */ })
360
409
 
361
410
  ### Replay Mode
362
411
 
412
+ `ReplayController` plays a historical `DataSeries` forward at controlled speed. Decoupled from `Chart` — wire it into any sink (chart for UI playback, or a strategy fn for headless backtests).
413
+
363
414
  ```typescript
364
- chart.replayStart({ data: historicalBars, speed: 2, startIndex: 100 })
365
- chart.replayPause()
366
- chart.replayResume()
367
- chart.replayStop()
368
- const { current, total, percent } = chart.getReplayProgress()
415
+ import { ReplayController } from '@tradecanvas/chart'
416
+
417
+ const replay = new ReplayController({
418
+ data: historicalBars,
419
+ speed: 10, // bars per second
420
+ startIndex: 0,
421
+ })
422
+
423
+ // Seed the chart with the prefix before replay starts
424
+ chart.setData(replay.getPrefix())
425
+
426
+ // Each emitted bar drives the chart forward
427
+ replay.on('bar', ({ bar }) => chart.appendBar(bar))
428
+ replay.on('finished', () => console.log('done'))
429
+
430
+ replay.start()
431
+ // replay.pause(); replay.resume(); replay.step(5); replay.seek(200); replay.setSpeed(20)
369
432
  ```
370
433
 
434
+ ### Backtesting (`@tradecanvas/analytics`)
435
+
436
+ Bar-by-bar strategy backtester with virtual fills, commission/slippage models, and a full risk-metrics report.
437
+
438
+ ```typescript
439
+ import { Backtester, PercentCommission, PercentSlippage } from '@tradecanvas/analytics'
440
+
441
+ const bt = new Backtester({
442
+ initialCash: 10_000,
443
+ commission: new PercentCommission(0.0005),
444
+ slippage: new PercentSlippage(0.0003),
445
+ })
446
+
447
+ const result = bt.run(historicalBars, (ctx) => {
448
+ // Strategy fn runs at close of each bar; orders fill on the NEXT bar.
449
+ if (!ctx.position && smaFast > smaSlow) {
450
+ ctx.placeOrder({ side: 'long', type: 'market', quantity: 1 })
451
+ } else if (ctx.position && smaFast < smaSlow) {
452
+ ctx.close()
453
+ }
454
+ })
455
+
456
+ console.log(result.metrics.sharpe) // 1.42
457
+ console.log(result.metrics.maxDrawdownPct) // 0.087
458
+ console.log(result.equityCurve) // → feed into the chart via EquityCurveRenderer
459
+ ```
460
+
461
+ Returns: `fills`, closed `trades`, `equityCurve`, `metrics` (Sharpe, Sortino, Calmar, CAGR, max drawdown, win rate, profit factor, expectancy). See the [live backtest demo](https://bonguynvan.github.io/tradecanvas/docs/analytics/).
462
+
371
463
  ## Comparison
372
464
 
373
465
  | Feature | @tradecanvas/chart | lightweight-charts | chart.js | Highcharts Stock |
374
466
  |---|---|---|---|---|
375
- | Chart types | 12 + 6 finance | 4 | 8 (non-financial) | 10+ |
467
+ | Chart types | 17 + 6 finance | 4 | 8 (non-financial) | 10+ |
376
468
  | Finance charts | Sparkline, Depth, Equity, Heatmap, Waterfall, Gauge | None | None | Some |
377
- | Built-in indicators | 33+ | 0 | 0 | ~30 |
378
- | Drawing tools | 23 | 0 | 0 | Some |
469
+ | Built-in indicators | 33 | 0 | 0 | ~30 |
470
+ | Drawing tools | 24 | 0 | 0 | Some |
379
471
  | Trading overlay | Full (pos + orders + drag) | None | None | None |
380
472
  | Real-time streaming | Built-in (Binance) | Manual | Manual | Built-in |
381
473
  | Save/load state | Yes | No | No | Yes |
382
- | Replay mode | Yes | No | No | No |
383
- | Multi-panel | Yes | No | No | Yes |
384
- | Bundle (gzip) | ~50 KB | ~45 KB | ~70 KB | ~200 KB |
474
+ | Replay mode | Yes (`ReplayController`) | No | No | No |
475
+ | Backtester | Yes (`@tradecanvas/analytics`) | No | No | No |
476
+ | Multi-chart grid | Yes (`ChartGrid`) | No | No | Yes |
477
+ | Bundle (gzip) | ~56 KB core | ~45 KB | ~70 KB | ~200 KB |
385
478
  | Dependencies | 0 | 1 | 0 | 0 |
386
479
  | Widget (complete UI) | Yes (`ChartWidget`) | No | No | No |
387
480
  | License | MIT | Apache 2.0 | MIT | Commercial |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tradecanvas/chart",
3
- "version": "0.8.1",
3
+ "version": "0.8.2",
4
4
  "type": "module",
5
5
  "description": "High-performance canvas trading chart with built-in indicators, drawing tools, and real-time streaming. Zero external dependencies.",
6
6
  "license": "MIT",
@@ -47,8 +47,8 @@
47
47
  "README.md"
48
48
  ],
49
49
  "dependencies": {
50
- "@tradecanvas/commons": "0.8.1",
51
- "@tradecanvas/core": "0.8.1"
50
+ "@tradecanvas/commons": "0.8.2",
51
+ "@tradecanvas/core": "0.8.2"
52
52
  },
53
53
  "devDependencies": {
54
54
  "vitest": "^2.1.8"