ScriptingHow toHow to shade the area between two plots

How to shade the area between two plots

plot returns a plot value. Assign each plot to a variable at the top level, then hand both variables to fill.

fill(series plot p1, series plot p2, const string title, series color color, const bool fillgaps) → void
indicator("Close against its average", "maband", true)

float average = talib.sma(close, 20)

c = plot(close, title = "Close", color = color.gray)
a = plot(average, title = "Average", color = color.blue)

fill(p1 = c, p2 = a, title = "Gap", color = color.new(color.blue, 0.1))

Name the arguments. There is a second fill form that joins two horizontal lines, and it takes the colour and the title in the opposite order. Naming them removes the difference.

If the shading should change colour per bar

The color parameter is series-form, so it may differ on every bar. Put the condition in that argument.

fill(p1 = c, p2 = a, title = "Gap", color = close > average ? color.new(color.green, 0.1) : color.new(color.red, 0.1))

If you are filling between two levels instead

Use hline twice and the other fill form. You cannot mix the two: fill joins two plots or two horizontal lines, and there is no form that takes one of each.

The failures to watch for

  • Both plots must carry the same offset for the fill to keep one. If they disagree, the fill uses no offset. Set the same offset on both, or on neither.
  • The plot variables have to be assigned at the top level, like the plot calls themselves.

See Fills and levels.

Ready to put this into practice?
Try GoCharting Premium
Unlock advanced orderflow, market profile, options desk, and real-time data — everything you just read about, live in your charts.
Upgrade