diff --git a/docs/implementation.md b/docs/implementation.md index 23c6652..a2a4f79 100644 --- a/docs/implementation.md +++ b/docs/implementation.md @@ -1175,3 +1175,17 @@ hours so a new refresh token is written to the volume. Status grows `needs_login` when the error is a dead grant; the header then offers **reconnect**, which starts the existing `/api/qt` flow and writes the token on callback instead of asking anyone to paste a URL into a prompt. + +### 2026-08-18 — SPY open/close + +Intraday charts can draw faint vertical lines at 09:30 and 16:00 ET on +weekdays — cash hours, not the CME session. They are a Lightweight Charts +series primitive on the candle pane (`attachPrimitive`), the same extension +point as `createPriceLine`, not another DOM overlay. Off on the daily chart. +Layer toggle **SPY open/close**, on by default. + +### 2026-08-18 — Trendline slope in the tooltip + +Hovering a sloped line now shows signed points per hour (`+1.25 /h`, +`-0.40 /h`). Degrees follow the zoom; points per bar follow the timeframe. +The stored slope is still price per second. Flat levels omit the line. diff --git a/static/app.js b/static/app.js index 7d123f6..15b5426 100644 --- a/static/app.js +++ b/static/app.js @@ -69,7 +69,7 @@ async function apiFetch(url, options = {}) { const defaultPrefs = { base_tf: '1m', - enabled: { ma: { '1d': [10, 20, 50, 100, 200] }, drawings: true, manual: true, auto: false, horizontal: true, vwap: true }, + enabled: { ma: { '1d': [10, 20, 50, 100, 200] }, drawings: true, manual: true, auto: false, horizontal: true, vwap: true, rth: true }, hidden_levels_score: false, }; @@ -1155,6 +1155,7 @@ createApp({ localStorage.setItem('chart-layer-prefs', JSON.stringify(prefs.value)); syncVisibleLevels(); syncComments(); + if (chartApi) chartApi.setRthEnabled(prefs.value.enabled.rth); sendPrefs(); }, { deep: true }); @@ -1225,6 +1226,7 @@ createApp({ // guessed at: __chart.lineData(level) against timeToCoordinate(). window.__chart = chartApi; chartApi.create(document.getElementById('chart')); + chartApi.setRthEnabled(prefs.value.enabled.rth); chartApi.setClickHandler(handleChartClick); chartApi.setToolCompleteHandler(handleToolComplete); chartApi.setLineChangeHandler(line => { diff --git a/static/chart.js b/static/chart.js index 75521f4..70cd7aa 100644 --- a/static/chart.js +++ b/static/chart.js @@ -1,3 +1,77 @@ +// Vertical session lines via Lightweight Charts' series primitive — canvas, +// not a DOM overlay. createPriceLine is horizontal only; attachPrimitive is +// the matching hook for a time. +class RthLinesPrimitive { + constructor() { + this._times = []; + this._styles = []; + this._xs = []; + this._chart = null; + this._requestUpdate = null; + this._view = { + update: () => this._sync(), + renderer: () => this._renderer, + }; + this._renderer = { draw: target => this._draw(target) }; + } + + attached(param) { + this._chart = param.chart; + this._requestUpdate = param.requestUpdate; + } + + detached() { + this._chart = null; + this._requestUpdate = null; + } + + setMarks(marks) { + this._times = marks.map(mark => mark.t); + this._styles = marks.map(mark => mark.kind); + if (this._requestUpdate) this._requestUpdate(); + } + + updateAllViews() { + this._view.update(); + } + + paneViews() { + return [this._view]; + } + + _sync() { + if (!this._chart) { + this._xs = []; + return; + } + const scale = this._chart.timeScale(); + this._xs = this._times.map(time => scale.timeToCoordinate(time)); + } + + _draw(target) { + if (!this._xs.length) return; + target.useBitmapCoordinateSpace(scope => { + const ctx = scope.context; + const height = scope.bitmapSize.height; + const width = Math.max(1, Math.round(scope.horizontalPixelRatio)); + ctx.save(); + for (let i = 0; i < this._xs.length; i += 1) { + const x = this._xs[i]; + if (x == null) continue; + const px = Math.round(x * scope.horizontalPixelRatio); + ctx.strokeStyle = 'rgba(44,41,36,0.20)'; + ctx.lineWidth = width; + ctx.setLineDash(this._styles[i] === 'close' ? [3 * width, 4 * width] : []); + ctx.beginPath(); + ctx.moveTo(px + 0.5, 0); + ctx.lineTo(px + 0.5, height); + ctx.stroke(); + } + ctx.restore(); + }); + } +} + class ConfluenceChart { constructor() { this.chart = null; @@ -58,6 +132,8 @@ class ConfluenceChart { this.toolUpListener = null; this.pendingView = null; this.overlayLayer = null; + this.rthPrimitive = null; + this.rthEnabled = true; this.futureSpace = null; this.snapDot = null; this.snapLabel = null; @@ -246,6 +322,8 @@ class ConfluenceChart { // blank — you could watch two trendlines converge and not know when. These // points carry no value and draw nothing; they exist so the axis has // something to label and so timeToCoordinate answers out there. + this.rthPrimitive = new RthLinesPrimitive(); + this.candles.attachPrimitive(this.rthPrimitive); this.futureSpace = this.chart.addSeries(LightweightCharts.LineSeries, { priceScaleId: 'right', color: 'rgba(0,0,0,0)', @@ -460,6 +538,7 @@ class ConfluenceChart { }; this.chart.timeScale().setVisibleRange(this.pendingView); } + this.syncRthLines(); this.scheduleOverlays(true); } @@ -577,6 +656,7 @@ class ConfluenceChart { if (this.bars.length && this.bars[this.bars.length - 1].t === bar.t) this.bars[this.bars.length - 1] = bar; else this.bars.push(bar); if (advanced) this.extendManualLineSeries(last, bar); + if (advanced) this.syncRthLines(); this.scheduleOverlays(true); } @@ -1864,6 +1944,7 @@ class ConfluenceChart { const color = this.describeDrawingColor?.(line.color); this.tooltip.textContent = [ `#${line.number} ${line.label}`, + ConfluenceChart.slopeLabel(line), color?.name, color?.annotation, ].filter(Boolean).join('\n'); @@ -2509,6 +2590,110 @@ class ConfluenceChart { }; } + setRthEnabled(enabled) { + this.rthEnabled = enabled; + this.syncRthLines(); + } + + syncRthLines() { + if (!this.rthPrimitive) return; + if (!this.rthEnabled || !this.bars.length || this.bars[0]?.tf === '1d') { + this.rthPrimitive.setMarks([]); + return; + } + const first = this.bars[0].t; + const last = this.bars[this.bars.length - 1].t; + const marks = []; + const seen = new Set(); + for (const mark of ConfluenceChart.rthInstants(first, last)) { + const time = this.barContaining(mark.t); + if (time == null || seen.has(time)) continue; + seen.add(time); + marks.push({ t: time, kind: mark.kind }); + } + this.rthPrimitive.setMarks(marks); + } + + barContaining(time) { + const bars = this.bars; + if (!bars.length || time < bars[0].t) return null; + let lo = 0; + let hi = bars.length - 1; + while (lo < hi) { + const mid = (lo + hi + 1) >> 1; + if (bars[mid].t <= time) lo = mid; + else hi = mid - 1; + } + return bars[lo].t; + } + + static easternParts(epochSec) { + const parts = Object.fromEntries( + new Intl.DateTimeFormat('en-US', { + timeZone: 'America/New_York', + hour12: false, + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + }).formatToParts(new Date(epochSec * 1000)) + .filter(part => part.type !== 'literal') + .map(part => [part.type, part.value]), + ); + return { + year: Number(parts.year), + month: Number(parts.month), + day: Number(parts.day), + hour: Number(parts.hour) % 24, + minute: Number(parts.minute), + }; + } + + static epochAtEastern(year, month, day, hour, minute) { + let guess = Date.UTC(year, month - 1, day, hour + 4, minute); + for (let i = 0; i < 4; i += 1) { + const got = ConfluenceChart.easternParts(guess / 1000); + const delta = Date.UTC(year, month - 1, day, hour, minute) + - Date.UTC(got.year, got.month - 1, got.day, got.hour, got.minute); + if (delta === 0) break; + guess += delta; + } + return Math.floor(guess / 1000); + } + + static rthInstants(fromT, toT) { + const marks = []; + const seen = new Set(); + let cursor = fromT - 12 * 3600; + const stop = toT + 12 * 3600; + while (cursor <= stop) { + const parts = ConfluenceChart.easternParts(cursor); + const key = `${parts.year}-${parts.month}-${parts.day}`; + const weekday = new Date(Date.UTC(parts.year, parts.month - 1, parts.day)).getUTCDay(); + if (!seen.has(key) && weekday !== 0 && weekday !== 6) { + seen.add(key); + marks.push({ + t: ConfluenceChart.epochAtEastern(parts.year, parts.month, parts.day, 9, 30), + kind: 'open', + }); + marks.push({ + t: ConfluenceChart.epochAtEastern(parts.year, parts.month, parts.day, 16, 0), + kind: 'close', + }); + } + cursor += 12 * 3600; + } + return marks.filter(mark => mark.t >= fromT - 3600 && mark.t <= toT + 3600); + } + + static slopeLabel(line) { + if (ConfluenceChart.isFlat(line) || !line.slope) return ''; + const perHour = line.slope * 3600; + const decimals = Math.abs(perHour) >= 10 ? 1 : 2; + return `${perHour > 0 ? '+' : ''}${perHour.toFixed(decimals)} /h`; + } + toCandle(bar) { return { time: bar.t, open: bar.o, high: bar.h, low: bar.l, close: bar.c }; } diff --git a/static/index.html b/static/index.html index f0f708c..2c715a7 100644 --- a/static/index.html +++ b/static/index.html @@ -48,6 +48,7 @@