pine-scriptfilltradingview-api

How to change the display order of a fill


I want to do the fill between 2 plots first, so the fill is line #37 in my code:

37 fill(oblv, obl, color=color.new(color.white, 64), title='Fill Overbought' )

after the fill, I would like plot my moving averages and other stuff like in line 45:

45 plot(rsiMA, "RSI-based MA", color=color.orange)

in my code fill is the 1st thing I want to do and then plot other stuff(ma, rsi, ...), but the problem with PineScript is it always does fill last, so fill covers the other plots.

In Style under Settings it appears last instead of first, see the attached:

enter image description here

Is there any way to control the order of fills?


Solution

  • The order of the plots, fills and hlines could be controlled with the explicit_plot_zorder parameter of the indicator() function. If true, the plots will be drawn based on the order in which they appear in the indicator's code, each newer plot being drawn above the previous ones. This script, for example, calls for a plot() function after the fill(), hence plots on top:

    //@version=5
    indicator("fill", overlay=true, explicit_plot_zorder = true)
    ma1 = ta.sma(close, 20)
    ma2 = ta.sma(close, 50)
    ma3 = ta.sma(close, 200)
    
    plotMa1 = plot(ma1)
    plotMa3 = plot(ma3)
    
    fill(plotMa1, plotMa3, color=color.blue)
    
    plot(ma2, color = color.red, linewidth = 3)
    

    enter image description here

    Using the explicit_plot_zorder = false will force the script to draw all plots first, and only after that all the fill() functions will be drawn on top. enter image description here

    More info about he Pine Script z-index: https://www.tradingview.com/pine-script-docs/en/v5/concepts/Colors.html?highlight=explicit_plot_zorder#z-index