Skip to main content

fetchData

Fetches historical market data (klines/candlesticks) from a data provider and populates a time series.

Function Signature

fetchData(
  source: Source,
  baseSer: TSer,
  ticker: string,
  tframe: TFrame,
  tzone: string,
  startTime?: number,
  limit?: number
): Promise<number>

Parameters

source
Source
required
Data source provider: Source.yfinance or Source.binance
baseSer
TSer
required
Base time series object to populate with fetched kline data
ticker
string
required
Symbol/ticker to fetch (e.g., “BTCUSDT” for Binance, “AAPL” for Yahoo Finance)
tframe
TFrame
required
Timeframe object specifying the interval (e.g., 1m, 5m, 1h, 1d)
tzone
string
required
Timezone string for time calculations (e.g., “America/New_York”, “UTC”)
startTime
number
Starting timestamp in milliseconds. If not provided, defaults to limit periods back from now.
limit
number
Maximum number of klines to fetch. If not provided, defaults to provider-specific limits.

Return Value

Promise<number>
number
Returns a promise that resolves to the timestamp of the latest kline fetched, or undefined if no data was fetched.

Example Usage

import { fetchData, Source } from './lib/domain/DataFecther';
import { TSer } from './lib/timeseris/TSer';
import { TFrame } from './lib/timeseris/TFrame';

const baseSer = new TSer();
const tframe = new TFrame('1h'); // 1 hour timeframe

// Fetch Bitcoin data from Binance
const latestTime = await fetchData(
  Source.binance,
  baseSer,
  'BTCUSDT',
  tframe,
  'UTC',
  undefined,  // Auto-calculate start time
  500         // Fetch 500 candles
);

console.log('Latest candle time:', new Date(latestTime));

// Fetch Apple stock data from Yahoo Finance
await fetchData(
  Source.yfinance,
  baseSer,
  'AAPL',
  new TFrame('1d'),
  'America/New_York',
  Date.now() - 365 * 24 * 60 * 60 * 1000,  // 1 year ago
  365  // 365 days
);

Data Processing

  1. Fetches klines from the specified provider
  2. Sorts klines chronologically by open time
  3. Removes any duplicate klines
  4. Creates Kline objects and adds them to baseSer using KVAR_NAME (“kline”)
  5. Returns the timestamp of the latest kline

Fallback Behavior

If the provider fetch fails or returns no data, the function attempts to load data from a local ./klines.json file. Source: DataFecther.ts:13-35

fetchSymbolList

Fetches a list of available symbols/tickers from a data provider, filtered by search text.

Function Signature

fetchSymbolList(
  source: Source,
  filterText: string,
  init: RequestInit
): Promise<{ ticker: string }[]>

Parameters

source
Source
required
Data source provider: Source.yfinance or Source.binance
filterText
string
required
Search/filter text to match against symbol names (e.g., “BTC”, “AAPL”)
init
RequestInit
required
Fetch API initialization options (for request cancellation, headers, etc.)

Return Value

Promise<Array>
{ ticker: string }[]
Returns a promise that resolves to an array of objects, each containing a ticker property with the symbol name.

Example Usage

import { fetchSymbolList, Source } from './lib/domain/DataFecther';

// Search for Bitcoin symbols on Binance
const binanceSymbols = await fetchSymbolList(
  Source.binance,
  'BTC',
  { signal: abortController.signal }
);

console.log(binanceSymbols);
// Output: [{ ticker: 'BTCUSDT' }, { ticker: 'BTCBUSD' }, ...]

// Search for Apple-related symbols on Yahoo Finance
const yahooSymbols = await fetchSymbolList(
  Source.yfinance,
  'AAPL',
  {}
);

console.log(yahooSymbols);
// Output: [{ ticker: 'AAPL' }, { ticker: 'AAPL.MX' }, ...]

Use Cases

  • Symbol search/autocomplete in trading interfaces
  • Building watchlists
  • Symbol validation before fetching data
Source: DataFecther.ts:120-139

Source Enum

Enumeration of supported data providers.

Values

yfinance
number
Yahoo Finance - supports stocks, ETFs, indices, forex, cryptocurrencies
binance
number
Binance - cryptocurrency exchange data

Example

import { Source } from './lib/domain/DataFecther';

// Use enum values
const source = Source.binance;

// Check source type
if (source === Source.yfinance) {
  console.log('Using Yahoo Finance');
} else if (source === Source.binance) {
  console.log('Using Binance');
}
Source: DataFecther.ts:8-11

Kline Class

Represents a single candlestick/kline with OHLCV (Open-High-Low-Close-Volume) data.

Constructor

constructor(
  time: number,
  open: number,
  high: number,
  low: number,
  close: number,
  volume: number,
  openTime: number,
  closeTime: number,
  isClosed: boolean
)

Properties

time
number
Primary timestamp for this kline (milliseconds)
open
number
Opening price
high
number
Highest price during the period
low
number
Lowest price during the period
close
number
Closing price
volume
number
Trading volume during the period
openTime
number
Timestamp when the kline period opened (milliseconds)
closeTime
number
Timestamp when the kline period closed (milliseconds)
isClosed
boolean
Whether this kline is complete (closed) or still forming (real-time)
value
number
Getter that returns the close price (for compatibility with TVal)

Methods

update
(value: number, amount: number) => Kline
Updates a real-time kline with a new price tick and volume.
  • Updates high if the new value is higher
  • Updates low if the new value is lower
  • Sets close to the new value
  • Adds amount to cumulative volume
Returns: this (for method chaining)Source: Kline.ts:43-50
closeIt
() => Kline
Marks the kline as closed/completed.Returns: this (for method chaining)Source: Kline.ts:52-56
toJson
() => object
Serializes the kline to a JSON object.Returns: Object with properties: time, open, high, low, close, volume, isClosedSource: Kline.ts:58-68

Example Usage

import { Kline } from './lib/domain/Kline';

// Create a new kline
const kline = new Kline(
  Date.now(),          // time
  50000,               // open
  50500,               // high
  49800,               // low
  50200,               // close
  1500.5,              // volume
  Date.now() - 3600000, // openTime (1 hour ago)
  Date.now(),          // closeTime
  true                 // isClosed
);

// Access properties
console.log('Close price:', kline.close);
console.log('Value:', kline.value); // Same as close

// Update a real-time kline
kline.isClosed = false;
kline.update(50300, 50); // New price: 50300, volume: 50

// Close the kline
kline.closeIt();

// Serialize to JSON
const json = kline.toJson();
console.log(json);

Constant

KVAR_NAME
string
The variable name used to store klines in time series: "kline"Source: Kline.ts:4
Source: Kline.ts:6-71

Provider-Specific Notes

Binance Provider

  • Uses the pinets library’s Binance provider
  • Supports cryptocurrency pairs (e.g., BTCUSDT, ETHUSDT)
  • Timeframe conversion: VibeTrader timeframes are mapped to Binance/pinets equivalents
  • Data includes: open, high, low, close, volume, openTime, closeTime
Source: DataFecther.ts:49-86

Yahoo Finance Provider

  • Supports stocks, ETFs, indices, forex, and cryptocurrencies
  • Ticker format: Standard Yahoo Finance symbols (e.g., “AAPL”, “^GSPC”)
  • Timestamps are converted to Unix seconds for the Yahoo Finance API
  • Data includes: time, open, high, low, close, volume
Source: DataFecther.ts:88-117

Timeframe Handling

Both providers automatically calculate the start time based on:
  • The specified limit (number of klines)
  • The tframe (timeframe interval)
  • The tzone (timezone)
If startTime is provided, it overrides the calculated value.

Error Handling

If the remote provider fetch fails, the system attempts to load data from a local ./klines.json file as a fallback. This is useful for development and offline testing. Source: DataFecther.ts:37-47

Build docs developers (and LLMs) love