ex.bandit
ex.bandit

Reputation: 1

Pine Script Indicator: Highlight Background Based on d1, d2, d3, and d4 Values

I'm developing a Pine Script indicator that calculates four Stochastic Oscillator lines with different parameters (d1, d2, d3, and d4). I want to highlight the background area on the chart based on the values of these variables.

Goal:

Highlight the background green when all four variables (d1, d2, d3, and d4) are under 20. Highlight the background red when all four variables are over 80.

indicator(title="Sto4", shorttitle="Sto4", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
periodK1 = input.int(8, title="%K Length 1", minval=1)
smoothK1 = input.int(1, title="%K Smoothing 1", minval=1)
periodD1 = input.int(3, title="%D Smoothing 1", minval=1)
k1 = ta.sma(ta.stoch(close, high, low, periodK1), smoothK1)
d1 = ta.sma(k1, periodD1)
plot(k1, title="%K1", color=color.rgb(41, 98, 255, 100))
plot(d1, title="%D1", color=color.rgb(255, 255, 255))

periodK2 = input.int(12, title="%K Length 2", minval=1)
smoothK2 = input.int(1, title="%K Smoothing 2", minval=1)
periodD2 = input.int(6, title="%D Smoothing 2", minval=1)
k2 = ta.sma(ta.stoch(close, high, low, periodK2), smoothK2)
d2 = ta.sma(k2, periodD2)
plot(k2, title="%K2", color=color.rgb(41, 98, 255, 100))
plot(d2, title="%D2", color=color.rgb(255, 251, 0))

periodK3 = input.int(30, title="%K Length 3", minval=1)
smoothK3 = input.int(1, title="%K Smoothing 3", minval=1)
periodD3 = input.int(2, title="%D Smoothing 3", minval=1)
k3 = ta.sma(ta.stoch(close, high, low, periodK3), smoothK3)
d3 = ta.sma(k3, periodD3)
plot(k3, title="%K3", color=color.rgb(41, 98, 255, 100))
plot(d3, title="%D3", color=#FF6D00)

periodK4 = input.int(40, title="%K Length 4", minval=1)
smoothK4 = input.int(1, title="%K Smoothing 4", minval=1)
periodD4 = input.int(8, title="%D Smoothing 4", minval=1)
k4 = ta.sma(ta.stoch(close, high, low, periodK4), smoothK4)
d4 = ta.sma(k4, periodD4)
plot(k4, title="%K4", color=color.rgb(41, 98, 255, 100))
plot(d4, title="%D4", color=color.rgb(139, 33, 14))

h01 = hline(80, "Upper Band", color=#787B86)
hline(50, "Middle Band", color=color.new(#787B86, 50))
h11 = hline(20, "Lower Band", color=#787B86)
fill(h01, h11, color=color.rgb(33, 150, 243, 90), title="Background")`

I'd like to highlight the vertical area green in the background every time all four of the following; d1, d2, d3, and d4 variable are under 20 at the same time. I'd like to highlight the vertical area red in the background every time all four of the following; d1, d2, d3, and d4 are over 80 at the same time.

I've also referenced the following site but couldn't figure out the highlighting for just these periods of time where the variables are true.

https://www.tradingview.com/pine-script-docs/concepts/backgrounds/

Can someone please help me with this? I don't mind if this is just a vertical green and red line when the conditions are true. I've seen other indicators plot red and green circles. Any help is greatly appreciated.

I've tried the following but I get a "Syntax error at input 'line' at line 39, if isAllUnder20 and the same with line 44, if isAllOver80

 ```// Plot vertical lines for all d values under 20
isAllUnder20 = d1 < 20 and d2 < 20 and d3 < 20 and d4 < 20; // Add semicolon here
if isAllUnder20
  line.new(bar_index, low, bar_index, high, color=color.green, linewidth=2, extend=extend.both)

// Plot vertical lines for all d values over 80
isAllOver80 = d1 > 80 and d2 > 80 and d3 > 80 and d4 > 80;
if isAllOver80
  line.new(bar_index, low, bar_index, high, color=color.red, linewidth=2, extend=extend.both)```

Problem:

I'm encountering a syntax error ("Syntax error at input 'line'") when trying to implement the logic for highlighting the background with vertical lines. I've reviewed the Pine Script documentation on backgrounds (https://www.youtube.com/watch?v=iRLRy0badw8) but couldn't find a solution for highlighting specific timeframes based on variable conditions.

Desired Outcome:

Ideally, I'd like to have a solid green or red background fill the entire chart area when the respective conditions are met.
Alternatively, using vertical green and red lines at the current bar would also be acceptable.
Question:

Can anyone assist me with implementing the desired background highlighting based on the values of d1, d2, d3, and d4 in Pine Script?

Additional Information:

I've already tried using line.new to create vertical lines, but I'm open to alternative approaches that might achieve the desired effect.

Thank you!

Upvotes: 0

Views: 17

Answers (0)

Related Questions