Apply this Pine Script v6 indicator to leg A on standard time-based candles. Select leg B and set both instruments to the intended regular-session context. The code requests B in its regular session and compares its bar endpoints with A. It plots a weighted difference or ratio only on confirmed chart bars with matching data; it does not recreate native spread OHLC.
Use A = 120, B = 80 and coefficient = 1.5 to check the arithmetic: weighted difference 0 or ratio 1.5. Defaults are demonstration inputs.pine
//@version=6
indicator("Aligned close expression monitor", overlay = false)
string symbolB = input.symbol("NASDAQ:MSFT", "Leg B (chart is leg A)")
string mode = input.string("Weighted difference", "Expression", options = ["Weighted difference", "Ratio"])
float coefficient = input.float(1.5, "B coefficient for difference")
float threshold = input.float(0.0, "Upper alert threshold")
string regularB = ticker.modify(symbolB, session = session.regular)
[closeB, openTimeB, closeTimeB] = request.security(regularB, timeframe.period, [close, time, time_close], gaps = barmerge.gaps_on, lookahead = barmerge.lookahead_off)
bool aligned = not na(closeB) and not na(close) and openTimeB == time and closeTimeB == time_close
bool validRatio = mode != "Ratio" or closeB != 0
bool ready = aligned and validRatio and barstate.isconfirmed
float value = ready ? (mode == "Ratio" ? close / closeB : close - coefficient * closeB) : na
bool crossedUp = not na(value) and not na(value[1]) and value > threshold and value[1] <= threshold
plot(value, "Expression close", color = color.blue, linewidth = 2, style = plot.style_linebr)
plot(threshold, "Upper threshold", color = color.orange)
plot(ready ? 1 : 0, "Accepted observation", display = display.data_window)
alertcondition(crossedUp, "Expression crossed above", "Aligned expression crossed above its upper threshold.")