Functions

Functions exported by @reincharts/datafeed.

import { aggregateBars, alignSeries, alignTime, ... } from "@reincharts/datafeed";

aggregateBars

Builds higher timeframe bars (e.g. 1-minute → 1h, daily → weekly / monthly) from sorted bars. Weeks start on Monday and months on the 1st, in options.timeZone (default UTC). The last bar may be partial (still forming).

function aggregateBars(bars: readonly Bar[], resolution: Resolution, options?: AlignOptions): Bar[]

alignSeries

Aligns a second series (compare symbol) to base by time: each base item gets [key] set to the other bar with the same time. Plot it with e.g. <LineSeries y={(d) => d.AAPL?.close} /> on a percent-mode price scale to compare symbols.

function alignSeries<T extends { time: number; }, O extends { time: number; }, K extends string>(
    base: readonly T[],
    other: readonly O[],
    key: K,
    options?: AlignSeriesOptions,
): Array<T & { [P in K]: O | undefined; }>

alignTime

Start time of the bar containing time (ms epoch). Seconds / minutes align to the epoch, days to midnight, weeks to weekStart, months to the 1st — in timeZone (default UTC).

function alignTime(time: number, resolution: Resolution, options?: AlignOptions): number

applyTick

Realtime tick merge (pure): folds tick into the last bar or appends a new bar.

function applyTick(
    data: readonly Bar[],
    tick: Tick,
    resolution: Resolution,
    options?: AlignOptions,
): Bar[]

compareResolutions

Compares resolutions by duration (for sorting).

function compareResolutions(a: Resolution, b: Resolution): number

createMemoryDatafeed

A datafeed serving in-memory bars, e.g. parseCsv(text) output. Requests for a higher resolution than options.resolution are aggregated (weekly / monthly aligned); lower resolutions return no data.

function createMemoryDatafeed(
    barsBySymbol: Record<string, readonly Bar[]>,
    options?: MemoryDatafeedOptions,
): MemoryDatafeed

createRandomDatafeed

A browser-only demo / test feed: deterministic random-walk prices for any symbol and resolution (open / close consistent across resolutions and pages) plus a realtime ticker that updates the last bar and rolls new ones.

function createRandomDatafeed(options?: RandomDatafeedOptions): RandomDatafeed

formatResolution

Human label: "1s", "5m", "1h", "4h", "1D", "1W", "1M", "1Y" (style: "short", default) or "5 minutes", "1 hour" (style: "long").

function formatResolution(resolution: Resolution, style?: "short" | "long"): string

isResolution

Returns true when the string is a valid resolution.

function isResolution(value: string): value is Resolution

mergeBar

Realtime merge (pure): replaces the last bar when bar.time equals it, appends when newer, and ignores older bars (returns data unchanged).

function mergeBar<T extends Bar>(data: readonly T[], bar: T): T[]

mergeBars

Merges two sorted series (e.g. an older history page into loaded data). On equal times newer wins. Existing objects are reused, so React / chart identity checks stay cheap.

function mergeBars<T extends Bar>(older: readonly T[], newer: readonly T[]): T[]

nextBarTime

Start time of the bar following the one that starts at (or contains) time.

function nextBarTime(time: number, resolution: Resolution, options?: AlignOptions): number

normalizeResolution

Canonical resolution string: "1h" → "60", "1d" → "1D", "1y" → "12M", "1s" → "1S".

function normalizeResolution(resolution: Resolution): Resolution

parseCsv

Parses CSV / TSV OHLC text (with a header row) into bars sorted by time. Rows with an invalid time or no price are skipped; missing open/high/low default to close.

function parseCsv(text: string, options?: ParseCsvOptions): CsvBar[]

parseResolution

Parses a resolution string. Throws on invalid input. Case matters only for "m" (minute) vs "M" (month), like most exchanges.

function parseResolution(resolution: Resolution): ParsedResolution

parseTimeValue

Default time cell parser (see ParseCsvOptions.parseTime).

function parseTimeValue(value: string): number

resolutionToMs

Nominal bar duration in milliseconds (months count as 30 days).

function resolutionToMs(resolution: Resolution): number

sliceBars

Returns bars with time < to: the last countBack ones, or those >= from when countBack is 0.

function sliceBars(
    bars: readonly Bar[],
    { from, to, countBack, firstDataRequest }: GetBarsRequest,
): GetBarsResult

sortBars

Sorts by time and removes duplicates (the last occurrence wins). Returns the input when already clean.

function sortBars<T extends Bar>(bars: readonly T[]): T[]

ticksToBars

Builds bars from trades (seconds or minute bars from ticks). Ticks must be sorted by time; out-of-order ticks older than the current bar are ignored.

function ticksToBars(ticks: readonly Tick[], resolution: Resolution, options?: AlignOptions): Bar[]

tickToBar

Updates (or starts) the bar for tick. bar is returned untouched when the tick is older.

function tickToBar(
    bar: Bar | undefined,
    tick: Tick,
    resolution: Resolution,
    options?: AlignOptions,
): Bar

timeZoneOffset

Offset (ms) of timeZone from UTC at time: local wall-clock = time + offset.

function timeZoneOffset(time: number, timeZone?: string): number

toChartBar

Adds date so the bar can be passed straight to <ChartCanvas data>. Returns the bar itself when already done.

function toChartBar<T extends Bar>(bar: T): T & ChartBar

On this page