本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Fib Thirds - Clean", overlay=true, max_lines_count=10, max_boxes_count=5, max_labels_count=5)
// ---------- Inputs ----------
p1 = input.price(22922.45, "Point 1 (top or bottom)", group="Anchor points")
p2 = input.price(22411.30, "Point 2 (other end)", group="Anchor points")
col = input.color(color.orange, "Level colour", group="Colours")
hlOn = input.bool(true, "Highlight levels around current price", group="Colours")
hlCol = input.color(color.aqua, "Highlight colour", group="Colours")
lineMode = input.string("Short at right", "Chart lines", options=["Off", "Short at right", "Full width"], group="Chart lines")
lineLen = input.int(30, "Short line length (bars)", minval=5, group="Chart lines")
lineStyle = input.string("Dotted", "Line style", options=["Solid", "Dashed", "Dotted"], group="Chart lines")
lineTr = input.int(55, "Line transparency", minval=0, maxval=100, group="Chart lines")
shadeOn = input.bool(true, "Shade the third where price is", group="Zone")
shadeTr = input.int(88, "Shade transparency", minval=0, maxval=100, group="Zone")
infoOn = input.bool(true, "Show zone label", group="Zone")
// ---------- Levels ----------
hi = math.max(p1, p2)
lo = math.min(p1, p2)
rng = hi - lo
step = rng / 3
l0 = hi
l1 = hi - step
l2 = hi - 2 * step
l3 = lo
// zone: 1 = upper third, 2 = middle, 3 = lower third
zone = close > l1 ? 1 : close > l2 ? 2 : 3
pct = rng > 0 ? (close - lo) / rng * 100 : na
c0 = hlOn and zone == 1 ? hlCol : col
c1 = hlOn and (zone == 1 or zone == 2) ? hlCol : col
c2 = hlOn and (zone == 2 or zone == 3) ? hlCol : col
c3 = hlOn and zone == 3 ? hlCol : col
// ---------- Price scale labels (always on) ----------
plot(l0, "Level 0", color=c0, display=display.price_scale)
plot(l1, "Level 0.3333", color=c1, display=display.price_scale)
plot(l2, "Level 0.6667", color=c2, display=display.price_scale)
plot(l3, "Level 1", color=c3, display=display.price_scale)
// ---------- Optional drawings (last bar only, so history stays clean) ----------
var line ln0 = na
var line ln1 = na
var line ln2 = na
var line ln3 = na
var box zoneBox = na
var label info = na
styleVal = lineStyle == "Solid" ? line.style_solid : lineStyle == "Dashed" ? line.style_dashed : line.style_dotted
drawLine(float lvl, color c) =>
xLeft = math.max(bar_index - lineLen, 0)
if lineMode == "Full width"
line.new(bar_index - 1, lvl, bar_index, lvl, extend=extend.both, color=color.new(c, lineTr), style=styleVal, width=1)
else
line.new(xLeft, lvl, bar_index + 5, lvl, color=color.new(c, lineTr), style=styleVal, width=1)
if barstate.islast
line.delete(ln0)
line.delete(ln1)
line.delete(ln2)
line.delete(ln3)
box.delete(zoneBox)
label.delete(info)
xLeft = lineMode == "Full width" ? math.max(bar_index - 500, 0) : math.max(bar_index - lineLen, 0)
if lineMode != "Off"
ln0 := drawLine(l0, c0)
ln1 := drawLine(l1, c1)
ln2 := drawLine(l2, c2)
ln3 := drawLine(l3, c3)
zTop = zone == 1 ? l0 : zone == 2 ? l1 : l2
zBot = zone == 1 ? l1 : zone == 2 ? l2 : l3
if shadeOn
zoneBox := box.new(xLeft, zTop, bar_index + 5, zBot, border_color=na, bgcolor=color.new(hlCol, shadeTr))
if infoOn
zName = zone == 1 ? "Upper third" : zone == 2 ? "Middle third" : "Lower third"
txt = zName + " | " + str.tostring(pct, "#") + "%"
info := label.new(bar_index + 6, (zTop + zBot) / 2, txt, style=label.style_label_left, color=color.new(hlCol, 100), textcolor=hlCol, size=size.small)