本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RAYLIGHT TRADES
// ╔════════════════════════════════╗ //
// ║ CREATED BY ║ //
// ╚════════════════════════════════╝ //
//
// ██████╗ █████╗ ██╗ ██╗██╗ ██╗ ██████╗ ██╗ ██╗████████╗
// ██╔══██╗██╔══██╗╚██╗ ██╔╝██║ ██║██╔════╝ ██║ ██║╚══██╔══╝
// ██████╔╝███████║ ╚████╔╝ ██║ ██║██║ ███╗███████║ ██║
// ██╔══██╗██╔══██║ ╚██╔╝ ██║ ██║██║ ██║██╔══██║ ██║
// ██║ ██║██║ ██║ ██║ ███████╗██║╚██████╔╝██║ ██║ ██║
// ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝
//
// ████████╗██████╗ █████╗ ██████╗ ███████╗███████╗
// ╚══██╔══╝██╔══██╗██╔══██╗██╔══██╗██╔════╝██╔════╝
// ██║ ██████╔╝███████║██║ ██║█████╗ ███████╗
// ██║ ██╔══██╗██╔══██║██║ ██║██╔══╝ ╚════██║
// ██║ ██║ ██║██║ ██║██████╔╝███████╗███████║
// ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝ ╚══════╝╚══════╝
//
// ◆ RLT SNAPBACK · RAYLIGHT TRADES ◆
//@version=6
indicator("RLT Snapback · RAYLIGHT TRADES", shorttitle = "RLT Snapback", overlay = true, max_lines_count = 500, max_boxes_count = 5, max_labels_count = 500)
// ─────────────── Swing points (user clicks them on the chart) ───────────────
// When the indicator is added, TradingView asks you to click Point A, then Point B.
// Drag the points later from the indicator settings or by re-adding the tool.
t1 = input.time(timestamp("2024-01-01 00:00"), "Point A (swing start)", confirm = true, inline = "A", group = "Swing points")
p1 = input.price(0.0, "", confirm = true, inline = "A", group = "Swing points")
t2 = input.time(timestamp("2024-01-01 00:01"), "Point B (swing end)", confirm = true, inline = "B", group = "Swing points")
p2 = input.price(0.0, "", confirm = true, inline = "B", group = "Swing points")
// ─────────────── Style ───────────────
zoneFill = input.color(color.new(#F0B90B, 88), "Zone fill", group = "Style")
keyCol = input.color(#F0B90B, "25% / 75% lines", group = "Style")
midCol = input.color(color.new(#9AA3B2, 30), "50% line", group = "Style")
rangeFill = input.color(color.new(#2F7BFF, 88), "Range box fill", group = "Style")
rangeLine = input.color(#2F7BFF, "Range box border", group = "Style")
showMid = input.bool(true, "Show 50% line", group = "Style")
showPrices = input.bool(true, "Show prices on labels", group = "Style")
showStatus = input.bool(true, "Show live status label", group = "Style")
brandPos = input.string("Top right", "Badge position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = "Style")
// ─────────────── Math ───────────────
// 0% = Point B (latest swing), 100% = Point A. Levels measure the pullback from B back toward A.
lvl(float pct) => p2 + (p1 - p2) * pct
l25 = lvl(0.25)
l50 = lvl(0.50)
l75 = lvl(0.75)
bull = p2 > p1 // swing low → swing high = bullish swing
// bar count between A and B (for the info label)
var int iA = na
var int iB = na
if na(iA) and time >= t1
iA := bar_index
if na(iB) and time >= t2
iB := bar_index
// deepest pullback since Point B (uses wicks): once it goes beyond 75% the setup stays invalid
var float deep = na
if time > t2 and p1 != p2
wick = bull ? low : high
r = (p2 - wick) / (p2 - p1) * 100
deep := na(deep) ? r : math.max(deep, r)
// ─────────────── Drawing ───────────────
var box rBox = na
var box zBox = na
var line ln25 = na
var line ln50 = na
var line ln75 = na
var label lb25 = na
var label lb50 = na
var label lb75 = na
var label lbInfo = na
var label lbStat = na
fmt(float v) => str.tostring(v, format.mintick)
if barstate.islast and p1 != p2
box.delete(rBox), box.delete(zBox)
line.delete(ln25), line.delete(ln50), line.delete(ln75)
label.delete(lb25), label.delete(lb50), label.delete(lb75), label.delete(lbInfo), label.delete(lbStat)
top = math.max(p1, p2)
bot = math.min(p1, p2)
// date-range style box from swing to swing
rBox := box.new(t1, top, t2, bot, xloc = xloc.bar_time, border_color = rangeLine, border_width = 1, bgcolor = rangeFill)
// entry zone 25–75%, extends to the right
zBox := box.new(t2, math.max(l25, l75), time, math.min(l25, l75), xloc = xloc.bar_time, extend = extend.right, border_color = color(na), bgcolor = zoneFill)
// level lines
ln25 := line.new(t1, l25, time, l25, xloc = xloc.bar_time, extend = extend.right, color = keyCol, width = 2)
ln75 := line.new(t1, l75, time, l75, xloc = xloc.bar_time, extend = extend.right, color = keyCol, width = 2)
if showMid
ln50 := line.new(t1, l50, time, l50, xloc = xloc.bar_time, extend = extend.right, color = midCol, width = 1, style = line.style_dashed)
// level labels on the left edge of the range
// level labels on the right, like TradingView's own fib tool
lx = bar_index + 12
lbBg = color.new(#0B0E14, 15)
lb25 := label.new(lx, l25, "25%" + (showPrices ? " " + fmt(l25) : ""), style = label.style_label_left, color = lbBg, textcolor = keyCol, size = size.normal)
lb75 := label.new(lx, l75, "75%" + (showPrices ? " " + fmt(l75) : ""), style = label.style_label_left, color = lbBg, textcolor = keyCol, size = size.normal)
if showMid
lb50 := label.new(lx, l50, "50%" + (showPrices ? " " + fmt(l50) : ""), style = label.style_label_left, color = lbBg, textcolor = midCol, size = size.small)
// info label above/below point B (like the TradingView date range label)
movePct = (p2 - p1) / p1 * 100
bars = na(iA) or na(iB) ? "" : " · " + str.tostring(iB - iA) + " bars"
infoTxt = (movePct >= 0 ? "+" : "") + str.tostring(movePct, "#.##") + "%" + bars
lbInfo := label.new(t2, p2, infoTxt, xloc = xloc.bar_time, style = bull ? label.style_label_down : label.style_label_up, color = rangeLine, textcolor = color.white, size = size.small)
// live status: how deep is the current pullback?
if showStatus
retr = (p2 - close) / (p2 - p1) * 100
dp = na(deep) ? retr : deep
txt = ""
col = color.new(#5E6776, 0)
if dp > 75
txt := "INVALID · pullback reached " + str.tostring(dp, "#") + "%\nBeyond 75% of the swing"
col := color.new(#F6465D, 0)
else if retr >= 25 and retr <= 75
txt := "In zone · " + str.tostring(retr, "#") + "%"
col := color.new(#B8860B, 0)
else
txt := "Pullback · " + str.tostring(math.max(retr, 0), "#") + "%"
lbStat := label.new(bar_index + 2, close, txt, style = label.style_label_left, color = col, textcolor = color.white, size = size.normal)
// ─────────────── Market structure: HH / HL / LH / LL (major swings only, from Point A onward) ───────────────
// A swing is only confirmed when price reverses by at least X% of your A→B swing, so small noise wiggles are ignored.
// Highs and lows always alternate (high, low, high, low...), like a proper swing chart.
showMS = input.bool(true, "Show HH / HL / LH / LL", group = "Market structure")
minSw = input.float(30.0, "Min swing size (% of A→B range)", minval = 5, maxval = 100, step = 5, group = "Market structure")
msUp = input.color(#2EBD85, "HH / HL colour", group = "Market structure")
msDn = input.color(#F6465D, "LH / LL colour", group = "Market structure")
var float lastH = na
var float lastL = na
var bool started = false
var bool seekHigh = true
var float cand = na
var int candT = na
thr = math.abs(p2 - p1) * minSw / 100
if showMS and p1 != p2 and time >= t1
if not started
// Point A is the first swing: a low for a bullish swing, a high for a bearish one
if bull
lastL := p1
seekHigh := true
cand := high
else
lastH := p1
seekHigh := false
cand := low
candT := time
started := true
else if seekHigh
if high >= cand
cand := high
candT := time
else if cand - low >= thr
tagH = na(lastH) ? "H" : cand > lastH ? "HH" : "LH"
cH = tagH == "LH" ? msDn : tagH == "H" ? #8A93A3 : msUp
label.new(candT, cand, tagH, xloc = xloc.bar_time, style = label.style_label_down, color = color.new(cH, 80), textcolor = cH, size = size.normal)
lastH := cand
seekHigh := false
cand := low
candT := time
else
if low <= cand
cand := low
candT := time
else if high - cand >= thr
tagL = na(lastL) ? "L" : cand > lastL ? "HL" : "LL"
cL = tagL == "LL" ? msDn : tagL == "L" ? #8A93A3 : msUp
label.new(candT, cand, tagL, xloc = xloc.bar_time, style = label.style_label_up, color = color.new(cL, 80), textcolor = cL, size = size.normal)
lastL := cand
seekHigh := true
cand := high
candT := time
// ─────────────── Branding badge ───────────────
pos = brandPos == "Top left" ? position.top_left : brandPos == "Bottom right" ? position.bottom_right : brandPos == "Bottom left" ? position.bottom_left : position.top_right
var table badge = table.new(pos, 1, 2, bgcolor = color.new(#0E131C, 10), border_color = color.new(#F0B90B, 40), border_width = 1, frame_color = color.new(#F0B90B, 40), frame_width = 1)
if barstate.islast
table.cell(badge, 0, 0, "◆ RLT SNAPBACK", text_color = #F0B90B, text_size = size.normal, text_halign = text.align_left)
table.cell(badge, 0, 1, "by RAYLIGHT TRADES", text_color = color.new(#F2F4F8, 20), text_size = size.small, text_halign = text.align_left)