OPEN-SOURCE SCRIPT

BAY_PIVOT S/R(4 Full Lines + ALL Labels)

68
//version=5
indicator("BAY_PIVOT S/R(4 Full Lines + ALL Labels)", overlay=true, max_labels_count=500, max_lines_count=500)

// ────────────────────── TOGGLES ──────────────────────
showPivot = input.bool(true, "Show Pivot (Full Line + Label)")
showTarget = input.bool(true, "Show Target (Full Line + Label)")
showLast = input.bool(true, "Show Last Close (Full Line + Label)")
showPrevClose = input.bool(true, "Show Previous Close (Full Line + Label)")

useBarchartLast = input.bool(true, "Use Barchart 'Last' (Settlement Price)")

showR1R2R3 = input.bool(true, "Show R1 • R2 • R3")
showS1S2S3 = input.bool(true, "Show S1 • S2 • S3")
showStdDev = input.bool(true, "Show ±1σ ±2σ ±3σ")
showFib4W = input.bool(true, "Show 4-Week Fibs")
showFib13W = input.bool(true, "Show 13-Week Fibs")
showMonthHL = input.bool(true, "Show 1M High / Low")

showEntry1 = input.bool(false, "Show Manual Entry 1")
showEntry2 = input.bool(false, "Show Manual Entry 2")
entry1 = input.float(0.0, "Manual Entry 1", step=0.25)
entry2 = input.float(0.0, "Manual Entry 2", step=0.25)

stdLen = input.int(20, "StdDev Length", minval=1)
fib4wBars = input.int(20, "4W Fib Lookback")
fib13wBars = input.int(65, "13W Fib Lookback")

// ────────────────────── DAILY CALCULATIONS ──────────────────────
high_y = request.security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)
low_y = request.security(syminfo.tickerid, "D", low[1], lookahead=barmerge.lookahead_on)
close_y = request.security(syminfo.tickerid, "D", close[1], lookahead=barmerge.lookahead_on)

pivot = (high_y + low_y + close_y) / 3
r1 = pivot + 0.382 * (high_y - low_y)
r2 = pivot + 0.618 * (high_y - low_y)
r3 = pivot + (high_y - low_y)
s1 = pivot - 0.382 * (high_y - low_y)
s2 = pivot - 0.618 * (high_y - low_y)
s3 = pivot - (high_y - low_y)

prevClose = close_y
last = useBarchartLast ? request.security(syminfo.tickerid, "D", close[1], lookahead=barmerge.lookahead_off) : close[1]
target = pivot + (pivot - prevClose)

// StdDev + Fibs + Monthly (unchanged)
basis = ta.sma(close, stdLen)
dev = ta.stdev(close, stdLen)
stdRes1 = basis + dev
stdRes2 = basis + dev*2
stdRes3 = basis + dev*3
stdSup1 = basis - dev
stdSup2 = basis - dev*2
stdSup3 = basis - dev*3

high4w = ta.highest(high, fib4wBars)
low4w = ta.lowest(low, fib4wBars)
fib382_4w = high4w - (high4w - low4w) * 0.382
fib50_4w = high4w - (high4w - low4w) * 0.500

high13w = ta.highest(high, fib13wBars)
low13w = ta.lowest(low, fib13wBars)
fib382_13w_high = high13w - (high13w - low13w) * 0.382
fib50_13w = high13w - (high13w - low13w) * 0.500
fib382_13w_low = low13w + (high13w - low13w) * 0.382

monthHigh = ta.highest(high, 30)
monthLow = ta.lowest(low, 30)

// ────────────────────── COLORS ──────────────────────
colRed = color.rgb(255,0,0)
colLime = color.rgb(0,255,0)
colYellow = color.rgb(255,255,0)
colOrange = color.rgb(255,165,0)
colWhite = color.rgb(255,255,255)
colGray = color.rgb(128,128,128)
colMagenta = color.rgb(255,0,255)
colPink = color.rgb(233,30,99)
colCyan = color.rgb(0,188,212)
colBlue = color.rgb(0,122,255)
colPurple = color.rgb(128,0,128)
colRed50 = color.new(colRed,50)
colGreen50 = color.new(colLime,50)

// ────────────────────── 4 KEY FULL LINES ──────────────────────
plot(showPivot ? pivot : na, title="PIVOT", color=colYellow, linewidth=3, style=plot.style_linebr)
plot(showTarget ? target : na, title="TARGET", color=colOrange, linewidth=2, style=plot.style_linebr)
plot(showLast ? last : na, title="LAST", color=colWhite, linewidth=2, style=plot.style_linebr)
plot(showPrevClose ? prevClose : na, title="PREV CLOSE",color=colGray, linewidth=1, style=plot.style_linebr)

// ────────────────────── LABELS FOR ALL 4 KEY LEVELS (SAME STYLE AS OTHERS) ──────────────────────
f_label(price, txt, bgColor, txtColor) =>
if barstate.islast and not na(price)
label.new(bar_index, price, txt, style=label.style_label_left, color=bgColor, textcolor=txtColor, size=size.small)

if barstate.islast
showPivot ? f_label(pivot, "PIVOT\n" + str.tostring(pivot, "#.##"), colYellow, color.black) : na
showTarget ? f_label(target, "TARGET\n" + str.tostring(target, "#.##"), colOrange, color.white) : na
showLast ? f_label(last, "LAST\n" + str.tostring(last, "#.##"), colWhite, color.black) : na
showPrevClose ? f_label(prevClose, "PREV CLOSE\n"+ str.tostring(prevClose, "#.##"), colGray, color.white) : na

// ────────────────────── OTHER LEVELS – line stops at label ──────────────────────
f_level(p, txt, tc, lc, w=1) =>
if barstate.islast and not na(p)
lbl = label.new(bar_index, p, txt, style=label.style_label_left, color=lc, textcolor=tc, size=size.small)
line.new(bar_index-400, p, label.get_x(lbl), p, extend=extend.none, color=lc, width=w)

if barstate.islast
if showR1R2R3
f_level(r1, "R1\n" + str.tostring(r1, "#.##"), color.white, colRed)
f_level(r2, "R2\n" + str.tostring(r2, "#.##"), color.white, colRed)
f_level(r3, "R3\n" + str.tostring(r3, "#.##"), color.white, colRed, 2)

if showS1S2S3
f_level(s1, "S1\n" + str.tostring(s1, "#.##"), color.black, colLime)
f_level(s2, "S2\n" + str.tostring(s2, "#.##"), color.black, colLime)
f_level(s3, "S3\n" + str.tostring(s3, "#.##"), color.black, colLime, 2)

if showStdDev
f_level(stdRes1, "+1σ\n" + str.tostring(stdRes1, "#.##"), color.white, colPink)
f_level(stdRes2, "+2σ\n" + str.tostring(stdRes2, "#.##"), color.white, colPink)
f_level(stdRes3, "+3σ\n" + str.tostring(stdRes3, "#.##"), color.white, colPink, 2)
f_level(stdSup1, "-1σ\n" + str.tostring(stdSup1, "#.##"), color.white, colCyan)
f_level(stdSup2, "-2σ\n" + str.tostring(stdSup2, "#.##"), color.white, colCyan)
f_level(stdSup3, "-3σ\n" + str.tostring(stdSup3, "#.##"), color.white, colCyan, 2)

if showFib4W
f_level(fib382_4w, "38.2% 4W\n" + str.tostring(fib382_4w, "#.##"), color.white, colMagenta)
f_level(fib50_4w, "50% 4W\n" + str.tostring(fib50_4w, "#.##"), color.white, colMagenta)

if showFib13W
f_level(fib382_13w_high, "38.2% 13W High\n" + str.tostring(fib382_13w_high, "#.##"), color.white, colMagenta)
f_level(fib50_13w, "50% 13W\n" + str.tostring(fib50_13w, "#.##"), color.white, colMagenta)
f_level(fib382_13w_low, "38.2% 13W Low\n" + str.tostring(fib382_13w_low, "#.##"), color.white, colMagenta)

if showMonthHL
f_level(monthHigh, "1M HIGH\n" + str.tostring(monthHigh, "#.##"), color.white, colRed50, 2)
f_level(monthLow, "1M LOW\n" + str.tostring(monthLow, "#.##"), color.white, colGreen50, 2)

// Manual entries
plot(showEntry1 and entry1 > 0 ? entry1 : na, "Entry 1", color=colBlue, linewidth=2, style=plot.style_linebr)
plot(showEntry2 and entry2 > 0 ? entry2 : na, "Entry 2", color=colPurple, linewidth=2, style=plot.style_linebr)

// Background
bgcolor(close > pivot ? color.new(color.blue, 95) : color.new(color.red, 95))

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.