'Trouble finding the 2 values in this Tradingview script

I'm trying to add the crossing of the two lines (red and blue) in the indicator as part of my enterlong and entershort trading conditions but cant seem to figure out the 2 lines. I'd like to have them at a < or > and have tried enterlong = (val > signalperiod) , (val > ma) but can't seem to figure it out

length = input(20, title="BB Length")
mult = input(2.0,title="BB MultFactor")
lengthKC=input(20, title="KC Length")
multKC = input(1.5, title="KC MultFactor")
SignalPeriod=input(5, title="Signal Length")

useTrueRange = input(true, title="Use TrueRange (KC)", type=bool)

// Calculate BB
source = close
basis = sma(source, length)
dev = multKC * stdev(source, length)
upperBB = basis + dev
lowerBB = basis - dev

// Calculate KC
ma = sma(source, lengthKC)
range = useTrueRange ? tr : (high - low)
rangema = sma(range, lengthKC)
upperKC = ma + rangema * multKC
lowerKC = ma - rangema * multKC

sqzOn  = (lowerBB > lowerKC) and (upperBB < upperKC)
sqzOff = (lowerBB < lowerKC) and (upperBB > upperKC)
noSqz  = (sqzOn == false) and (sqzOff == false)

val = linreg(source  -  avg(avg(highest(high, lengthKC), lowest(low, 
lengthKC)),sma(close,lengthKC)), 
        lengthKC,0)

bcolor = iff( val > 0, 
        iff( val > nz(val[1]), lime, green),
        iff( val < nz(val[1]), red, maroon))
scolor = noSqz ? blue : sqzOn ? black : gray 
plot(val, color=blue, linewidth=2)
plot(0, color=scolor, style=cross, linewidth=2)
plot(sma(val,SignalPeriod), color=red, linewidth=2)


Solution 1:[1]

The blue line is val and the red line is sma(val,SignalPeriod).

Once we know this, we can use the crossover() and crossunder() functions to check if they crossed each other.

For example:

val_sma = sma(val,SignalPeriod)

enterlong = crossover(val, val_sma)
entershort = crossunder(val, val_sma)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vitruvius