First & Last Bar
FXC911 – Forex & Crypto News & Insights
Goals
The script creates a simple indicator that highlights the first bar of the chart in green and the last bar in red. This can be useful for visual references when analyzing data that spans a large period or for identifying the start and end of a dataset.
Pine Script Code
//@version=5
indicator("First & Last Bar")
isFirstBar = if(barstate.isfirst)
	1
else
	0
isLastBar = if(barstate.islast)
	1
else
	0
	
plot(isFirstBar, color=color.green)
plot(isLastBar, color=color.red)Code Output
//@version=5
indicator("First & Last Bar")//@version=5: This line specifies that the script is written in Pine Script version 5, the latest version at the time.indicator("First & Last Bar"): This line defines the script as an indicator and sets its name to "First & Last Bar". This name will appear in the chart's indicators list in TradingView.
isFirstBar = if(barstate.isfirst)
    1
else
    0isFirstBar: This variable is defined to detect if the current bar is the first bar of the chart or dataset.barstate.isfirstis a built-in variable in Pine Script that returnstruewhen the current bar is the first bar on the chart.The
ifstatement checks ifbarstate.isfirstistrue. If it is,isFirstBaris set to1; otherwise, it is set to0.
isLastBar = if(barstate.islast)
    1
else
    0isLastBar: This variable checks if the current bar is the last bar of the dataset.barstate.islastis a built-in variable that returnstrueif the current bar is the last bar on the chart (the most recent data point).Similar to
isFirstBar, theifstatement setsisLastBarto1ifbarstate.islastistrueand0otherwise.
plot(isFirstBar, color=color.green)
plot(isLastBar, color=color.red)plot(isFirstBar, color=color.green): This line plots the value ofisFirstBaron the chart. IfisFirstBaris1(meaning the current bar is the first one), it will plot a green point. If it's0, nothing is plotted.plot(isLastBar, color=color.red): This line plots the value ofisLastBaron the chart. IfisLastBaris1(meaning the current bar is the last one), it will plot a red point. If it's0, nothing is plotted.
Keywords
pine script, trading view, script editor, indicators, strategies, backtesting, alerts, custom scripts, stock analysis, charting tools, technical analysis, built-in functions, pine script syntax, trading bots, automated trading, scripting language, market data, trading signals, financial markets, programming trading strategies
Visit me on:
🚀 Finance & Trading Insights: fxc911.ir
🐦 Twitter: x.com/fxc911_official/
Last updated