本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// =====================================================================================
// PIVOT DASHBOARD
// Floating table-based panel
// =====================================================================================
indicator("Pivot Dashboard [KG]", shorttitle = "PivotBox", overlay = true)
// ------------------------------------------------------------------ INPUTS
string G1 = "Pivot settings"
modeIn = input.string("Standard", "Pivot type", options = ["Standard", "Fibonacci", "Camarilla"], group = G1)
tfIn = input.string("Daily", "Pivot timeframe", options = ["Daily", "Weekly", "Monthly", "Custom"], group = G1)
customTf = input.timeframe("D", "Custom timeframe (when 'Custom')", group = G1)
refIn = input.string("Live close", "Reference price (LTP)", options = ["Live close", "Last confirmed close"], group = G1)
string G2 = "Panel layout"
posIn = input.string("Top Right", "Panel position", options = ["Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right"], group = G2)
widthPct = input.float(44.0, "Panel width (% of chart width)", minval = 20.0, maxval = 95.0, step = 1.0, group = G2)
txtIn = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = G2)
dens = input.float(1.0, "Row height scale", minval = 0.5, maxval = 2.5, step = 0.1, group = G2)
compact = input.bool(false, "Compact mode (no header, tighter rows)", group = G2)
trackH = input.float(1.0, "Track thickness (% of chart height)", minval = 0.3, maxval = 3.0, step = 0.1, group = G2)
normIn = input.string("Auto (farthest level)", "Bar normalization", options = ["Auto (farthest level)", "Fixed % of price"], group = G2)
fixedPct = input.float(1.5, "Fixed normalization (% of price)", minval = 0.05, maxval = 50.0, step = 0.05, group = G2)
panelTr = input.int(0, "Panel background transparency", minval = 0, maxval = 100, group = G2)
string G3 = "Row visibility"
showR3 = input.bool(true, "R3", group = G3)
showR2 = input.bool(true, "R2", group = G3)
showR1 = input.bool(true, "R1", group = G3)
showS1 = input.bool(true, "S1", group = G3)
showS2 = input.bool(true, "S2", group = G3)
showS3 = input.bool(true, "S3", group = G3)
string G4 = "Theme and colors"
themeIn = input.string("Auto", "Theme", options = ["Auto", "Light", "Dark"], group = G4, tooltip = "Auto follows the chart background colour.")
colRes = input.color(#07915F, "Resistance bars / text", group = G4)
colSup = input.color(#F5333F, "Support bars / text", group = G4)
colSelOn = input.color(#07915F, "Selector: selected", group = G4)
// ------------------------------------------------------------------ CONSTANTS / DERIVED INPUTS
const int H = 10 // segments per side
const int COLS = 2 * H + 1 // 21 columns (centre column index = H)
tblPos = posIn == "Top Left" ? position.top_left : posIn == "Top Center" ? position.top_center : posIn == "Top Right" ? position.top_right : posIn == "Middle Left" ? position.middle_left : posIn == "Middle Center" ? position.middle_center : posIn == "Middle Right" ? position.middle_right : posIn == "Bottom Left" ? position.bottom_left : posIn == "Bottom Center" ? position.bottom_center : position.bottom_right
txtSz = txtIn == "Tiny" ? size.tiny : txtIn == "Small" ? size.small : txtIn == "Normal" ? size.normal : txtIn == "Large" ? size.large : size.huge
// row heights grow with the chosen text size so larger text is not squeezed
float szF = txtIn == "Tiny" ? 0.8 : txtIn == "Small" ? 1.0 : txtIn == "Normal" ? 1.3 : txtIn == "Large" ? 1.7 : 2.3
float cw = 0.3 // centre-marker column width (% of chart width)
float sw = (widthPct - cw) / (2.0 * H) // segment column width
float textH = (compact ? 1.5 : 2.2) * dens * szF
float hdrH = 2.4 * dens * szF
float selH = (compact ? 2.0 : 2.8) * dens * szF
int nBar = (showR3 ? 1 : 0) + (showR2 ? 1 : 0) + (showR1 ? 1 : 0) + (showS1 ? 1 : 0) + (showS2 ? 1 : 0) + (showS3 ? 1 : 0)
int nRows = 1 + (compact ? 0 : 1) + 2 * nBar + 1 + 1 // ruler + header + (text+bar)*levels + P row + selector
// ------------------------------------------------------------------ THEME PALETTE
color bgc = nz(chart.bg_color, color.white)
float lum = 0.299 * color.r(bgc) + 0.587 * color.g(bgc) + 0.114 * color.b(bgc)
bool isDark = themeIn == "Dark" or (themeIn == "Auto" and lum < 128)
color cPanel = color.new(isDark ? #131722 : #FFFFFF, panelTr)
color cFrame = isDark ? color.rgb(105, 114, 22) : #D5DBE6
color cTrack = isDark ? #363C4E : #BCC5D8
color cText = isDark ? #A9B4CC : #5B6B8C
color cMark = isDark ? #7F95BF : #3B5B86
color cTag = isDark ? #E0E3EB : #0F172A
color cTagTxt = isDark ? #131722 : #FFFFFF
color cSelOff = isDark ? #1E2434 : #E8F5F0
color cSelTxt = isDark ? #D1D4DC : #1F2A44
// ------------------------------------------------------------------ HELPERS
// Number with thousands separators and tick-aware decimals (absolute value)
fmtPrice(float x) =>
string s = str.tostring(math.abs(x), format.mintick)
int dot = str.pos(s, ".")
string ip = na(dot) ? s : str.substring(s, 0, dot)
string fp = na(dot) ? "" : str.substring(s, dot)
string out = ""
int n = str.length(ip)
for i = 0 to n - 1
out += str.substring(ip, i, i + 1)
int remaining = n - 1 - i
if remaining > 0 and remaining % 3 == 0
out += ","
out + fp
fmtDist(float d) =>
(d >= 0 ? "+" : "-") + fmtPrice(d)
// Returns [R3, R2, R1, P, S1, S2, S3]
pivotLevels(float h, float l, float c, string mode) =>
float p = (h + l + c) / 3.0
float r = h - l
float r1 = 2.0 * p - l
float r2 = p + r
float r3 = h + 2.0 * (p - l)
float s1 = 2.0 * p - h
float s2 = p - r
float s3 = l - 2.0 * (h - p)
if mode == "Fibonacci"
r1 := p + 0.382 * r
r2 := p + 0.618 * r
r3 := p + r
s1 := p - 0.382 * r
s2 := p - 0.618 * r
s3 := p - r
else if mode == "Camarilla"
r1 := c + r * 1.1 / 12.0
r2 := c + r * 1.1 / 6.0
r3 := c + r * 1.1 / 4.0
s1 := c - r * 1.1 / 12.0
s2 := c - r * 1.1 / 6.0
s3 := c - r * 1.1 / 4.0
[r3, r2, r1, p, s1, s2, s3]
// ------------------------------------------------------------------ DATA (no lookahead bias)
// Previous COMPLETED period H/L/C: [1] offset + lookahead_on is the documented non-repainting pattern.
string pivotTf = tfIn == "Daily" ? "D" : tfIn == "Weekly" ? "W" : tfIn == "Monthly" ? "M" : customTf
[ph, pl, pc] = request.security(syminfo.tickerid, pivotTf, [high[1], low[1], close[1]], lookahead = barmerge.lookahead_on)
[r3v, r2v, r1v, ppv, s1v, s2v, s3v] = pivotLevels(ph, pl, pc, modeIn)
float ref = refIn == "Live close" ? close : nz(close[1], close)
// ------------------------------------------------------------------ TABLE (created once, colours updated every refresh)
var table panel = table.new(tblPos, COLS, nRows, border_width = 0)
var bool built = false
if barstate.islast and not na(ppv)
// theme-dependent table colours
table.set_bgcolor(panel, cPanel)
table.set_frame_color(panel, cFrame)
table.set_frame_width(panel, 1)
// ---- 1. create every cell once so merges are safe
if not built
for c = 0 to COLS - 1
for rr = 0 to nRows - 1
table.cell(panel, c, rr, "", text_size = txtSz, text_font_family = font.family_monospace, bgcolor = cPanel)
array<float> lvls = array.from(r3v, r2v, r1v, ppv, s1v, s2v, s3v)
array<string> names = array.from("R3", "R2", "R1", "P", "S1", "S2", "S3")
array<bool> vis = array.from(showR3, showR2, showR1, true, showS1, showS2, showS3)
// ---- 2. bar normalisation scale
float maxD = 0.0
for i = 0 to 6
if i != 3 and vis.get(i)
maxD := math.max(maxD, math.abs(lvls.get(i) - ref))
float scale = normIn == "Fixed % of price" ? ref * fixedPct / 100.0 : maxD
if na(scale) or scale <= 0
scale := 1.0
// ---- 3. ruler row
for c = 0 to COLS - 1
table.cell(panel, c, 0, "", width = c == H ? cw : sw, height = 0.1, text_size = size.tiny, bgcolor = cPanel)
int row = 1
// ---- 4. header
if not compact
if not built
table.merge_cells(panel, 0, row, COLS - 1, row)
string warn = timeframe.in_seconds(pivotTf) < timeframe.in_seconds() ? " (!) pivot TF < chart TF" : ""
string hdr = syminfo.ticker + " " + str.tostring(ref, format.mintick) + " | " + modeIn + " · " + pivotTf + warn
table.cell(panel, 0, row, hdr, height = hdrH, text_color = cText, text_halign = text.align_left, text_valign = text.align_center, text_size = txtSz, text_font_family = font.family_monospace, bgcolor = cPanel)
row += 1
// ---- 5. pivot rows
for i = 0 to 6
if vis.get(i)
float lv = lvls.get(i)
string nm = names.get(i)
if i == 3
// reference row: pivot value both sides, dashed line, LTP tag
if not built
table.merge_cells(panel, 0, row, 5, row)
table.merge_cells(panel, 6, row, 7, row)
table.merge_cells(panel, 8, row, 12, row)
table.merge_cells(panel, 13, row, 14, row)
table.merge_cells(panel, 15, row, COLS - 1, row)
table.cell(panel, 0, row, fmtPrice(lv) + " ●", height = textH * 1.3, text_color = cText, text_halign = text.align_left, text_valign = text.align_center, text_size = txtSz, text_font_family = font.family_monospace, bgcolor = cPanel)
table.cell(panel, 6, row, "- - - -", text_color = cTrack, text_halign = text.align_center, text_valign = text.align_center, text_size = txtSz, text_font_family = font.family_monospace, bgcolor = cPanel)
table.cell(panel, 8, row, "LTP: " + str.tostring(ref, format.mintick), text_color = cTagTxt, text_halign = text.align_center, text_valign = text.align_center, text_size = txtSz, text_font_family = font.family_monospace, bgcolor = cTag)
table.cell(panel, 13, row, "- - - -", text_color = cTrack, text_halign = text.align_center, text_valign = text.align_center, text_size = txtSz, text_font_family = font.family_monospace, bgcolor = cPanel)
table.cell(panel, 15, row, "● " + fmtPrice(lv), text_color = cText, text_halign = text.align_right, text_valign = text.align_center, text_size = txtSz, text_font_family = font.family_monospace, bgcolor = cPanel)
row += 1
else
bool isR = i < 3
float d = lv - ref
color accent = isR ? colRes : colSup
// text row
if not built
table.merge_cells(panel, 0, row, H - 1, row)
table.merge_cells(panel, H + 1, row, COLS - 1, row)
string leftTxt = isR ? nm + " " + fmtDist(d) : fmtPrice(lv)
string rightTxt = isR ? fmtPrice(lv) : fmtDist(d) + " " + nm
color leftCol = isR ? accent : cText
color rightCol = isR ? cText : accent
table.cell(panel, 0, row, leftTxt, height = textH, text_color = leftCol, text_halign = text.align_left, text_valign = text.align_bottom, text_size = txtSz, text_font_family = font.family_monospace, bgcolor = cPanel)
table.cell(panel, H, row, "", bgcolor = cMark)
table.cell(panel, H + 1, row, rightTxt, text_color = rightCol, text_halign = text.align_right, text_valign = text.align_bottom, text_size = txtSz, text_font_family = font.family_monospace, bgcolor = cPanel)
row += 1
// bar row
int filled = int(math.round(math.min(math.abs(d) / scale, 1.0) * H))
for c = 0 to COLS - 1
color bg = cTrack
if c == H
bg := cMark
else if c < H
// left half -> support (red) fills outward from centre
if not isR and (H - c) <= filled
bg := colSup
else
// right half -> resistance (green) fills outward from centre
if isR and (c - H) <= filled
bg := colRes
table.cell(panel, c, row, "", height = trackH, text_size = size.tiny, bgcolor = bg)
row += 1
// ---- 6. selector row (Standard | Fibonacci | Camarilla)
if not built
table.merge_cells(panel, 0, row, 6, row)
table.merge_cells(panel, 7, row, 13, row)
table.merge_cells(panel, 14, row, COLS - 1, row)
table.cell(panel, 0, row, "Standard", height = selH, text_color = modeIn == "Standard" ? color.white : cSelTxt, text_halign = text.align_center, text_valign = text.align_center, text_size = txtSz, text_font_family = font.family_monospace, bgcolor = modeIn == "Standard" ? colSelOn : cSelOff)
table.cell(panel, 7, row, "Fibonacci", text_color = modeIn == "Fibonacci" ? color.white : cSelTxt, text_halign = text.align_center, text_valign = text.align_center, text_size = txtSz, text_font_family = font.family_monospace, bgcolor = modeIn == "Fibonacci" ? colSelOn : cSelOff)
table.cell(panel, 14, row, "Camarilla", text_color = modeIn == "Camarilla" ? color.white : cSelTxt, text_halign = text.align_center, text_valign = text.align_center, text_size = txtSz, text_font_family = font.family_monospace, bgcolor = modeIn == "Camarilla" ? colSelOn : cSelOff)
built := true