iBars

Returns the total number of bars available in the specified symbol's price history.

Syntax

iBars(Symbol: string, TimeFrame: number): number

Parameters

  • Symbol: The symbol to get data for

  • TimeFrame: The timeframe of the data (in minutes)

Return Value

Returns a number representing the total count of available bars.

Description

The iBars method returns the total number of bars available in the price history for a given symbol and timeframe. This count includes all bars from the oldest available bar up to the current (most recent) bar. The method is useful for determining the size of the historical data and for implementing lookback periods in technical analysis.

Example

// Get total number of bars for EURUSD on H1 timeframe
const totalBars = this.api.iBars("EURUSD", 60);

// Check if enough historical data is available
const requiredBars = 100;
if (this.api.iBars("EURUSD", 60) >= requiredBars) {
  console.log("Sufficient historical data available");
}

// Calculate average over all available bars
let sum = 0;
const bars = this.api.iBars("EURUSD", 60);
for (let i = 0; i < bars; i++) {
  sum += this.api.iClose("EURUSD", 60, i);
}
const average = sum / bars;

// Find the oldest available bar's time
const oldestBarIndex = this.api.iBars("EURUSD", 60) - 1;
const oldestTime = this.api.iTime("EURUSD", 60, oldestBarIndex);

// Check data availability across timeframes
const m1Bars = this.api.iBars("EURUSD", 1);
const h1Bars = this.api.iBars("EURUSD", 60);
const d1Bars = this.api.iBars("EURUSD", 1440);

Last updated