Context.calc_on(): a @sec_context callback runs
over the external candles, and its results are merged into the chart timescale.
CSV columns
The file must have a header row with these columns (any order, header names are case-insensitive, unknown extra columns are ignored):candles.csv
Declaring the source
External candle close
@sec_context callback runs over the external candles exactly as it would over a
secondary instrument, and source=sources.Csv(...) tells the platform where those candles come from.
The source argument is mutually exclusive with exchange and ticker — an external source has no exchange or
ticker of its own. Like any Context.calc_on() call, the source must be declared in __init__.
The time frame is required
For a candle CSV thetime_frame argument of Context.calc_on() is required. The time frame is a property of your
file — the platform does not guess it from the data:
time_framedeclares the cadence of the rows. If rows arrive more frequently than the declared time frame, the file is rejected with theexternal_timeframe_mismatcherror. The reverse mismatch — rows sparser than the declared time frame — is not detected.- Tick time frames are not supported for external sources.
- Omitting
time_frameis a compilation error.
How external candles align with the chart
An external candle CSV behaves like a secondary instrument on its own time frame. On each chart bar, the merged series returns the value of the latest external candle that has closed by the close of that bar:5-minute CSV on a 1-minute chart
- The candle that opens at 00:05 closes at 00:10, so its value 20 first appears on the chart bar that also closes at 00:10 — the 00:09 bar. On a daily CSV and a 1-hour chart this means Monday’s candle first appears on the bar covering Monday’s final hour.
- The last candle (close 30) has not closed within the loaded history, so history bars never see it. When the chart switches to realtime, that candle is the freshest data available and the series starts returning 30.
- Chart bars that come before the first loaded candle read that candle’s values instead of being empty.
- These rules assume the CSV time frame is equal to or larger than the chart’s. With a finer CSV, each chart bar shows the last external candle that opened at or before the bar’s open.
lookahead=True a candle is visible already from the chart bar where it opens:
Same file with lookahead=True
lookahead=True always requires an explicit time_frame.
Where external candles differ from market instruments
- Timestamps are interpreted as UTC and the external instrument runs on a 24/7 session — there are no trading-session gaps or exchange timezone.
- There are no realtime updates: the series is history-only.