Skip to content

0009 — Live tail polling and history paging

Status: Accepted Date: 2026-11-04 Amended by: ADR-0056 (png, summary and both read their indicators’ warmup)

The web frontend loads one generate_chart(format=series) payload and then sits still. Two TradingView behaviours are missing:

  1. Live candles. The newest, still-forming candle should move while the page is open, and a new candle should appear when the interval rolls over. Indicator values on those candles must be right too.
  2. Scroll-back history. Scrolling left should load older bars on demand instead of fixing the chart at the first bars returned.

Indicators are computed on the server and the browser does no indicator math (ADR-0003, ADR-0007). That rules out fetching raw candles and computing values in the client. An indicator value at bar i depends on the bars before i, so a short request (“the last 2 bars”) or an older page (“the 500 bars before X”) needs extra earlier bars to compute correct values for the bars it returns.

  • Keep indicator math in one place: the Rust indicator engine.
  • Reuse the existing MCP tool and its Zod-validated response shape instead of adding a new transport.
  • Make E2E tests possible without network access.
  • Keep requests bounded. Binance serves at most 1000 klines per call; Yahoo keeps only a limited intraday history.

Poll generate_chart for the tail. The client calls generate_chart(format=series, bars=2, warmup=W) every few seconds and merges the two bars (the last closed bar and the forming bar) into its arrays by t. Stateless, and it uses the same tool, the same auth and the same response schema. The cost is one small request per poll and a latency equal to the poll interval.

SSE push from src/feed.rs. The server already runs a Binance WebSocket feed for the trading engine and could push bar updates over GET /mcp. Latency is lower, but it needs per-subscriber symbol/interval state, recomputation of indicators per push, and reconnect/backfill logic. The feed covers Binance only. Deferred, not rejected.

Browser connects straight to the Binance WebSocket. Lowest latency, but the browser would get raw candles with no indicator values. It would have to compute indicators itself (ruled out by ADR-0003/0007) or ask the server again on every tick. It is also Binance-only. Rejected.

Return the full history every time. The client asks for a larger bars count and replaces its arrays. Simple, but each scroll re-fetches and re-serializes everything already loaded, the cost grows without limit, and Binance’s 1000-bar cap still applies. Rejected.

Page with end_time plus warmup. The client asks for the bars bars that open strictly before end_time, which is the oldest bar it has loaded. warmup extra bars are fetched before them so the indicators are warmed up when the returned window starts. The server computes over bars + warmup and returns only the last bars. Every request has a bounded cost, and pages join exactly at end_time. Chosen.

Chosen: tail polling via generate_chart + end_time/warmup paging. Push is deferred.

  • end_time (ms since epoch, 1900-01-01 up to one year ahead) is an exclusive upper bound on bar open time.
  • Whenever a ticker is given, a failed upstream fetch is a tool error in every format. The old silent sample-data fallback would put synthetic bars into a real history or a live tail. Only the no-ticker demo path still renders sample data.
  • warmup (series only, at most 1000) adds bars that are used for computation and then trimmed. Every index-based channel is trimmed and re-indexed: lines, histogram, fills, signals, divlines. Divlines that end at or before the window’s first bar are dropped. Divlines that cross into the returned window are clipped at its first bar. Indicators that declare Indicator::windowed_profile() (VPVR, HVN/LVN, TPO) have their hbars and hlines recomputed over the returned window, so warmup bars don’t enter the profile.
  • Clients keep profile hbars and profile-derived hlines from the initial full load and ignore them on tail-poll and history-page responses: those profiles cover only the slice the response carries.
  • has_more in the series payload tells the client to keep paging. For Binance and the sample source it is true when the full bars + warmup count came back. For Yahoo it is true unless the window was cut off by Yahoo’s lookback limit or the symbol’s first trade date, because one Yahoo window can legitimately hold fewer bars than requested (1m: about 1950 per 7-day window).
  • Binance pagination goes backwards with endTime past the 1000-bar cap. Yahoo uses period1/period2 windows sized to the request. Its live bar is snapped onto its period start. Its 1h→4h aggregation anchors buckets at each trading day’s session open for exchange-traded symbols (TradingView parity) and on the UTC 4h grid for crypto/FX. Both rules depend only on the bars themselves, so bucket boundaries match between pages.
  • CHARTGEN_DATA_SOURCE=sample switches to a deterministic offline source. Each bar’s price is a pure function of the symbol and the bar’s open time, so pages that overlap agree exactly. The forming bar moves with wall-clock time.

Positive. Live candles and scroll-back history need no new transport, no client-side indicator code, and no server-side session state. E2E tests can run offline against stable data.

Negative.

  • Cumulative-indicator seam. Indicators with unbounded memory (EMA, Wilder-smoothed RSI/ATR, OBV, CVD, A/D line, VWAP sessions) never fully converge with a finite warmup. Recursive ones converge with geometrically shrinking error. Running sums (OBV, CVD, A/D) are offset by a constant that depends on where the computation started. At a page seam the values from the two requests can differ slightly, or by a constant for cumulative sums. The same applies to a tail poll compared with the initially loaded history. Finite-window indicators (SMA, Bollinger, Donchian) match exactly once warmup is at least their lookback. A larger warmup reduces the difference but cannot remove it.
  • Polling adds a request every interval per open chart, and latency is bounded by the poll period. Upstream requests share one HTTP client with a 10 s timeout, and a paginated Binance fetch has a 20 s budget.
  • Profile overlays don’t update on tail polls. They stay as computed at the initial load until the client reloads the full window.
  • Yahoo intraday history is short (1m ≈ 30 days, sub-hour ≈ 60 days, 1h ≈ 730 days). Paging ends there and has_more becomes false.

Neutral. png, summary and both ignore warmup and render exactly bars. end_time applies to every format.