How To Plot Previous Close Value?

FXC911 – Forex & Crypto News & Insights

Goals

The script plots two lines on the chart:

  • The current close value in red.

  • The previous close value in white.

Pine Script Code

//@version=5
indicator("Plot Previous Close")

// This is the current close value
plot(close, color=color.red)

// This is the previous close value
plot(close[1], color=color.white)

Code Output

Explanation

1. Version Declaration:

//@version=5
  • This line specifies that the script is written in Pine Script version 5.

2. Indicator Declaration:

indicator("Plot Previous Close")
  • This line defines a new indicator named "Plot Previous Close". When this script is applied to a chart, this name will be shown in the list of applied indicators.

3. Current Close Value:

plot(close, color=color.red)
  • plot(close, color=color.red): The plot function is used to draw a plot on the chart.

  • close: This is a built-in variable in Pine Script that represents the closing price of the current bar (candlestick).

  • color=color.red: This specifies that the plot of the current close price will be colored red.

4. Previous Close Value:

plot(close[1], color=color.white)
  • plot(close[1], color=color.white): Again, the plot function is used to draw a plot.

  • close[1]: This represents the closing price of the previous bar. In Pine Script, appending [1] to a variable references its value from one bar ago.

  • color=color.white: This specifies that the plot of the previous close price will be colored white.

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:

Last updated