Home > Software design >  Boolean input to negate if statements in pinescript
Boolean input to negate if statements in pinescript

Time:01-31

In short I have an indicator that has multiple requirements for setting background colors and bar colors. All of this was fine until I wanted to add a small wrinkle to it.

Basically I have two lines (plots), one of which I call the signal line and the other the baseline, If a value is above one or the other then the background is blue and if it is below then background is red (this also goes for barcolor if people so wish to have bar coloring on). I have two separate boolean inputs for each of these two lines so that the background color or bar color is colored depending on what the user prefers. I also have a boolean for bar colors just when the value crosses up or down one of the two lines, but we'll ignore that to keep this simpler.

One of the two lines I mentioned above is a moving average (signal line) of the values being measured by the indicator. I want to add a boolean input that if true only colors the bars or background IF the signal line is increasing and not if it is decreasing.

So, if value is above signal line or baseline AND signal line is rising then color background blue, and if not, red. The problem I've run into is that regardless of the signal line rising being true, if the indicator value is above the signal or baseline then the background or bars are always going to be colored in those areas. What I want is if the boolean input for the rising signal line is true then it ONLY colors behind those areas where it is true and no other areas. If that makes sense. Problem is it always colors for areas where the indicator value is above the signal/baseline regardless if the signal line is rising or not, even if the boolean for a rising signal line is true, which defeats the purpose of having this rising signal line requirement.

I'm going to add the pertinent code below. If someone could help that would be wonderful. I can't figure out how to make a negation of background colors and bar colors, which it seems is what I need? The only other alternative I can think of (because I'm an awful coder) is to have separate boolean inputs for 'if value is above signal line' and 'if value is above signal line and signal line is rising' and 'if value is above baseline' and 'if value is above baseline and signal line is rising', plus bar coloring and the crossing up or down vs. being simply above or below. This would make for a grand total of 16 boolean inputs, which is insane obviously.

//BAR COLOR AND BACKGROUND COLOR SIGNAL LINE INPUTS
barcolor_signal = input.bool(defval=false, title='Bar Colors', group='Signal Line')
bg_signal = input.bool(defval=false, title='Background Colored', group='Signal Line')
xover_signal = input.bool(false, 'Volatility Advance', group='Signal Line')
xunder_signal = input.bool(false, 'Volatility Decline', group='Signal Line')

//BAR COLOR AND BACKGROUND COLOR BASELINE INPUTS
barcolor_bline = input.bool(defval=true, title='Bar Colors', group='Baseline')
bg_bline = input.bool(defval=false, title='Background Colored', group='Baseline')
xover_bline = input.bool(false, 'Volatility Advance', group='Baseline')
xunder_bline = input.bool(false, 'Volatility Decline', group='Baseline')

////BOOLEAN
signal_rise = input.bool(defval=true, title='Require Signal Line Rising', tooltip='Require the signal line to be rising to highlight increasing volatility.')

For the inputs, the first group is for if the value is above/below or crossing the signal line, the second group is for if the value is above/below or crossing the baseline, and the last input is signal line rising input.

///BACKGROUND AND CANDLE COLORING
sig_rise = signal_rise and signal_line > signal_line[1]

bline_rise = avg_range > baseline
bline_fall = avg_range < baseline
avg_sig_rise = avg_range > signal_line
avg_sig_fall = avg_range < signal_line


bgcolor(bg_bline and bline_rise ? color.new(#445b84, 50) : bg_bline and bline_fall ? color.new(#844444, 100) : na, title='Baseline Background')

bgcolor(sig_rise and bg_bline and bline_rise ? color.new(#445b84, 50) : sig_rise and bg_bline and bline_fall ? color.new(#844444, 100) : na, title='Baseline Background | Signal Line Rising')

bgcolor(bg_signal and avg_sig_rise ? color.new(#445b84, 50) : bg_signal and avg_sig_fall ? color.new(#844444, 100) : na, title='Signal Line Background')

bgcolor(sig_rise and bg_signal and avg_sig_rise ? color.new(#445b84, 50) : sig_rise and bg_signal and avg_sig_fall ? color.new(#844444, 100) : na, title='Signal Line Background | Signal Line Rising')

I'm not adding the code for barcolor because whatever solution there is should work for that too I would think.

You can see in the first line for bgcolor that that is the simplest--if background is true and avg_range is above baseline then COLOR. The one below requires the signal line to be rising, and the two below are if the avg_range is above the signal line.

I need for the first and third lines to be false if the second or fourth are true, OR I need another way to write this.

Appreciate any help with this.

CodePudding user response:

Basically the way you use ternary operators in your bgcolor() is correct.

If those bgs are dependent on each other I'd evaluate first in variables then apply the coloring.

baseLineRising = bg_bline and bline_rise
baseLineFalling = bg_bline and bline_fall
baseLineRisingSigRising = sig_rise and bg_bline and bline_rise
baseLineFallingSigRising = sig_rise and bg_bline and bline_fall
avgSigRising = bg_signal and avg_sig_rise
avgSigFalling = bg_signal and avg_sig_fall
sigRisingAvgRising = sig_rise and bg_signal and avg_sig_rise
sigRisingAvgFalling = sig_rise and bg_signal and avg_sig_fall

bgcolor(baseLineRising ? color.new(#445b84, 50) : baseLineFalling ? color.new(#844444, 100) : na, title='Baseline Background')

bgcolor(baseLineSigRising ? color.new(#445b84, 50) : baseLineFallingSigRising ? color.new(#844444, 100) : na, title='Baseline Background | Signal Line Rising')

bgcolor(avgSigRising ? color.new(#445b84, 50) : avgSigFalling ? color.new(#844444, 100) : na, title='Signal Line Background')

bgcolor(sigRisingAvgRising ? color.new(#445b84, 50) : sigRisingAvgFalling ? color.new(#844444, 100) : na, title='Signal Line Background | Signal Line Rising')

This way you are much more flexible combining your conditions. Eg.

secondOrForthtrue = baseLineSigRising or baseLineFallingSigRising or sigRisingAvgRising or sigRisingAvgFalling // I assume they are true if they have a coloring at all
bgcolor(baseLineRising and not secondOrForthtrue ? color.new(#445b84, 50) : baseLineFalling and not secondOrForthtrue ? color.new(#844444, 100) : na, title='Baseline Background')

CodePudding user response:

I finally figured out what I needed to do. For me, being a non-coder and not in the tech profession and only having some experience coding in pinescript (the only language I can code in) this took a significant amount of my time, but I'm happy to have figured it out (I can be quite dogged at times).

Essentially instead of trying to declare everything using ternary operators in bgcolor and barcolor (because it won't work), I created some if-else-if conditions that I hoped would allow me to negate a condition that would be true all the time even if a certain other input variable was also true. Basically, if A and B are true, then C. But if input D is also true, then not C, but E. This actually is stated like if A and B and not D are true, then C, else if A, B and D are true then E. The 'not D' part is what I was struggling with using ternary operators (and maybe it's not possible in this way).

//SIGNAL LINE RISING/FALLING
signal_rising = signal_rise and signal_line >= signal_line[1]
signal_falling = signal_rise and signal_line < signal_line[1]


//BACKGROUND CONDITIONS AND COLORING
above_bline = bg_bline and avg_range > baseline
below_bline = bg_bline and avg_range < baseline
above_signal = bg_signal and avg_range > signal_line
below_signal = bg_signal and avg_range < signal_line

AboveBaselineSignalRising = if (signal_rising and above_bline)
    color.new(#445b84, 50)
else if (signal_rising and below_bline or signal_falling and below_bline)
    color.new(#844444, 100)
AboveBaseline = if (not signal_rise and above_bline)
    color.new(#445b84, 50)
else if (not signal_rise and below_bline)
    color.new(#844444, 100)

AboveSignalLineSignalRising = if (signal_rising and above_signal)
    color.new(#445b84, 50)
else if (signal_rising and below_signal or signal_falling and below_signal)
    color.new(#844444, 100)
AboveSignalLine = if (not signal_rise and above_signal)
    color.new(#445b84, 50)
else if (not signal_rise and below_signal)
    color.new(#844444, 100)


bgcolor(AboveBaselineSignalRising, title='Background | ACBR > Baseline & Signal Rising')
bgcolor(AboveBaseline, title='Background | ACBR > Baseline')
bgcolor(AboveSignalLineSignalRising, title='Background | ACBR > Signal Line & Signal Rising')
bgcolor(AboveSignalLine, title='Background | ACBR > Signal Line')


//BAR COLOR CONDITIONS AND COLORING (ACBR CROSSING ABOVE OR BELOW BASELINE/SIGNAL LINE)
barcolor_xover_bline = xover_bline and ta.crossover(avg_range, baseline)
barcolor_xunder_bline = xunder_bline and ta.crossunder(avg_range, baseline)
barcolor_xover_signal = xover_signal and ta.crossover(avg_range, signal_line)
barcolor_xunder_signal = xunder_signal and ta.crossunder(avg_range, signal_line)

BarColorBaselineCrossSignalRising = if (signal_rising and barcolor_xover_bline)
    #848044
else if (barcolor_xunder_bline)
    #5a4484
BarColorBaselineCross = if (not signal_rise and barcolor_xover_bline)
    #848044
else if (not signal_rise and barcolor_xunder_bline)
    #5a4484
BarColorSignalLineCrossSignalRising = if (signal_rising and barcolor_xover_signal)
    #848044
else if (barcolor_xunder_signal)
    #5a4484
BarColorSignalLineCross = if (not signal_rise and barcolor_xover_signal)
    #848044
else if (not signal_rise and barcolor_xunder_signal)
    #5a4484


barcolor(BarColorBaselineCrossSignalRising, title='Bar Color | ACBR Baseline Cross & Signal Rising')
barcolor(BarColorBaselineCross, title='Bar Color | ACBR Baseline Cross ')
barcolor(BarColorSignalLineCrossSignalRising, title='Bar Color | ACBR Signal Line Cross & Signal Rising')
barcolor(BarColorSignalLineCross, title='Bar Color | ACBR Signal Line Cross')


//BAR COLOR CONDITIONS AND COLORING (ACBR ABOVE OR BELOW BASELINE/SIGNAL LINE)
barcolor_above_bline = barcolor_bline and avg_range > baseline
barcolor_below_bline = barcolor_bline and avg_range < baseline
barcolor_above_signal = barcolor_signal and avg_range > signal_line
barcolor_below_signal = barcolor_signal and avg_range < signal_line
BarColorAboveBaselineSignalRising = if (signal_rising and barcolor_above_bline)
    color.new(#445b84, 0)
else if (signal_rising and barcolor_below_bline or signal_falling and barcolor_above_bline or signal_falling and barcolor_below_bline)
    color.new(#844444, 0)
BarColorAboveBaseline = if (not signal_rise and barcolor_above_bline)
    color.new(#445b84, 0)
else if (not signal_rise and barcolor_below_bline)
    color.new(#844444, 0)

BarColorAboveSignalLineSignalRising = if (signal_rising and barcolor_above_signal)
    color.new(#445b84, 0)
else if (signal_rising and barcolor_below_signal or signal_falling and barcolor_above_signal or signal_falling and barcolor_below_signal)
    color.new(#844444, 0)
BarColorAboveSignalLine = if (not signal_rise and barcolor_above_signal)
    color.new(#445b84, 0)
else if (not signal_rise and barcolor_below_signal)
    color.new(#844444, 0)


barcolor(BarColorAboveBaselineSignalRising, title='Bar Color | ACBR > Baseline & Signal Rising')
barcolor(BarColorAboveBaseline, title='Bar Color | ACBR > Baseline')
barcolor(BarColorAboveSignalLineSignalRising, title='Bar Color | ACBR > Signal Line & Signal Rising')
barcolor(BarColorAboveSignalLine, title='Bar Color | ACBR > Signal Line')
  • Related