Skip to content

fix: bound interval-window load via in-place resolution switch - #11625

Draft
sahar-fehri wants to merge 7 commits into
mainfrom
fix/advanced-chart-set-timeframe-interval-window
Draft

sahar-fehri wants to merge 7 commits into
mainfrom
fix/advanced-chart-set-timeframe-interval-window

Conversation

@sahar-fehri

@sahar-fehri sahar-fehri commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Explanation

How TradingView sizes its initial data request (by default)

The widget is mounted in its container and knows the container's pixel dimensions before it ever calls getBars. The data request is derived from the width, not the other way around.

The actual order:

  1. Widget constructed in your container
    → TV knows containerWidthPx + default barSpacing (px/bar) immediately.
  2. TV computes the bar count it needs:
    visibleBars ≈ containerWidthPx ÷ barSpacingPx
    countBack = visibleBars + indicator lookback + buffer (≈ 300)
  3. TV calls getBars({ resolution: '1', countBack, to: now })
    → datafeed returns ~300 one-minute bars.
  4. TV lays them out and shows the most-recent visibleBars of them, right edge = latest bar.

So the sequence is layout → countBack → getBars → render. getBars is step 3; the width-based sizing is step 2. That is also why the docs say "the library calculates the amount of data necessary to fill the chart space" — "chart space" is the already-known container size.

So how does it "know" to show last hour vs last 6 hours?

By default — it doesn't, and it doesn't try to. There is no time-span target. TV targets a bar count (width ÷ barSpacing), and the time span you end up seeing is just:

visible time span = visibleBars × intervalDuration

For a 1-minute chart on a typical container this works out to roughly the last hour; for a 1-day chart the same bar count covers roughly the last year. The span is a side effect of the bar count, not a goal.

The problem on an interval switch

When the user switches intervals, TradingView preserves the previous interval's visible time span and sizes the first getBars request's countBack to fill that inherited range at the new bar duration. A coarse→fine switch (e.g. 1W → 1m) therefore derives an enormous countBack — hundreds of thousands of bars — triggering a deep backward-pagination storm that can hang the chart.

Why TV's documented range APIs don't help

Our architecture pushes pre-fetched data from the host and uses TradingView as a rendering engine (the chart iframe has connect-src: none — it cannot fetch data itself). TV's countBack is computed internally from the inherited viewport span and is not influenced by any range-setting API (setVisibleRange, setTimeFrame, onIntervalChanged timeframe injection — all tested, none resize the fetch). Those APIs are designed for the lifecycle where TV owns data loading; in our inverted architecture (host pushes data, datafeed serves from memory) they set the display range but do not feed back into the switch-fetch sizing. The fix therefore operates at the datafeed layer where we have full control.

The fix

Two-part fix:

  1. Datafeed pagination cap. An RN-backed (Strategy B) backward-pagination request with countBack above MAX_RNBACKED_PAGINATION_COUNTBACK (10,000) is the inherited-range artifact, never a real scroll-back. It is halted with noData: true. Real scroll-back asks for only a viewport's worth of bars and passes through unaffected.

  2. Synchronous visible-range application. On an in-place resolution switch, the setResolution callback applies the per-interval visible range immediately (applyVisibleRangeImmediate) instead of subscribing to onDataLoaded — which never re-fires for in-place switches. This ensures the window lands on the correct per-interval range after every switch.

Opt-in (default OFF). An optional atomicIntervalWindow boolean on the SET_OHLCV_DATA payload gates the synchronous visible-range application and the oldest-bar clamping. When true, the per-interval visible-range pin applied after data loads is clamped so its start never predates the oldest loaded bar, keeping framing inside the bars already in memory. When the flag is absent or false, the pin uses the requested start unclamped — byte-for-byte like today's framing — so existing consumers (Social Leaderboard, mobile) are unaffected until they explicitly opt in.

Details:

  • In-place switch. On a resolution change the widget is kept; setResolution(newResolution, cb) runs. In the callback the main price scale auto-scale is reset, the data lifecycle is notified, and the per-interval visible range is applied (synchronously for opt-in, via onDataLoaded for non-opt-in). CHART_LAYOUT_SETTLED is emitted.
  • Gated clamp. The start clamp (max(visibleFromMs, oldestLoadedBar)) on the post-data visible-range pin is gated by the atomicIntervalWindow flag — non-opt-in consumers keep the unclamped start, preserving today's framing exactly.
  • Datafeed backstop. The MAX_RNBACKED_PAGINATION_COUNTBACK cap halts inherited-range pagination storms at the datafeed layer. The cap is unconditional (applies regardless of the opt-in flag) because the storm is always an artifact, never a real user scroll-back.
  • The datafeed scroll-back / pagination cursor logic is otherwise not touched.

Changelog

@metamask/advanced-chart-core

  • Added: Opt-in atomicIntervalWindow flag on the SET_OHLCV_DATA payload that bounds the initial historical-data load on interval switches by clamping the per-interval visible-range pin to the loaded bars and capping backward-pagination storms at the datafeed layer. Off by default; consumers keep today's unclamped framing until they opt in.

References

Related to ASSETS-4400

Manual testing steps

With the opt-in enabled (atomicIntervalWindow: true — e.g. the extension):

  1. Load a token/perps chart.
  2. Switch coarse→fine (e.g. 1D → 1m) and confirm the initial load completes quickly — no burst of deep backward-pagination requests (the datafeed cap halts the inherited-range storm).
  3. Switch fine→coarse (e.g. 1m → 1W) and confirm the same: no pagination storm.
  4. After switching, scroll back in time and confirm older bars still paginate in (lazy history is preserved).
  5. Apply a study / moving average / volume and change the chart type, then switch intervals; confirm they all survive the switch with no flash or reset.
  6. Confirm the visible window frames a sensible range for each interval and the right edge stays near the latest candle.

With the opt-in absent/false (default — e.g. Social Leaderboard / mobile):

  1. Switch intervals and confirm behavior is unchanged from today's: the visible-range start is unclamped — exactly as before.

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

🛑 Files requiring codeowner review

To merge this PR, you'll need to get approvals from the following owner groups.
For files with multiple owners, approval from one owner is sufficient.

@MetaMask/metamask-assets or @MetaMask/perps (6 files)
  • packages/advanced-chart-core/CHANGELOG.md
  • packages/advanced-chart-core/src/core/state.ts
  • packages/advanced-chart-core/src/messages/contract.ts
  • packages/advanced-chart-core/src/widget/__tests__/ohlcvIngestion.test.ts
  • packages/advanced-chart-core/src/widget/datafeed.ts
  • packages/advanced-chart-core/src/widget/ohlcvIngestion.ts

ℹ️ Files co-owned by @MetaMask/core-platform (expand for more)

[!NOTE]
For convenience in maintaining this repo, @MetaMask/core-platform co-owns
the following files. It is not required to seek approval from
@MetaMask/core-platform to change these files. This section is purely
informational.

  • packages/advanced-chart-core/CHANGELOG.md

@sahar-fehri

Copy link
Copy Markdown
Contributor Author

@metamaskbot publish-preview

@github-actions

Copy link
Copy Markdown
Contributor

The "Publish preview" workflow has been updated to a new version that uses OIDC for authentication. Please use the new workflow instead of this one.

To trigger the new workflow, go to the Publish a preview build workflow, click "Run workflow", and enter 11625 as the pull request number.

Replace the atomic `setTimeFrame` interval-switch mechanism with an
in-place resolution switch on the persistent widget. Before calling
`setResolution`, the time scale's absolute visible-range pin is released
via `setRightOffset`, so TradingView preserves the visible bar count
(bounded) instead of inheriting the previous interval's wide time span,
which drove the coarse->fine pagination storm. The widget is no longer
rebuilt, so studies, MAs, volume and chart type survive a switch.

The opt-in `atomicIntervalWindow` flag now clamps the per-interval
visible-range pin so its start never predates the oldest loaded bar,
keeping framing inside loaded bars. A `countBack` backstop in the
datafeed halts any over-large RN-backed pagination request as a final
guard.

Remove the now-dead `setTimeFrame`/`takeClientScreenshot` widget type
declarations and the double-buffer/dimmed-snapshot remnants, and update
the CHANGELOG and docs to describe the in-place approach.

Co-authored-by: Cursor <cursoragent@cursor.com>
@sahar-fehri sahar-fehri changed the title fix: bound initial load per interval via atomic setTimeFrame fix: bound interval-window load via in-place resolution switch Nov 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Preview builds have been published. Learn how to use preview builds in other projects.

Expand for full list of packages and versions.
@metamask-previews/advanced-chart-core@1.1.2-preview-f114d4be2

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant