Bars

Returns the total number of bars.

Syntax

Bars(): number

Return Value

Returns a number representing the total count of available bars.

Description

The Bars method returns the total number of price bars available in the current symbol's history. This count includes all bars from the earliest available data point up to and including the current bar.

Example

// Get total number of bars
const totalBars = this.api.Bars();
console.log(`Total available bars: ${totalBars}`);

// Check if enough history for analysis
const requiredBars = 20;
if (this.api.Bars() >= requiredBars) {
  // Perform analysis requiring 20 bars of history
}

// Process last 10 bars (if available)
const barsToProcess = Math.min(10, this.api.Bars());
for (let i = 0; i < barsToProcess; i++) {
  const close = this.api.Close(i);
  console.log(`Bar -${i} close price: ${close}`);
}

// Calculate valid shift range
const maxShift = this.api.Bars() - 1;
console.log(`Valid shift range: 0 to ${maxShift}`);

Last updated