fib fixes

This commit is contained in:
Chris Amow 2026-08-19 09:51:21 +00:00
parent b932fa2d75
commit fbb469de57

View file

@ -153,27 +153,28 @@ class FibLevelsPrimitive {
const ctx = scope.context;
const right = scope.mediaSize.width;
ctx.save();
ctx.font = '10px "IBM Plex Mono", ui-monospace, monospace';
for (const item of this._items) {
if (item.kind === 'rail') {
ctx.strokeStyle = item.color;
ctx.fillStyle = item.color;
ctx.lineWidth = item.width;
ctx.setLineDash([]);
ctx.globalAlpha = 0.85;
ctx.beginPath();
ctx.moveTo(item.x + 0.5, item.y0);
ctx.lineTo(item.x + 0.5, item.y1);
ctx.stroke();
ctx.save();
ctx.translate(item.x, (item.y0 + item.y1) / 2);
ctx.rotate(-Math.PI / 2);
ctx.textAlign = 'center';
ctx.textBaseline = item.x > 14 ? 'bottom' : 'top';
ctx.fillText('fibonacci', 0, item.x > 14 ? -2 : 2);
ctx.restore();
continue;
}
ctx.font = '10px "IBM Plex Mono", ui-monospace, monospace';
for (const item of this._items) {
if (item.kind === 'rail') {
ctx.strokeStyle = item.color;
ctx.fillStyle = item.color;
ctx.lineWidth = item.width;
ctx.setLineDash([]);
ctx.globalAlpha = 0.85;
ctx.beginPath();
ctx.moveTo(item.x + 0.5, item.y0);
ctx.lineTo(item.x + 0.5, item.y1);
ctx.stroke();
ctx.save();
ctx.font = '20px "IBM Plex Mono", ui-monospace, monospace';
ctx.translate(item.x, (item.y0 + item.y1) / 2);
ctx.rotate(-Math.PI / 2);
ctx.textAlign = 'center';
ctx.textBaseline = item.x > 22 ? 'bottom' : 'top';
ctx.fillText('fibonacci', 0, item.x > 22 ? -2 : 2);
ctx.restore();
continue;
}
const start = item.x0 == null ? 0 : Math.max(0, item.x0);
if (start > right) continue;
ctx.strokeStyle = item.color;
@ -2577,10 +2578,17 @@ class ConfluenceChart {
}));
}
handleLevel(id) {
return this.levels.find(value => value.id === id)
|| this.fibs.find(item => item.id === id)
|| null;
}
fibAsHandleLevel() {
const fib = this.fibs.find(item => item.id === this.selectedLineId && !item.hidden);
if (!fib) return null;
return { ...fib, kind: 'manual', slope: Number(fib.slope) || 0 };
if (fib.slope == null) fib.slope = 0;
return fib;
}
renderAnchorHandles() {
@ -2732,7 +2740,7 @@ class ConfluenceChart {
startAnchorDrag(event, anchor) {
event.preventDefault();
event.stopPropagation();
const level = this.levels.find(value => value.id === this.selectedLineId);
const level = this.handleLevel(this.selectedLineId);
this.draggingAnchor = {
id: this.selectedLineId,
anchor: level && ConfluenceChart.isFlat(level) ? 'price' : anchor,
@ -2743,7 +2751,7 @@ class ConfluenceChart {
moveAnchor(event) {
if (!this.draggingAnchor || !this.chartEl) return;
const level = this.levels.find(value => value.id === this.draggingAnchor.id);
const level = this.handleLevel(this.draggingAnchor.id);
if (!level || !this.bars.length) return;
const point = this.eventPoint(event);
if (!point || !this.withinPlot(point)) {
@ -2764,19 +2772,23 @@ class ConfluenceChart {
return;
}
const secondPrice = level.anchor_p + level.slope * (level.last_t - level.anchor_t);
const fib = level.kind === 'fibonacci';
if (this.draggingAnchor.anchor === 'start') {
if (time >= level.last_t) return;
if (!fib && time >= level.last_t) return;
if (fib && time === level.last_t) return;
level.anchor_t = time;
level.anchor_p = price;
level.slope = (secondPrice - price) / (level.last_t - time);
} else {
if (time <= level.anchor_t) return;
if (!fib && time <= level.anchor_t) return;
if (fib && time === level.anchor_t) return;
if (level.cutoff_t != null && time >= level.cutoff_t) return;
level.last_t = time;
level.slope = (price - level.anchor_p) / (time - level.anchor_t);
}
const entry = this.levelSeries.get(level.id);
if (entry) entry.series.setData(this.seriesLineData(level));
if (fib) this.syncFibLines();
this.renderAnchorHandles();
this.renderLineBridges();
}
@ -2784,7 +2796,7 @@ class ConfluenceChart {
finishAnchorDrag(event) {
if (!this.draggingAnchor) return;
event.preventDefault();
const level = this.levels.find(value => value.id === this.draggingAnchor.id);
const level = this.handleLevel(this.draggingAnchor.id);
this.draggingAnchor = null;
this.detachPointers('anchor');
this.hideSnapDot();