本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("PDH / PDL / EQ [Precise]", overlay=true, max_lines_count=50, max_labels_count=50)
// ==========================================
// --- Inputs ---
// ==========================================
grpG = "Global UI Settings"
visualMode = input.string("Dotted Trace + Solid Tag", "Line Visual Mode", options=["Dotted Trace + Solid Tag", "Classic Single Line"], group=grpG)
showLabels = input.bool(true, "Master Label Switch", group=grpG)
showPrice = input.bool(false, "Show Price on Labels", group=grpG)
markSwept = input.bool(false, "Mark Swept Levels (x)", group=grpG, tooltip="Appends ' (x)' to PDH/PDL label once today's price sweeps the level.")
dotMargin = input.int(3, "Right Margin Gap (Bars)", minval=0, maxval=30, group=grpG, tooltip="Bars past the current candle before the solid tag begins (or total offset in Classic mode).")
solidLen = input.int(6, "Solid Tag Length (Bars)", minval=2, maxval=30, group=grpG)
textSizeIn = input.string("Small", "Label Text Size", options=["Tiny", "Small", "Normal", "Large"], group=grpG)
grpD = "Previous Daily Levels"
showPdh = input.bool(true, "", tooltip="Show PDH Line", inline="d1", group=grpD)
showLabPdh = input.bool(true, "", tooltip="Show PDH Label", inline="d1", group=grpD)
txtPdh = input.string("PDH", "", inline="d1", group=grpD)
colPdh = input.color(color.new(#f23645, 0), "", inline="d1", group=grpD)
styPdh = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="d1", group=grpD)
widPdh = input.int(1, "", minval=1, maxval=4, inline="d1", group=grpD)
showPdl = input.bool(true, "", tooltip="Show PDL Line", inline="d2", group=grpD)
showLabPdl = input.bool(true, "", tooltip="Show PDL Label", inline="d2", group=grpD)
txtPdl = input.string("PDL", "", inline="d2", group=grpD)
colPdl = input.color(color.new(#089981, 0), "", inline="d2", group=grpD)
styPdl = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="d2", group=grpD)
widPdl = input.int(1, "", minval=1, maxval=4, inline="d2", group=grpD)
showPdEq = input.bool(true, "", tooltip="Show EQ (Bullish: [Low+Close]/2 | Bearish: [High+Close]/2)", inline="d3", group=grpD)
showLabPdEq = input.bool(true, "", tooltip="Show EQ Label", inline="d3", group=grpD)
txtPdEq = input.string("EQ", "", inline="d3", group=grpD)
colPdEq = input.color(color.new(#787b86, 0), "", inline="d3", group=grpD)
styPdEq = input.string("Dotted", "", options=["Solid", "Dashed", "Dotted"], inline="d3", group=grpD)
widPdEq = input.int(1, "", minval=1, maxval=4, inline="d3", group=grpD)
// ==========================================
// --- Helper Functions ---
// ==========================================
f_style(s) => s == "Dashed" ? line.style_dashed : s == "Dotted" ? line.style_dotted : line.style_solid
f_size(s) => s == "Tiny" ? size.tiny : s == "Normal" ? size.normal : s == "Large" ? size.large : size.small
labSz = f_size(textSizeIn)
isDualMode = visualMode == "Dotted Trace + Solid Tag"
canShow = timeframe.isintraday or timeframe.isdaily
f_draw(_ld, _ls, _lb, _shL, _shB, _t1, _y, _txt, _swept, _col, _sty, _wid) =>
if canShow and _shL and not na(_t1) and not na(_y)
tf_ms = timeframe.in_seconds() * 1000
t_mid = time + tf_ms * dotMargin
t_end = isDualMode ? t_mid + tf_ms * solidLen : t_mid
if isDualMode
line.set_xy1(_ld, _t1, _y), line.set_xy2(_ld, t_mid, _y), line.set_color(_ld, _col), line.set_width(_ld, 1), line.set_style(_ld, line.style_dotted)
line.set_xy1(_ls, t_mid, _y), line.set_xy2(_ls, t_end, _y), line.set_color(_ls, _col), line.set_width(_ls, _wid), line.set_style(_ls, f_style(_sty))
else
line.set_xy1(_ld, _t1, _y), line.set_xy2(_ld, t_end, _y), line.set_color(_ld, _col), line.set_width(_ld, _wid), line.set_style(_ld, f_style(_sty))
line.set_xy1(_ls, na, na)
if showLabels and _shB
sweepTag = (markSwept and _swept) ? " (x)" : ""
priceTag = showPrice ? " " + str.tostring(_y, format.mintick) : ""
label.set_xy(_lb, t_end, _y)
label.set_text(_lb, _txt + sweepTag + priceTag)
label.set_textcolor(_lb, _col)
label.set_size(_lb, labSz)
label.set_style(_lb, isDualMode ? label.style_label_lower_right : label.style_label_left)
else
label.set_xy(_lb, na, na)
else
line.set_xy1(_ld, na, na), line.set_xy1(_ls, na, na), label.set_xy(_lb, na, na)
// ==========================================
// --- Precision Wick-to-Wick Tracking ---
// ==========================================
d_time = time("D")
newDay = not na(d_time) and (ta.change(d_time) != 0 or barstate.isfirst)
var float cur_do = na, var float cur_dh = na, var int cur_dh_t = na, var float cur_dl = na, var int cur_dl_t = na
var float pd_o = na, var float pd_h = na, var int pd_h_t = na, var float pd_l = na, var int pd_l_t = na
var float pd_c = na, var int d_anc = na
if newDay
pd_o := cur_do
pd_h := cur_dh, pd_h_t := cur_dh_t
pd_l := cur_dl, pd_l_t := cur_dl_t
pd_c := close[1]
d_anc := time
cur_do := open
cur_dh := high, cur_dh_t := time
cur_dl := low, cur_dl_t := time
else
if high > cur_dh or na(cur_dh)
cur_dh := high, cur_dh_t := time
if low < cur_dl or na(cur_dl)
cur_dl := low, cur_dl_t := time
// Fallback only if chart has less than 1 full day of bars loaded
[sec_o1, sec_h1, sec_l1, sec_c1] = request.security(syminfo.tickerid, "D", [open[1], high[1], low[1], close[1]], barmerge.gaps_off, barmerge.lookahead_on)
float use_o = not na(pd_o) ? pd_o : sec_o1
float use_h = not na(pd_h) ? pd_h : sec_h1
float use_l = not na(pd_l) ? pd_l : sec_l1
float use_c = not na(pd_c) ? pd_c : sec_c1
// Directional Previous Day EQ:
// - Bullish Day (Close >= Open): Midpoint between Low and Close
// - Bearish Day (Close < Open): Midpoint between High and Close
float pd_eq = not na(use_c) and not na(use_o) ? (use_c >= use_o ? (use_c + use_l) / 2.0 : (use_c + use_h) / 2.0) : na
int safe_pdh_t1 = not na(pd_h_t) ? pd_h_t : d_anc
int safe_pdl_t1 = not na(pd_l_t) ? pd_l_t : d_anc
bool pdhSwept = not na(cur_dh) and not na(use_h) and cur_dh > use_h
bool pdlSwept = not na(cur_dl) and not na(use_l) and cur_dl < use_l
// ==========================================
// --- Render Initialization & Updates ---
// ==========================================
var line pdhD = line.new(na, na, na, na, xloc=xloc.bar_time), var line pdhS = line.new(na, na, na, na, xloc=xloc.bar_time), var label pdhB = label.new(na, na, "", xloc=xloc.bar_time, color=color.new(color.white, 100))
var line pdlD = line.new(na, na, na, na, xloc=xloc.bar_time), var line pdlS = line.new(na, na, na, na, xloc=xloc.bar_time), var label pdlB = label.new(na, na, "", xloc=xloc.bar_time, color=color.new(color.white, 100))
var line pdeD = line.new(na, na, na, na, xloc=xloc.bar_time), var line pdeS = line.new(na, na, na, na, xloc=xloc.bar_time), var label pdeB = label.new(na, na, "", xloc=xloc.bar_time, color=color.new(color.white, 100))
if barstate.islast
f_draw(pdhD, pdhS, pdhB, showPdh, showLabPdh, safe_pdh_t1, use_h, txtPdh, pdhSwept, colPdh, styPdh, widPdh)
f_draw(pdlD, pdlS, pdlB, showPdl, showLabPdl, safe_pdl_t1, use_l, txtPdl, pdlSwept, colPdl, styPdl, widPdl)
f_draw(pdeD, pdeS, pdeB, showPdEq, showLabPdEq, d_anc, pd_eq, txtPdEq, false, colPdEq, styPdEq, widPdEq)
// ==========================================
// --- Alerts ---
// ==========================================
alertcondition(ta.crossover(high, use_h), title="PDH Swept / Crossed", message="Price swept Previous Day High (PDH)")
alertcondition(ta.crossunder(low, use_l), title="PDL Swept / Crossed", message="Price swept Previous Day Low (PDL)")
alertcondition(ta.cross(close, pd_eq), title="Crossed Prev Day EQ", message="Price crossed Previous Day Equilibrium (EQ)")