PnlTracker
Developer

Trade chart

How the trade chart works in PnlTracker - Polygon integration, symbol mapping, caching, and rate limiting.

The trade chart is displayed inside the trade detail modal in the daily view. It renders a candlestick chart sourced from Polygon.io and overlays the trade entry, exit, and optionally adjacent trades.

Symbol mapping

tradeToPolygonSymbol() converts a trade symbol to a Polygon ticker based on the trade instrumentType.

  • Forex: 6-letter pairs are prefixed with C: (e.g. EURUSDC:EURUSD). Metal aliases such as GOLD or XAU are mapped to C:XAUUSD.
  • Crypto: matched against X:BASEQUOTE. USDT is normalized to USD (e.g. BTCUSDTX:BTCUSD).
  • Stock: bare ticker (e.g. AAPL).
  • Future: bare ticker with month/year code (e.g. ESZ5).
  • Option: prefixed with O: (e.g. AAPL240315C00150000O:AAPL240315C00150000).
  • Any / undefined: if the symbol looks like a futures ticker, it is treated as a future.

Polygon alias (spgn)

For symbols that don't map automatically, add a custom field named spgn on the symbol configuration. The value is the Polygon ticker to use. An optional prefix forces the instrument type used to call the Polygon API:

PrefixInstrument typeExample
ft:Futureft:ES
fx:Forexfx:EURUSD
cr:Cryptocr:BTCUSD
st:Stockst:AAPL

This is useful when a broker symbol differs from the Polygon ticker or when the instrument type needs to be overridden.

Data fetching

Standard aggregates API

Stocks, forex, crypto, and options use:

https://api.polygon.io/v2/aggs/ticker/{ticker}/range/{multiplier}/{timespan}/{from}/{to}

Timestamps are returned in milliseconds and converted to seconds for the chart.

Futures API

Futures use a dedicated endpoint:

https://api.polygon.io/futures/v1/aggs/{ticker}

The futures API returns nanosecond timestamps and uses a different resolution format (1min, 1hour, 1session).

Contract resolution

If a futures symbol has no expiration code (e.g. MGC) or if the trade falls inside the expiration month, the application queries the /futures/v1/contracts endpoint to find the front-month contract. Contracts in their expiration month are skipped to avoid degraded data, with a fallback to the nearest contract if necessary.

Rate limiting

The Polygon free plan allows 5 requests per minute. The client enforces a minimum 12-second delay between requests and retries up to 3 times on HTTP 429 with exponential backoff (12s, 24s, 48s).

Caching

Bars are cached in the browser via idb-keyval (IndexedDB). The cache uses period keys:

  • Timeframes ≤ 5 minutes: ISO weeks (YYYY-Www)
  • Timeframes > 5 minutes: months (YYYY-MM)

Each period is stored separately under a key like polygon:{ticker}:{tf}:{periodKey}. Historical periods are never refreshed; current or future periods are refreshed at most once per minute. A Clear Polygon cache button in Settings → Options deletes all cached entries.

Date range and timeframes

Available timeframes: 1, 5, 15 minutes, 1 hour, 4 hours, daily. The requested range is centered on the trade with an adaptive buffer:

TimeframeBuffer barsApproximate context
1 min1500~25 hours
5 min1000~83 hours
15 min800~200 hours
1 hour500~500 hours
4 hours400~1600 hours
Daily250~250 days

On load, the chart scrolls to a visible window centered on the trade midpoint. The visible window is also adaptive by timeframe.

Timezone display

The chart axis and crosshair labels respect the timezone configured in Settings → Options (CURRENT, LOCAL, or UTC). This is independent of the raw UTC timestamps stored by Polygon.

Markers and overlays

  • Entry / exit markers: BUY/SELL arrows below the entry bar, EXIT arrow above the exit bar.
  • Price segments: short horizontal lines at the exact entry and exit prices.
  • Trade line: dashed line connecting entry and exit prices.
  • Adjacent trades: optional monochrome markers and price segments for other trades on the same symbol within the visible range.

Markers and segments that fall outside the fetched data range are skipped so the chart never crashes on missing data.

Configuration

The Polygon API key is configured in Settings → Options. The key is stored in the browser (persisted in localStorage) and used for all trade chart requests. There is no fallback to a public or environment key.

Copyright © 2026