Skip to Content
API ReferenceHelper Functions

Helper Functions

The GoCharting SDK exports utility helpers for common datafeed tasks. The only public helper today is getDateRangeForDuration() (also on window.GoChartingSDK in the UMD build). Other SDK utilities (createChart, setTheme, overrides, featuresets, etc.) live on the chart/widget APIs — not as standalone helper exports.

Interactive check

In the browser console (or any page that loads the UMD/@gocharting/chart-sdk):

const { getDateRangeForDuration } = window.GoChartingSDK || require("@gocharting/chart-sdk"); console.table( ["1D", "5D", "15D", "1M", "3M", "6M", "1Y", "5Y", "All"].map((d) => { const r = getDateRangeForDuration(d); return { duration: d, interval: r.interval, days: Math.round((r.end_date - r.start_date) / 86400000), }; }), );

getDateRangeForDuration()

Calculates a millisecond start/end window and a suggested bar interval for a duration string. Useful when your backend needs explicit start_date / end_date instead of only a row count.

Signature:

function getDateRangeForDuration(duration: DurationString): DateRangeResult type DurationString = | "1D" | "5D" | "15D" | "1M" | "3M" | "6M" | "1Y" | "5Y" | "All" type DateRangeResult = { start_date: number // Start timestamp in milliseconds (epoch UTC) end_date: number // End timestamp in milliseconds (Date.now()) interval: string // Suggested interval for this duration }

Import:

import { getDateRangeForDuration } from "@gocharting/chart-sdk"; // UMD: GoChartingSDK.getDateRangeForDuration(...)

Supported durations (matches the exported helper)

DurationApprox. lookbackRecommended interval
"1D"1 day"1m"
"5D"5 days"15m"
"15D"15 days"30m"
"1M"30 days"1h"
"3M"90 days"2h"
"6M"180 days"4h"
"1Y"365 days"1D"
"5Y"5 × 365 days"1W"
"All"20 × 365 days"1M"

Unknown duration strings fall back to 1 day / "1m".

Note

Important notes

  1. Timestampsstart_date / end_date are Unix epoch milliseconds (UTC).
  2. end_date — always Date.now() at call time.
  3. Lookbacks — fixed day multiples (30 * 86400000 for "1M"), not calendar months.
  4. interval — a suggestion only; you can pass a different resolution to getBars / setInterval.
  5. "All" — ~20 years of lookback in the public helper.

Usage examples

Basic usage

import { getDateRangeForDuration } from "@gocharting/chart-sdk"; const { start_date, end_date, interval } = getDateRangeForDuration("1D"); console.log("Start:", new Date(start_date)); // ~1 day ago console.log("End:", new Date(end_date)); // now console.log("Interval:", interval); // "1m"

Datafeed: use the range when fetching bars

resolveSymbol must return a full GoCharting security object (asset_type, exchange_info, precision fields, etc.). The helper is for time windows, not symbol metadata.

import { getDateRangeForDuration } from "@gocharting/chart-sdk"; const myDatafeed = { async getBars(symbolInfo, resolution, periodParams) { const { from, to } = periodParams; const response = await fetch( `/api/bars?symbol=${encodeURIComponent(symbolInfo.symbol)}` + `&from=${from.getTime()}&to=${to.getTime()}` + `&interval=${encodeURIComponent( typeof resolution === "string" ? resolution : resolution?.type || "5m", )}`, ); const data = await response.json(); return { bars: data.map((bar) => ({ time: bar.timestamp, open: bar.open, high: bar.high, low: bar.low, close: bar.close, volume: bar.volume, })), }; }, resolveSymbol(symbolName, onResolve, onError) { // Optional: pick an initial suggested interval for your own preload logic const { interval } = getDateRangeForDuration("1M"); // "1h" const parts = String(symbolName).split(":"); const exchange = parts[0] || "BYBIT"; const segment = parts.length >= 3 ? parts[1] : "FUTURE"; const symbol = parts[parts.length - 1] || "BTCUSDT"; const name = `${symbol} perpetual`; const validIntervals = ["1m", "5m", "15m", "1h", "4h", "1D"]; onResolve({ exchange, segment, symbol, name, asset_type: "CRYPTO", source_id: symbol, tradeable: true, is_index: false, is_formula: false, delay_seconds: 0, data_status: "streaming", contract_size: 1, tick_size: 0.1, display_tick_size: 0.1, volume_size_increment: 1, max_tick_precision: 1, max_volume_precision: 0, quote_currency: "USDT", pair: ["BTC", "USDT"], supports: { footprint: false }, exchange_info: { name: exchange.toLowerCase(), code: exchange, country_cd: "US", zone: "UTC", hours: Array.from({ length: 7 }, () => ({ open: true })), valid_intervals: validIntervals, }, ticker: symbol, full_name: `${exchange}:${segment}:${symbol}`, description: name, type: "crypto", session: "24x7", timezone: "UTC", has_intraday: true, has_daily: true, supported_resolutions: validIntervals, }); void interval; // use in preload / UI if you want void onError; }, };

Duration selector + setInterval

import { createChart, getDateRangeForDuration } from "@gocharting/chart-sdk"; const durations = ["1D", "5D", "15D", "1M", "3M", "6M", "1Y", "5Y"]; const chart = createChart("#chart", { symbol: "BYBIT:FUTURE:BTCUSDT", interval: "1h", datafeed: myDatafeed, licenseKey: "YOUR_LICENSE_KEY", onReady: () => { durations.forEach((duration) => { document .getElementById(`btn-${duration}`) ?.addEventListener("click", () => { const { start_date, end_date, interval } = getDateRangeForDuration(duration); console.log(`Loading ${duration}`); console.log(`From: ${new Date(start_date)}`); console.log(`To: ${new Date(end_date)}`); console.log(`Recommended interval: ${interval}`); // Wrapper also exposes setInterval chart.setInterval(interval); }); }); }, });

Pre-load with the helper window

import { createChart, getDateRangeForDuration } from "@gocharting/chart-sdk"; async function preloadChartData(symbol, duration) { const { start_date, end_date, interval } = getDateRangeForDuration(duration); const response = await fetch( `/api/bars?symbol=${encodeURIComponent(symbol)}` + `&interval=${encodeURIComponent(interval)}` + `&from=${start_date}&to=${end_date}`, ); const data = await response.json(); console.log(`Pre-loaded ${data.length} bars for ${duration}`); return data; } const cachedData = await preloadChartData("BYBIT:FUTURE:BTCUSDT", "1M"); const chart = createChart("#chart", { symbol: "BYBIT:FUTURE:BTCUSDT", interval: "1h", datafeed: { async getBars(symbolInfo, resolution, periodParams) { if (cachedData) { return { bars: cachedData }; } // fetch from API using periodParams.from / .to ... return { bars: [] }; }, resolveSymbol(symbolName, onResolve, onError) { // same full security shape as above myDatafeed.resolveSymbol(symbolName, onResolve, onError); }, }, licenseKey: "YOUR_LICENSE_KEY", });

Optimal interval for a duration

import { createChart, getDateRangeForDuration } from "@gocharting/chart-sdk"; const selectedDuration = "1M"; const { interval: optimalInterval } = getDateRangeForDuration(selectedDuration); // "1h" const chart = createChart("#chart", { symbol: "BYBIT:FUTURE:BTCUSDT", interval: optimalInterval, datafeed: myDatafeed, licenseKey: "YOUR_LICENSE_KEY", });

Reference

AspectDetails
PurposeStart/end ms window + suggested interval
Input"1D" | "5D" | "15D" | "1M" | "3M" | "6M" | "1Y" | "5Y" | "All"
Output{ start_date, end_date, interval }
Packageimport { getDateRangeForDuration } from "@gocharting/chart-sdk"
UMDGoChartingSDK.getDateRangeForDuration(...)
Last updated on