hyperprop-charting-library 0.1.126 → 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.
- package/dist/hyperprop-charting-library.cjs +1237 -10
- package/dist/hyperprop-charting-library.d.ts +26 -1
- package/dist/hyperprop-charting-library.js +1237 -10
- package/dist/index.cjs +1237 -10
- package/dist/index.d.cts +26 -1
- package/dist/index.d.ts +26 -1
- package/dist/index.js +1237 -10
- package/docs/API.md +24 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -215,6 +215,11 @@ var DEFAULT_OPTIONS = {
|
|
|
215
215
|
tickSize: 0,
|
|
216
216
|
candleColorMode: "openClose",
|
|
217
217
|
candleColorEpsilon: -1,
|
|
218
|
+
chartType: "candles",
|
|
219
|
+
lineColor: "#2962ff",
|
|
220
|
+
lineWidth: 2,
|
|
221
|
+
areaFillOpacity: 0.12,
|
|
222
|
+
baselinePrice: null,
|
|
218
223
|
// 0 = deterministic scale (lightweight-charts behavior): the y-range is a
|
|
219
224
|
// pure function of the visible data, so it can never drift or "breathe".
|
|
220
225
|
// Values > 0 re-enable eased contraction for hosts that prefer it.
|
|
@@ -623,6 +628,79 @@ var withCachedSeries = (key, data, compute) => {
|
|
|
623
628
|
builtInSeriesCache.set(key, { fingerprint, values });
|
|
624
629
|
return values;
|
|
625
630
|
};
|
|
631
|
+
var builtInComputationCache = /* @__PURE__ */ new Map();
|
|
632
|
+
var withCachedComputation = (key, data, compute) => {
|
|
633
|
+
const fingerprint = getSeriesFingerprint(data);
|
|
634
|
+
const existing = builtInComputationCache.get(key);
|
|
635
|
+
if (existing && existing.fingerprint === fingerprint) {
|
|
636
|
+
return existing.value;
|
|
637
|
+
}
|
|
638
|
+
const value = compute();
|
|
639
|
+
builtInComputationCache.set(key, { fingerprint, value });
|
|
640
|
+
return value;
|
|
641
|
+
};
|
|
642
|
+
var rollingExtremeSeries = (values, length, mode) => {
|
|
643
|
+
const count = values.length;
|
|
644
|
+
const result = new Array(count).fill(null);
|
|
645
|
+
const deque = [];
|
|
646
|
+
const better = (a, b) => mode === "max" ? a >= b : a <= b;
|
|
647
|
+
for (let i = 0; i < count; i += 1) {
|
|
648
|
+
const value = values[i];
|
|
649
|
+
while (deque.length > 0 && better(value, values[deque[deque.length - 1]])) {
|
|
650
|
+
deque.pop();
|
|
651
|
+
}
|
|
652
|
+
deque.push(i);
|
|
653
|
+
if (deque[0] <= i - length) {
|
|
654
|
+
deque.shift();
|
|
655
|
+
}
|
|
656
|
+
if (i >= length - 1) {
|
|
657
|
+
result[i] = values[deque[0]];
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
return result;
|
|
661
|
+
};
|
|
662
|
+
var smaFromValues = (values, length) => {
|
|
663
|
+
const result = new Array(values.length).fill(null);
|
|
664
|
+
let sum = 0;
|
|
665
|
+
let valid = 0;
|
|
666
|
+
for (let i = 0; i < values.length; i += 1) {
|
|
667
|
+
const value = values[i];
|
|
668
|
+
if (value != null && Number.isFinite(value)) {
|
|
669
|
+
sum += value;
|
|
670
|
+
valid += 1;
|
|
671
|
+
} else {
|
|
672
|
+
sum = 0;
|
|
673
|
+
valid = 0;
|
|
674
|
+
continue;
|
|
675
|
+
}
|
|
676
|
+
if (valid > length) {
|
|
677
|
+
sum -= values[i - length];
|
|
678
|
+
valid = length;
|
|
679
|
+
}
|
|
680
|
+
if (valid === length) {
|
|
681
|
+
result[i] = sum / length;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
return result;
|
|
685
|
+
};
|
|
686
|
+
var emaFromValues = (values, length) => {
|
|
687
|
+
const result = new Array(values.length).fill(null);
|
|
688
|
+
const alpha = 2 / (length + 1);
|
|
689
|
+
let prev = null;
|
|
690
|
+
let seen = 0;
|
|
691
|
+
for (let i = 0; i < values.length; i += 1) {
|
|
692
|
+
const value = values[i];
|
|
693
|
+
if (value == null || !Number.isFinite(value)) {
|
|
694
|
+
continue;
|
|
695
|
+
}
|
|
696
|
+
prev = prev === null ? value : alpha * value + (1 - alpha) * prev;
|
|
697
|
+
seen += 1;
|
|
698
|
+
if (seen >= length) {
|
|
699
|
+
result[i] = prev;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
return result;
|
|
703
|
+
};
|
|
626
704
|
var drawOverlaySeries = (ctx, renderContext, values, color, width) => {
|
|
627
705
|
if (!renderContext.yFromPrice) return;
|
|
628
706
|
const yFromPrice = renderContext.yFromPrice;
|
|
@@ -1168,6 +1246,960 @@ var BUILTIN_RSI_INDICATOR = {
|
|
|
1168
1246
|
);
|
|
1169
1247
|
}
|
|
1170
1248
|
};
|
|
1249
|
+
var rmaFromValues = (values, length) => {
|
|
1250
|
+
const result = new Array(values.length).fill(null);
|
|
1251
|
+
let prev = null;
|
|
1252
|
+
let seedSum = 0;
|
|
1253
|
+
let seedCount = 0;
|
|
1254
|
+
for (let i = 0; i < values.length; i += 1) {
|
|
1255
|
+
const value = values[i];
|
|
1256
|
+
if (value == null || !Number.isFinite(value)) continue;
|
|
1257
|
+
if (prev === null) {
|
|
1258
|
+
seedSum += value;
|
|
1259
|
+
seedCount += 1;
|
|
1260
|
+
if (seedCount === length) {
|
|
1261
|
+
prev = seedSum / length;
|
|
1262
|
+
result[i] = prev;
|
|
1263
|
+
}
|
|
1264
|
+
continue;
|
|
1265
|
+
}
|
|
1266
|
+
prev = (prev * (length - 1) + value) / length;
|
|
1267
|
+
result[i] = prev;
|
|
1268
|
+
}
|
|
1269
|
+
return result;
|
|
1270
|
+
};
|
|
1271
|
+
var formatCompactNumber = (value) => {
|
|
1272
|
+
const abs = Math.abs(value);
|
|
1273
|
+
if (abs >= 1e9) return `${(value / 1e9).toFixed(2)}B`;
|
|
1274
|
+
if (abs >= 1e6) return `${(value / 1e6).toFixed(2)}M`;
|
|
1275
|
+
if (abs >= 1e3) return `${(value / 1e3).toFixed(1)}K`;
|
|
1276
|
+
return value.toFixed(0);
|
|
1277
|
+
};
|
|
1278
|
+
var drawSeparateMultiSeries = (ctx, renderContext, seriesList, options = {}) => {
|
|
1279
|
+
let min = Number.POSITIVE_INFINITY;
|
|
1280
|
+
let max = Number.NEGATIVE_INFINITY;
|
|
1281
|
+
for (const spec of seriesList) {
|
|
1282
|
+
for (let index = renderContext.startIndex; index <= renderContext.endIndex; index += 1) {
|
|
1283
|
+
const value = spec.values[index];
|
|
1284
|
+
if (value == null || !Number.isFinite(value)) continue;
|
|
1285
|
+
if (value < min) min = value;
|
|
1286
|
+
if (value > max) max = value;
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
if (min > max) return void 0;
|
|
1290
|
+
if (options.includeZero || seriesList.some((spec) => spec.histogram)) {
|
|
1291
|
+
min = Math.min(min, 0);
|
|
1292
|
+
max = Math.max(max, 0);
|
|
1293
|
+
}
|
|
1294
|
+
const minValue = options.minOverride ?? min;
|
|
1295
|
+
const maxValue = options.maxOverride ?? max;
|
|
1296
|
+
const range = maxValue - minValue || 1;
|
|
1297
|
+
const yFromValue = (value) => {
|
|
1298
|
+
const ratio = (value - minValue) / range;
|
|
1299
|
+
return renderContext.chartBottom - ratio * renderContext.chartHeight;
|
|
1300
|
+
};
|
|
1301
|
+
if (options.guideLines && options.guideLines.length > 0) {
|
|
1302
|
+
ctx.save();
|
|
1303
|
+
ctx.strokeStyle = "rgba(148,163,184,0.35)";
|
|
1304
|
+
ctx.lineWidth = 1;
|
|
1305
|
+
ctx.setLineDash([4, 4]);
|
|
1306
|
+
for (const guide of options.guideLines) {
|
|
1307
|
+
const y = yFromValue(guide);
|
|
1308
|
+
ctx.beginPath();
|
|
1309
|
+
ctx.moveTo(renderContext.chartLeft, y);
|
|
1310
|
+
ctx.lineTo(renderContext.chartRight, y);
|
|
1311
|
+
ctx.stroke();
|
|
1312
|
+
}
|
|
1313
|
+
ctx.restore();
|
|
1314
|
+
}
|
|
1315
|
+
for (const spec of seriesList) {
|
|
1316
|
+
if (!spec.histogram) continue;
|
|
1317
|
+
const zeroY = yFromValue(0);
|
|
1318
|
+
const barWidth = Math.max(
|
|
1319
|
+
1,
|
|
1320
|
+
Math.min(Math.max(1, renderContext.candleSpacing - 1), Math.floor(renderContext.candleSpacing * 0.7))
|
|
1321
|
+
);
|
|
1322
|
+
const positive = new Path2D();
|
|
1323
|
+
const negative = new Path2D();
|
|
1324
|
+
for (let index = renderContext.startIndex; index <= renderContext.endIndex; index += 1) {
|
|
1325
|
+
const value = spec.values[index];
|
|
1326
|
+
if (value == null || !Number.isFinite(value)) continue;
|
|
1327
|
+
const x = Math.round(renderContext.xFromIndex(index) - barWidth / 2);
|
|
1328
|
+
const y = yFromValue(value);
|
|
1329
|
+
const top = Math.min(y, zeroY);
|
|
1330
|
+
const height = Math.max(1, Math.abs(y - zeroY));
|
|
1331
|
+
(value >= 0 ? positive : negative).rect(x, top, barWidth, height);
|
|
1332
|
+
}
|
|
1333
|
+
ctx.save();
|
|
1334
|
+
ctx.globalAlpha = 0.85;
|
|
1335
|
+
ctx.fillStyle = spec.color;
|
|
1336
|
+
ctx.fill(positive);
|
|
1337
|
+
ctx.fillStyle = spec.negativeColor ?? spec.color;
|
|
1338
|
+
ctx.fill(negative);
|
|
1339
|
+
ctx.restore();
|
|
1340
|
+
}
|
|
1341
|
+
for (const spec of seriesList) {
|
|
1342
|
+
if (spec.histogram) continue;
|
|
1343
|
+
ctx.save();
|
|
1344
|
+
ctx.strokeStyle = spec.color;
|
|
1345
|
+
ctx.lineWidth = Math.max(1, spec.width ?? 2);
|
|
1346
|
+
ctx.setLineDash([]);
|
|
1347
|
+
let drawing = false;
|
|
1348
|
+
for (let index = renderContext.startIndex; index <= renderContext.endIndex; index += 1) {
|
|
1349
|
+
const value = spec.values[index];
|
|
1350
|
+
if (!Number.isFinite(value ?? Number.NaN)) {
|
|
1351
|
+
drawing = false;
|
|
1352
|
+
continue;
|
|
1353
|
+
}
|
|
1354
|
+
const x = renderContext.xFromIndex(index);
|
|
1355
|
+
const y = yFromValue(value);
|
|
1356
|
+
if (!drawing) {
|
|
1357
|
+
ctx.beginPath();
|
|
1358
|
+
ctx.moveTo(x, y);
|
|
1359
|
+
drawing = true;
|
|
1360
|
+
} else {
|
|
1361
|
+
ctx.lineTo(x, y);
|
|
1362
|
+
}
|
|
1363
|
+
const nextValue = spec.values[index + 1];
|
|
1364
|
+
if (!Number.isFinite(nextValue ?? Number.NaN) || index === renderContext.endIndex) {
|
|
1365
|
+
ctx.stroke();
|
|
1366
|
+
drawing = false;
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
ctx.restore();
|
|
1370
|
+
}
|
|
1371
|
+
const decimals = options.decimals ?? 2;
|
|
1372
|
+
const formatValue = options.format ?? ((value) => value.toFixed(decimals));
|
|
1373
|
+
const latestOf = (values) => {
|
|
1374
|
+
for (let index = values.length - 1; index >= 0; index -= 1) {
|
|
1375
|
+
const value = values[index];
|
|
1376
|
+
if (Number.isFinite(value ?? Number.NaN)) return value;
|
|
1377
|
+
}
|
|
1378
|
+
return null;
|
|
1379
|
+
};
|
|
1380
|
+
const paneInfo = {
|
|
1381
|
+
...options.title ? { title: options.title } : {},
|
|
1382
|
+
axis: {
|
|
1383
|
+
min: minValue,
|
|
1384
|
+
max: maxValue,
|
|
1385
|
+
...options.axisTicks ? { ticks: options.axisTicks } : options.guideLines ? { ticks: options.guideLines } : {},
|
|
1386
|
+
decimals,
|
|
1387
|
+
format: formatValue
|
|
1388
|
+
}
|
|
1389
|
+
};
|
|
1390
|
+
if (options.guideLines) {
|
|
1391
|
+
paneInfo.guideLines = options.guideLines.map((value) => ({
|
|
1392
|
+
value,
|
|
1393
|
+
label: formatValue(value),
|
|
1394
|
+
style: "dotted"
|
|
1395
|
+
}));
|
|
1396
|
+
}
|
|
1397
|
+
const legendValues = [];
|
|
1398
|
+
for (const spec of seriesList) {
|
|
1399
|
+
const latest = latestOf(spec.values);
|
|
1400
|
+
if (latest === null) continue;
|
|
1401
|
+
legendValues.push({
|
|
1402
|
+
...spec.label ? { label: spec.label } : {},
|
|
1403
|
+
value: latest,
|
|
1404
|
+
text: formatValue(latest),
|
|
1405
|
+
color: spec.color
|
|
1406
|
+
});
|
|
1407
|
+
}
|
|
1408
|
+
if (legendValues.length > 0) {
|
|
1409
|
+
paneInfo.legendValues = legendValues;
|
|
1410
|
+
}
|
|
1411
|
+
const labelSpec = seriesList[options.valueLabelSeriesIndex ?? 0];
|
|
1412
|
+
const labelLatest = labelSpec ? latestOf(labelSpec.values) : null;
|
|
1413
|
+
if (labelSpec && labelLatest !== null) {
|
|
1414
|
+
paneInfo.valueLabels = [
|
|
1415
|
+
{
|
|
1416
|
+
value: labelLatest,
|
|
1417
|
+
text: formatValue(labelLatest),
|
|
1418
|
+
color: labelSpec.color,
|
|
1419
|
+
backgroundColor: labelSpec.color,
|
|
1420
|
+
textColor: "#0f172a"
|
|
1421
|
+
}
|
|
1422
|
+
];
|
|
1423
|
+
}
|
|
1424
|
+
return paneInfo;
|
|
1425
|
+
};
|
|
1426
|
+
var computeMacd = (data, fast, slow, signalLength) => {
|
|
1427
|
+
const fastEma = computeEmaSeries(data, fast, "close");
|
|
1428
|
+
const slowEma = computeEmaSeries(data, slow, "close");
|
|
1429
|
+
const macd = fastEma.map((value, idx) => {
|
|
1430
|
+
const slowValue = slowEma[idx];
|
|
1431
|
+
return value == null || slowValue == null ? null : value - slowValue;
|
|
1432
|
+
});
|
|
1433
|
+
const signal = emaFromValues(macd, signalLength);
|
|
1434
|
+
const hist = macd.map((value, idx) => {
|
|
1435
|
+
const signalValue = signal[idx];
|
|
1436
|
+
return value == null || signalValue == null ? null : value - signalValue;
|
|
1437
|
+
});
|
|
1438
|
+
return { macd, signal, hist };
|
|
1439
|
+
};
|
|
1440
|
+
var computeStochastic = (data, kLength, kSmoothing, dLength) => {
|
|
1441
|
+
const highestHigh = rollingExtremeSeries(data.map((point) => point.h), kLength, "max");
|
|
1442
|
+
const lowestLow = rollingExtremeSeries(data.map((point) => point.l), kLength, "min");
|
|
1443
|
+
const rawK = data.map((point, idx) => {
|
|
1444
|
+
const hh = highestHigh[idx];
|
|
1445
|
+
const ll = lowestLow[idx];
|
|
1446
|
+
if (hh == null || ll == null) return null;
|
|
1447
|
+
const range = hh - ll;
|
|
1448
|
+
return range === 0 ? 50 : (point.c - ll) / range * 100;
|
|
1449
|
+
});
|
|
1450
|
+
const k = kSmoothing > 1 ? smaFromValues(rawK, kSmoothing) : rawK;
|
|
1451
|
+
const d = smaFromValues(k, dLength);
|
|
1452
|
+
return { k, d };
|
|
1453
|
+
};
|
|
1454
|
+
var computeStochRsi = (data, rsiLength, stochLength, kSmoothing, dSmoothing) => {
|
|
1455
|
+
const rsi = computeRsiSeries(data, rsiLength);
|
|
1456
|
+
const finiteRsi = rsi.map((value) => value == null ? Number.NaN : value);
|
|
1457
|
+
const highest = rollingExtremeSeries(finiteRsi, stochLength, "max");
|
|
1458
|
+
const lowest = rollingExtremeSeries(finiteRsi, stochLength, "min");
|
|
1459
|
+
const rawK = rsi.map((value, idx) => {
|
|
1460
|
+
const hh = highest[idx];
|
|
1461
|
+
const ll = lowest[idx];
|
|
1462
|
+
if (value == null || hh == null || ll == null || !Number.isFinite(hh) || !Number.isFinite(ll)) return null;
|
|
1463
|
+
const range = hh - ll;
|
|
1464
|
+
return range === 0 ? 50 : (value - ll) / range * 100;
|
|
1465
|
+
});
|
|
1466
|
+
const k = smaFromValues(rawK, Math.max(1, kSmoothing));
|
|
1467
|
+
const d = smaFromValues(k, Math.max(1, dSmoothing));
|
|
1468
|
+
return { k, d };
|
|
1469
|
+
};
|
|
1470
|
+
var computeDmi = (data, length) => {
|
|
1471
|
+
const count = data.length;
|
|
1472
|
+
const plusDm = new Array(count).fill(null);
|
|
1473
|
+
const minusDm = new Array(count).fill(null);
|
|
1474
|
+
const trueRange = new Array(count).fill(null);
|
|
1475
|
+
for (let i = 1; i < count; i += 1) {
|
|
1476
|
+
const point = data[i];
|
|
1477
|
+
const prev = data[i - 1];
|
|
1478
|
+
const upMove = point.h - prev.h;
|
|
1479
|
+
const downMove = prev.l - point.l;
|
|
1480
|
+
plusDm[i] = upMove > downMove && upMove > 0 ? upMove : 0;
|
|
1481
|
+
minusDm[i] = downMove > upMove && downMove > 0 ? downMove : 0;
|
|
1482
|
+
trueRange[i] = Math.max(point.h - point.l, Math.abs(point.h - prev.c), Math.abs(point.l - prev.c));
|
|
1483
|
+
}
|
|
1484
|
+
const smoothPlus = rmaFromValues(plusDm, length);
|
|
1485
|
+
const smoothMinus = rmaFromValues(minusDm, length);
|
|
1486
|
+
const smoothTr = rmaFromValues(trueRange, length);
|
|
1487
|
+
const plusDi = smoothPlus.map((value, idx) => {
|
|
1488
|
+
const tr = smoothTr[idx];
|
|
1489
|
+
return value == null || tr == null || tr === 0 ? null : 100 * value / tr;
|
|
1490
|
+
});
|
|
1491
|
+
const minusDi = smoothMinus.map((value, idx) => {
|
|
1492
|
+
const tr = smoothTr[idx];
|
|
1493
|
+
return value == null || tr == null || tr === 0 ? null : 100 * value / tr;
|
|
1494
|
+
});
|
|
1495
|
+
const dx = plusDi.map((value, idx) => {
|
|
1496
|
+
const minus = minusDi[idx];
|
|
1497
|
+
if (value == null || minus == null) return null;
|
|
1498
|
+
const sum = value + minus;
|
|
1499
|
+
return sum === 0 ? 0 : 100 * Math.abs(value - minus) / sum;
|
|
1500
|
+
});
|
|
1501
|
+
const adx = rmaFromValues(dx, length);
|
|
1502
|
+
return { adx, plusDi, minusDi };
|
|
1503
|
+
};
|
|
1504
|
+
var computeObvSeries = (data) => {
|
|
1505
|
+
const result = new Array(data.length).fill(null);
|
|
1506
|
+
let obv = 0;
|
|
1507
|
+
for (let i = 0; i < data.length; i += 1) {
|
|
1508
|
+
const point = data[i];
|
|
1509
|
+
if (i > 0) {
|
|
1510
|
+
const prev = data[i - 1];
|
|
1511
|
+
const volume = Math.max(0, point.v ?? 0);
|
|
1512
|
+
if (point.c > prev.c) obv += volume;
|
|
1513
|
+
else if (point.c < prev.c) obv -= volume;
|
|
1514
|
+
}
|
|
1515
|
+
result[i] = obv;
|
|
1516
|
+
}
|
|
1517
|
+
return result;
|
|
1518
|
+
};
|
|
1519
|
+
var computeMfiSeries = (data, length) => {
|
|
1520
|
+
const result = new Array(data.length).fill(null);
|
|
1521
|
+
const positiveFlow = new Array(data.length).fill(0);
|
|
1522
|
+
const negativeFlow = new Array(data.length).fill(0);
|
|
1523
|
+
let prevTypical = null;
|
|
1524
|
+
for (let i = 0; i < data.length; i += 1) {
|
|
1525
|
+
const point = data[i];
|
|
1526
|
+
const typical = (point.h + point.l + point.c) / 3;
|
|
1527
|
+
const flow = typical * Math.max(0, point.v ?? 0);
|
|
1528
|
+
if (prevTypical !== null) {
|
|
1529
|
+
if (typical > prevTypical) positiveFlow[i] = flow;
|
|
1530
|
+
else if (typical < prevTypical) negativeFlow[i] = flow;
|
|
1531
|
+
}
|
|
1532
|
+
prevTypical = typical;
|
|
1533
|
+
}
|
|
1534
|
+
let posSum = 0;
|
|
1535
|
+
let negSum = 0;
|
|
1536
|
+
for (let i = 0; i < data.length; i += 1) {
|
|
1537
|
+
posSum += positiveFlow[i];
|
|
1538
|
+
negSum += negativeFlow[i];
|
|
1539
|
+
if (i >= length) {
|
|
1540
|
+
posSum -= positiveFlow[i - length];
|
|
1541
|
+
negSum -= negativeFlow[i - length];
|
|
1542
|
+
}
|
|
1543
|
+
if (i >= length) {
|
|
1544
|
+
result[i] = negSum === 0 ? 100 : 100 - 100 / (1 + posSum / negSum);
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
return result;
|
|
1548
|
+
};
|
|
1549
|
+
var computeCciSeries = (data, length) => {
|
|
1550
|
+
const result = new Array(data.length).fill(null);
|
|
1551
|
+
const typical = data.map((point) => (point.h + point.l + point.c) / 3);
|
|
1552
|
+
const smaTypical = smaFromValues(typical, length);
|
|
1553
|
+
for (let i = length - 1; i < data.length; i += 1) {
|
|
1554
|
+
const mean = smaTypical[i];
|
|
1555
|
+
if (mean == null) continue;
|
|
1556
|
+
let meanDeviation = 0;
|
|
1557
|
+
for (let j = 0; j < length; j += 1) {
|
|
1558
|
+
meanDeviation += Math.abs(typical[i - j] - mean);
|
|
1559
|
+
}
|
|
1560
|
+
meanDeviation /= length;
|
|
1561
|
+
result[i] = meanDeviation === 0 ? 0 : (typical[i] - mean) / (0.015 * meanDeviation);
|
|
1562
|
+
}
|
|
1563
|
+
return result;
|
|
1564
|
+
};
|
|
1565
|
+
var computeWilliamsRSeries = (data, length) => {
|
|
1566
|
+
const highest = rollingExtremeSeries(data.map((point) => point.h), length, "max");
|
|
1567
|
+
const lowest = rollingExtremeSeries(data.map((point) => point.l), length, "min");
|
|
1568
|
+
return data.map((point, idx) => {
|
|
1569
|
+
const hh = highest[idx];
|
|
1570
|
+
const ll = lowest[idx];
|
|
1571
|
+
if (hh == null || ll == null) return null;
|
|
1572
|
+
const range = hh - ll;
|
|
1573
|
+
return range === 0 ? -50 : (hh - point.c) / range * -100;
|
|
1574
|
+
});
|
|
1575
|
+
};
|
|
1576
|
+
var computeRocSeries = (data, length) => {
|
|
1577
|
+
return data.map((point, idx) => {
|
|
1578
|
+
if (idx < length) return null;
|
|
1579
|
+
const past = data[idx - length].c;
|
|
1580
|
+
return past === 0 ? null : 100 * (point.c - past) / past;
|
|
1581
|
+
});
|
|
1582
|
+
};
|
|
1583
|
+
var computeMomentumSeries = (data, length) => {
|
|
1584
|
+
return data.map((point, idx) => idx < length ? null : point.c - data[idx - length].c);
|
|
1585
|
+
};
|
|
1586
|
+
var computePsarSeries = (data, start, increment, maximum) => {
|
|
1587
|
+
const count = data.length;
|
|
1588
|
+
const result = new Array(count).fill(null);
|
|
1589
|
+
if (count < 2) return result;
|
|
1590
|
+
let isUp = data[1].c >= data[0].c;
|
|
1591
|
+
let sar = isUp ? data[0].l : data[0].h;
|
|
1592
|
+
let extremePoint = isUp ? data[0].h : data[0].l;
|
|
1593
|
+
let accelerationFactor = start;
|
|
1594
|
+
for (let i = 1; i < count; i += 1) {
|
|
1595
|
+
const point = data[i];
|
|
1596
|
+
sar += accelerationFactor * (extremePoint - sar);
|
|
1597
|
+
if (isUp) {
|
|
1598
|
+
sar = Math.min(sar, data[i - 1].l, data[Math.max(0, i - 2)].l);
|
|
1599
|
+
if (point.l < sar) {
|
|
1600
|
+
isUp = false;
|
|
1601
|
+
sar = extremePoint;
|
|
1602
|
+
extremePoint = point.l;
|
|
1603
|
+
accelerationFactor = start;
|
|
1604
|
+
} else if (point.h > extremePoint) {
|
|
1605
|
+
extremePoint = point.h;
|
|
1606
|
+
accelerationFactor = Math.min(maximum, accelerationFactor + increment);
|
|
1607
|
+
}
|
|
1608
|
+
} else {
|
|
1609
|
+
sar = Math.max(sar, data[i - 1].h, data[Math.max(0, i - 2)].h);
|
|
1610
|
+
if (point.h > sar) {
|
|
1611
|
+
isUp = true;
|
|
1612
|
+
sar = extremePoint;
|
|
1613
|
+
extremePoint = point.h;
|
|
1614
|
+
accelerationFactor = start;
|
|
1615
|
+
} else if (point.l < extremePoint) {
|
|
1616
|
+
extremePoint = point.l;
|
|
1617
|
+
accelerationFactor = Math.min(maximum, accelerationFactor + increment);
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
result[i] = sar;
|
|
1621
|
+
}
|
|
1622
|
+
return result;
|
|
1623
|
+
};
|
|
1624
|
+
var computeSuperTrend = (data, atrLength, multiplier) => {
|
|
1625
|
+
const count = data.length;
|
|
1626
|
+
const up = new Array(count).fill(null);
|
|
1627
|
+
const down = new Array(count).fill(null);
|
|
1628
|
+
const atr = computeAtrSeries(data, atrLength);
|
|
1629
|
+
let prevUpper = Number.NaN;
|
|
1630
|
+
let prevLower = Number.NaN;
|
|
1631
|
+
let trend = 1;
|
|
1632
|
+
let prevClose = 0;
|
|
1633
|
+
for (let i = 0; i < count; i += 1) {
|
|
1634
|
+
const point = data[i];
|
|
1635
|
+
const atrValue = atr[i];
|
|
1636
|
+
if (atrValue == null) {
|
|
1637
|
+
prevClose = point.c;
|
|
1638
|
+
continue;
|
|
1639
|
+
}
|
|
1640
|
+
const mid = (point.h + point.l) / 2;
|
|
1641
|
+
const basicUpper = mid + multiplier * atrValue;
|
|
1642
|
+
const basicLower = mid - multiplier * atrValue;
|
|
1643
|
+
if (!Number.isFinite(prevUpper)) {
|
|
1644
|
+
prevUpper = basicUpper;
|
|
1645
|
+
prevLower = basicLower;
|
|
1646
|
+
trend = point.c >= mid ? 1 : -1;
|
|
1647
|
+
} else {
|
|
1648
|
+
const finalUpper = basicUpper < prevUpper || prevClose > prevUpper ? basicUpper : prevUpper;
|
|
1649
|
+
const finalLower = basicLower > prevLower || prevClose < prevLower ? basicLower : prevLower;
|
|
1650
|
+
if (trend === 1) {
|
|
1651
|
+
trend = point.c < finalLower ? -1 : 1;
|
|
1652
|
+
} else {
|
|
1653
|
+
trend = point.c > finalUpper ? 1 : -1;
|
|
1654
|
+
}
|
|
1655
|
+
prevUpper = finalUpper;
|
|
1656
|
+
prevLower = finalLower;
|
|
1657
|
+
}
|
|
1658
|
+
if (trend === 1) up[i] = prevLower;
|
|
1659
|
+
else down[i] = prevUpper;
|
|
1660
|
+
prevClose = point.c;
|
|
1661
|
+
}
|
|
1662
|
+
return { up, down };
|
|
1663
|
+
};
|
|
1664
|
+
var computeIchimoku = (data, conversionLength, baseLength, spanBLength, displacement) => {
|
|
1665
|
+
const count = data.length;
|
|
1666
|
+
const highs = data.map((point) => point.h);
|
|
1667
|
+
const lows = data.map((point) => point.l);
|
|
1668
|
+
const midlineOf = (length) => {
|
|
1669
|
+
const highest = rollingExtremeSeries(highs, length, "max");
|
|
1670
|
+
const lowest = rollingExtremeSeries(lows, length, "min");
|
|
1671
|
+
return highest.map((value, idx) => {
|
|
1672
|
+
const low = lowest[idx];
|
|
1673
|
+
return value == null || low == null ? null : (value + low) / 2;
|
|
1674
|
+
});
|
|
1675
|
+
};
|
|
1676
|
+
const tenkan = midlineOf(conversionLength);
|
|
1677
|
+
const kijun = midlineOf(baseLength);
|
|
1678
|
+
const spanARaw = tenkan.map((value, idx) => {
|
|
1679
|
+
const base = kijun[idx];
|
|
1680
|
+
return value == null || base == null ? null : (value + base) / 2;
|
|
1681
|
+
});
|
|
1682
|
+
const spanBRaw = midlineOf(spanBLength);
|
|
1683
|
+
const spanA = new Array(count).fill(null);
|
|
1684
|
+
const spanB = new Array(count).fill(null);
|
|
1685
|
+
const chikou = new Array(count).fill(null);
|
|
1686
|
+
for (let i = 0; i < count; i += 1) {
|
|
1687
|
+
const shifted = i - displacement;
|
|
1688
|
+
if (shifted >= 0) {
|
|
1689
|
+
spanA[i] = spanARaw[shifted] ?? null;
|
|
1690
|
+
spanB[i] = spanBRaw[shifted] ?? null;
|
|
1691
|
+
}
|
|
1692
|
+
const forward = i + displacement;
|
|
1693
|
+
if (forward < count) {
|
|
1694
|
+
chikou[i] = data[forward].c;
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
return { tenkan, kijun, spanA, spanB, chikou };
|
|
1698
|
+
};
|
|
1699
|
+
var computeKeltner = (data, emaLength, atrLength, multiplier) => {
|
|
1700
|
+
const basis = computeEmaSeries(data, emaLength, "close");
|
|
1701
|
+
const atr = computeAtrSeries(data, atrLength);
|
|
1702
|
+
const upper = basis.map((value, idx) => {
|
|
1703
|
+
const atrValue = atr[idx];
|
|
1704
|
+
return value == null || atrValue == null ? null : value + multiplier * atrValue;
|
|
1705
|
+
});
|
|
1706
|
+
const lower = basis.map((value, idx) => {
|
|
1707
|
+
const atrValue = atr[idx];
|
|
1708
|
+
return value == null || atrValue == null ? null : value - multiplier * atrValue;
|
|
1709
|
+
});
|
|
1710
|
+
return { basis, upper, lower };
|
|
1711
|
+
};
|
|
1712
|
+
var computeDonchian = (data, length) => {
|
|
1713
|
+
const upper = rollingExtremeSeries(data.map((point) => point.h), length, "max");
|
|
1714
|
+
const lower = rollingExtremeSeries(data.map((point) => point.l), length, "min");
|
|
1715
|
+
const basis = upper.map((value, idx) => {
|
|
1716
|
+
const low = lower[idx];
|
|
1717
|
+
return value == null || low == null ? null : (value + low) / 2;
|
|
1718
|
+
});
|
|
1719
|
+
return { upper, lower, basis };
|
|
1720
|
+
};
|
|
1721
|
+
var BUILTIN_MACD_INDICATOR = {
|
|
1722
|
+
id: "macd",
|
|
1723
|
+
name: "MACD",
|
|
1724
|
+
pane: "separate",
|
|
1725
|
+
paneHeightRatio: 0.2,
|
|
1726
|
+
defaultInputs: {
|
|
1727
|
+
fast: 12,
|
|
1728
|
+
slow: 26,
|
|
1729
|
+
signal: 9,
|
|
1730
|
+
macdColor: "#2962ff",
|
|
1731
|
+
signalColor: "#ff6d00",
|
|
1732
|
+
histUpColor: "#26a69a",
|
|
1733
|
+
histDownColor: "#ef5350"
|
|
1734
|
+
},
|
|
1735
|
+
draw: (ctx, renderContext, inputs) => {
|
|
1736
|
+
const fast = clampIndicatorLength(inputs.fast, 12);
|
|
1737
|
+
const slow = clampIndicatorLength(inputs.slow, 26);
|
|
1738
|
+
const signalLength = clampIndicatorLength(inputs.signal, 9);
|
|
1739
|
+
const { macd, signal, hist } = withCachedComputation(
|
|
1740
|
+
`macd|${fast}|${slow}|${signalLength}`,
|
|
1741
|
+
renderContext.data,
|
|
1742
|
+
() => computeMacd(renderContext.data, fast, slow, signalLength)
|
|
1743
|
+
);
|
|
1744
|
+
return drawSeparateMultiSeries(
|
|
1745
|
+
ctx,
|
|
1746
|
+
renderContext,
|
|
1747
|
+
[
|
|
1748
|
+
{
|
|
1749
|
+
values: hist,
|
|
1750
|
+
color: inputs.histUpColor ?? "#26a69a",
|
|
1751
|
+
negativeColor: inputs.histDownColor ?? "#ef5350",
|
|
1752
|
+
histogram: true
|
|
1753
|
+
},
|
|
1754
|
+
{ values: macd, color: inputs.macdColor ?? "#2962ff", label: "MACD" },
|
|
1755
|
+
{ values: signal, color: inputs.signalColor ?? "#ff6d00", label: "Signal" }
|
|
1756
|
+
],
|
|
1757
|
+
{
|
|
1758
|
+
title: `MACD ${fast} ${slow} ${signalLength}`,
|
|
1759
|
+
includeZero: true,
|
|
1760
|
+
guideLines: [0],
|
|
1761
|
+
decimals: 2,
|
|
1762
|
+
valueLabelSeriesIndex: 1
|
|
1763
|
+
}
|
|
1764
|
+
);
|
|
1765
|
+
}
|
|
1766
|
+
};
|
|
1767
|
+
var BUILTIN_STOCHASTIC_INDICATOR = {
|
|
1768
|
+
id: "stochastic",
|
|
1769
|
+
name: "Stoch",
|
|
1770
|
+
pane: "separate",
|
|
1771
|
+
paneHeightRatio: 0.18,
|
|
1772
|
+
defaultInputs: { kLength: 14, kSmoothing: 1, dLength: 3, kColor: "#2962ff", dColor: "#ff6d00" },
|
|
1773
|
+
draw: (ctx, renderContext, inputs) => {
|
|
1774
|
+
const kLength = clampIndicatorLength(inputs.kLength, 14);
|
|
1775
|
+
const kSmoothing = clampIndicatorLength(inputs.kSmoothing, 1);
|
|
1776
|
+
const dLength = clampIndicatorLength(inputs.dLength, 3);
|
|
1777
|
+
const { k, d } = withCachedComputation(
|
|
1778
|
+
`stochastic|${kLength}|${kSmoothing}|${dLength}`,
|
|
1779
|
+
renderContext.data,
|
|
1780
|
+
() => computeStochastic(renderContext.data, kLength, kSmoothing, dLength)
|
|
1781
|
+
);
|
|
1782
|
+
return drawSeparateMultiSeries(
|
|
1783
|
+
ctx,
|
|
1784
|
+
renderContext,
|
|
1785
|
+
[
|
|
1786
|
+
{ values: k, color: inputs.kColor ?? "#2962ff", label: "%K" },
|
|
1787
|
+
{ values: d, color: inputs.dColor ?? "#ff6d00", label: "%D" }
|
|
1788
|
+
],
|
|
1789
|
+
{
|
|
1790
|
+
title: `Stoch ${kLength} ${kSmoothing} ${dLength}`,
|
|
1791
|
+
minOverride: 0,
|
|
1792
|
+
maxOverride: 100,
|
|
1793
|
+
guideLines: [20, 80],
|
|
1794
|
+
axisTicks: [0, 20, 50, 80, 100],
|
|
1795
|
+
decimals: 2
|
|
1796
|
+
}
|
|
1797
|
+
);
|
|
1798
|
+
}
|
|
1799
|
+
};
|
|
1800
|
+
var BUILTIN_STOCHRSI_INDICATOR = {
|
|
1801
|
+
id: "stochrsi",
|
|
1802
|
+
name: "Stoch RSI",
|
|
1803
|
+
pane: "separate",
|
|
1804
|
+
paneHeightRatio: 0.18,
|
|
1805
|
+
defaultInputs: { rsiLength: 14, stochLength: 14, kSmoothing: 3, dSmoothing: 3, kColor: "#2962ff", dColor: "#ff6d00" },
|
|
1806
|
+
draw: (ctx, renderContext, inputs) => {
|
|
1807
|
+
const rsiLength = clampIndicatorLength(inputs.rsiLength, 14);
|
|
1808
|
+
const stochLength = clampIndicatorLength(inputs.stochLength, 14);
|
|
1809
|
+
const kSmoothing = clampIndicatorLength(inputs.kSmoothing, 3);
|
|
1810
|
+
const dSmoothing = clampIndicatorLength(inputs.dSmoothing, 3);
|
|
1811
|
+
const { k, d } = withCachedComputation(
|
|
1812
|
+
`stochrsi|${rsiLength}|${stochLength}|${kSmoothing}|${dSmoothing}`,
|
|
1813
|
+
renderContext.data,
|
|
1814
|
+
() => computeStochRsi(renderContext.data, rsiLength, stochLength, kSmoothing, dSmoothing)
|
|
1815
|
+
);
|
|
1816
|
+
return drawSeparateMultiSeries(
|
|
1817
|
+
ctx,
|
|
1818
|
+
renderContext,
|
|
1819
|
+
[
|
|
1820
|
+
{ values: k, color: inputs.kColor ?? "#2962ff", label: "%K" },
|
|
1821
|
+
{ values: d, color: inputs.dColor ?? "#ff6d00", label: "%D" }
|
|
1822
|
+
],
|
|
1823
|
+
{
|
|
1824
|
+
title: `Stoch RSI ${rsiLength} ${stochLength}`,
|
|
1825
|
+
minOverride: 0,
|
|
1826
|
+
maxOverride: 100,
|
|
1827
|
+
guideLines: [20, 80],
|
|
1828
|
+
axisTicks: [0, 20, 50, 80, 100],
|
|
1829
|
+
decimals: 2
|
|
1830
|
+
}
|
|
1831
|
+
);
|
|
1832
|
+
}
|
|
1833
|
+
};
|
|
1834
|
+
var BUILTIN_ADX_INDICATOR = {
|
|
1835
|
+
id: "adx",
|
|
1836
|
+
name: "ADX/DMI",
|
|
1837
|
+
pane: "separate",
|
|
1838
|
+
paneHeightRatio: 0.18,
|
|
1839
|
+
defaultInputs: { length: 14, adxColor: "#ff6d00", plusDiColor: "#26a69a", minusDiColor: "#ef5350", showDi: true },
|
|
1840
|
+
draw: (ctx, renderContext, inputs) => {
|
|
1841
|
+
const length = clampIndicatorLength(inputs.length, 14);
|
|
1842
|
+
const { adx, plusDi, minusDi } = withCachedComputation(
|
|
1843
|
+
`adx|${length}`,
|
|
1844
|
+
renderContext.data,
|
|
1845
|
+
() => computeDmi(renderContext.data, length)
|
|
1846
|
+
);
|
|
1847
|
+
const seriesList = [
|
|
1848
|
+
{ values: adx, color: inputs.adxColor ?? "#ff6d00", label: "ADX" }
|
|
1849
|
+
];
|
|
1850
|
+
if (inputs.showDi !== false) {
|
|
1851
|
+
seriesList.push(
|
|
1852
|
+
{ values: plusDi, color: inputs.plusDiColor ?? "#26a69a", label: "+DI", width: 1 },
|
|
1853
|
+
{ values: minusDi, color: inputs.minusDiColor ?? "#ef5350", label: "-DI", width: 1 }
|
|
1854
|
+
);
|
|
1855
|
+
}
|
|
1856
|
+
return drawSeparateMultiSeries(ctx, renderContext, seriesList, {
|
|
1857
|
+
title: `ADX ${length}`,
|
|
1858
|
+
minOverride: 0,
|
|
1859
|
+
guideLines: [20],
|
|
1860
|
+
decimals: 2
|
|
1861
|
+
});
|
|
1862
|
+
}
|
|
1863
|
+
};
|
|
1864
|
+
var BUILTIN_OBV_INDICATOR = {
|
|
1865
|
+
id: "obv",
|
|
1866
|
+
name: "OBV",
|
|
1867
|
+
pane: "separate",
|
|
1868
|
+
paneHeightRatio: 0.16,
|
|
1869
|
+
defaultInputs: { color: "#2962ff", width: 2 },
|
|
1870
|
+
draw: (ctx, renderContext, inputs) => {
|
|
1871
|
+
const values = withCachedSeries("obv", renderContext.data, () => computeObvSeries(renderContext.data));
|
|
1872
|
+
return drawSeparateMultiSeries(
|
|
1873
|
+
ctx,
|
|
1874
|
+
renderContext,
|
|
1875
|
+
[{ values, color: inputs.color ?? "#2962ff", width: Number(inputs.width) || 2, label: "OBV" }],
|
|
1876
|
+
{ title: "OBV", format: formatCompactNumber }
|
|
1877
|
+
);
|
|
1878
|
+
}
|
|
1879
|
+
};
|
|
1880
|
+
var BUILTIN_MFI_INDICATOR = {
|
|
1881
|
+
id: "mfi",
|
|
1882
|
+
name: "MFI",
|
|
1883
|
+
pane: "separate",
|
|
1884
|
+
paneHeightRatio: 0.16,
|
|
1885
|
+
defaultInputs: { length: 14, color: "#7e57c2", width: 2 },
|
|
1886
|
+
draw: (ctx, renderContext, inputs) => {
|
|
1887
|
+
const length = clampIndicatorLength(inputs.length, 14);
|
|
1888
|
+
const values = withCachedSeries(
|
|
1889
|
+
`mfi|${length}`,
|
|
1890
|
+
renderContext.data,
|
|
1891
|
+
() => computeMfiSeries(renderContext.data, length)
|
|
1892
|
+
);
|
|
1893
|
+
return drawSeparateMultiSeries(
|
|
1894
|
+
ctx,
|
|
1895
|
+
renderContext,
|
|
1896
|
+
[{ values, color: inputs.color ?? "#7e57c2", width: Number(inputs.width) || 2 }],
|
|
1897
|
+
{
|
|
1898
|
+
title: `MFI ${length}`,
|
|
1899
|
+
minOverride: 0,
|
|
1900
|
+
maxOverride: 100,
|
|
1901
|
+
guideLines: [20, 80],
|
|
1902
|
+
axisTicks: [0, 20, 50, 80, 100],
|
|
1903
|
+
decimals: 2
|
|
1904
|
+
}
|
|
1905
|
+
);
|
|
1906
|
+
}
|
|
1907
|
+
};
|
|
1908
|
+
var BUILTIN_CCI_INDICATOR = {
|
|
1909
|
+
id: "cci",
|
|
1910
|
+
name: "CCI",
|
|
1911
|
+
pane: "separate",
|
|
1912
|
+
paneHeightRatio: 0.16,
|
|
1913
|
+
defaultInputs: { length: 20, color: "#2962ff", width: 2 },
|
|
1914
|
+
draw: (ctx, renderContext, inputs) => {
|
|
1915
|
+
const length = clampIndicatorLength(inputs.length, 20);
|
|
1916
|
+
const values = withCachedSeries(
|
|
1917
|
+
`cci|${length}`,
|
|
1918
|
+
renderContext.data,
|
|
1919
|
+
() => computeCciSeries(renderContext.data, length)
|
|
1920
|
+
);
|
|
1921
|
+
return drawSeparateMultiSeries(
|
|
1922
|
+
ctx,
|
|
1923
|
+
renderContext,
|
|
1924
|
+
[{ values, color: inputs.color ?? "#2962ff", width: Number(inputs.width) || 2 }],
|
|
1925
|
+
{ title: `CCI ${length}`, guideLines: [-100, 0, 100], decimals: 2 }
|
|
1926
|
+
);
|
|
1927
|
+
}
|
|
1928
|
+
};
|
|
1929
|
+
var BUILTIN_WILLIAMSR_INDICATOR = {
|
|
1930
|
+
id: "williamsr",
|
|
1931
|
+
name: "Williams %R",
|
|
1932
|
+
pane: "separate",
|
|
1933
|
+
paneHeightRatio: 0.16,
|
|
1934
|
+
defaultInputs: { length: 14, color: "#7e57c2", width: 2 },
|
|
1935
|
+
draw: (ctx, renderContext, inputs) => {
|
|
1936
|
+
const length = clampIndicatorLength(inputs.length, 14);
|
|
1937
|
+
const values = withCachedSeries(
|
|
1938
|
+
`williamsr|${length}`,
|
|
1939
|
+
renderContext.data,
|
|
1940
|
+
() => computeWilliamsRSeries(renderContext.data, length)
|
|
1941
|
+
);
|
|
1942
|
+
return drawSeparateMultiSeries(
|
|
1943
|
+
ctx,
|
|
1944
|
+
renderContext,
|
|
1945
|
+
[{ values, color: inputs.color ?? "#7e57c2", width: Number(inputs.width) || 2 }],
|
|
1946
|
+
{
|
|
1947
|
+
title: `%R ${length}`,
|
|
1948
|
+
minOverride: -100,
|
|
1949
|
+
maxOverride: 0,
|
|
1950
|
+
guideLines: [-80, -20],
|
|
1951
|
+
axisTicks: [-100, -80, -50, -20, 0],
|
|
1952
|
+
decimals: 2
|
|
1953
|
+
}
|
|
1954
|
+
);
|
|
1955
|
+
}
|
|
1956
|
+
};
|
|
1957
|
+
var BUILTIN_ROC_INDICATOR = {
|
|
1958
|
+
id: "roc",
|
|
1959
|
+
name: "ROC",
|
|
1960
|
+
pane: "separate",
|
|
1961
|
+
paneHeightRatio: 0.16,
|
|
1962
|
+
defaultInputs: { length: 9, color: "#2962ff", width: 2 },
|
|
1963
|
+
draw: (ctx, renderContext, inputs) => {
|
|
1964
|
+
const length = clampIndicatorLength(inputs.length, 9);
|
|
1965
|
+
const values = withCachedSeries(
|
|
1966
|
+
`roc|${length}`,
|
|
1967
|
+
renderContext.data,
|
|
1968
|
+
() => computeRocSeries(renderContext.data, length)
|
|
1969
|
+
);
|
|
1970
|
+
return drawSeparateMultiSeries(
|
|
1971
|
+
ctx,
|
|
1972
|
+
renderContext,
|
|
1973
|
+
[{ values, color: inputs.color ?? "#2962ff", width: Number(inputs.width) || 2 }],
|
|
1974
|
+
{ title: `ROC ${length}`, includeZero: true, guideLines: [0], decimals: 2 }
|
|
1975
|
+
);
|
|
1976
|
+
}
|
|
1977
|
+
};
|
|
1978
|
+
var BUILTIN_MOMENTUM_INDICATOR = {
|
|
1979
|
+
id: "momentum",
|
|
1980
|
+
name: "Momentum",
|
|
1981
|
+
pane: "separate",
|
|
1982
|
+
paneHeightRatio: 0.16,
|
|
1983
|
+
defaultInputs: { length: 10, color: "#2962ff", width: 2 },
|
|
1984
|
+
draw: (ctx, renderContext, inputs) => {
|
|
1985
|
+
const length = clampIndicatorLength(inputs.length, 10);
|
|
1986
|
+
const values = withCachedSeries(
|
|
1987
|
+
`momentum|${length}`,
|
|
1988
|
+
renderContext.data,
|
|
1989
|
+
() => computeMomentumSeries(renderContext.data, length)
|
|
1990
|
+
);
|
|
1991
|
+
return drawSeparateMultiSeries(
|
|
1992
|
+
ctx,
|
|
1993
|
+
renderContext,
|
|
1994
|
+
[{ values, color: inputs.color ?? "#2962ff", width: Number(inputs.width) || 2 }],
|
|
1995
|
+
{ title: `Mom ${length}`, includeZero: true, guideLines: [0], decimals: 2 }
|
|
1996
|
+
);
|
|
1997
|
+
}
|
|
1998
|
+
};
|
|
1999
|
+
var BUILTIN_PSAR_INDICATOR = {
|
|
2000
|
+
id: "psar",
|
|
2001
|
+
name: "PSAR",
|
|
2002
|
+
pane: "overlay",
|
|
2003
|
+
defaultInputs: { start: 0.02, increment: 0.02, maximum: 0.2, color: "#2962ff" },
|
|
2004
|
+
draw: (ctx, renderContext, inputs) => {
|
|
2005
|
+
if (!renderContext.yFromPrice) return;
|
|
2006
|
+
const yFromPrice = renderContext.yFromPrice;
|
|
2007
|
+
const start = Math.max(1e-3, Number(inputs.start) || 0.02);
|
|
2008
|
+
const increment = Math.max(1e-3, Number(inputs.increment) || 0.02);
|
|
2009
|
+
const maximum = Math.max(increment, Number(inputs.maximum) || 0.2);
|
|
2010
|
+
const values = withCachedSeries(
|
|
2011
|
+
`psar|${start}|${increment}|${maximum}`,
|
|
2012
|
+
renderContext.data,
|
|
2013
|
+
() => computePsarSeries(renderContext.data, start, increment, maximum)
|
|
2014
|
+
);
|
|
2015
|
+
const radius = Math.max(1, Math.min(2.5, renderContext.candleSpacing * 0.15));
|
|
2016
|
+
const dots = new Path2D();
|
|
2017
|
+
for (let index = renderContext.startIndex; index <= renderContext.endIndex; index += 1) {
|
|
2018
|
+
const value = values[index];
|
|
2019
|
+
if (!Number.isFinite(value ?? Number.NaN)) continue;
|
|
2020
|
+
const x = renderContext.xFromIndex(index);
|
|
2021
|
+
const y = yFromPrice(value);
|
|
2022
|
+
dots.moveTo(x + radius, y);
|
|
2023
|
+
dots.arc(x, y, radius, 0, Math.PI * 2);
|
|
2024
|
+
}
|
|
2025
|
+
ctx.save();
|
|
2026
|
+
ctx.fillStyle = inputs.color ?? "#2962ff";
|
|
2027
|
+
ctx.fill(dots);
|
|
2028
|
+
ctx.restore();
|
|
2029
|
+
},
|
|
2030
|
+
getAutoscaleRange: (data, startIndex, endIndex, inputs, skipIndex) => {
|
|
2031
|
+
const start = Math.max(1e-3, Number(inputs.start) || 0.02);
|
|
2032
|
+
const increment = Math.max(1e-3, Number(inputs.increment) || 0.02);
|
|
2033
|
+
const maximum = Math.max(increment, Number(inputs.maximum) || 0.2);
|
|
2034
|
+
const values = withCachedSeries(
|
|
2035
|
+
`psar|${start}|${increment}|${maximum}`,
|
|
2036
|
+
data,
|
|
2037
|
+
() => computePsarSeries(data, start, increment, maximum)
|
|
2038
|
+
);
|
|
2039
|
+
return rangeOfSeries([values], startIndex, endIndex, skipIndex);
|
|
2040
|
+
}
|
|
2041
|
+
};
|
|
2042
|
+
var BUILTIN_SUPERTREND_INDICATOR = {
|
|
2043
|
+
id: "supertrend",
|
|
2044
|
+
name: "SuperTrend",
|
|
2045
|
+
pane: "overlay",
|
|
2046
|
+
defaultInputs: { atrLength: 10, multiplier: 3, upColor: "#26a69a", downColor: "#ef5350", width: 2 },
|
|
2047
|
+
draw: (ctx, renderContext, inputs) => {
|
|
2048
|
+
const atrLength = clampIndicatorLength(inputs.atrLength, 10);
|
|
2049
|
+
const multiplier = Math.max(0.1, Number(inputs.multiplier) || 3);
|
|
2050
|
+
const { up, down } = withCachedComputation(
|
|
2051
|
+
`supertrend|${atrLength}|${multiplier}`,
|
|
2052
|
+
renderContext.data,
|
|
2053
|
+
() => computeSuperTrend(renderContext.data, atrLength, multiplier)
|
|
2054
|
+
);
|
|
2055
|
+
const width = Number(inputs.width) || 2;
|
|
2056
|
+
drawOverlaySeries(ctx, renderContext, up, inputs.upColor ?? "#26a69a", width);
|
|
2057
|
+
drawOverlaySeries(ctx, renderContext, down, inputs.downColor ?? "#ef5350", width);
|
|
2058
|
+
},
|
|
2059
|
+
getAutoscaleRange: (data, startIndex, endIndex, inputs, skipIndex) => {
|
|
2060
|
+
const atrLength = clampIndicatorLength(inputs.atrLength, 10);
|
|
2061
|
+
const multiplier = Math.max(0.1, Number(inputs.multiplier) || 3);
|
|
2062
|
+
const { up, down } = withCachedComputation(
|
|
2063
|
+
`supertrend|${atrLength}|${multiplier}`,
|
|
2064
|
+
data,
|
|
2065
|
+
() => computeSuperTrend(data, atrLength, multiplier)
|
|
2066
|
+
);
|
|
2067
|
+
return rangeOfSeries([up, down], startIndex, endIndex, skipIndex);
|
|
2068
|
+
}
|
|
2069
|
+
};
|
|
2070
|
+
var BUILTIN_ICHIMOKU_INDICATOR = {
|
|
2071
|
+
id: "ichimoku",
|
|
2072
|
+
name: "Ichimoku",
|
|
2073
|
+
pane: "overlay",
|
|
2074
|
+
defaultInputs: {
|
|
2075
|
+
conversionLength: 9,
|
|
2076
|
+
baseLength: 26,
|
|
2077
|
+
spanBLength: 52,
|
|
2078
|
+
displacement: 26,
|
|
2079
|
+
tenkanColor: "#2962ff",
|
|
2080
|
+
kijunColor: "#b71c1c",
|
|
2081
|
+
spanAColor: "#26a69a",
|
|
2082
|
+
spanBColor: "#ef5350",
|
|
2083
|
+
chikouColor: "#43a047",
|
|
2084
|
+
cloudOpacity: 0.08,
|
|
2085
|
+
showChikou: true
|
|
2086
|
+
},
|
|
2087
|
+
draw: (ctx, renderContext, inputs) => {
|
|
2088
|
+
const conversionLength = clampIndicatorLength(inputs.conversionLength, 9);
|
|
2089
|
+
const baseLength = clampIndicatorLength(inputs.baseLength, 26);
|
|
2090
|
+
const spanBLength = clampIndicatorLength(inputs.spanBLength, 52);
|
|
2091
|
+
const displacement = clampIndicatorLength(inputs.displacement, 26);
|
|
2092
|
+
const { tenkan, kijun, spanA, spanB, chikou } = withCachedComputation(
|
|
2093
|
+
`ichimoku|${conversionLength}|${baseLength}|${spanBLength}|${displacement}`,
|
|
2094
|
+
renderContext.data,
|
|
2095
|
+
() => computeIchimoku(renderContext.data, conversionLength, baseLength, spanBLength, displacement)
|
|
2096
|
+
);
|
|
2097
|
+
const cloudOpacity = Math.min(0.5, Math.max(0, Number(inputs.cloudOpacity) || 0.08));
|
|
2098
|
+
const bullishA = spanA.map((value, idx) => {
|
|
2099
|
+
const other = spanB[idx];
|
|
2100
|
+
return value != null && other != null && value >= other ? value : null;
|
|
2101
|
+
});
|
|
2102
|
+
const bullishB = spanB.map((value, idx) => {
|
|
2103
|
+
const other = spanA[idx];
|
|
2104
|
+
return value != null && other != null && other >= value ? value : null;
|
|
2105
|
+
});
|
|
2106
|
+
const bearishA = spanA.map((value, idx) => {
|
|
2107
|
+
const other = spanB[idx];
|
|
2108
|
+
return value != null && other != null && value < other ? value : null;
|
|
2109
|
+
});
|
|
2110
|
+
const bearishB = spanB.map((value, idx) => {
|
|
2111
|
+
const other = spanA[idx];
|
|
2112
|
+
return value != null && other != null && other < value ? value : null;
|
|
2113
|
+
});
|
|
2114
|
+
fillBetweenSeries(ctx, renderContext, bullishA, bullishB, inputs.spanAColor ?? "#26a69a", cloudOpacity);
|
|
2115
|
+
fillBetweenSeries(ctx, renderContext, bearishB, bearishA, inputs.spanBColor ?? "#ef5350", cloudOpacity);
|
|
2116
|
+
drawOverlaySeries(ctx, renderContext, spanA, inputs.spanAColor ?? "#26a69a", 1);
|
|
2117
|
+
drawOverlaySeries(ctx, renderContext, spanB, inputs.spanBColor ?? "#ef5350", 1);
|
|
2118
|
+
drawOverlaySeries(ctx, renderContext, tenkan, inputs.tenkanColor ?? "#2962ff", 1.5);
|
|
2119
|
+
drawOverlaySeries(ctx, renderContext, kijun, inputs.kijunColor ?? "#b71c1c", 1.5);
|
|
2120
|
+
if (inputs.showChikou !== false) {
|
|
2121
|
+
drawOverlaySeries(ctx, renderContext, chikou, inputs.chikouColor ?? "#43a047", 1);
|
|
2122
|
+
}
|
|
2123
|
+
},
|
|
2124
|
+
getAutoscaleRange: (data, startIndex, endIndex, inputs, skipIndex) => {
|
|
2125
|
+
const conversionLength = clampIndicatorLength(inputs.conversionLength, 9);
|
|
2126
|
+
const baseLength = clampIndicatorLength(inputs.baseLength, 26);
|
|
2127
|
+
const spanBLength = clampIndicatorLength(inputs.spanBLength, 52);
|
|
2128
|
+
const displacement = clampIndicatorLength(inputs.displacement, 26);
|
|
2129
|
+
const { tenkan, kijun, spanA, spanB } = withCachedComputation(
|
|
2130
|
+
`ichimoku|${conversionLength}|${baseLength}|${spanBLength}|${displacement}`,
|
|
2131
|
+
data,
|
|
2132
|
+
() => computeIchimoku(data, conversionLength, baseLength, spanBLength, displacement)
|
|
2133
|
+
);
|
|
2134
|
+
return rangeOfSeries([tenkan, kijun, spanA, spanB], startIndex, endIndex, skipIndex);
|
|
2135
|
+
}
|
|
2136
|
+
};
|
|
2137
|
+
var BUILTIN_KELTNER_INDICATOR = {
|
|
2138
|
+
id: "keltner",
|
|
2139
|
+
name: "KC",
|
|
2140
|
+
pane: "overlay",
|
|
2141
|
+
defaultInputs: {
|
|
2142
|
+
emaLength: 20,
|
|
2143
|
+
atrLength: 10,
|
|
2144
|
+
multiplier: 2,
|
|
2145
|
+
basisColor: "#2962ff",
|
|
2146
|
+
bandColor: "#2962ff",
|
|
2147
|
+
width: 1.5,
|
|
2148
|
+
fillOpacity: 0.04
|
|
2149
|
+
},
|
|
2150
|
+
draw: (ctx, renderContext, inputs) => {
|
|
2151
|
+
const emaLength = clampIndicatorLength(inputs.emaLength, 20);
|
|
2152
|
+
const atrLength = clampIndicatorLength(inputs.atrLength, 10);
|
|
2153
|
+
const multiplier = Math.max(0.1, Number(inputs.multiplier) || 2);
|
|
2154
|
+
const { basis, upper, lower } = withCachedComputation(
|
|
2155
|
+
`keltner|${emaLength}|${atrLength}|${multiplier}`,
|
|
2156
|
+
renderContext.data,
|
|
2157
|
+
() => computeKeltner(renderContext.data, emaLength, atrLength, multiplier)
|
|
2158
|
+
);
|
|
2159
|
+
const bandColor = inputs.bandColor ?? "#2962ff";
|
|
2160
|
+
const width = Number(inputs.width) || 1.5;
|
|
2161
|
+
fillBetweenSeries(ctx, renderContext, upper, lower, bandColor, Number(inputs.fillOpacity) || 0.04);
|
|
2162
|
+
drawOverlaySeries(ctx, renderContext, upper, bandColor, width);
|
|
2163
|
+
drawOverlaySeries(ctx, renderContext, lower, bandColor, width);
|
|
2164
|
+
drawOverlaySeries(ctx, renderContext, basis, inputs.basisColor ?? "#2962ff", width);
|
|
2165
|
+
},
|
|
2166
|
+
getAutoscaleRange: (data, startIndex, endIndex, inputs, skipIndex) => {
|
|
2167
|
+
const emaLength = clampIndicatorLength(inputs.emaLength, 20);
|
|
2168
|
+
const atrLength = clampIndicatorLength(inputs.atrLength, 10);
|
|
2169
|
+
const multiplier = Math.max(0.1, Number(inputs.multiplier) || 2);
|
|
2170
|
+
const { upper, lower } = withCachedComputation(
|
|
2171
|
+
`keltner|${emaLength}|${atrLength}|${multiplier}`,
|
|
2172
|
+
data,
|
|
2173
|
+
() => computeKeltner(data, emaLength, atrLength, multiplier)
|
|
2174
|
+
);
|
|
2175
|
+
return rangeOfSeries([upper, lower], startIndex, endIndex, skipIndex);
|
|
2176
|
+
}
|
|
2177
|
+
};
|
|
2178
|
+
var BUILTIN_DONCHIAN_INDICATOR = {
|
|
2179
|
+
id: "donchian",
|
|
2180
|
+
name: "DC",
|
|
2181
|
+
pane: "overlay",
|
|
2182
|
+
defaultInputs: { length: 20, bandColor: "#2962ff", basisColor: "#ff6d00", width: 1.5, fillOpacity: 0.04 },
|
|
2183
|
+
draw: (ctx, renderContext, inputs) => {
|
|
2184
|
+
const length = clampIndicatorLength(inputs.length, 20);
|
|
2185
|
+
const { upper, lower, basis } = withCachedComputation(
|
|
2186
|
+
`donchian|${length}`,
|
|
2187
|
+
renderContext.data,
|
|
2188
|
+
() => computeDonchian(renderContext.data, length)
|
|
2189
|
+
);
|
|
2190
|
+
const bandColor = inputs.bandColor ?? "#2962ff";
|
|
2191
|
+
const width = Number(inputs.width) || 1.5;
|
|
2192
|
+
fillBetweenSeries(ctx, renderContext, upper, lower, bandColor, Number(inputs.fillOpacity) || 0.04);
|
|
2193
|
+
drawOverlaySeries(ctx, renderContext, upper, bandColor, width);
|
|
2194
|
+
drawOverlaySeries(ctx, renderContext, lower, bandColor, width);
|
|
2195
|
+
drawOverlaySeries(ctx, renderContext, basis, inputs.basisColor ?? "#ff6d00", width);
|
|
2196
|
+
},
|
|
2197
|
+
getAutoscaleRange: (data, startIndex, endIndex, inputs, skipIndex) => {
|
|
2198
|
+
const length = clampIndicatorLength(inputs.length, 20);
|
|
2199
|
+
const { upper, lower } = withCachedComputation(`donchian|${length}`, data, () => computeDonchian(data, length));
|
|
2200
|
+
return rangeOfSeries([upper, lower], startIndex, endIndex, skipIndex);
|
|
2201
|
+
}
|
|
2202
|
+
};
|
|
1171
2203
|
var BUILTIN_INDICATORS = [
|
|
1172
2204
|
BUILTIN_VOLUME_INDICATOR,
|
|
1173
2205
|
BUILTIN_SMA_INDICATOR,
|
|
@@ -1180,7 +2212,22 @@ var BUILTIN_INDICATORS = [
|
|
|
1180
2212
|
BUILTIN_VWAP_INDICATOR,
|
|
1181
2213
|
BUILTIN_BOLLINGER_INDICATOR,
|
|
1182
2214
|
BUILTIN_STDDEV_INDICATOR,
|
|
1183
|
-
BUILTIN_ATR_INDICATOR
|
|
2215
|
+
BUILTIN_ATR_INDICATOR,
|
|
2216
|
+
BUILTIN_MACD_INDICATOR,
|
|
2217
|
+
BUILTIN_STOCHASTIC_INDICATOR,
|
|
2218
|
+
BUILTIN_STOCHRSI_INDICATOR,
|
|
2219
|
+
BUILTIN_ADX_INDICATOR,
|
|
2220
|
+
BUILTIN_OBV_INDICATOR,
|
|
2221
|
+
BUILTIN_MFI_INDICATOR,
|
|
2222
|
+
BUILTIN_CCI_INDICATOR,
|
|
2223
|
+
BUILTIN_WILLIAMSR_INDICATOR,
|
|
2224
|
+
BUILTIN_ROC_INDICATOR,
|
|
2225
|
+
BUILTIN_MOMENTUM_INDICATOR,
|
|
2226
|
+
BUILTIN_PSAR_INDICATOR,
|
|
2227
|
+
BUILTIN_SUPERTREND_INDICATOR,
|
|
2228
|
+
BUILTIN_ICHIMOKU_INDICATOR,
|
|
2229
|
+
BUILTIN_KELTNER_INDICATOR,
|
|
2230
|
+
BUILTIN_DONCHIAN_INDICATOR
|
|
1184
2231
|
];
|
|
1185
2232
|
function createChart(element, options = {}) {
|
|
1186
2233
|
let mergedOptions = mergeChartOptions(DEFAULT_OPTIONS, options);
|
|
@@ -1904,6 +2951,36 @@ function createChart(element, options = {}) {
|
|
|
1904
2951
|
}
|
|
1905
2952
|
return direction > 0 ? "up" : "down";
|
|
1906
2953
|
};
|
|
2954
|
+
let heikinAshiCache = null;
|
|
2955
|
+
let heikinAshiFingerprint = "";
|
|
2956
|
+
const getHeikinAshiData = () => {
|
|
2957
|
+
const last = data[data.length - 1];
|
|
2958
|
+
const fingerprint = !last ? "empty" : `${data.length}|${last.time.getTime()}|${last.o}|${last.h}|${last.l}|${last.c}`;
|
|
2959
|
+
if (heikinAshiCache && heikinAshiFingerprint === fingerprint) {
|
|
2960
|
+
return heikinAshiCache;
|
|
2961
|
+
}
|
|
2962
|
+
const result = new Array(data.length);
|
|
2963
|
+
let prevOpen = 0;
|
|
2964
|
+
let prevClose = 0;
|
|
2965
|
+
for (let i = 0; i < data.length; i += 1) {
|
|
2966
|
+
const point = data[i];
|
|
2967
|
+
const haClose = (point.o + point.h + point.l + point.c) / 4;
|
|
2968
|
+
const haOpen = i === 0 ? (point.o + point.c) / 2 : (prevOpen + prevClose) / 2;
|
|
2969
|
+
result[i] = {
|
|
2970
|
+
time: point.time,
|
|
2971
|
+
o: haOpen,
|
|
2972
|
+
h: Math.max(point.h, haOpen, haClose),
|
|
2973
|
+
l: Math.min(point.l, haOpen, haClose),
|
|
2974
|
+
c: haClose,
|
|
2975
|
+
...point.v === void 0 ? {} : { v: point.v }
|
|
2976
|
+
};
|
|
2977
|
+
prevOpen = haOpen;
|
|
2978
|
+
prevClose = haClose;
|
|
2979
|
+
}
|
|
2980
|
+
heikinAshiCache = result;
|
|
2981
|
+
heikinAshiFingerprint = fingerprint;
|
|
2982
|
+
return result;
|
|
2983
|
+
};
|
|
1907
2984
|
const formatHoverTimeLabel = (time, mode) => {
|
|
1908
2985
|
if (mode === "time") {
|
|
1909
2986
|
return time.toLocaleTimeString(void 0, {
|
|
@@ -2481,23 +3558,34 @@ function createChart(element, options = {}) {
|
|
|
2481
3558
|
const xEnd = xStart + xSpan;
|
|
2482
3559
|
const startIndex = Math.max(0, Math.floor(xStart));
|
|
2483
3560
|
const endIndex = Math.min(data.length - 1, Math.ceil(xEnd) - 1);
|
|
3561
|
+
const chartType = mergedOptions.chartType;
|
|
3562
|
+
const seriesData = chartType === "heikin-ashi" ? getHeikinAshiData() : data;
|
|
3563
|
+
const isLineStyleChart = chartType === "line" || chartType === "area" || chartType === "baseline";
|
|
2484
3564
|
const skipLatestIndex = mergedOptions.autoScaleIgnoreLatestCandle && data.length > 1 ? data.length - 1 : -1;
|
|
2485
3565
|
const scanCandleRange = (from, to) => {
|
|
2486
3566
|
let min = Number.POSITIVE_INFINITY;
|
|
2487
3567
|
let max = Number.NEGATIVE_INFINITY;
|
|
2488
3568
|
for (let idx = from; idx <= to; idx += 1) {
|
|
2489
3569
|
if (idx === skipLatestIndex) continue;
|
|
2490
|
-
const point =
|
|
3570
|
+
const point = seriesData[idx];
|
|
2491
3571
|
if (!point) continue;
|
|
2492
|
-
if (
|
|
2493
|
-
|
|
3572
|
+
if (isLineStyleChart) {
|
|
3573
|
+
if (point.c < min) min = point.c;
|
|
3574
|
+
if (point.c > max) max = point.c;
|
|
3575
|
+
} else {
|
|
3576
|
+
if (point.l < min) min = point.l;
|
|
3577
|
+
if (point.h > max) max = point.h;
|
|
3578
|
+
}
|
|
2494
3579
|
}
|
|
2495
3580
|
return min <= max ? { min, max } : null;
|
|
2496
3581
|
};
|
|
2497
3582
|
let candleRange = scanCandleRange(startIndex, endIndex);
|
|
2498
3583
|
if (!candleRange) {
|
|
2499
3584
|
const latestIndex = data.length - 1;
|
|
2500
|
-
candleRange = scanCandleRange(Math.max(0, latestIndex - 120), latestIndex - 1) ?? scanCandleRange(0, latestIndex) ?? {
|
|
3585
|
+
candleRange = scanCandleRange(Math.max(0, latestIndex - 120), latestIndex - 1) ?? scanCandleRange(0, latestIndex) ?? {
|
|
3586
|
+
min: isLineStyleChart ? seriesData[latestIndex].c : seriesData[latestIndex].l,
|
|
3587
|
+
max: isLineStyleChart ? seriesData[latestIndex].c : seriesData[latestIndex].h
|
|
3588
|
+
};
|
|
2501
3589
|
}
|
|
2502
3590
|
let minPrice = candleRange.min;
|
|
2503
3591
|
let maxPrice = candleRange.max;
|
|
@@ -3393,23 +4481,137 @@ function createChart(element, options = {}) {
|
|
|
3393
4481
|
}
|
|
3394
4482
|
return data[index]?.v;
|
|
3395
4483
|
};
|
|
3396
|
-
{
|
|
4484
|
+
if (isLineStyleChart) {
|
|
4485
|
+
const seriesLineWidth = Math.max(1, mergedOptions.lineWidth);
|
|
4486
|
+
const linePath = new Path2D();
|
|
4487
|
+
let started = false;
|
|
4488
|
+
let firstX = 0;
|
|
4489
|
+
let lastX = 0;
|
|
4490
|
+
for (let index = startIndex; index <= endIndex; index += 1) {
|
|
4491
|
+
const point = seriesData[index];
|
|
4492
|
+
if (!point) continue;
|
|
4493
|
+
const close = useSmoothedCandle && index === lastDataIndex ? smoothedTickerPrice : point.c;
|
|
4494
|
+
const x = chartLeft + (index + 0.5 - xStart) / xSpan * chartWidth;
|
|
4495
|
+
const y = yFromPrice(close);
|
|
4496
|
+
if (!started) {
|
|
4497
|
+
linePath.moveTo(x, y);
|
|
4498
|
+
firstX = x;
|
|
4499
|
+
started = true;
|
|
4500
|
+
} else {
|
|
4501
|
+
linePath.lineTo(x, y);
|
|
4502
|
+
}
|
|
4503
|
+
lastX = x;
|
|
4504
|
+
}
|
|
4505
|
+
if (started) {
|
|
4506
|
+
if (chartType === "baseline") {
|
|
4507
|
+
const baselinePrice = mergedOptions.baselinePrice ?? (yMin + yMax) / 2;
|
|
4508
|
+
const baselineY = yFromPrice(baselinePrice);
|
|
4509
|
+
const fillPath = new Path2D(linePath);
|
|
4510
|
+
fillPath.lineTo(lastX, baselineY);
|
|
4511
|
+
fillPath.lineTo(firstX, baselineY);
|
|
4512
|
+
fillPath.closePath();
|
|
4513
|
+
const halves = [
|
|
4514
|
+
{ clipTop: chartTop, clipBottom: baselineY, color: mergedOptions.upColor },
|
|
4515
|
+
{ clipTop: baselineY, clipBottom: chartBottom, color: mergedOptions.downColor }
|
|
4516
|
+
];
|
|
4517
|
+
for (const half of halves) {
|
|
4518
|
+
const clipHeight = half.clipBottom - half.clipTop;
|
|
4519
|
+
if (clipHeight <= 0) continue;
|
|
4520
|
+
ctx.save();
|
|
4521
|
+
ctx.beginPath();
|
|
4522
|
+
ctx.rect(chartLeft, half.clipTop, chartWidth, clipHeight);
|
|
4523
|
+
ctx.clip();
|
|
4524
|
+
ctx.globalAlpha = Math.max(0, Math.min(1, mergedOptions.areaFillOpacity));
|
|
4525
|
+
ctx.fillStyle = half.color;
|
|
4526
|
+
ctx.fill(fillPath);
|
|
4527
|
+
ctx.globalAlpha = 1;
|
|
4528
|
+
ctx.strokeStyle = half.color;
|
|
4529
|
+
ctx.lineWidth = seriesLineWidth;
|
|
4530
|
+
ctx.lineJoin = "round";
|
|
4531
|
+
ctx.stroke(linePath);
|
|
4532
|
+
ctx.restore();
|
|
4533
|
+
}
|
|
4534
|
+
ctx.save();
|
|
4535
|
+
ctx.strokeStyle = "rgba(148,163,184,0.5)";
|
|
4536
|
+
ctx.lineWidth = 1;
|
|
4537
|
+
ctx.setLineDash([4, 4]);
|
|
4538
|
+
ctx.beginPath();
|
|
4539
|
+
ctx.moveTo(chartLeft, crisp(baselineY));
|
|
4540
|
+
ctx.lineTo(chartRight, crisp(baselineY));
|
|
4541
|
+
ctx.stroke();
|
|
4542
|
+
ctx.restore();
|
|
4543
|
+
} else {
|
|
4544
|
+
if (chartType === "area") {
|
|
4545
|
+
const fillPath = new Path2D(linePath);
|
|
4546
|
+
fillPath.lineTo(lastX, chartBottom);
|
|
4547
|
+
fillPath.lineTo(firstX, chartBottom);
|
|
4548
|
+
fillPath.closePath();
|
|
4549
|
+
ctx.save();
|
|
4550
|
+
ctx.globalAlpha = Math.max(0, Math.min(1, mergedOptions.areaFillOpacity));
|
|
4551
|
+
ctx.fillStyle = mergedOptions.lineColor;
|
|
4552
|
+
ctx.fill(fillPath);
|
|
4553
|
+
ctx.restore();
|
|
4554
|
+
}
|
|
4555
|
+
ctx.save();
|
|
4556
|
+
ctx.strokeStyle = mergedOptions.lineColor;
|
|
4557
|
+
ctx.lineWidth = seriesLineWidth;
|
|
4558
|
+
ctx.lineJoin = "round";
|
|
4559
|
+
ctx.stroke(linePath);
|
|
4560
|
+
ctx.restore();
|
|
4561
|
+
}
|
|
4562
|
+
}
|
|
4563
|
+
} else if (chartType === "bars") {
|
|
4564
|
+
const upBars = new Path2D();
|
|
4565
|
+
const downBars = new Path2D();
|
|
4566
|
+
const tickLength = Math.max(2, Math.floor(bodyWidth / 2));
|
|
4567
|
+
for (let index = startIndex; index <= endIndex; index += 1) {
|
|
4568
|
+
const point = seriesData[index];
|
|
4569
|
+
if (!point) continue;
|
|
4570
|
+
const isLastCandle = useSmoothedCandle && index === lastDataIndex;
|
|
4571
|
+
const actualDirection = point.c >= point.o ? "up" : "down";
|
|
4572
|
+
const displayClose = isLastCandle ? actualDirection === "up" ? Math.max(point.o, smoothedTickerPrice) : Math.min(point.o, smoothedTickerPrice) : point.c;
|
|
4573
|
+
const displayHigh = isLastCandle && actualDirection === "up" ? Math.max(point.h, displayClose) : point.h;
|
|
4574
|
+
const displayLow = isLastCandle && actualDirection === "down" ? Math.min(point.l, displayClose) : point.l;
|
|
4575
|
+
const roundedCenterX = Math.round(chartLeft + (index + 0.5 - xStart) / xSpan * chartWidth);
|
|
4576
|
+
const direction = isLastCandle ? actualDirection : getCandleDirectionByIndex(index);
|
|
4577
|
+
const path = direction === "up" ? upBars : downBars;
|
|
4578
|
+
const centerX = roundedCenterX + 0.5;
|
|
4579
|
+
path.moveTo(centerX, crisp(yFromPrice(displayHigh)));
|
|
4580
|
+
path.lineTo(centerX, crisp(yFromPrice(displayLow)));
|
|
4581
|
+
const openY = crisp(yFromPrice(point.o));
|
|
4582
|
+
const closeY = crisp(yFromPrice(displayClose));
|
|
4583
|
+
path.moveTo(centerX - tickLength, openY);
|
|
4584
|
+
path.lineTo(centerX, openY);
|
|
4585
|
+
path.moveTo(centerX, closeY);
|
|
4586
|
+
path.lineTo(centerX + tickLength, closeY);
|
|
4587
|
+
}
|
|
4588
|
+
ctx.lineWidth = Math.max(1, candleWickWidth);
|
|
4589
|
+
ctx.strokeStyle = mergedOptions.upColor;
|
|
4590
|
+
ctx.stroke(upBars);
|
|
4591
|
+
ctx.strokeStyle = mergedOptions.downColor;
|
|
4592
|
+
ctx.stroke(downBars);
|
|
4593
|
+
} else {
|
|
4594
|
+
const isHeikinAshi = chartType === "heikin-ashi";
|
|
4595
|
+
const isHollow = chartType === "hollow-candles";
|
|
4596
|
+
const smoothLastCandle = useSmoothedCandle && !isHeikinAshi;
|
|
3397
4597
|
const upWicks = new Path2D();
|
|
3398
4598
|
const downWicks = new Path2D();
|
|
3399
4599
|
const upBodies = new Path2D();
|
|
3400
4600
|
const downBodies = new Path2D();
|
|
4601
|
+
const upHollowBodies = new Path2D();
|
|
4602
|
+
const downHollowBodies = new Path2D();
|
|
3401
4603
|
for (let index = startIndex; index <= endIndex; index += 1) {
|
|
3402
|
-
const point =
|
|
4604
|
+
const point = seriesData[index];
|
|
3403
4605
|
if (!point) {
|
|
3404
4606
|
continue;
|
|
3405
4607
|
}
|
|
3406
|
-
const isLastCandle =
|
|
4608
|
+
const isLastCandle = smoothLastCandle && index === lastDataIndex;
|
|
3407
4609
|
const actualDirection = point.c >= point.o ? "up" : "down";
|
|
3408
4610
|
const displayClose = isLastCandle ? actualDirection === "up" ? Math.max(point.o, smoothedTickerPrice) : Math.min(point.o, smoothedTickerPrice) : point.c;
|
|
3409
4611
|
const displayHigh = isLastCandle ? actualDirection === "up" ? Math.max(point.h, displayClose) : point.h : point.h;
|
|
3410
4612
|
const displayLow = isLastCandle ? actualDirection === "up" ? point.l : Math.min(point.l, displayClose) : point.l;
|
|
3411
4613
|
const centerX = chartLeft + (index + 0.5 - xStart) / xSpan * chartWidth;
|
|
3412
|
-
const direction = isLastCandle ? actualDirection : getCandleDirectionByIndex(index);
|
|
4614
|
+
const direction = isHeikinAshi || isLastCandle ? actualDirection : getCandleDirectionByIndex(index);
|
|
3413
4615
|
const isUp = direction === "up";
|
|
3414
4616
|
const roundedCenterX = Math.round(centerX);
|
|
3415
4617
|
const wicks = isUp ? upWicks : downWicks;
|
|
@@ -3421,7 +4623,9 @@ function createChart(element, options = {}) {
|
|
|
3421
4623
|
const bodyIsUp = displayClose >= point.o;
|
|
3422
4624
|
const bodyTop = bodyIsUp ? Math.min(closeYPx, openYPx - 1) : openYPx;
|
|
3423
4625
|
const bodyBottom = bodyIsUp ? openYPx : Math.max(closeYPx, openYPx + 1);
|
|
3424
|
-
|
|
4626
|
+
const useHollowBody = isHollow && bodyIsUp;
|
|
4627
|
+
const bodies = useHollowBody ? isUp ? upHollowBodies : downHollowBodies : isUp ? upBodies : downBodies;
|
|
4628
|
+
bodies.rect(bodyLeft, bodyTop, bodyWidth, bodyBottom - bodyTop);
|
|
3425
4629
|
}
|
|
3426
4630
|
ctx.lineWidth = candleWickWidth;
|
|
3427
4631
|
ctx.strokeStyle = mergedOptions.upColor;
|
|
@@ -3432,6 +4636,16 @@ function createChart(element, options = {}) {
|
|
|
3432
4636
|
ctx.stroke(downWicks);
|
|
3433
4637
|
ctx.fillStyle = mergedOptions.downColor;
|
|
3434
4638
|
ctx.fill(downBodies);
|
|
4639
|
+
if (isHollow) {
|
|
4640
|
+
ctx.fillStyle = mergedOptions.backgroundColor;
|
|
4641
|
+
ctx.fill(upHollowBodies);
|
|
4642
|
+
ctx.fill(downHollowBodies);
|
|
4643
|
+
ctx.lineWidth = 1;
|
|
4644
|
+
ctx.strokeStyle = mergedOptions.upColor;
|
|
4645
|
+
ctx.stroke(upHollowBodies);
|
|
4646
|
+
ctx.strokeStyle = mergedOptions.downColor;
|
|
4647
|
+
ctx.stroke(downHollowBodies);
|
|
4648
|
+
}
|
|
3435
4649
|
}
|
|
3436
4650
|
const activeOverlayIndicators = indicators.filter((indicator) => indicator.visible).map((indicator) => ({ indicator, plugin: indicatorRegistry.get(indicator.type) })).filter(
|
|
3437
4651
|
(value) => value.plugin !== void 0 && (value.indicator.pane ?? value.plugin.pane ?? "overlay") === "overlay"
|
|
@@ -5728,6 +6942,9 @@ function createChart(element, options = {}) {
|
|
|
5728
6942
|
return;
|
|
5729
6943
|
}
|
|
5730
6944
|
const point = getCanvasPoint(event);
|
|
6945
|
+
if (getCrosshairPriceActionRegion(point.x, point.y) || getOrderActionRegion(point.x, point.y)) {
|
|
6946
|
+
return;
|
|
6947
|
+
}
|
|
5731
6948
|
const region = getHitRegion(point.x, point.y);
|
|
5732
6949
|
if (region === "outside") {
|
|
5733
6950
|
return;
|
|
@@ -5824,6 +7041,14 @@ function createChart(element, options = {}) {
|
|
|
5824
7041
|
}
|
|
5825
7042
|
scheduleDraw();
|
|
5826
7043
|
};
|
|
7044
|
+
const setChartType = (type) => {
|
|
7045
|
+
if (mergedOptions.chartType === type) {
|
|
7046
|
+
return;
|
|
7047
|
+
}
|
|
7048
|
+
mergedOptions = { ...mergedOptions, chartType: type };
|
|
7049
|
+
scheduleDraw({ updateAutoScale: true });
|
|
7050
|
+
};
|
|
7051
|
+
const getChartType = () => mergedOptions.chartType;
|
|
5827
7052
|
const resize = (nextWidth, nextHeight) => {
|
|
5828
7053
|
if (nextWidth && nextWidth > 0) {
|
|
5829
7054
|
width = nextWidth;
|
|
@@ -6181,6 +7406,8 @@ function createChart(element, options = {}) {
|
|
|
6181
7406
|
draw();
|
|
6182
7407
|
return {
|
|
6183
7408
|
updateOptions,
|
|
7409
|
+
setChartType,
|
|
7410
|
+
getChartType,
|
|
6184
7411
|
setData,
|
|
6185
7412
|
upsertBar,
|
|
6186
7413
|
setPriceLines,
|