First & Last Bar

Yaser Rahmati | یاسر رحمتی

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
    0
  • isFirstBar: This variable is defined to detect if the current bar is the first bar of the chart or dataset.

    • barstate.isfirst is a built-in variable in Pine Script that returns true when the current bar is the first bar on the chart.

    • The if statement checks if barstate.isfirst is true. If it is, isFirstBar is set to 1; otherwise, it is set to 0.

isLastBar = if(barstate.islast)
    1
else
    0
  • isLastBar: This variable checks if the current bar is the last bar of the dataset.

    • barstate.islast is a built-in variable that returns true if the current bar is the last bar on the chart (the most recent data point).

    • Similar to isFirstBar, the if statement sets isLastBar to 1 if barstate.islast is true and 0 otherwise.

plot(isFirstBar, color=color.green)
plot(isLastBar, color=color.red)
  • plot(isFirstBar, color=color.green): This line plots the value of isFirstBar on the chart. If isFirstBar is 1 (meaning the current bar is the first one), it will plot a green point. If it's 0, nothing is plotted.

  • plot(isLastBar, color=color.red): This line plots the value of isLastBar on the chart. If isLastBar is 1 (meaning the current bar is the last one), it will plot a red point. If it's 0, 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, یاسر رحمتی

🌐 Personal Website 📄 Resume 🎥 Video Archive 💼 Finance Blog 🔐 Network & Security Notebook 🎬 Aparat Channel

Last updated