hyperprop-charting-library 0.1.127 → 0.1.128

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.
@@ -189,6 +189,11 @@ var DEFAULT_OPTIONS = {
189
189
  tickSize: 0,
190
190
  candleColorMode: "openClose",
191
191
  candleColorEpsilon: -1,
192
+ chartType: "candles",
193
+ lineColor: "#2962ff",
194
+ lineWidth: 2,
195
+ areaFillOpacity: 0.12,
196
+ baselinePrice: null,
192
197
  // 0 = deterministic scale (lightweight-charts behavior): the y-range is a
193
198
  // pure function of the visible data, so it can never drift or "breathe".
194
199
  // Values > 0 re-enable eased contraction for hosts that prefer it.
@@ -597,6 +602,79 @@ var withCachedSeries = (key, data, compute) => {
597
602
  builtInSeriesCache.set(key, { fingerprint, values });
598
603
  return values;
599
604
  };
605
+ var builtInComputationCache = /* @__PURE__ */ new Map();
606
+ var withCachedComputation = (key, data, compute) => {
607
+ const fingerprint = getSeriesFingerprint(data);
608
+ const existing = builtInComputationCache.get(key);
609
+ if (existing && existing.fingerprint === fingerprint) {
610
+ return existing.value;
611
+ }
612
+ const value = compute();
613
+ builtInComputationCache.set(key, { fingerprint, value });
614
+ return value;
615
+ };
616
+ var rollingExtremeSeries = (values, length, mode) => {
617
+ const count = values.length;
618
+ const result = new Array(count).fill(null);
619
+ const deque = [];
620
+ const better = (a, b) => mode === "max" ? a >= b : a <= b;
621
+ for (let i = 0; i < count; i += 1) {
622
+ const value = values[i];
623
+ while (deque.length > 0 && better(value, values[deque[deque.length - 1]])) {
624
+ deque.pop();
625
+ }
626
+ deque.push(i);
627
+ if (deque[0] <= i - length) {
628
+ deque.shift();
629
+ }
630
+ if (i >= length - 1) {
631
+ result[i] = values[deque[0]];
632
+ }
633
+ }
634
+ return result;
635
+ };
636
+ var smaFromValues = (values, length) => {
637
+ const result = new Array(values.length).fill(null);
638
+ let sum = 0;
639
+ let valid = 0;
640
+ for (let i = 0; i < values.length; i += 1) {
641
+ const value = values[i];
642
+ if (value != null && Number.isFinite(value)) {
643
+ sum += value;
644
+ valid += 1;
645
+ } else {
646
+ sum = 0;
647
+ valid = 0;
648
+ continue;
649
+ }
650
+ if (valid > length) {
651
+ sum -= values[i - length];
652
+ valid = length;
653
+ }
654
+ if (valid === length) {
655
+ result[i] = sum / length;
656
+ }
657
+ }
658
+ return result;
659
+ };
660
+ var emaFromValues = (values, length) => {
661
+ const result = new Array(values.length).fill(null);
662
+ const alpha = 2 / (length + 1);
663
+ let prev = null;
664
+ let seen = 0;
665
+ for (let i = 0; i < values.length; i += 1) {
666
+ const value = values[i];
667
+ if (value == null || !Number.isFinite(value)) {
668
+ continue;
669
+ }
670
+ prev = prev === null ? value : alpha * value + (1 - alpha) * prev;
671
+ seen += 1;
672
+ if (seen >= length) {
673
+ result[i] = prev;
674
+ }
675
+ }
676
+ return result;
677
+ };
600
678
  var drawOverlaySeries = (ctx, renderContext, values, color, width) => {
601
679
  if (!renderContext.yFromPrice) return;
602
680
  const yFromPrice = renderContext.yFromPrice;
@@ -1142,6 +1220,960 @@ var BUILTIN_RSI_INDICATOR = {
1142
1220
  );
1143
1221
  }
1144
1222
  };
1223
+ var rmaFromValues = (values, length) => {
1224
+ const result = new Array(values.length).fill(null);
1225
+ let prev = null;
1226
+ let seedSum = 0;
1227
+ let seedCount = 0;
1228
+ for (let i = 0; i < values.length; i += 1) {
1229
+ const value = values[i];
1230
+ if (value == null || !Number.isFinite(value)) continue;
1231
+ if (prev === null) {
1232
+ seedSum += value;
1233
+ seedCount += 1;
1234
+ if (seedCount === length) {
1235
+ prev = seedSum / length;
1236
+ result[i] = prev;
1237
+ }
1238
+ continue;
1239
+ }
1240
+ prev = (prev * (length - 1) + value) / length;
1241
+ result[i] = prev;
1242
+ }
1243
+ return result;
1244
+ };
1245
+ var formatCompactNumber = (value) => {
1246
+ const abs = Math.abs(value);
1247
+ if (abs >= 1e9) return `${(value / 1e9).toFixed(2)}B`;
1248
+ if (abs >= 1e6) return `${(value / 1e6).toFixed(2)}M`;
1249
+ if (abs >= 1e3) return `${(value / 1e3).toFixed(1)}K`;
1250
+ return value.toFixed(0);
1251
+ };
1252
+ var drawSeparateMultiSeries = (ctx, renderContext, seriesList, options = {}) => {
1253
+ let min = Number.POSITIVE_INFINITY;
1254
+ let max = Number.NEGATIVE_INFINITY;
1255
+ for (const spec of seriesList) {
1256
+ for (let index = renderContext.startIndex; index <= renderContext.endIndex; index += 1) {
1257
+ const value = spec.values[index];
1258
+ if (value == null || !Number.isFinite(value)) continue;
1259
+ if (value < min) min = value;
1260
+ if (value > max) max = value;
1261
+ }
1262
+ }
1263
+ if (min > max) return void 0;
1264
+ if (options.includeZero || seriesList.some((spec) => spec.histogram)) {
1265
+ min = Math.min(min, 0);
1266
+ max = Math.max(max, 0);
1267
+ }
1268
+ const minValue = options.minOverride ?? min;
1269
+ const maxValue = options.maxOverride ?? max;
1270
+ const range = maxValue - minValue || 1;
1271
+ const yFromValue = (value) => {
1272
+ const ratio = (value - minValue) / range;
1273
+ return renderContext.chartBottom - ratio * renderContext.chartHeight;
1274
+ };
1275
+ if (options.guideLines && options.guideLines.length > 0) {
1276
+ ctx.save();
1277
+ ctx.strokeStyle = "rgba(148,163,184,0.35)";
1278
+ ctx.lineWidth = 1;
1279
+ ctx.setLineDash([4, 4]);
1280
+ for (const guide of options.guideLines) {
1281
+ const y = yFromValue(guide);
1282
+ ctx.beginPath();
1283
+ ctx.moveTo(renderContext.chartLeft, y);
1284
+ ctx.lineTo(renderContext.chartRight, y);
1285
+ ctx.stroke();
1286
+ }
1287
+ ctx.restore();
1288
+ }
1289
+ for (const spec of seriesList) {
1290
+ if (!spec.histogram) continue;
1291
+ const zeroY = yFromValue(0);
1292
+ const barWidth = Math.max(
1293
+ 1,
1294
+ Math.min(Math.max(1, renderContext.candleSpacing - 1), Math.floor(renderContext.candleSpacing * 0.7))
1295
+ );
1296
+ const positive = new Path2D();
1297
+ const negative = new Path2D();
1298
+ for (let index = renderContext.startIndex; index <= renderContext.endIndex; index += 1) {
1299
+ const value = spec.values[index];
1300
+ if (value == null || !Number.isFinite(value)) continue;
1301
+ const x = Math.round(renderContext.xFromIndex(index) - barWidth / 2);
1302
+ const y = yFromValue(value);
1303
+ const top = Math.min(y, zeroY);
1304
+ const height = Math.max(1, Math.abs(y - zeroY));
1305
+ (value >= 0 ? positive : negative).rect(x, top, barWidth, height);
1306
+ }
1307
+ ctx.save();
1308
+ ctx.globalAlpha = 0.85;
1309
+ ctx.fillStyle = spec.color;
1310
+ ctx.fill(positive);
1311
+ ctx.fillStyle = spec.negativeColor ?? spec.color;
1312
+ ctx.fill(negative);
1313
+ ctx.restore();
1314
+ }
1315
+ for (const spec of seriesList) {
1316
+ if (spec.histogram) continue;
1317
+ ctx.save();
1318
+ ctx.strokeStyle = spec.color;
1319
+ ctx.lineWidth = Math.max(1, spec.width ?? 2);
1320
+ ctx.setLineDash([]);
1321
+ let drawing = false;
1322
+ for (let index = renderContext.startIndex; index <= renderContext.endIndex; index += 1) {
1323
+ const value = spec.values[index];
1324
+ if (!Number.isFinite(value ?? Number.NaN)) {
1325
+ drawing = false;
1326
+ continue;
1327
+ }
1328
+ const x = renderContext.xFromIndex(index);
1329
+ const y = yFromValue(value);
1330
+ if (!drawing) {
1331
+ ctx.beginPath();
1332
+ ctx.moveTo(x, y);
1333
+ drawing = true;
1334
+ } else {
1335
+ ctx.lineTo(x, y);
1336
+ }
1337
+ const nextValue = spec.values[index + 1];
1338
+ if (!Number.isFinite(nextValue ?? Number.NaN) || index === renderContext.endIndex) {
1339
+ ctx.stroke();
1340
+ drawing = false;
1341
+ }
1342
+ }
1343
+ ctx.restore();
1344
+ }
1345
+ const decimals = options.decimals ?? 2;
1346
+ const formatValue = options.format ?? ((value) => value.toFixed(decimals));
1347
+ const latestOf = (values) => {
1348
+ for (let index = values.length - 1; index >= 0; index -= 1) {
1349
+ const value = values[index];
1350
+ if (Number.isFinite(value ?? Number.NaN)) return value;
1351
+ }
1352
+ return null;
1353
+ };
1354
+ const paneInfo = {
1355
+ ...options.title ? { title: options.title } : {},
1356
+ axis: {
1357
+ min: minValue,
1358
+ max: maxValue,
1359
+ ...options.axisTicks ? { ticks: options.axisTicks } : options.guideLines ? { ticks: options.guideLines } : {},
1360
+ decimals,
1361
+ format: formatValue
1362
+ }
1363
+ };
1364
+ if (options.guideLines) {
1365
+ paneInfo.guideLines = options.guideLines.map((value) => ({
1366
+ value,
1367
+ label: formatValue(value),
1368
+ style: "dotted"
1369
+ }));
1370
+ }
1371
+ const legendValues = [];
1372
+ for (const spec of seriesList) {
1373
+ const latest = latestOf(spec.values);
1374
+ if (latest === null) continue;
1375
+ legendValues.push({
1376
+ ...spec.label ? { label: spec.label } : {},
1377
+ value: latest,
1378
+ text: formatValue(latest),
1379
+ color: spec.color
1380
+ });
1381
+ }
1382
+ if (legendValues.length > 0) {
1383
+ paneInfo.legendValues = legendValues;
1384
+ }
1385
+ const labelSpec = seriesList[options.valueLabelSeriesIndex ?? 0];
1386
+ const labelLatest = labelSpec ? latestOf(labelSpec.values) : null;
1387
+ if (labelSpec && labelLatest !== null) {
1388
+ paneInfo.valueLabels = [
1389
+ {
1390
+ value: labelLatest,
1391
+ text: formatValue(labelLatest),
1392
+ color: labelSpec.color,
1393
+ backgroundColor: labelSpec.color,
1394
+ textColor: "#0f172a"
1395
+ }
1396
+ ];
1397
+ }
1398
+ return paneInfo;
1399
+ };
1400
+ var computeMacd = (data, fast, slow, signalLength) => {
1401
+ const fastEma = computeEmaSeries(data, fast, "close");
1402
+ const slowEma = computeEmaSeries(data, slow, "close");
1403
+ const macd = fastEma.map((value, idx) => {
1404
+ const slowValue = slowEma[idx];
1405
+ return value == null || slowValue == null ? null : value - slowValue;
1406
+ });
1407
+ const signal = emaFromValues(macd, signalLength);
1408
+ const hist = macd.map((value, idx) => {
1409
+ const signalValue = signal[idx];
1410
+ return value == null || signalValue == null ? null : value - signalValue;
1411
+ });
1412
+ return { macd, signal, hist };
1413
+ };
1414
+ var computeStochastic = (data, kLength, kSmoothing, dLength) => {
1415
+ const highestHigh = rollingExtremeSeries(data.map((point) => point.h), kLength, "max");
1416
+ const lowestLow = rollingExtremeSeries(data.map((point) => point.l), kLength, "min");
1417
+ const rawK = data.map((point, idx) => {
1418
+ const hh = highestHigh[idx];
1419
+ const ll = lowestLow[idx];
1420
+ if (hh == null || ll == null) return null;
1421
+ const range = hh - ll;
1422
+ return range === 0 ? 50 : (point.c - ll) / range * 100;
1423
+ });
1424
+ const k = kSmoothing > 1 ? smaFromValues(rawK, kSmoothing) : rawK;
1425
+ const d = smaFromValues(k, dLength);
1426
+ return { k, d };
1427
+ };
1428
+ var computeStochRsi = (data, rsiLength, stochLength, kSmoothing, dSmoothing) => {
1429
+ const rsi = computeRsiSeries(data, rsiLength);
1430
+ const finiteRsi = rsi.map((value) => value == null ? Number.NaN : value);
1431
+ const highest = rollingExtremeSeries(finiteRsi, stochLength, "max");
1432
+ const lowest = rollingExtremeSeries(finiteRsi, stochLength, "min");
1433
+ const rawK = rsi.map((value, idx) => {
1434
+ const hh = highest[idx];
1435
+ const ll = lowest[idx];
1436
+ if (value == null || hh == null || ll == null || !Number.isFinite(hh) || !Number.isFinite(ll)) return null;
1437
+ const range = hh - ll;
1438
+ return range === 0 ? 50 : (value - ll) / range * 100;
1439
+ });
1440
+ const k = smaFromValues(rawK, Math.max(1, kSmoothing));
1441
+ const d = smaFromValues(k, Math.max(1, dSmoothing));
1442
+ return { k, d };
1443
+ };
1444
+ var computeDmi = (data, length) => {
1445
+ const count = data.length;
1446
+ const plusDm = new Array(count).fill(null);
1447
+ const minusDm = new Array(count).fill(null);
1448
+ const trueRange = new Array(count).fill(null);
1449
+ for (let i = 1; i < count; i += 1) {
1450
+ const point = data[i];
1451
+ const prev = data[i - 1];
1452
+ const upMove = point.h - prev.h;
1453
+ const downMove = prev.l - point.l;
1454
+ plusDm[i] = upMove > downMove && upMove > 0 ? upMove : 0;
1455
+ minusDm[i] = downMove > upMove && downMove > 0 ? downMove : 0;
1456
+ trueRange[i] = Math.max(point.h - point.l, Math.abs(point.h - prev.c), Math.abs(point.l - prev.c));
1457
+ }
1458
+ const smoothPlus = rmaFromValues(plusDm, length);
1459
+ const smoothMinus = rmaFromValues(minusDm, length);
1460
+ const smoothTr = rmaFromValues(trueRange, length);
1461
+ const plusDi = smoothPlus.map((value, idx) => {
1462
+ const tr = smoothTr[idx];
1463
+ return value == null || tr == null || tr === 0 ? null : 100 * value / tr;
1464
+ });
1465
+ const minusDi = smoothMinus.map((value, idx) => {
1466
+ const tr = smoothTr[idx];
1467
+ return value == null || tr == null || tr === 0 ? null : 100 * value / tr;
1468
+ });
1469
+ const dx = plusDi.map((value, idx) => {
1470
+ const minus = minusDi[idx];
1471
+ if (value == null || minus == null) return null;
1472
+ const sum = value + minus;
1473
+ return sum === 0 ? 0 : 100 * Math.abs(value - minus) / sum;
1474
+ });
1475
+ const adx = rmaFromValues(dx, length);
1476
+ return { adx, plusDi, minusDi };
1477
+ };
1478
+ var computeObvSeries = (data) => {
1479
+ const result = new Array(data.length).fill(null);
1480
+ let obv = 0;
1481
+ for (let i = 0; i < data.length; i += 1) {
1482
+ const point = data[i];
1483
+ if (i > 0) {
1484
+ const prev = data[i - 1];
1485
+ const volume = Math.max(0, point.v ?? 0);
1486
+ if (point.c > prev.c) obv += volume;
1487
+ else if (point.c < prev.c) obv -= volume;
1488
+ }
1489
+ result[i] = obv;
1490
+ }
1491
+ return result;
1492
+ };
1493
+ var computeMfiSeries = (data, length) => {
1494
+ const result = new Array(data.length).fill(null);
1495
+ const positiveFlow = new Array(data.length).fill(0);
1496
+ const negativeFlow = new Array(data.length).fill(0);
1497
+ let prevTypical = null;
1498
+ for (let i = 0; i < data.length; i += 1) {
1499
+ const point = data[i];
1500
+ const typical = (point.h + point.l + point.c) / 3;
1501
+ const flow = typical * Math.max(0, point.v ?? 0);
1502
+ if (prevTypical !== null) {
1503
+ if (typical > prevTypical) positiveFlow[i] = flow;
1504
+ else if (typical < prevTypical) negativeFlow[i] = flow;
1505
+ }
1506
+ prevTypical = typical;
1507
+ }
1508
+ let posSum = 0;
1509
+ let negSum = 0;
1510
+ for (let i = 0; i < data.length; i += 1) {
1511
+ posSum += positiveFlow[i];
1512
+ negSum += negativeFlow[i];
1513
+ if (i >= length) {
1514
+ posSum -= positiveFlow[i - length];
1515
+ negSum -= negativeFlow[i - length];
1516
+ }
1517
+ if (i >= length) {
1518
+ result[i] = negSum === 0 ? 100 : 100 - 100 / (1 + posSum / negSum);
1519
+ }
1520
+ }
1521
+ return result;
1522
+ };
1523
+ var computeCciSeries = (data, length) => {
1524
+ const result = new Array(data.length).fill(null);
1525
+ const typical = data.map((point) => (point.h + point.l + point.c) / 3);
1526
+ const smaTypical = smaFromValues(typical, length);
1527
+ for (let i = length - 1; i < data.length; i += 1) {
1528
+ const mean = smaTypical[i];
1529
+ if (mean == null) continue;
1530
+ let meanDeviation = 0;
1531
+ for (let j = 0; j < length; j += 1) {
1532
+ meanDeviation += Math.abs(typical[i - j] - mean);
1533
+ }
1534
+ meanDeviation /= length;
1535
+ result[i] = meanDeviation === 0 ? 0 : (typical[i] - mean) / (0.015 * meanDeviation);
1536
+ }
1537
+ return result;
1538
+ };
1539
+ var computeWilliamsRSeries = (data, length) => {
1540
+ const highest = rollingExtremeSeries(data.map((point) => point.h), length, "max");
1541
+ const lowest = rollingExtremeSeries(data.map((point) => point.l), length, "min");
1542
+ return data.map((point, idx) => {
1543
+ const hh = highest[idx];
1544
+ const ll = lowest[idx];
1545
+ if (hh == null || ll == null) return null;
1546
+ const range = hh - ll;
1547
+ return range === 0 ? -50 : (hh - point.c) / range * -100;
1548
+ });
1549
+ };
1550
+ var computeRocSeries = (data, length) => {
1551
+ return data.map((point, idx) => {
1552
+ if (idx < length) return null;
1553
+ const past = data[idx - length].c;
1554
+ return past === 0 ? null : 100 * (point.c - past) / past;
1555
+ });
1556
+ };
1557
+ var computeMomentumSeries = (data, length) => {
1558
+ return data.map((point, idx) => idx < length ? null : point.c - data[idx - length].c);
1559
+ };
1560
+ var computePsarSeries = (data, start, increment, maximum) => {
1561
+ const count = data.length;
1562
+ const result = new Array(count).fill(null);
1563
+ if (count < 2) return result;
1564
+ let isUp = data[1].c >= data[0].c;
1565
+ let sar = isUp ? data[0].l : data[0].h;
1566
+ let extremePoint = isUp ? data[0].h : data[0].l;
1567
+ let accelerationFactor = start;
1568
+ for (let i = 1; i < count; i += 1) {
1569
+ const point = data[i];
1570
+ sar += accelerationFactor * (extremePoint - sar);
1571
+ if (isUp) {
1572
+ sar = Math.min(sar, data[i - 1].l, data[Math.max(0, i - 2)].l);
1573
+ if (point.l < sar) {
1574
+ isUp = false;
1575
+ sar = extremePoint;
1576
+ extremePoint = point.l;
1577
+ accelerationFactor = start;
1578
+ } else if (point.h > extremePoint) {
1579
+ extremePoint = point.h;
1580
+ accelerationFactor = Math.min(maximum, accelerationFactor + increment);
1581
+ }
1582
+ } else {
1583
+ sar = Math.max(sar, data[i - 1].h, data[Math.max(0, i - 2)].h);
1584
+ if (point.h > sar) {
1585
+ isUp = true;
1586
+ sar = extremePoint;
1587
+ extremePoint = point.h;
1588
+ accelerationFactor = start;
1589
+ } else if (point.l < extremePoint) {
1590
+ extremePoint = point.l;
1591
+ accelerationFactor = Math.min(maximum, accelerationFactor + increment);
1592
+ }
1593
+ }
1594
+ result[i] = sar;
1595
+ }
1596
+ return result;
1597
+ };
1598
+ var computeSuperTrend = (data, atrLength, multiplier) => {
1599
+ const count = data.length;
1600
+ const up = new Array(count).fill(null);
1601
+ const down = new Array(count).fill(null);
1602
+ const atr = computeAtrSeries(data, atrLength);
1603
+ let prevUpper = Number.NaN;
1604
+ let prevLower = Number.NaN;
1605
+ let trend = 1;
1606
+ let prevClose = 0;
1607
+ for (let i = 0; i < count; i += 1) {
1608
+ const point = data[i];
1609
+ const atrValue = atr[i];
1610
+ if (atrValue == null) {
1611
+ prevClose = point.c;
1612
+ continue;
1613
+ }
1614
+ const mid = (point.h + point.l) / 2;
1615
+ const basicUpper = mid + multiplier * atrValue;
1616
+ const basicLower = mid - multiplier * atrValue;
1617
+ if (!Number.isFinite(prevUpper)) {
1618
+ prevUpper = basicUpper;
1619
+ prevLower = basicLower;
1620
+ trend = point.c >= mid ? 1 : -1;
1621
+ } else {
1622
+ const finalUpper = basicUpper < prevUpper || prevClose > prevUpper ? basicUpper : prevUpper;
1623
+ const finalLower = basicLower > prevLower || prevClose < prevLower ? basicLower : prevLower;
1624
+ if (trend === 1) {
1625
+ trend = point.c < finalLower ? -1 : 1;
1626
+ } else {
1627
+ trend = point.c > finalUpper ? 1 : -1;
1628
+ }
1629
+ prevUpper = finalUpper;
1630
+ prevLower = finalLower;
1631
+ }
1632
+ if (trend === 1) up[i] = prevLower;
1633
+ else down[i] = prevUpper;
1634
+ prevClose = point.c;
1635
+ }
1636
+ return { up, down };
1637
+ };
1638
+ var computeIchimoku = (data, conversionLength, baseLength, spanBLength, displacement) => {
1639
+ const count = data.length;
1640
+ const highs = data.map((point) => point.h);
1641
+ const lows = data.map((point) => point.l);
1642
+ const midlineOf = (length) => {
1643
+ const highest = rollingExtremeSeries(highs, length, "max");
1644
+ const lowest = rollingExtremeSeries(lows, length, "min");
1645
+ return highest.map((value, idx) => {
1646
+ const low = lowest[idx];
1647
+ return value == null || low == null ? null : (value + low) / 2;
1648
+ });
1649
+ };
1650
+ const tenkan = midlineOf(conversionLength);
1651
+ const kijun = midlineOf(baseLength);
1652
+ const spanARaw = tenkan.map((value, idx) => {
1653
+ const base = kijun[idx];
1654
+ return value == null || base == null ? null : (value + base) / 2;
1655
+ });
1656
+ const spanBRaw = midlineOf(spanBLength);
1657
+ const spanA = new Array(count).fill(null);
1658
+ const spanB = new Array(count).fill(null);
1659
+ const chikou = new Array(count).fill(null);
1660
+ for (let i = 0; i < count; i += 1) {
1661
+ const shifted = i - displacement;
1662
+ if (shifted >= 0) {
1663
+ spanA[i] = spanARaw[shifted] ?? null;
1664
+ spanB[i] = spanBRaw[shifted] ?? null;
1665
+ }
1666
+ const forward = i + displacement;
1667
+ if (forward < count) {
1668
+ chikou[i] = data[forward].c;
1669
+ }
1670
+ }
1671
+ return { tenkan, kijun, spanA, spanB, chikou };
1672
+ };
1673
+ var computeKeltner = (data, emaLength, atrLength, multiplier) => {
1674
+ const basis = computeEmaSeries(data, emaLength, "close");
1675
+ const atr = computeAtrSeries(data, atrLength);
1676
+ const upper = basis.map((value, idx) => {
1677
+ const atrValue = atr[idx];
1678
+ return value == null || atrValue == null ? null : value + multiplier * atrValue;
1679
+ });
1680
+ const lower = basis.map((value, idx) => {
1681
+ const atrValue = atr[idx];
1682
+ return value == null || atrValue == null ? null : value - multiplier * atrValue;
1683
+ });
1684
+ return { basis, upper, lower };
1685
+ };
1686
+ var computeDonchian = (data, length) => {
1687
+ const upper = rollingExtremeSeries(data.map((point) => point.h), length, "max");
1688
+ const lower = rollingExtremeSeries(data.map((point) => point.l), length, "min");
1689
+ const basis = upper.map((value, idx) => {
1690
+ const low = lower[idx];
1691
+ return value == null || low == null ? null : (value + low) / 2;
1692
+ });
1693
+ return { upper, lower, basis };
1694
+ };
1695
+ var BUILTIN_MACD_INDICATOR = {
1696
+ id: "macd",
1697
+ name: "MACD",
1698
+ pane: "separate",
1699
+ paneHeightRatio: 0.2,
1700
+ defaultInputs: {
1701
+ fast: 12,
1702
+ slow: 26,
1703
+ signal: 9,
1704
+ macdColor: "#2962ff",
1705
+ signalColor: "#ff6d00",
1706
+ histUpColor: "#26a69a",
1707
+ histDownColor: "#ef5350"
1708
+ },
1709
+ draw: (ctx, renderContext, inputs) => {
1710
+ const fast = clampIndicatorLength(inputs.fast, 12);
1711
+ const slow = clampIndicatorLength(inputs.slow, 26);
1712
+ const signalLength = clampIndicatorLength(inputs.signal, 9);
1713
+ const { macd, signal, hist } = withCachedComputation(
1714
+ `macd|${fast}|${slow}|${signalLength}`,
1715
+ renderContext.data,
1716
+ () => computeMacd(renderContext.data, fast, slow, signalLength)
1717
+ );
1718
+ return drawSeparateMultiSeries(
1719
+ ctx,
1720
+ renderContext,
1721
+ [
1722
+ {
1723
+ values: hist,
1724
+ color: inputs.histUpColor ?? "#26a69a",
1725
+ negativeColor: inputs.histDownColor ?? "#ef5350",
1726
+ histogram: true
1727
+ },
1728
+ { values: macd, color: inputs.macdColor ?? "#2962ff", label: "MACD" },
1729
+ { values: signal, color: inputs.signalColor ?? "#ff6d00", label: "Signal" }
1730
+ ],
1731
+ {
1732
+ title: `MACD ${fast} ${slow} ${signalLength}`,
1733
+ includeZero: true,
1734
+ guideLines: [0],
1735
+ decimals: 2,
1736
+ valueLabelSeriesIndex: 1
1737
+ }
1738
+ );
1739
+ }
1740
+ };
1741
+ var BUILTIN_STOCHASTIC_INDICATOR = {
1742
+ id: "stochastic",
1743
+ name: "Stoch",
1744
+ pane: "separate",
1745
+ paneHeightRatio: 0.18,
1746
+ defaultInputs: { kLength: 14, kSmoothing: 1, dLength: 3, kColor: "#2962ff", dColor: "#ff6d00" },
1747
+ draw: (ctx, renderContext, inputs) => {
1748
+ const kLength = clampIndicatorLength(inputs.kLength, 14);
1749
+ const kSmoothing = clampIndicatorLength(inputs.kSmoothing, 1);
1750
+ const dLength = clampIndicatorLength(inputs.dLength, 3);
1751
+ const { k, d } = withCachedComputation(
1752
+ `stochastic|${kLength}|${kSmoothing}|${dLength}`,
1753
+ renderContext.data,
1754
+ () => computeStochastic(renderContext.data, kLength, kSmoothing, dLength)
1755
+ );
1756
+ return drawSeparateMultiSeries(
1757
+ ctx,
1758
+ renderContext,
1759
+ [
1760
+ { values: k, color: inputs.kColor ?? "#2962ff", label: "%K" },
1761
+ { values: d, color: inputs.dColor ?? "#ff6d00", label: "%D" }
1762
+ ],
1763
+ {
1764
+ title: `Stoch ${kLength} ${kSmoothing} ${dLength}`,
1765
+ minOverride: 0,
1766
+ maxOverride: 100,
1767
+ guideLines: [20, 80],
1768
+ axisTicks: [0, 20, 50, 80, 100],
1769
+ decimals: 2
1770
+ }
1771
+ );
1772
+ }
1773
+ };
1774
+ var BUILTIN_STOCHRSI_INDICATOR = {
1775
+ id: "stochrsi",
1776
+ name: "Stoch RSI",
1777
+ pane: "separate",
1778
+ paneHeightRatio: 0.18,
1779
+ defaultInputs: { rsiLength: 14, stochLength: 14, kSmoothing: 3, dSmoothing: 3, kColor: "#2962ff", dColor: "#ff6d00" },
1780
+ draw: (ctx, renderContext, inputs) => {
1781
+ const rsiLength = clampIndicatorLength(inputs.rsiLength, 14);
1782
+ const stochLength = clampIndicatorLength(inputs.stochLength, 14);
1783
+ const kSmoothing = clampIndicatorLength(inputs.kSmoothing, 3);
1784
+ const dSmoothing = clampIndicatorLength(inputs.dSmoothing, 3);
1785
+ const { k, d } = withCachedComputation(
1786
+ `stochrsi|${rsiLength}|${stochLength}|${kSmoothing}|${dSmoothing}`,
1787
+ renderContext.data,
1788
+ () => computeStochRsi(renderContext.data, rsiLength, stochLength, kSmoothing, dSmoothing)
1789
+ );
1790
+ return drawSeparateMultiSeries(
1791
+ ctx,
1792
+ renderContext,
1793
+ [
1794
+ { values: k, color: inputs.kColor ?? "#2962ff", label: "%K" },
1795
+ { values: d, color: inputs.dColor ?? "#ff6d00", label: "%D" }
1796
+ ],
1797
+ {
1798
+ title: `Stoch RSI ${rsiLength} ${stochLength}`,
1799
+ minOverride: 0,
1800
+ maxOverride: 100,
1801
+ guideLines: [20, 80],
1802
+ axisTicks: [0, 20, 50, 80, 100],
1803
+ decimals: 2
1804
+ }
1805
+ );
1806
+ }
1807
+ };
1808
+ var BUILTIN_ADX_INDICATOR = {
1809
+ id: "adx",
1810
+ name: "ADX/DMI",
1811
+ pane: "separate",
1812
+ paneHeightRatio: 0.18,
1813
+ defaultInputs: { length: 14, adxColor: "#ff6d00", plusDiColor: "#26a69a", minusDiColor: "#ef5350", showDi: true },
1814
+ draw: (ctx, renderContext, inputs) => {
1815
+ const length = clampIndicatorLength(inputs.length, 14);
1816
+ const { adx, plusDi, minusDi } = withCachedComputation(
1817
+ `adx|${length}`,
1818
+ renderContext.data,
1819
+ () => computeDmi(renderContext.data, length)
1820
+ );
1821
+ const seriesList = [
1822
+ { values: adx, color: inputs.adxColor ?? "#ff6d00", label: "ADX" }
1823
+ ];
1824
+ if (inputs.showDi !== false) {
1825
+ seriesList.push(
1826
+ { values: plusDi, color: inputs.plusDiColor ?? "#26a69a", label: "+DI", width: 1 },
1827
+ { values: minusDi, color: inputs.minusDiColor ?? "#ef5350", label: "-DI", width: 1 }
1828
+ );
1829
+ }
1830
+ return drawSeparateMultiSeries(ctx, renderContext, seriesList, {
1831
+ title: `ADX ${length}`,
1832
+ minOverride: 0,
1833
+ guideLines: [20],
1834
+ decimals: 2
1835
+ });
1836
+ }
1837
+ };
1838
+ var BUILTIN_OBV_INDICATOR = {
1839
+ id: "obv",
1840
+ name: "OBV",
1841
+ pane: "separate",
1842
+ paneHeightRatio: 0.16,
1843
+ defaultInputs: { color: "#2962ff", width: 2 },
1844
+ draw: (ctx, renderContext, inputs) => {
1845
+ const values = withCachedSeries("obv", renderContext.data, () => computeObvSeries(renderContext.data));
1846
+ return drawSeparateMultiSeries(
1847
+ ctx,
1848
+ renderContext,
1849
+ [{ values, color: inputs.color ?? "#2962ff", width: Number(inputs.width) || 2, label: "OBV" }],
1850
+ { title: "OBV", format: formatCompactNumber }
1851
+ );
1852
+ }
1853
+ };
1854
+ var BUILTIN_MFI_INDICATOR = {
1855
+ id: "mfi",
1856
+ name: "MFI",
1857
+ pane: "separate",
1858
+ paneHeightRatio: 0.16,
1859
+ defaultInputs: { length: 14, color: "#7e57c2", width: 2 },
1860
+ draw: (ctx, renderContext, inputs) => {
1861
+ const length = clampIndicatorLength(inputs.length, 14);
1862
+ const values = withCachedSeries(
1863
+ `mfi|${length}`,
1864
+ renderContext.data,
1865
+ () => computeMfiSeries(renderContext.data, length)
1866
+ );
1867
+ return drawSeparateMultiSeries(
1868
+ ctx,
1869
+ renderContext,
1870
+ [{ values, color: inputs.color ?? "#7e57c2", width: Number(inputs.width) || 2 }],
1871
+ {
1872
+ title: `MFI ${length}`,
1873
+ minOverride: 0,
1874
+ maxOverride: 100,
1875
+ guideLines: [20, 80],
1876
+ axisTicks: [0, 20, 50, 80, 100],
1877
+ decimals: 2
1878
+ }
1879
+ );
1880
+ }
1881
+ };
1882
+ var BUILTIN_CCI_INDICATOR = {
1883
+ id: "cci",
1884
+ name: "CCI",
1885
+ pane: "separate",
1886
+ paneHeightRatio: 0.16,
1887
+ defaultInputs: { length: 20, color: "#2962ff", width: 2 },
1888
+ draw: (ctx, renderContext, inputs) => {
1889
+ const length = clampIndicatorLength(inputs.length, 20);
1890
+ const values = withCachedSeries(
1891
+ `cci|${length}`,
1892
+ renderContext.data,
1893
+ () => computeCciSeries(renderContext.data, length)
1894
+ );
1895
+ return drawSeparateMultiSeries(
1896
+ ctx,
1897
+ renderContext,
1898
+ [{ values, color: inputs.color ?? "#2962ff", width: Number(inputs.width) || 2 }],
1899
+ { title: `CCI ${length}`, guideLines: [-100, 0, 100], decimals: 2 }
1900
+ );
1901
+ }
1902
+ };
1903
+ var BUILTIN_WILLIAMSR_INDICATOR = {
1904
+ id: "williamsr",
1905
+ name: "Williams %R",
1906
+ pane: "separate",
1907
+ paneHeightRatio: 0.16,
1908
+ defaultInputs: { length: 14, color: "#7e57c2", width: 2 },
1909
+ draw: (ctx, renderContext, inputs) => {
1910
+ const length = clampIndicatorLength(inputs.length, 14);
1911
+ const values = withCachedSeries(
1912
+ `williamsr|${length}`,
1913
+ renderContext.data,
1914
+ () => computeWilliamsRSeries(renderContext.data, length)
1915
+ );
1916
+ return drawSeparateMultiSeries(
1917
+ ctx,
1918
+ renderContext,
1919
+ [{ values, color: inputs.color ?? "#7e57c2", width: Number(inputs.width) || 2 }],
1920
+ {
1921
+ title: `%R ${length}`,
1922
+ minOverride: -100,
1923
+ maxOverride: 0,
1924
+ guideLines: [-80, -20],
1925
+ axisTicks: [-100, -80, -50, -20, 0],
1926
+ decimals: 2
1927
+ }
1928
+ );
1929
+ }
1930
+ };
1931
+ var BUILTIN_ROC_INDICATOR = {
1932
+ id: "roc",
1933
+ name: "ROC",
1934
+ pane: "separate",
1935
+ paneHeightRatio: 0.16,
1936
+ defaultInputs: { length: 9, color: "#2962ff", width: 2 },
1937
+ draw: (ctx, renderContext, inputs) => {
1938
+ const length = clampIndicatorLength(inputs.length, 9);
1939
+ const values = withCachedSeries(
1940
+ `roc|${length}`,
1941
+ renderContext.data,
1942
+ () => computeRocSeries(renderContext.data, length)
1943
+ );
1944
+ return drawSeparateMultiSeries(
1945
+ ctx,
1946
+ renderContext,
1947
+ [{ values, color: inputs.color ?? "#2962ff", width: Number(inputs.width) || 2 }],
1948
+ { title: `ROC ${length}`, includeZero: true, guideLines: [0], decimals: 2 }
1949
+ );
1950
+ }
1951
+ };
1952
+ var BUILTIN_MOMENTUM_INDICATOR = {
1953
+ id: "momentum",
1954
+ name: "Momentum",
1955
+ pane: "separate",
1956
+ paneHeightRatio: 0.16,
1957
+ defaultInputs: { length: 10, color: "#2962ff", width: 2 },
1958
+ draw: (ctx, renderContext, inputs) => {
1959
+ const length = clampIndicatorLength(inputs.length, 10);
1960
+ const values = withCachedSeries(
1961
+ `momentum|${length}`,
1962
+ renderContext.data,
1963
+ () => computeMomentumSeries(renderContext.data, length)
1964
+ );
1965
+ return drawSeparateMultiSeries(
1966
+ ctx,
1967
+ renderContext,
1968
+ [{ values, color: inputs.color ?? "#2962ff", width: Number(inputs.width) || 2 }],
1969
+ { title: `Mom ${length}`, includeZero: true, guideLines: [0], decimals: 2 }
1970
+ );
1971
+ }
1972
+ };
1973
+ var BUILTIN_PSAR_INDICATOR = {
1974
+ id: "psar",
1975
+ name: "PSAR",
1976
+ pane: "overlay",
1977
+ defaultInputs: { start: 0.02, increment: 0.02, maximum: 0.2, color: "#2962ff" },
1978
+ draw: (ctx, renderContext, inputs) => {
1979
+ if (!renderContext.yFromPrice) return;
1980
+ const yFromPrice = renderContext.yFromPrice;
1981
+ const start = Math.max(1e-3, Number(inputs.start) || 0.02);
1982
+ const increment = Math.max(1e-3, Number(inputs.increment) || 0.02);
1983
+ const maximum = Math.max(increment, Number(inputs.maximum) || 0.2);
1984
+ const values = withCachedSeries(
1985
+ `psar|${start}|${increment}|${maximum}`,
1986
+ renderContext.data,
1987
+ () => computePsarSeries(renderContext.data, start, increment, maximum)
1988
+ );
1989
+ const radius = Math.max(1, Math.min(2.5, renderContext.candleSpacing * 0.15));
1990
+ const dots = new Path2D();
1991
+ for (let index = renderContext.startIndex; index <= renderContext.endIndex; index += 1) {
1992
+ const value = values[index];
1993
+ if (!Number.isFinite(value ?? Number.NaN)) continue;
1994
+ const x = renderContext.xFromIndex(index);
1995
+ const y = yFromPrice(value);
1996
+ dots.moveTo(x + radius, y);
1997
+ dots.arc(x, y, radius, 0, Math.PI * 2);
1998
+ }
1999
+ ctx.save();
2000
+ ctx.fillStyle = inputs.color ?? "#2962ff";
2001
+ ctx.fill(dots);
2002
+ ctx.restore();
2003
+ },
2004
+ getAutoscaleRange: (data, startIndex, endIndex, inputs, skipIndex) => {
2005
+ const start = Math.max(1e-3, Number(inputs.start) || 0.02);
2006
+ const increment = Math.max(1e-3, Number(inputs.increment) || 0.02);
2007
+ const maximum = Math.max(increment, Number(inputs.maximum) || 0.2);
2008
+ const values = withCachedSeries(
2009
+ `psar|${start}|${increment}|${maximum}`,
2010
+ data,
2011
+ () => computePsarSeries(data, start, increment, maximum)
2012
+ );
2013
+ return rangeOfSeries([values], startIndex, endIndex, skipIndex);
2014
+ }
2015
+ };
2016
+ var BUILTIN_SUPERTREND_INDICATOR = {
2017
+ id: "supertrend",
2018
+ name: "SuperTrend",
2019
+ pane: "overlay",
2020
+ defaultInputs: { atrLength: 10, multiplier: 3, upColor: "#26a69a", downColor: "#ef5350", width: 2 },
2021
+ draw: (ctx, renderContext, inputs) => {
2022
+ const atrLength = clampIndicatorLength(inputs.atrLength, 10);
2023
+ const multiplier = Math.max(0.1, Number(inputs.multiplier) || 3);
2024
+ const { up, down } = withCachedComputation(
2025
+ `supertrend|${atrLength}|${multiplier}`,
2026
+ renderContext.data,
2027
+ () => computeSuperTrend(renderContext.data, atrLength, multiplier)
2028
+ );
2029
+ const width = Number(inputs.width) || 2;
2030
+ drawOverlaySeries(ctx, renderContext, up, inputs.upColor ?? "#26a69a", width);
2031
+ drawOverlaySeries(ctx, renderContext, down, inputs.downColor ?? "#ef5350", width);
2032
+ },
2033
+ getAutoscaleRange: (data, startIndex, endIndex, inputs, skipIndex) => {
2034
+ const atrLength = clampIndicatorLength(inputs.atrLength, 10);
2035
+ const multiplier = Math.max(0.1, Number(inputs.multiplier) || 3);
2036
+ const { up, down } = withCachedComputation(
2037
+ `supertrend|${atrLength}|${multiplier}`,
2038
+ data,
2039
+ () => computeSuperTrend(data, atrLength, multiplier)
2040
+ );
2041
+ return rangeOfSeries([up, down], startIndex, endIndex, skipIndex);
2042
+ }
2043
+ };
2044
+ var BUILTIN_ICHIMOKU_INDICATOR = {
2045
+ id: "ichimoku",
2046
+ name: "Ichimoku",
2047
+ pane: "overlay",
2048
+ defaultInputs: {
2049
+ conversionLength: 9,
2050
+ baseLength: 26,
2051
+ spanBLength: 52,
2052
+ displacement: 26,
2053
+ tenkanColor: "#2962ff",
2054
+ kijunColor: "#b71c1c",
2055
+ spanAColor: "#26a69a",
2056
+ spanBColor: "#ef5350",
2057
+ chikouColor: "#43a047",
2058
+ cloudOpacity: 0.08,
2059
+ showChikou: true
2060
+ },
2061
+ draw: (ctx, renderContext, inputs) => {
2062
+ const conversionLength = clampIndicatorLength(inputs.conversionLength, 9);
2063
+ const baseLength = clampIndicatorLength(inputs.baseLength, 26);
2064
+ const spanBLength = clampIndicatorLength(inputs.spanBLength, 52);
2065
+ const displacement = clampIndicatorLength(inputs.displacement, 26);
2066
+ const { tenkan, kijun, spanA, spanB, chikou } = withCachedComputation(
2067
+ `ichimoku|${conversionLength}|${baseLength}|${spanBLength}|${displacement}`,
2068
+ renderContext.data,
2069
+ () => computeIchimoku(renderContext.data, conversionLength, baseLength, spanBLength, displacement)
2070
+ );
2071
+ const cloudOpacity = Math.min(0.5, Math.max(0, Number(inputs.cloudOpacity) || 0.08));
2072
+ const bullishA = spanA.map((value, idx) => {
2073
+ const other = spanB[idx];
2074
+ return value != null && other != null && value >= other ? value : null;
2075
+ });
2076
+ const bullishB = spanB.map((value, idx) => {
2077
+ const other = spanA[idx];
2078
+ return value != null && other != null && other >= value ? value : null;
2079
+ });
2080
+ const bearishA = spanA.map((value, idx) => {
2081
+ const other = spanB[idx];
2082
+ return value != null && other != null && value < other ? value : null;
2083
+ });
2084
+ const bearishB = spanB.map((value, idx) => {
2085
+ const other = spanA[idx];
2086
+ return value != null && other != null && other < value ? value : null;
2087
+ });
2088
+ fillBetweenSeries(ctx, renderContext, bullishA, bullishB, inputs.spanAColor ?? "#26a69a", cloudOpacity);
2089
+ fillBetweenSeries(ctx, renderContext, bearishB, bearishA, inputs.spanBColor ?? "#ef5350", cloudOpacity);
2090
+ drawOverlaySeries(ctx, renderContext, spanA, inputs.spanAColor ?? "#26a69a", 1);
2091
+ drawOverlaySeries(ctx, renderContext, spanB, inputs.spanBColor ?? "#ef5350", 1);
2092
+ drawOverlaySeries(ctx, renderContext, tenkan, inputs.tenkanColor ?? "#2962ff", 1.5);
2093
+ drawOverlaySeries(ctx, renderContext, kijun, inputs.kijunColor ?? "#b71c1c", 1.5);
2094
+ if (inputs.showChikou !== false) {
2095
+ drawOverlaySeries(ctx, renderContext, chikou, inputs.chikouColor ?? "#43a047", 1);
2096
+ }
2097
+ },
2098
+ getAutoscaleRange: (data, startIndex, endIndex, inputs, skipIndex) => {
2099
+ const conversionLength = clampIndicatorLength(inputs.conversionLength, 9);
2100
+ const baseLength = clampIndicatorLength(inputs.baseLength, 26);
2101
+ const spanBLength = clampIndicatorLength(inputs.spanBLength, 52);
2102
+ const displacement = clampIndicatorLength(inputs.displacement, 26);
2103
+ const { tenkan, kijun, spanA, spanB } = withCachedComputation(
2104
+ `ichimoku|${conversionLength}|${baseLength}|${spanBLength}|${displacement}`,
2105
+ data,
2106
+ () => computeIchimoku(data, conversionLength, baseLength, spanBLength, displacement)
2107
+ );
2108
+ return rangeOfSeries([tenkan, kijun, spanA, spanB], startIndex, endIndex, skipIndex);
2109
+ }
2110
+ };
2111
+ var BUILTIN_KELTNER_INDICATOR = {
2112
+ id: "keltner",
2113
+ name: "KC",
2114
+ pane: "overlay",
2115
+ defaultInputs: {
2116
+ emaLength: 20,
2117
+ atrLength: 10,
2118
+ multiplier: 2,
2119
+ basisColor: "#2962ff",
2120
+ bandColor: "#2962ff",
2121
+ width: 1.5,
2122
+ fillOpacity: 0.04
2123
+ },
2124
+ draw: (ctx, renderContext, inputs) => {
2125
+ const emaLength = clampIndicatorLength(inputs.emaLength, 20);
2126
+ const atrLength = clampIndicatorLength(inputs.atrLength, 10);
2127
+ const multiplier = Math.max(0.1, Number(inputs.multiplier) || 2);
2128
+ const { basis, upper, lower } = withCachedComputation(
2129
+ `keltner|${emaLength}|${atrLength}|${multiplier}`,
2130
+ renderContext.data,
2131
+ () => computeKeltner(renderContext.data, emaLength, atrLength, multiplier)
2132
+ );
2133
+ const bandColor = inputs.bandColor ?? "#2962ff";
2134
+ const width = Number(inputs.width) || 1.5;
2135
+ fillBetweenSeries(ctx, renderContext, upper, lower, bandColor, Number(inputs.fillOpacity) || 0.04);
2136
+ drawOverlaySeries(ctx, renderContext, upper, bandColor, width);
2137
+ drawOverlaySeries(ctx, renderContext, lower, bandColor, width);
2138
+ drawOverlaySeries(ctx, renderContext, basis, inputs.basisColor ?? "#2962ff", width);
2139
+ },
2140
+ getAutoscaleRange: (data, startIndex, endIndex, inputs, skipIndex) => {
2141
+ const emaLength = clampIndicatorLength(inputs.emaLength, 20);
2142
+ const atrLength = clampIndicatorLength(inputs.atrLength, 10);
2143
+ const multiplier = Math.max(0.1, Number(inputs.multiplier) || 2);
2144
+ const { upper, lower } = withCachedComputation(
2145
+ `keltner|${emaLength}|${atrLength}|${multiplier}`,
2146
+ data,
2147
+ () => computeKeltner(data, emaLength, atrLength, multiplier)
2148
+ );
2149
+ return rangeOfSeries([upper, lower], startIndex, endIndex, skipIndex);
2150
+ }
2151
+ };
2152
+ var BUILTIN_DONCHIAN_INDICATOR = {
2153
+ id: "donchian",
2154
+ name: "DC",
2155
+ pane: "overlay",
2156
+ defaultInputs: { length: 20, bandColor: "#2962ff", basisColor: "#ff6d00", width: 1.5, fillOpacity: 0.04 },
2157
+ draw: (ctx, renderContext, inputs) => {
2158
+ const length = clampIndicatorLength(inputs.length, 20);
2159
+ const { upper, lower, basis } = withCachedComputation(
2160
+ `donchian|${length}`,
2161
+ renderContext.data,
2162
+ () => computeDonchian(renderContext.data, length)
2163
+ );
2164
+ const bandColor = inputs.bandColor ?? "#2962ff";
2165
+ const width = Number(inputs.width) || 1.5;
2166
+ fillBetweenSeries(ctx, renderContext, upper, lower, bandColor, Number(inputs.fillOpacity) || 0.04);
2167
+ drawOverlaySeries(ctx, renderContext, upper, bandColor, width);
2168
+ drawOverlaySeries(ctx, renderContext, lower, bandColor, width);
2169
+ drawOverlaySeries(ctx, renderContext, basis, inputs.basisColor ?? "#ff6d00", width);
2170
+ },
2171
+ getAutoscaleRange: (data, startIndex, endIndex, inputs, skipIndex) => {
2172
+ const length = clampIndicatorLength(inputs.length, 20);
2173
+ const { upper, lower } = withCachedComputation(`donchian|${length}`, data, () => computeDonchian(data, length));
2174
+ return rangeOfSeries([upper, lower], startIndex, endIndex, skipIndex);
2175
+ }
2176
+ };
1145
2177
  var BUILTIN_INDICATORS = [
1146
2178
  BUILTIN_VOLUME_INDICATOR,
1147
2179
  BUILTIN_SMA_INDICATOR,
@@ -1154,7 +2186,22 @@ var BUILTIN_INDICATORS = [
1154
2186
  BUILTIN_VWAP_INDICATOR,
1155
2187
  BUILTIN_BOLLINGER_INDICATOR,
1156
2188
  BUILTIN_STDDEV_INDICATOR,
1157
- BUILTIN_ATR_INDICATOR
2189
+ BUILTIN_ATR_INDICATOR,
2190
+ BUILTIN_MACD_INDICATOR,
2191
+ BUILTIN_STOCHASTIC_INDICATOR,
2192
+ BUILTIN_STOCHRSI_INDICATOR,
2193
+ BUILTIN_ADX_INDICATOR,
2194
+ BUILTIN_OBV_INDICATOR,
2195
+ BUILTIN_MFI_INDICATOR,
2196
+ BUILTIN_CCI_INDICATOR,
2197
+ BUILTIN_WILLIAMSR_INDICATOR,
2198
+ BUILTIN_ROC_INDICATOR,
2199
+ BUILTIN_MOMENTUM_INDICATOR,
2200
+ BUILTIN_PSAR_INDICATOR,
2201
+ BUILTIN_SUPERTREND_INDICATOR,
2202
+ BUILTIN_ICHIMOKU_INDICATOR,
2203
+ BUILTIN_KELTNER_INDICATOR,
2204
+ BUILTIN_DONCHIAN_INDICATOR
1158
2205
  ];
1159
2206
  function createChart(element, options = {}) {
1160
2207
  let mergedOptions = mergeChartOptions(DEFAULT_OPTIONS, options);
@@ -1878,6 +2925,36 @@ function createChart(element, options = {}) {
1878
2925
  }
1879
2926
  return direction > 0 ? "up" : "down";
1880
2927
  };
2928
+ let heikinAshiCache = null;
2929
+ let heikinAshiFingerprint = "";
2930
+ const getHeikinAshiData = () => {
2931
+ const last = data[data.length - 1];
2932
+ const fingerprint = !last ? "empty" : `${data.length}|${last.time.getTime()}|${last.o}|${last.h}|${last.l}|${last.c}`;
2933
+ if (heikinAshiCache && heikinAshiFingerprint === fingerprint) {
2934
+ return heikinAshiCache;
2935
+ }
2936
+ const result = new Array(data.length);
2937
+ let prevOpen = 0;
2938
+ let prevClose = 0;
2939
+ for (let i = 0; i < data.length; i += 1) {
2940
+ const point = data[i];
2941
+ const haClose = (point.o + point.h + point.l + point.c) / 4;
2942
+ const haOpen = i === 0 ? (point.o + point.c) / 2 : (prevOpen + prevClose) / 2;
2943
+ result[i] = {
2944
+ time: point.time,
2945
+ o: haOpen,
2946
+ h: Math.max(point.h, haOpen, haClose),
2947
+ l: Math.min(point.l, haOpen, haClose),
2948
+ c: haClose,
2949
+ ...point.v === void 0 ? {} : { v: point.v }
2950
+ };
2951
+ prevOpen = haOpen;
2952
+ prevClose = haClose;
2953
+ }
2954
+ heikinAshiCache = result;
2955
+ heikinAshiFingerprint = fingerprint;
2956
+ return result;
2957
+ };
1881
2958
  const formatHoverTimeLabel = (time, mode) => {
1882
2959
  if (mode === "time") {
1883
2960
  return time.toLocaleTimeString(void 0, {
@@ -2455,23 +3532,34 @@ function createChart(element, options = {}) {
2455
3532
  const xEnd = xStart + xSpan;
2456
3533
  const startIndex = Math.max(0, Math.floor(xStart));
2457
3534
  const endIndex = Math.min(data.length - 1, Math.ceil(xEnd) - 1);
3535
+ const chartType = mergedOptions.chartType;
3536
+ const seriesData = chartType === "heikin-ashi" ? getHeikinAshiData() : data;
3537
+ const isLineStyleChart = chartType === "line" || chartType === "area" || chartType === "baseline";
2458
3538
  const skipLatestIndex = mergedOptions.autoScaleIgnoreLatestCandle && data.length > 1 ? data.length - 1 : -1;
2459
3539
  const scanCandleRange = (from, to) => {
2460
3540
  let min = Number.POSITIVE_INFINITY;
2461
3541
  let max = Number.NEGATIVE_INFINITY;
2462
3542
  for (let idx = from; idx <= to; idx += 1) {
2463
3543
  if (idx === skipLatestIndex) continue;
2464
- const point = data[idx];
3544
+ const point = seriesData[idx];
2465
3545
  if (!point) continue;
2466
- if (point.l < min) min = point.l;
2467
- if (point.h > max) max = point.h;
3546
+ if (isLineStyleChart) {
3547
+ if (point.c < min) min = point.c;
3548
+ if (point.c > max) max = point.c;
3549
+ } else {
3550
+ if (point.l < min) min = point.l;
3551
+ if (point.h > max) max = point.h;
3552
+ }
2468
3553
  }
2469
3554
  return min <= max ? { min, max } : null;
2470
3555
  };
2471
3556
  let candleRange = scanCandleRange(startIndex, endIndex);
2472
3557
  if (!candleRange) {
2473
3558
  const latestIndex = data.length - 1;
2474
- candleRange = scanCandleRange(Math.max(0, latestIndex - 120), latestIndex - 1) ?? scanCandleRange(0, latestIndex) ?? { min: data[latestIndex].l, max: data[latestIndex].h };
3559
+ candleRange = scanCandleRange(Math.max(0, latestIndex - 120), latestIndex - 1) ?? scanCandleRange(0, latestIndex) ?? {
3560
+ min: isLineStyleChart ? seriesData[latestIndex].c : seriesData[latestIndex].l,
3561
+ max: isLineStyleChart ? seriesData[latestIndex].c : seriesData[latestIndex].h
3562
+ };
2475
3563
  }
2476
3564
  let minPrice = candleRange.min;
2477
3565
  let maxPrice = candleRange.max;
@@ -3367,23 +4455,137 @@ function createChart(element, options = {}) {
3367
4455
  }
3368
4456
  return data[index]?.v;
3369
4457
  };
3370
- {
4458
+ if (isLineStyleChart) {
4459
+ const seriesLineWidth = Math.max(1, mergedOptions.lineWidth);
4460
+ const linePath = new Path2D();
4461
+ let started = false;
4462
+ let firstX = 0;
4463
+ let lastX = 0;
4464
+ for (let index = startIndex; index <= endIndex; index += 1) {
4465
+ const point = seriesData[index];
4466
+ if (!point) continue;
4467
+ const close = useSmoothedCandle && index === lastDataIndex ? smoothedTickerPrice : point.c;
4468
+ const x = chartLeft + (index + 0.5 - xStart) / xSpan * chartWidth;
4469
+ const y = yFromPrice(close);
4470
+ if (!started) {
4471
+ linePath.moveTo(x, y);
4472
+ firstX = x;
4473
+ started = true;
4474
+ } else {
4475
+ linePath.lineTo(x, y);
4476
+ }
4477
+ lastX = x;
4478
+ }
4479
+ if (started) {
4480
+ if (chartType === "baseline") {
4481
+ const baselinePrice = mergedOptions.baselinePrice ?? (yMin + yMax) / 2;
4482
+ const baselineY = yFromPrice(baselinePrice);
4483
+ const fillPath = new Path2D(linePath);
4484
+ fillPath.lineTo(lastX, baselineY);
4485
+ fillPath.lineTo(firstX, baselineY);
4486
+ fillPath.closePath();
4487
+ const halves = [
4488
+ { clipTop: chartTop, clipBottom: baselineY, color: mergedOptions.upColor },
4489
+ { clipTop: baselineY, clipBottom: chartBottom, color: mergedOptions.downColor }
4490
+ ];
4491
+ for (const half of halves) {
4492
+ const clipHeight = half.clipBottom - half.clipTop;
4493
+ if (clipHeight <= 0) continue;
4494
+ ctx.save();
4495
+ ctx.beginPath();
4496
+ ctx.rect(chartLeft, half.clipTop, chartWidth, clipHeight);
4497
+ ctx.clip();
4498
+ ctx.globalAlpha = Math.max(0, Math.min(1, mergedOptions.areaFillOpacity));
4499
+ ctx.fillStyle = half.color;
4500
+ ctx.fill(fillPath);
4501
+ ctx.globalAlpha = 1;
4502
+ ctx.strokeStyle = half.color;
4503
+ ctx.lineWidth = seriesLineWidth;
4504
+ ctx.lineJoin = "round";
4505
+ ctx.stroke(linePath);
4506
+ ctx.restore();
4507
+ }
4508
+ ctx.save();
4509
+ ctx.strokeStyle = "rgba(148,163,184,0.5)";
4510
+ ctx.lineWidth = 1;
4511
+ ctx.setLineDash([4, 4]);
4512
+ ctx.beginPath();
4513
+ ctx.moveTo(chartLeft, crisp(baselineY));
4514
+ ctx.lineTo(chartRight, crisp(baselineY));
4515
+ ctx.stroke();
4516
+ ctx.restore();
4517
+ } else {
4518
+ if (chartType === "area") {
4519
+ const fillPath = new Path2D(linePath);
4520
+ fillPath.lineTo(lastX, chartBottom);
4521
+ fillPath.lineTo(firstX, chartBottom);
4522
+ fillPath.closePath();
4523
+ ctx.save();
4524
+ ctx.globalAlpha = Math.max(0, Math.min(1, mergedOptions.areaFillOpacity));
4525
+ ctx.fillStyle = mergedOptions.lineColor;
4526
+ ctx.fill(fillPath);
4527
+ ctx.restore();
4528
+ }
4529
+ ctx.save();
4530
+ ctx.strokeStyle = mergedOptions.lineColor;
4531
+ ctx.lineWidth = seriesLineWidth;
4532
+ ctx.lineJoin = "round";
4533
+ ctx.stroke(linePath);
4534
+ ctx.restore();
4535
+ }
4536
+ }
4537
+ } else if (chartType === "bars") {
4538
+ const upBars = new Path2D();
4539
+ const downBars = new Path2D();
4540
+ const tickLength = Math.max(2, Math.floor(bodyWidth / 2));
4541
+ for (let index = startIndex; index <= endIndex; index += 1) {
4542
+ const point = seriesData[index];
4543
+ if (!point) continue;
4544
+ const isLastCandle = useSmoothedCandle && index === lastDataIndex;
4545
+ const actualDirection = point.c >= point.o ? "up" : "down";
4546
+ const displayClose = isLastCandle ? actualDirection === "up" ? Math.max(point.o, smoothedTickerPrice) : Math.min(point.o, smoothedTickerPrice) : point.c;
4547
+ const displayHigh = isLastCandle && actualDirection === "up" ? Math.max(point.h, displayClose) : point.h;
4548
+ const displayLow = isLastCandle && actualDirection === "down" ? Math.min(point.l, displayClose) : point.l;
4549
+ const roundedCenterX = Math.round(chartLeft + (index + 0.5 - xStart) / xSpan * chartWidth);
4550
+ const direction = isLastCandle ? actualDirection : getCandleDirectionByIndex(index);
4551
+ const path = direction === "up" ? upBars : downBars;
4552
+ const centerX = roundedCenterX + 0.5;
4553
+ path.moveTo(centerX, crisp(yFromPrice(displayHigh)));
4554
+ path.lineTo(centerX, crisp(yFromPrice(displayLow)));
4555
+ const openY = crisp(yFromPrice(point.o));
4556
+ const closeY = crisp(yFromPrice(displayClose));
4557
+ path.moveTo(centerX - tickLength, openY);
4558
+ path.lineTo(centerX, openY);
4559
+ path.moveTo(centerX, closeY);
4560
+ path.lineTo(centerX + tickLength, closeY);
4561
+ }
4562
+ ctx.lineWidth = Math.max(1, candleWickWidth);
4563
+ ctx.strokeStyle = mergedOptions.upColor;
4564
+ ctx.stroke(upBars);
4565
+ ctx.strokeStyle = mergedOptions.downColor;
4566
+ ctx.stroke(downBars);
4567
+ } else {
4568
+ const isHeikinAshi = chartType === "heikin-ashi";
4569
+ const isHollow = chartType === "hollow-candles";
4570
+ const smoothLastCandle = useSmoothedCandle && !isHeikinAshi;
3371
4571
  const upWicks = new Path2D();
3372
4572
  const downWicks = new Path2D();
3373
4573
  const upBodies = new Path2D();
3374
4574
  const downBodies = new Path2D();
4575
+ const upHollowBodies = new Path2D();
4576
+ const downHollowBodies = new Path2D();
3375
4577
  for (let index = startIndex; index <= endIndex; index += 1) {
3376
- const point = data[index];
4578
+ const point = seriesData[index];
3377
4579
  if (!point) {
3378
4580
  continue;
3379
4581
  }
3380
- const isLastCandle = useSmoothedCandle && index === lastDataIndex;
4582
+ const isLastCandle = smoothLastCandle && index === lastDataIndex;
3381
4583
  const actualDirection = point.c >= point.o ? "up" : "down";
3382
4584
  const displayClose = isLastCandle ? actualDirection === "up" ? Math.max(point.o, smoothedTickerPrice) : Math.min(point.o, smoothedTickerPrice) : point.c;
3383
4585
  const displayHigh = isLastCandle ? actualDirection === "up" ? Math.max(point.h, displayClose) : point.h : point.h;
3384
4586
  const displayLow = isLastCandle ? actualDirection === "up" ? point.l : Math.min(point.l, displayClose) : point.l;
3385
4587
  const centerX = chartLeft + (index + 0.5 - xStart) / xSpan * chartWidth;
3386
- const direction = isLastCandle ? actualDirection : getCandleDirectionByIndex(index);
4588
+ const direction = isHeikinAshi || isLastCandle ? actualDirection : getCandleDirectionByIndex(index);
3387
4589
  const isUp = direction === "up";
3388
4590
  const roundedCenterX = Math.round(centerX);
3389
4591
  const wicks = isUp ? upWicks : downWicks;
@@ -3395,7 +4597,9 @@ function createChart(element, options = {}) {
3395
4597
  const bodyIsUp = displayClose >= point.o;
3396
4598
  const bodyTop = bodyIsUp ? Math.min(closeYPx, openYPx - 1) : openYPx;
3397
4599
  const bodyBottom = bodyIsUp ? openYPx : Math.max(closeYPx, openYPx + 1);
3398
- (isUp ? upBodies : downBodies).rect(bodyLeft, bodyTop, bodyWidth, bodyBottom - bodyTop);
4600
+ const useHollowBody = isHollow && bodyIsUp;
4601
+ const bodies = useHollowBody ? isUp ? upHollowBodies : downHollowBodies : isUp ? upBodies : downBodies;
4602
+ bodies.rect(bodyLeft, bodyTop, bodyWidth, bodyBottom - bodyTop);
3399
4603
  }
3400
4604
  ctx.lineWidth = candleWickWidth;
3401
4605
  ctx.strokeStyle = mergedOptions.upColor;
@@ -3406,6 +4610,16 @@ function createChart(element, options = {}) {
3406
4610
  ctx.stroke(downWicks);
3407
4611
  ctx.fillStyle = mergedOptions.downColor;
3408
4612
  ctx.fill(downBodies);
4613
+ if (isHollow) {
4614
+ ctx.fillStyle = mergedOptions.backgroundColor;
4615
+ ctx.fill(upHollowBodies);
4616
+ ctx.fill(downHollowBodies);
4617
+ ctx.lineWidth = 1;
4618
+ ctx.strokeStyle = mergedOptions.upColor;
4619
+ ctx.stroke(upHollowBodies);
4620
+ ctx.strokeStyle = mergedOptions.downColor;
4621
+ ctx.stroke(downHollowBodies);
4622
+ }
3409
4623
  }
3410
4624
  const activeOverlayIndicators = indicators.filter((indicator) => indicator.visible).map((indicator) => ({ indicator, plugin: indicatorRegistry.get(indicator.type) })).filter(
3411
4625
  (value) => value.plugin !== void 0 && (value.indicator.pane ?? value.plugin.pane ?? "overlay") === "overlay"
@@ -5801,6 +7015,14 @@ function createChart(element, options = {}) {
5801
7015
  }
5802
7016
  scheduleDraw();
5803
7017
  };
7018
+ const setChartType = (type) => {
7019
+ if (mergedOptions.chartType === type) {
7020
+ return;
7021
+ }
7022
+ mergedOptions = { ...mergedOptions, chartType: type };
7023
+ scheduleDraw({ updateAutoScale: true });
7024
+ };
7025
+ const getChartType = () => mergedOptions.chartType;
5804
7026
  const resize = (nextWidth, nextHeight) => {
5805
7027
  if (nextWidth && nextWidth > 0) {
5806
7028
  width = nextWidth;
@@ -6158,6 +7380,8 @@ function createChart(element, options = {}) {
6158
7380
  draw();
6159
7381
  return {
6160
7382
  updateOptions,
7383
+ setChartType,
7384
+ getChartType,
6161
7385
  setData,
6162
7386
  upsertBar,
6163
7387
  setPriceLines,