本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("FCP | Key Level Zones | Pivots & Daily Weekly Monthly High Low", overlay = true, max_boxes_count = 500, max_labels_count = 500)
BRAND_TEAL = #2FE3C6
BRAND_GOLD = #FFD84D
BRAND_RED = #FF6152
NO_COLOR = #00000000
DN = display.none
// ───────────────────────────── Inputs — Zones ─────────────────────────────
grpZone = "Zones"
tolATR = input.float(0.5, "Tolerance (× ATR)", minval = 0.05, maxval = 5.0, step = 0.05, group = grpZone, display = DN, tooltip = "An HL level and a pivot level form a zone when they are at most this far apart. The zone covers the price range between them.")
maxHATR = input.float(0.1, "Maximum height (× ATR)", minval = 0.0, maxval = 5.0, step = 0.05, group = grpZone, display = DN, tooltip = "A zone taller than this is trimmed equally from the top and the bottom, keeping its centre. Drawing only: the levels counted for its colour do not change. 0 = no limit.")
lookbackD = input.int(30, "Look-back (days)", minval = 1, maxval = 3650, group = grpZone, display = DN, tooltip = "Zones that started more than this many calendar days ago are removed from the chart.")
atrLen = input.int(14, "ATR length", minval = 1, maxval = 500, inline = "atr", group = grpZone, display = DN)
atrTF = input.timeframe("240", "", inline = "atr", group = grpZone, display = DN, tooltip = "ATR length and timeframe used for the tolerance and the maximum height. A fixed timeframe keeps the zones identical on every chart timeframe. Leave the timeframe empty to follow the chart instead.")
// ───────────────────────────── Inputs — HL Levels ─────────────────────────────
grpHL = "HL Levels"
useDayHL = input.bool(true, "Previous day", inline = "hl", group = grpHL, display = DN)
useWeekHL = input.bool(true, "Previous week", inline = "hl", group = grpHL, display = DN)
useMonthHL = input.bool(true, "Previous month", inline = "hl", group = grpHL, display = DN, tooltip = "High and low of the previous day, week and month.")
useCustomHL = input.bool(false, "Custom timeframe", inline = "hlc", group = grpHL, display = DN)
customTF = input.timeframe("240", "", inline = "hlc", group = grpHL, display = DN, tooltip = "High and low of the previous bar of this timeframe.")
useMidHL = input.bool(true, "Include mid levels", group = grpHL, display = DN, tooltip = "Adds the midpoint between each high and low.")
// ───────────────────────────── Inputs — Pivot Points ─────────────────────────────
grpPP = "Pivot Points"
pivotType = input.string("Traditional", "Type", options = ["Traditional", "Fibonacci", "Woodie", "Classic", "DM", "Camarilla"], group = grpPP, display = DN)
useDayPP = input.bool(true, "Daily", inline = "pp", group = grpPP, display = DN)
useWeekPP = input.bool(true, "Weekly", inline = "pp", group = grpPP, display = DN)
useMonthPP = input.bool(true, "Monthly", inline = "pp", group = grpPP, display = DN, tooltip = "Pivot timeframes to include.")
incP = input.bool(true, "P", inline = "lv1", group = grpPP, display = DN)
incL1 = input.bool(true, "R1 / S1", inline = "lv1", group = grpPP, display = DN)
incL2 = input.bool(true, "R2 / S2", inline = "lv1", group = grpPP, display = DN)
incL3 = input.bool(true, "R3 / S3", inline = "lv2", group = grpPP, display = DN)
incL4 = input.bool(false, "R4 / S4", inline = "lv2", group = grpPP, display = DN)
incL5 = input.bool(false, "R5 / S5", inline = "lv2", group = grpPP, display = DN, tooltip = "Pivot levels to include. Levels a pivot type does not define are skipped.")
// ───────────────────────────── Inputs — Style ─────────────────────────────
grpStyle = "Style"
color2 = input.color(BRAND_TEAL, "Colours", inline = "col", group = grpStyle, display = DN)
color3 = input.color(BRAND_GOLD, "", inline = "col", group = grpStyle, display = DN)
color4 = input.color(BRAND_RED, "", inline = "col", group = grpStyle, display = DN, tooltip = "Zones holding 2 levels, 3 levels, and 4 or more levels.")
zoneTransp = input.int(70, "Transparency", minval = 0, maxval = 95, group = grpStyle, display = DN)
showLabels = input.bool(false, "Labels", inline = "lbl", group = grpStyle, display = DN)
labelSize = input.string("small", "", options = ["tiny", "small", "normal"], inline = "lbl", group = grpStyle, display = DN, tooltip = "Names the levels behind each zone at its right end. Hover a label to see their prices.")
// ───────────────────────────── Source Data ─────────────────────────────
f_hl(simple string tf) =>
request.security(syminfo.tickerid, tf, [high[1], low[1]], barmerge.gaps_off, barmerge.lookahead_on)
f_pivots(simple string tf) =>
request.security(syminfo.tickerid, tf, ta.pivot_point_levels(pivotType, timeframe.change(timeframe.period)), lookahead = barmerge.lookahead_on)
[dH, dL] = f_hl("D")
[wH, wL] = f_hl("W")
[mH, mL] = f_hl("M")
[cH, cL] = f_hl(customTF)
ppDay = f_pivots("D")
ppWeek = f_pivots("W")
ppMonth = f_pivots("M")
atrValue = request.security(syminfo.tickerid, atrTF, ta.atr(atrLen)[1], lookahead = barmerge.lookahead_on)
var array<string> PP_NAMES = array.from("P", "R1", "S1", "R2", "S2", "R3", "S3", "R4", "S4", "R5", "S5")
chartSec = timeframe.in_seconds()
// ───────────────────────────── Level Collection ─────────────────────────────
var array<float> lvPrice = array.new<float>()
var array<int> lvSrc = array.new<int>()
var array<string> lvName = array.new<string>()
f_push(float p, int src, string nm) =>
if not na(p)
lvPrice.push(p)
lvSrc.push(src)
lvName.push(nm)
f_addHL(simple string tf, string tag, bool enabled, float h, float l) =>
if enabled and chartSec <= timeframe.in_seconds(tf) and not na(h) and not na(l)
f_push(h, 0, tag + "H")
f_push(l, 0, tag + "L")
if useMidHL
f_push((h + l) / 2, 0, tag + "M")
f_addPivots(simple string tf, string tag, bool enabled, array<float> levels) =>
if enabled and chartSec <= timeframe.in_seconds(tf) and not na(levels)
for [i, level] in levels
int tier = i == 0 ? 0 : int(math.floor((i + 1) / 2.0))
bool keep = tier == 0 ? incP : tier == 1 ? incL1 : tier == 2 ? incL2 : tier == 3 ? incL3 : tier == 4 ? incL4 : incL5
if keep
f_push(level, 1, tag + PP_NAMES.get(i))
f_gather() =>
lvPrice.clear()
lvSrc.clear()
lvName.clear()
f_addHL("D", "PD", useDayHL, dH, dL)
f_addHL("W", "PW", useWeekHL, wH, wL)
f_addHL("M", "PM", useMonthHL, mH, mL)
f_addHL(customTF, "PC", useCustomHL, cH, cL)
f_addPivots("D", "D ", useDayPP, ppDay)
f_addPivots("W", "W ", useWeekPP, ppWeek)
f_addPivots("M", "M ", useMonthPP, ppMonth)
lvPrice.size()
// ───────────────────────────── Zones ─────────────────────────────
var array<box> zBox = array.new<box>()
var array<label> zTag = array.new<label>()
var array<int> zTime = array.new<int>()
var int curN = 0
f_labelSize() =>
labelSize == "tiny" ? size.tiny : labelSize == "normal" ? size.normal : size.small
f_emit(float lo, float hi, float tol, int tEnd) =>
int members = 0
string names = ""
string tip = ""
for i = 0 to lvPrice.size() - 1
float p = lvPrice.get(i)
if p >= lo and p <= hi
members += 1
names := names + (members == 1 ? "" : " · ") + lvName.get(i)
tip := tip + "\n" + lvName.get(i) + " " + str.tostring(p, format.mintick)
color base = members >= 4 ? color4 : members == 3 ? color3 : color2
float pad = math.max(0.0, (tol * 0.08 - (hi - lo)) / 2)
float top = hi + pad
float bot = lo - pad
float maxH = maxHATR * atrValue
if maxHATR > 0 and top - bot > maxH
float mid = (hi + lo) / 2
top := mid + maxH / 2
bot := mid - maxH / 2
zBox.push(box.new(time, top, tEnd, bot, xloc = xloc.bar_time, border_color = NO_COLOR, border_width = 0, bgcolor = color.new(base, zoneTransp)))
label t = na
if showLabels
t := label.new(tEnd, (hi + lo) / 2, names, xloc = xloc.bar_time, style = label.style_label_left, color = NO_COLOR, textcolor = base, size = f_labelSize(), tooltip = str.tostring(lo, format.mintick) + " – " + str.tostring(hi, format.mintick) + "\n" + tip)
zTag.push(t)
zTime.push(time)
f_build(int tEnd) =>
int made = 0
if f_gather() > 0 and not na(atrValue)
float tol = tolATR * atrValue
array<float> pLo = array.new<float>()
array<float> pHi = array.new<float>()
for i = 0 to lvPrice.size() - 1
if lvSrc.get(i) == 0
for j = 0 to lvPrice.size() - 1
if lvSrc.get(j) == 1 and math.abs(lvPrice.get(i) - lvPrice.get(j)) <= tol
pLo.push(math.min(lvPrice.get(i), lvPrice.get(j)))
pHi.push(math.max(lvPrice.get(i), lvPrice.get(j)))
if pLo.size() > 0
array<int> ord = pLo.sort_indices(order.ascending)
float lo = pLo.get(ord.get(0))
float hi = pHi.get(ord.get(0))
if ord.size() > 1
for k = 1 to ord.size() - 1
float l = pLo.get(ord.get(k))
float h = pHi.get(ord.get(k))
if l <= hi
hi := math.max(hi, h)
else
f_emit(lo, hi, tol, tEnd)
made += 1
lo := l
hi := h
f_emit(lo, hi, tol, tEnd)
made += 1
made
f_trim() =>
int cutoff = time - lookbackD * 86400000
while zTime.size() > curN and zTime.first() < cutoff
zBox.shift().delete()
label t = zTag.shift()
if not na(t)
t.delete()
zTime.shift()
// ───────────────────────────── Drive ─────────────────────────────
bool canD = chartSec <= timeframe.in_seconds("D")
bool canW = chartSec <= timeframe.in_seconds("W")
bool canM = chartSec <= timeframe.in_seconds("M")
bool canC = chartSec <= timeframe.in_seconds(customTF)
bool onD = (useDayHL or useDayPP) and canD
bool onW = (useWeekHL or useWeekPP) and canW
bool onM = (useMonthHL or useMonthPP) and canM
bool onC = useCustomHL and canC
bool chgD = timeframe.change("D")
bool chgW = timeframe.change("W")
bool chgM = timeframe.change("M")
bool chgC = timeframe.change(customTF)
int endD = time_close("D")
int endW = time_close("W")
int endM = time_close("M")
int endC = time_close(customTF)
bool rollover = (onD and chgD) or (onW and chgW) or (onM and chgM) or (onC and chgC)
int periodEnd = na
if onD
periodEnd := endD
if onW
periodEnd := na(periodEnd) ? endW : math.min(periodEnd, endW)
if onM
periodEnd := na(periodEnd) ? endM : math.min(periodEnd, endM)
if onC
periodEnd := na(periodEnd) ? endC : math.min(periodEnd, endC)
if rollover and bar_index > 0 and not na(periodEnd)
curN := f_build(periodEnd)
f_trim()