How To Color a Bar?

FXC911 – Forex & Crypto News & Insights

Goals

This Pine Script code is written in version 5 and is used to create a simple indicator called "Bar Color" that changes the color of the bars on a TradingView chart based on the relationship between the closing and opening prices of the bars.

Pine Script Code

//@version=5
indicator("Bar Color")

barColor = if(close > open)
	color.white
else
	color.orange

barcolor(barColor)

Code Output

Explanation

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

indicator("Bar Color")
  • This line defines a new indicator with the name "Bar Color". This name will be displayed in TradingView's indicator list.

barColor = if(close > open) color.white else color.orange
  • This block of code creates a variable named barColor and assigns it a value based on a conditional statement:

    • if(close > open): Checks if the closing price of the bar is greater than the opening price.

      • If true, barColor is set to color.white.

      • If false, barColor is set to color.orange.

barcolor(barColor)
  • This function call changes the color of the bars on the chart to the color stored in barColor.

  • barColor will be color.white if the closing price is higher than the opening price, and color.orange if the closing price is lower than or equal to the opening price.

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