hyperprop-charting-library 0.1.70 → 0.1.72

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.
@@ -2113,6 +2113,7 @@ function createChart(element, options = {}) {
2113
2113
  };
2114
2114
  const xFromDrawingPoint = (point) => chartLeft + (point.index + 0.5 - xStart) / xSpan * chartWidth;
2115
2115
  const FIB_RATIOS = [0, 0.236, 0.382, 0.5, 0.618, 0.786, 1, 1.618];
2116
+ const FIB_EXT_RATIOS = [0, 0.382, 0.5, 0.618, 1, 1.272, 1.618, 2.618];
2116
2117
  const hexToRgba = (hex, alpha) => {
2117
2118
  const cleaned = hex.replace("#", "");
2118
2119
  if (cleaned.length !== 6) {
@@ -2313,6 +2314,165 @@ function createChart(element, options = {}) {
2313
2314
  });
2314
2315
  ctx.font = prevFont;
2315
2316
  }
2317
+ } else if (drawing.type === "fib-extension") {
2318
+ const p0 = drawing.points[0];
2319
+ const p1 = drawing.points[1];
2320
+ const p2 = drawing.points[2];
2321
+ if (p0 && p1) {
2322
+ const x0 = xFromDrawingPoint(p0);
2323
+ const y0 = yFromPrice(p0.price);
2324
+ const x1 = xFromDrawingPoint(p1);
2325
+ const y1 = yFromPrice(p1.price);
2326
+ ctx.save();
2327
+ ctx.strokeStyle = hexToRgba(drawing.color, 0.6);
2328
+ ctx.setLineDash([4, 4]);
2329
+ ctx.lineWidth = 1;
2330
+ ctx.beginPath();
2331
+ ctx.moveTo(x0, y0);
2332
+ ctx.lineTo(x1, y1);
2333
+ if (p2) {
2334
+ ctx.lineTo(xFromDrawingPoint(p2), yFromPrice(p2.price));
2335
+ }
2336
+ ctx.stroke();
2337
+ ctx.restore();
2338
+ drawDrawingHandle(x0, y0, drawing.color);
2339
+ drawDrawingHandle(x1, y1, drawing.color);
2340
+ if (p2) {
2341
+ const x2 = xFromDrawingPoint(p2);
2342
+ const y2 = yFromPrice(p2.price);
2343
+ drawDrawingHandle(x2, y2, drawing.color);
2344
+ const palette = drawing.colors.length > 0 ? drawing.colors : null;
2345
+ const levelColorAt = (index) => palette ? palette[index % palette.length] : drawing.color;
2346
+ const move = p1.price - p0.price;
2347
+ const levelLines = FIB_EXT_RATIOS.map((ratio) => {
2348
+ const price = p2.price + move * ratio;
2349
+ return { ratio, price, y: yFromPrice(price) };
2350
+ });
2351
+ for (let index = 0; index < levelLines.length - 1; index += 1) {
2352
+ const top = levelLines[index];
2353
+ const bottom = levelLines[index + 1];
2354
+ const bandTop = Math.min(top.y, bottom.y);
2355
+ const bandBottom = Math.max(top.y, bottom.y);
2356
+ ctx.save();
2357
+ ctx.fillStyle = hexToRgba(levelColorAt(index), 0.1);
2358
+ ctx.globalAlpha = draft ? 0.5 : 1;
2359
+ ctx.fillRect(chartLeft, bandTop, chartRight - chartLeft, bandBottom - bandTop);
2360
+ ctx.restore();
2361
+ }
2362
+ ctx.save();
2363
+ ctx.lineWidth = drawing.width;
2364
+ applyDashPattern(drawing.style, dashPatterns.dotted, dashPatterns.dashed);
2365
+ levelLines.forEach((level, index) => {
2366
+ ctx.strokeStyle = levelColorAt(index);
2367
+ ctx.beginPath();
2368
+ ctx.moveTo(crisp(chartLeft), crisp(level.y));
2369
+ ctx.lineTo(crisp(chartRight), crisp(level.y));
2370
+ ctx.stroke();
2371
+ });
2372
+ ctx.restore();
2373
+ const prevFont = ctx.font;
2374
+ ctx.font = `500 11px ${mergedOptions.fontFamily}`;
2375
+ levelLines.forEach((level, index) => {
2376
+ const labelText = `${level.ratio} (${formatPrice(level.price)})`;
2377
+ const textWidth = ctx.measureText(labelText).width;
2378
+ const padding = 4;
2379
+ const bgX = chartLeft + 4;
2380
+ const bgY = level.y - 9;
2381
+ ctx.fillStyle = mergedOptions.backgroundColor;
2382
+ fillRoundedRect(bgX, bgY, textWidth + padding * 2, 16, 3);
2383
+ ctx.fillStyle = levelColorAt(index);
2384
+ ctx.textAlign = "left";
2385
+ ctx.textBaseline = "middle";
2386
+ ctx.fillText(labelText, bgX + padding, bgY + 8);
2387
+ });
2388
+ ctx.font = prevFont;
2389
+ if (drawing.label) {
2390
+ drawDrawingLabel(drawing.label, (x0 + x2) / 2, Math.min(y0, y2), drawing.color);
2391
+ }
2392
+ }
2393
+ }
2394
+ } else if (drawing.type === "long-position" || drawing.type === "short-position") {
2395
+ const entry = drawing.points[0];
2396
+ const target = drawing.points[1];
2397
+ const stop = drawing.points[2];
2398
+ const right = drawing.points[3];
2399
+ if (entry && target && stop && right) {
2400
+ const leftX = xFromDrawingPoint(entry);
2401
+ const rightX = xFromDrawingPoint(right);
2402
+ const boxX0 = Math.min(leftX, rightX);
2403
+ const boxX1 = Math.max(leftX, rightX);
2404
+ const boxW = Math.max(1, boxX1 - boxX0);
2405
+ const entryY = yFromPrice(entry.price);
2406
+ const targetY = yFromPrice(target.price);
2407
+ const stopY = yFromPrice(stop.price);
2408
+ const profitFill = "rgba(38,166,154,0.16)";
2409
+ const lossFill = "rgba(239,83,80,0.16)";
2410
+ const profitLine = "rgba(38,166,154,0.9)";
2411
+ const lossLine = "rgba(239,83,80,0.9)";
2412
+ ctx.save();
2413
+ ctx.globalAlpha = draft ? 0.6 : 1;
2414
+ ctx.fillStyle = profitFill;
2415
+ ctx.fillRect(boxX0, Math.min(entryY, targetY), boxW, Math.abs(targetY - entryY));
2416
+ ctx.fillStyle = lossFill;
2417
+ ctx.fillRect(boxX0, Math.min(entryY, stopY), boxW, Math.abs(stopY - entryY));
2418
+ ctx.restore();
2419
+ ctx.save();
2420
+ ctx.setLineDash([]);
2421
+ ctx.lineWidth = Math.max(1, drawing.width);
2422
+ ctx.strokeStyle = profitLine;
2423
+ ctx.beginPath();
2424
+ ctx.moveTo(crisp(boxX0), crisp(targetY));
2425
+ ctx.lineTo(crisp(boxX1), crisp(targetY));
2426
+ ctx.stroke();
2427
+ ctx.strokeStyle = lossLine;
2428
+ ctx.beginPath();
2429
+ ctx.moveTo(crisp(boxX0), crisp(stopY));
2430
+ ctx.lineTo(crisp(boxX1), crisp(stopY));
2431
+ ctx.stroke();
2432
+ ctx.strokeStyle = drawing.color;
2433
+ ctx.beginPath();
2434
+ ctx.moveTo(crisp(boxX0), crisp(entryY));
2435
+ ctx.lineTo(crisp(boxX1), crisp(entryY));
2436
+ ctx.stroke();
2437
+ ctx.restore();
2438
+ drawDrawingHandle(leftX, targetY, drawing.color);
2439
+ drawDrawingHandle(leftX, entryY, drawing.color);
2440
+ drawDrawingHandle(leftX, stopY, drawing.color);
2441
+ drawDrawingHandle(rightX, entryY, drawing.color);
2442
+ const tick = getConfiguredTickSize();
2443
+ const pctOf = (price) => {
2444
+ if (!Number.isFinite(entry.price) || entry.price === 0) return "0.00%";
2445
+ return `${((price - entry.price) / Math.abs(entry.price) * 100).toFixed(2)}%`;
2446
+ };
2447
+ const ticksOf = (price) => tick > 0 ? ` ${Math.round(Math.abs(price - entry.price) / tick)}t` : "";
2448
+ const targetDist = Math.abs(target.price - entry.price);
2449
+ const stopDist = Math.abs(entry.price - stop.price);
2450
+ const rr = stopDist > 0 ? targetDist / stopDist : 0;
2451
+ const cx = (boxX0 + boxX1) / 2;
2452
+ const drawPositionPill = (text, centerX, centerY, bg) => {
2453
+ const prevFont = ctx.font;
2454
+ ctx.font = `500 11px ${mergedOptions.fontFamily}`;
2455
+ const padding = 6;
2456
+ const textW = ctx.measureText(text).width;
2457
+ const pillW = textW + padding * 2;
2458
+ const pillH = 18;
2459
+ const pillX = centerX - pillW / 2;
2460
+ const pillY = centerY - pillH / 2;
2461
+ ctx.fillStyle = bg;
2462
+ fillRoundedRect(pillX, pillY, pillW, pillH, 4);
2463
+ ctx.fillStyle = "#ffffff";
2464
+ ctx.textAlign = "center";
2465
+ ctx.textBaseline = "middle";
2466
+ ctx.fillText(text, centerX, pillY + pillH / 2);
2467
+ ctx.font = prevFont;
2468
+ };
2469
+ drawPositionPill(`Target ${formatPrice(target.price)} (${pctOf(target.price)})${ticksOf(target.price)}`, cx, targetY, profitLine);
2470
+ drawPositionPill(`Stop ${formatPrice(stop.price)} (${pctOf(stop.price)})${ticksOf(stop.price)}`, cx, stopY, lossLine);
2471
+ drawPositionPill(`Entry ${formatPrice(entry.price)} RR ${rr.toFixed(2)}`, cx, entryY, hexToRgba(drawing.color, 0.92));
2472
+ if (drawing.label) {
2473
+ drawDrawingLabel(drawing.label, cx, Math.min(targetY, stopY) - 4, drawing.color);
2474
+ }
2475
+ }
2316
2476
  }
2317
2477
  ctx.restore();
2318
2478
  };
@@ -3474,6 +3634,63 @@ function createChart(element, options = {}) {
3474
3634
  return { drawing, target: "line" };
3475
3635
  }
3476
3636
  }
3637
+ } else if (drawing.type === "fib-extension") {
3638
+ const p0 = drawing.points[0];
3639
+ const p1 = drawing.points[1];
3640
+ const p2 = drawing.points[2];
3641
+ if (!p0 || !p1) continue;
3642
+ const x0 = canvasXFromDrawingPoint(p0);
3643
+ const y0 = canvasYFromDrawingPrice(p0.price);
3644
+ const x1 = canvasXFromDrawingPoint(p1);
3645
+ const y1 = canvasYFromDrawingPrice(p1.price);
3646
+ if (Math.hypot(x - x0, y - y0) <= 8) {
3647
+ return { drawing, target: "handle", pointIndex: 0 };
3648
+ }
3649
+ if (Math.hypot(x - x1, y - y1) <= 8) {
3650
+ return { drawing, target: "handle", pointIndex: 1 };
3651
+ }
3652
+ if (p2) {
3653
+ const x2 = canvasXFromDrawingPoint(p2);
3654
+ const y2 = canvasYFromDrawingPrice(p2.price);
3655
+ if (Math.hypot(x - x2, y - y2) <= 8) {
3656
+ return { drawing, target: "handle", pointIndex: 2 };
3657
+ }
3658
+ const move = p1.price - p0.price;
3659
+ const fibExtRatios = [0, 0.382, 0.5, 0.618, 1, 1.272, 1.618, 2.618];
3660
+ for (const ratio of fibExtRatios) {
3661
+ const lineY = canvasYFromDrawingPrice(p2.price + move * ratio);
3662
+ if (Math.abs(y - lineY) <= 5) {
3663
+ return { drawing, target: "line" };
3664
+ }
3665
+ }
3666
+ if (distanceToSegment(x, y, x0, y0, x1, y1) <= 6 || distanceToSegment(x, y, x1, y1, x2, y2) <= 6) {
3667
+ return { drawing, target: "line" };
3668
+ }
3669
+ } else if (distanceToSegment(x, y, x0, y0, x1, y1) <= 6) {
3670
+ return { drawing, target: "line" };
3671
+ }
3672
+ } else if (drawing.type === "long-position" || drawing.type === "short-position") {
3673
+ const entry = drawing.points[0];
3674
+ const target = drawing.points[1];
3675
+ const stop = drawing.points[2];
3676
+ const right = drawing.points[3];
3677
+ if (!entry || !target || !stop || !right) continue;
3678
+ const leftX = canvasXFromDrawingPoint(entry);
3679
+ const rightX = canvasXFromDrawingPoint(right);
3680
+ const entryY = canvasYFromDrawingPrice(entry.price);
3681
+ const targetY = canvasYFromDrawingPrice(target.price);
3682
+ const stopY = canvasYFromDrawingPrice(stop.price);
3683
+ if (Math.hypot(x - leftX, y - targetY) <= 9) return { drawing, target: "handle", pointIndex: 1 };
3684
+ if (Math.hypot(x - leftX, y - entryY) <= 9) return { drawing, target: "handle", pointIndex: 0 };
3685
+ if (Math.hypot(x - leftX, y - stopY) <= 9) return { drawing, target: "handle", pointIndex: 2 };
3686
+ if (Math.hypot(x - rightX, y - entryY) <= 9) return { drawing, target: "handle", pointIndex: 3 };
3687
+ const x0 = Math.min(leftX, rightX);
3688
+ const x1 = Math.max(leftX, rightX);
3689
+ const yTop = Math.min(targetY, stopY, entryY);
3690
+ const yBot = Math.max(targetY, stopY, entryY);
3691
+ if (x >= x0 && x <= x1 && y >= yTop && y <= yBot) {
3692
+ return { drawing, target: "line" };
3693
+ }
3477
3694
  }
3478
3695
  }
3479
3696
  return null;
@@ -3641,6 +3858,66 @@ function createChart(element, options = {}) {
3641
3858
  draw();
3642
3859
  return true;
3643
3860
  }
3861
+ if (activeDrawingTool === "fib-extension") {
3862
+ if (draftDrawing?.type === "fib-extension") {
3863
+ if (draftDrawing.points.length < 3) {
3864
+ draftDrawing = normalizeDrawingState({
3865
+ ...serializeDrawing(draftDrawing),
3866
+ points: [...draftDrawing.points.slice(0, -1), point, point]
3867
+ });
3868
+ draw();
3869
+ return true;
3870
+ }
3871
+ const completed = normalizeDrawingState({
3872
+ ...serializeDrawing(draftDrawing),
3873
+ points: [draftDrawing.points[0], draftDrawing.points[1], point]
3874
+ });
3875
+ drawings.push(completed);
3876
+ draftDrawing = null;
3877
+ activeDrawingTool = null;
3878
+ emitDrawingsChange();
3879
+ draw();
3880
+ return true;
3881
+ }
3882
+ const defaults = getDrawingToolDefaults("fib-extension");
3883
+ draftDrawing = normalizeDrawingState({
3884
+ type: "fib-extension",
3885
+ points: [point, point],
3886
+ color: defaults.color ?? "#2563eb",
3887
+ colors: defaults.colors ?? FIB_DEFAULT_PALETTE,
3888
+ style: defaults.style ?? "solid",
3889
+ width: defaults.width ?? 1
3890
+ });
3891
+ draw();
3892
+ return true;
3893
+ }
3894
+ if (activeDrawingTool === "long-position" || activeDrawingTool === "short-position") {
3895
+ const isLong = activeDrawingTool === "long-position";
3896
+ const tick = getConfiguredTickSize();
3897
+ const priceOffset = tick > 0 ? tick * 20 : Math.abs(point.price) * 0.01 || 1;
3898
+ const width2 = Math.max(5, Math.round(xSpan * 0.15));
3899
+ const entryPrice = point.price;
3900
+ const targetPrice = isLong ? entryPrice + priceOffset : entryPrice - priceOffset;
3901
+ const stopPrice = isLong ? entryPrice - priceOffset : entryPrice + priceOffset;
3902
+ const defaults = getDrawingToolDefaults(activeDrawingTool);
3903
+ drawings.push(
3904
+ normalizeDrawingState({
3905
+ type: activeDrawingTool,
3906
+ points: [
3907
+ point,
3908
+ normalizeDrawingPoint(point.index, targetPrice),
3909
+ normalizeDrawingPoint(point.index, stopPrice),
3910
+ normalizeDrawingPoint(point.index + width2, entryPrice)
3911
+ ],
3912
+ color: defaults.color ?? "#2563eb",
3913
+ style: defaults.style ?? "solid",
3914
+ width: defaults.width ?? 1
3915
+ })
3916
+ );
3917
+ emitDrawingsChange();
3918
+ draw();
3919
+ return true;
3920
+ }
3644
3921
  return false;
3645
3922
  };
3646
3923
  const setDrawingDefaults = (tool, defaults) => {
@@ -3664,6 +3941,22 @@ function createChart(element, options = {}) {
3664
3941
  if (drawing.id !== drawingDragState?.drawingId || drawing.locked) {
3665
3942
  return drawing;
3666
3943
  }
3944
+ if (drawingDragState.target === "handle" && (drawing.type === "long-position" || drawing.type === "short-position")) {
3945
+ const pts = drawing.points.map((point) => ({ ...point }));
3946
+ const entry = pts[0];
3947
+ const pointIndex = drawingDragState.pointIndex ?? 0;
3948
+ if (pointIndex === 0) {
3949
+ pts[0] = normalizeDrawingPoint(entry.index, currentPoint.price);
3950
+ if (pts[3]) pts[3] = normalizeDrawingPoint(pts[3].index, currentPoint.price);
3951
+ } else if (pointIndex === 1 && pts[1]) {
3952
+ pts[1] = normalizeDrawingPoint(entry.index, currentPoint.price);
3953
+ } else if (pointIndex === 2 && pts[2]) {
3954
+ pts[2] = normalizeDrawingPoint(entry.index, currentPoint.price);
3955
+ } else if (pointIndex === 3 && pts[3]) {
3956
+ pts[3] = normalizeDrawingPoint(currentPoint.index, entry.price);
3957
+ }
3958
+ return { ...drawing, points: pts };
3959
+ }
3667
3960
  if (drawingDragState.target === "handle") {
3668
3961
  return {
3669
3962
  ...drawing,
@@ -3913,12 +4206,12 @@ function createChart(element, options = {}) {
3913
4206
  setCrosshairPoint(null);
3914
4207
  return;
3915
4208
  }
3916
- if (draftDrawing && (activeDrawingTool === "trendline" || activeDrawingTool === "ray" || activeDrawingTool === "fib-retracement")) {
4209
+ if (draftDrawing && (activeDrawingTool === "trendline" || activeDrawingTool === "ray" || activeDrawingTool === "fib-retracement" || activeDrawingTool === "fib-extension")) {
3917
4210
  const nextPoint = drawingPointFromCanvas(point.x, point.y);
3918
4211
  if (nextPoint) {
3919
4212
  draftDrawing = {
3920
4213
  ...draftDrawing,
3921
- points: [draftDrawing.points[0], nextPoint]
4214
+ points: [...draftDrawing.points.slice(0, -1), nextPoint]
3922
4215
  };
3923
4216
  canvas.style.cursor = "crosshair";
3924
4217
  draw();
@@ -44,7 +44,7 @@ interface ChartOptions {
44
44
  drawings?: DrawingObjectOptions[];
45
45
  }
46
46
  type IndicatorPane = "overlay" | "separate";
47
- type DrawingToolType = "horizontal-line" | "vertical-line" | "trendline" | "ray" | "fib-retracement";
47
+ type DrawingToolType = "horizontal-line" | "vertical-line" | "trendline" | "ray" | "fib-retracement" | "fib-extension" | "long-position" | "short-position";
48
48
  interface DrawingPoint {
49
49
  index: number;
50
50
  price: number;