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
Code Output
//@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
: 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 returnstrue
when the current bar is the first bar on the chart.The
if
statement checks ifbarstate.isfirst
istrue
. If it is,isFirstBar
is set to1
; otherwise, it is set to0
.
isLastBar
: This variable checks if the current bar is the last bar of the dataset.barstate.islast
is a built-in variable that returnstrue
if the current bar is the last bar on the chart (the most recent data point).Similar to
isFirstBar
, theif
statement setsisLastBar
to1
ifbarstate.islast
istrue
and0
otherwise.
plot(isFirstBar, color=color.green)
: This line plots the value ofisFirstBar
on the chart. IfisFirstBar
is1
(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 ofisLastBar
on the chart. IfisLastBar
is1
(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
, یاسر رحمتی
External Links
🌐 Personal Website 📄 Resume 🎥 Video Archive 💼 Finance Blog 🔐 Network & Security Notebook 🎬 Aparat Channel
Last updated