//@version=6 indicator("Workshop 14 Pair Distance", overlay=false) other = input.symbol("NASDAQ:PEP", "Second leg") beta = input.float(1.0, "Fixed price coefficient", minval=0.0) length = input.int(60, "Reference chart bars", minval=2) [peer, peerTime] = request.security(other, timeframe.period, [close, time_close], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_off) aligned = not na(peer) and peerTime == time_close difference = aligned ? close - beta * peer : na center = ta.sma(difference, length)[1] deviation = ta.stdev(difference, length, true)[1] count = math.sum(aligned ? 1.0 : 0.0, length)[1] ready = aligned and count == length and deviation > 0 z = ready ? (difference - center) / deviation : na plot(z, "Spread distance", color=color.blue, style=plot.style_linebr) hline(0, "Prior center", color=color.gray) hline(2, "Upper observation", color=color.gray) hline(-2, "Lower observation", color=color.gray)