Skip to main content
In this tutorial we plot the closing price and color it per bar: first green where the close is above the previous close and red where it is not, then with a smooth blend from red to green driven by the RSI. Every step is a whole script: replace what is in the editor, and check it.
1

plot the close

One line in the default color. plot’s color argument has a default and we have not passed one yet.
2

color it

plot takes a color, and the color may differ on every bar, so the test goes inside the argument. The conditional expression condition ? a : b produces a when the condition holds and b when it does not.
The line is green on every bar that closed above the bar before it and red on the rest. close[1] is the close one bar back.
3

name the condition

The plot call does two jobs. Move the test to its own line.
The chart is identical; the script reads better.
4

soften the colors

color.new(color, alpha) returns the same color at a different opacity. Opacity runs from 0 to 1: 1 is solid and 0 is fully transparent. Read it as how much of the color you want.
The line is thicker and both colors are half strength, so the bars underneath show through.A value outside 0 to 1 is clamped when the script runs, and a literal outside the range gets a warning from the editor. The usual slip is to write a percentage: color.new(color.red, 50) warns opacity 50 is outside 0–1; 1 is solid. Write 0.5.
5

color by a value

color.from_gradient(value, bottom_value, top_value, bottom_color, top_color) returns a color that blends from bottom_color to top_color as value goes from bottom_value to top_value. A value below the bottom gives the bottom color and a value above the top gives the top color; between them each channel, opacity included, is blended in proportion. The RSI runs from 0 to 100, so 30 and 70 make good ends.
The line is red where the 14-bar RSI is 30 or lower, green at 70 or higher, and shades through the colors in between. If bottom_value equals top_value, or any argument is na, the result is na (no color).

The mistake to avoid

It is tempting to write the condition around the call:
The compiler says: plot() must be called in the global scope. The plot family has to be called at the top level of the script, so the condition belongs in an argument, as in steps 2 to 5.

What we built

An indicator that answers a question per bar in the color of the line. The shape to reuse: compute a bool or a number, then use it inside plot’s color argument. The same shape colors the bars with barcolor and the background with bgcolor (see How to color bars by a condition).

Next

Color and transparency

the color constants, color.rgb and #RRGGBBAA literals.

Operators and expressions

the conditional expression and the other operators.

Branching

what if is for, given that it cannot wrap a plot.