Bar States
Introduction
The barstate namespace includes a set of built-in variables that allow scripts to identify various properties of the bar currently being processed.
These states enable you to limit the execution or logic of your code to specific bars.
Some of these built-in variables provide information about the trading session to which the current bar belongs. These are further detailed in the Session States section.
Bar State Built-In Variable
bar_index`
Represents the index of the current bar. The numbering starts from zero, with the first bar having an index of 0.
Type
series int
Example:
// Example usage of bar_index
plot(bar_index, title="Bar Index")
Note:
Note that bar indexing starts from 0 on the first historical bar.
Please note that using this variable/function can cause indicator repainting.
### barstate.isfirst`
Returns `true` if the current bar is the first bar in the barset; otherwise, it returns `false`.
#### Type
- **`series bool`**
Note:
Lipi script code that uses this variable could calculate differently on history and real-time data.
Please note that using this variable/function can cause indicator repainting.
### barstate.ishistory`
Returns `true` if the current bar is a historical bar; otherwise, it returns `false`.
#### Type
- **`series bool`**
Note:
Lipi script code that uses this variable could calculate differently on history and real-time data.
Please note that using this variable/function can cause indicator repainting.
### barstate.islast`
Returns `true` if the current bar is the last bar in the barset; otherwise, it returns `false`. This condition is always `true` for real-time bars in the barset.
#### Type
- **`series bool`**
Note:
Lipi script code that uses this variable could calculate differently on history and real-time data.
Please note that using this variable/function can cause indicator repainting.
### barstate.islastconfirmedhistory`
Returns `true` if the script is executing on the last bar of the dataset when the market is closed, or on the bar immediately preceding the real-time bar when the market is open. Returns `false` otherwise.
#### Type
- **`series bool`**
Note:
Lipi script code that uses this variable could calculate differently on history and real-time data.
Please note that using this variable/function can cause indicator repainting.
### barstate.isnew`
Returns `true` if the script is currently calculating on a new bar; otherwise, it returns `false`. This variable is `true` when processing historical bars or during the first update of a newly created real-time bar.
#### Type
- **`series bool`**
Example:
```js
indicator("")
updateNo() {
intra int updateNo = na
if barstate.isnew {
updateNo := 1
} else {
updateNo := updateNo + 1
}
return updateNo
Note:
Lipi script code that uses this variable could calculate differently on history and real-time data.
Please note that using this variable/function can cause indicator repainting.
### barstate.isrealtime`
Returns `true` if the current bar is a real-time bar; otherwise, it returns `false`.
#### Type
- **`series bool`**
Note:
Lipi script code that uses this variable could calculate differently on history and real-time data.
Please note that using this variable/function can cause indicator repainting.Ready to put this into practice?
Try GoCharting Premium
Unlock advanced orderflow, market profile, options desk, and real-time data — everything you just read about, live in your charts.