0
votes

I'm trying to have the entire background colored on a condition (e.g. green) if the last close is above the close 50 bars back and red if it's below.
I've tried different approaches and I can change the color, but it doesn't end up coloring the entire background, but only certain areas.

My script:

bgcolor(close[50] >= open[1] ? color.red : color.green, transp=70)

split colors in background

In this case the entire background should be red, since the last close is below the close 50 bars back (indicated by the yellow label).
Any ideas on what I need to change?

1
the last close do you mean the very last on the chart or last relative to a bar is being handled on current iteration? - Michel_T.
I suppose you mean the very last. You can't fill entire background in that way, because that'd mean that you have changed past, which is impossible. - Michel_T.
Hi Michel, basically I'll add the script to any chart I want to analyse. The "last" bar would be the last closed bar, disregarding the bar that is currently active. I was hoping to quickly visualize the current trend on the chart by coloring the background accordingly. That wouldn't require changing the past (which might be a nice power, though😉), only setting a color according to the relative position of the last close vs. the close 50 bars back. 🤔 Steve - steveR
Must that be an entire chart to fill with the color or half - is enough? I think you could fill half of the chart in one color and the second half - in another color and then depending on the condition, you might offset the background to half and get visible chart to be colored as you want. - Michel_T.
I could probably live with that. Or I may need to find another way to quickly display the trend over a certain period. An arrow, maybe? I know I could just look at the chart, but a visual indication would be neater and just a little quicker... - steveR

1 Answers

0
votes

This will color the background on your condition. It uses a very wide line to do it, and because of that the indicator is occupying all the background, so some chart functions like the measuring tool cannot be used with Shift-Click, but it will work if you select its tool explicitly.

You can play with the position and width of the background if you don't want it to cover the whole chart.

The background is very light. If you want to change its brightness, you'll need to play with the transparency in the two color.new() calls, as it cannot be controlled from an input:

//@version=4
study("", "", true)

offsetCalc  = input(50,     "Close lookback", minval = 2)
offstBg     = input(100,    "Background: Horizontal Offset to its Center", minval = 0, step = 5)
lineWidth   = input(10000,  "Background: Width", minval = 0, step = 100)

condUp      = barstate.islast and close[1] > close[offsetCalc]
condDn      = barstate.islast and close[1] < close[offsetCalc]
c_lineColor = condUp ? color.new(color.green, 97) : condDn ? color.new(color.maroon, 97) : na

if barstate.islast
    var line bg = na
    line.delete(bg)
    bg := line.new(bar_index[offstBg], low - tr, bar_index[offstBg], high + tr, color = c_lineColor, extend = extend.both, width = lineWidth)