'Grabbing data from Higher Time Frame yields choppy results

This strategy enters order long when both 15 & 3 min waves are in phase(long).

When I save and or open for the first time, the table fields are in a state (True or False) that matches the real life state of those plots as plotted on charts in there respective time frames (15 & 3). The 3m is from base chart so its consistency is not in question. However, the 15m HTF data, and its table field are not consistent with real life. When first opened, the field matches real life until the 3m bar runs out and switches to the next bar. I would like to know why this is.

See code below. Thanks in advance for any help.

//@version=5
strategy(title="Overstack", calc_on_every_tick=true )

////// sQUEEZE mOMO original //////////////////
length = input(20, inline="BollingerBands", group="Squeeze Momentum", title="Bollinger Bands Length")
mult = input(2.0, inline="BollingerBands", group="Squeeze Momentum", title="MultFactor")
lengthKC=input(20, inline="KeltnerChannel", group="Squeeze Momentum", title="Keltner Channel Length")
multKC = input(1.5, inline="KeltnerChannel", group="Squeeze Momentum", title="MultFactor")
useTrueRange = input.bool(true, inline="KeltnerChannel", group="Squeeze Momentum", title="Use TrueRange")

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

// Calculate KC
ma = ta.sma(source, lengthKC)
rangee = useTrueRange ? ta.tr : (high - low)
rangema = ta.sma(rangee, 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 = ta.linreg(source  -  math.avg(math.avg(ta.highest(high, lengthKC), ta.lowest(low, lengthKC)),ta.sma(close,lengthKC)), lengthKC,0)

// grab higher val data
val_15 = request.security(syminfo.ticker,"15",val)

       //////////////////// Long ///////////////////////////////////


// val slope calc (good for both long and short)
valslope_15 = fixnan(val_15 - nz(val_15[1]))
valslope_3 = fixnan(val - nz(val[1]))

// val slope sufficient or not for long?
vallong_15 = valslope_15 > 1
vallong_3 = valslope_3 > 1

///// LONG
Long_15 = vallong_15                             
Long_3 = vallong_3                              

// Go Long Rules and Order:
GoLong = Long_15 and Long_3 
strategy.entry("Long", strategy.long, when=GoLong)

strategy.exit("exit", "Long", profit=100, loss=40)

        /////////////// SHORT //////////////////////////////

// val slope sufficient or not for short?
valshort_15 = valslope_15 < -1
valshort_3 = valslope_3 < -1

///// Short
Short_15 = valshort_15            
Short_3 = valshort_3  

// Go Short Rules and Order:
GoShort = Short_15 and Short_3
strategy.entry("Short", strategy.short, when=GoShort)

strategy.exit("exit", "Short", profit=100, loss=40)

////// TABLE
var table myTable = table.new(position.middle_center, 22, 12,  bgcolor = color.black, border_width=1)
if barstate.islast
// Main Long
    table.cell(myTable, 0, 0, text_size=size.normal, bgcolor=(Long_15) ? color.lime : color.gray, text = "L15" + str.tostring(Long_15))
    table.cell(myTable, 1, 0, text_size=size.normal, bgcolor=(Long_3) ? color.lime : color.gray, text = "L3" + str.tostring(Long_3))
    table.cell(myTable, 2, 0, text_size=size.normal, bgcolor=(GoLong) ? color.fuchsia : color.gray, text = "Long" + str.tostring(GoLong))

// spacer
    table.cell(myTable, 0, 1, text_size=size.small, text = "" + str.tostring(Short_3))

// Main short
    table.cell(myTable, 0, 2, text_size=size.normal, bgcolor=(Short_15) ? color.red : color.gray, text = "S15" + str.tostring(Short_15))
    table.cell(myTable, 1, 2, text_size=size.normal, bgcolor=(Short_3) ? color.red : color.gray, text = "S3" + str.tostring(Short_3))
    table.cell(myTable, 2, 2, text_size=size.normal, bgcolor=(GoShort) ? color.fuchsia : color.gray, text = "Short" + str.tostring(GoShort))


Sources

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

Source: Stack Overflow

Solution Source