pine-scriptpine-script-v5pine-script-v4

Overlay "true" and "false" at the same time in PineScript


Recently I wrote a simple indicator with PineScript and published it on Tradingview so that my friends could use it. The code was like this:

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © YavuzAkbay

//@version=5
indicator("Hodrick-Prescott Cycle Component", overlay=false)

// Input for the filter smoothness parameter
lambda = input.int(100, "Smoothness Parameter (λ)", minval=1)

// Input to switch between percentage and price difference views
displayMode = input.string("Percentage", "Display Mode", options=["Percentage", "Price Difference"])

// Function to calculate the approximate HP trend component
hpFilter(src, lambda) =>
    var float trend = na
    trend := na(trend[1]) ? src : (src + (lambda - 1) * nz(trend[1])) / lambda
    trend

// Calculate the HP Filter (trend component)
hp_trend = hpFilter(close, lambda)

// Calculate the cycle component as both percentage and price difference
cycle_pct = (close - hp_trend) / hp_trend * 100
cycle_price = close - hp_trend

// Conditional plotting based on the selected display mode
plot(displayMode == "Percentage" ? cycle_pct : na, title="HP Filter Divergence (%)", color=color.red, linewidth=2)
plot(displayMode == "Price Difference" ? cycle_price : na, title="HP Filter Divergence (Price)", color=color.blue, linewidth=2)
hline(0, "Zero Line", linestyle=hline.style_dashed)

But I also needed to draw the “hp_trend” on the chart. So my friends would be able to see the trend and also see the difference between the trend and the price as a percentage in a new panel. But the problem here is that I thought I couldn't have the indicator plotted on the new panel and on the chart at the same time, so I shared both this indicator and the versions of the same code that plotted “hp_trend” on the chart.

Everything was going well and 2 days later I received a message from moderation that I could not post the same code twice. They told me that I could do it in one code. That's why they're deleting one of my indicators.

You can see the message here:

enter image description here

Is this possible and how can I do it? Because I really need both of them.

I've searched on Stackoverflow and in the documentation to put both overlays at the same time, but everyone says it can't be done. However, Tradingview moderation says it can be done.


Solution

  • A new argument for all drawing types (plot, line, label etc.) called force_overlay have been introduced lately.

    force_overlay (const bool) If true, the drawing will display on the main chart pane, even when the script occupies a separate pane. Optional. The default is false.

    So, you just set your indicator as overlay=false, then you plot whatever you want to plot in a separate pane as you would normally do.

    For the drawings you want to plot on your main chart, you set force_overlay of the function call to true.

    See below for an example:

    //@version=5
    indicator("My script", overlay=false)
    
    rsi_val = ta.rsi(close, 14)
    daily_high = request.security(syminfo.tickerid, "D", high, lookahead=barmerge.lookahead_on)
    
    
    plot(rsi_val, "RSI", color.purple)
    
    l = line.new(bar_index, daily_high, bar_index + 1, daily_high, color=color.green, extend=extend.both, force_overlay=true)
    line.delete(l[1])
    
    lbl = label.new(bar_index + 10, high, syminfo.tickerid, force_overlay=true)
    label.delete(lbl[1])
    
    plot(hl2, "HL2", color.white, 3, force_overlay=true)
    

    enter image description here