Skip to content

Bar

Kind: module

Namespace: bar

Provides functions for accessing the current bar state and detecting time-based boundaries.

bar.index()

Kind: func

Returns the index of the current bar.

Syntax

bar.index() → int

Returns

Zero-based index of the current bar. First bar is 0.

Example

javascript
function onTick() {
    // plot bar index as a rising line
    spline(bar.index(), {title: "Bar #"});
}

bar.isNew()

Kind: func

Checks if this is the first tick of a new bar.

Syntax

bar.isNew() → bool

Returns

true on the first tick of a new bar (including all historical bars), false on subsequent real-time updates to the same bar.

bar.isConfirmed()

Kind: func

Checks if the current bar is confirmed (closed).

Syntax

bar.isConfirmed() → bool

Returns

true if the bar is confirmed and will not change, false if the bar is still updating.

bar.isHistory()

Kind: func

Checks if the current bar is historical data.

Syntax

bar.isHistory() → bool

Returns

true if processing historical data, false if processing realtime data.

Example

javascript
function onTick() {
    const sma = ta.sma(close, 20);

    // different colors for history vs realtime
    const lineColor = bar.isHistory() ? color.GRAY : color.BLUE;

    spline(sma, {title: "SMA", color: lineColor});
}

bar.isNewDay()

Kind: func

Checks if this bar starts a new day.

Syntax

bar.isNewDay() → bool

Returns

true if the current bar is the first bar of a new day, false otherwise.

bar.isNewWeek()

Kind: func

Checks if this bar starts a new week.

Syntax

bar.isNewWeek() → bool

Returns

true if the current bar is the first bar of a new week, false otherwise.

bar.isNewMonth()

Kind: func

Checks if this bar starts a new month.

Syntax

bar.isNewMonth() → bool

Returns

true if the current bar is the first bar of a new month, false otherwise.