HTF Bars Discrepancy Detector (for debugging)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © moebius1977

//@version=5

// @descrition detects when for the same higher timeframe data comes one symbol and not for the other or comes with gaps
// see the data window (sometimes, e.g. after HTF bar 3 can come HTF bar 5 for one or both symbols)
indicator("HTF Bars Discrepancy Detecror")

TF = input.timeframe("3", "TF")
sym1 = input.symbol("BYBIT:SCUSDT.P", "Symbol 1")
sym2 = input.symbol("BYBIT:BTCUSDT.P", "Symbol 1")

htfBi1 = request.security(sym1, TF, bar_index)
htfBi2 = request.security(sym2, TF, bar_index)

var htfFirst1 = -1 
var htfFirst2 = -1 
if barstate.isfirst
    htfFirst1 := htfBi1
    htfFirst2 := htfBi2

newBarHtf1 = ta.change(htfBi1)
newBarHtf2 = ta.change(htfBi2)

bgcolor(        newBarHtf1 and newBarHtf2         ? color.rgb(82, 255, 117, 66) 
              : newBarHtf1                        ? color.rgb(252, 255, 82, 39) 
              : newBarHtf2                        ? color.rgb(255, 82, 206, 36) : na
          )


// make gaps in HTF bars brighter
bgcolor(newBarHtf1 >1  ? color.rgb(255, 249, 82, 2) : na )
bgcolor(newBarHtf2 >1  ? color.rgb(255, 66, 252, 2) : na )


plot(htfBi1-htfBi2, color = color.rgb(134, 255, 255), linewidth = 6, style = plot.style_stepline_diamond)
plotchar(htfBi1, "htfBi1", "", location = location.top)
plotchar(htfBi2, "htfBi2", "", location = location.top)
plotchar(htfFirst1, "htfFirst1", "", location = location.top)
plotchar(htfFirst2, "htfFirst2", "", location = location.top)
plotchar(newBarHtf1, "New HTF bars, Sym1", "", location = location.top)
plotchar(newBarHtf2, "New HTF bars, Sym2", "", location = location.top)
