本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("High-Probability Key Levels & Clean Price Action [Antigravity]", shorttitle="Clean KeyLevels", overlay=true, max_labels_count=200, max_lines_count=200)
// =============================================================================
// INPUTS & DISPLAY TOGGLES
// =============================================================================
grp_levels = "📌 1. Daily & Pre-Market Levels (PDH/PDL, PMH/PML)"
show_pd = input.bool(true, "Show Previous Day High & Low (PDH / PDL)", group=grp_levels)
show_pm = input.bool(true, "Show Pre-Market High & Low (PMH / PML)", group=grp_levels)
show_pdc = input.bool(false, "Show Previous Day Close (PDC)", group=grp_levels)
line_len = input.int(15, "Levels Right Projection (Bars ahead)", minval=5, maxval=50, group=grp_levels)
grp_sr = "🧱 2. Support / Resistance & Psychological Pivots"
show_sr = input.bool(true, "Show Clean Swing S/R Levels (Recent High/Low)", group=grp_sr)
pivot_len = input.int(15, "Pivot Lookback Strength", minval=5, group=grp_sr)
show_psych = input.bool(true, "Show Nearest Psychological Round Number", group=grp_sr)
grp_ema = "📈 3. Clean EMAs"
show_ema9 = input.bool(true, "9 EMA (Fast Trend - Cyan)", group=grp_ema)
show_ema21 = input.bool(true, "21 EMA (Base Guide - Green)", group=grp_ema)
show_ema50 = input.bool(false, "50 EMA (Dynamic S/R - Amber)", group=grp_ema)
grp_sig = "🎯 4. High-Probability Signals"
show_retest_sig = input.bool(true, "Show Breakout-Retest Signals (▲/▼)", group=grp_sig)
show_cross_sig = input.bool(true, "Show EMA 9/21 Crossovers (Small dots/arrows)", group=grp_sig)
filter_cross = input.bool(true, "Only Trigger Crossovers near Key Levels (Filters Chop)", group=grp_sig)
show_hud_pill = input.bool(true, "Show Compact Info Pill (Top Right)", group=grp_sig)
// =============================================================================
// 1. CLEAN PDH / PDL (SESSION-ONLY, NO INFINITE LASER LINES)
// =============================================================================
// Fetch true previous daily high/low/close from daily timeframe
[pdh_val, pdl_val, pdc_val] = request.security(syminfo.tickerid, "D", [high[1], low[1], close[1]], lookahead=barmerge.lookahead_on)
var line l_pdh = na
var line l_pdl = na
var line l_pdc = na
var label lbl_pdh = na
var label lbl_pdl = na
var label lbl_pdc = na
// Track current session starting bar
var int day_start_bar = na
bool new_day = ta.change(time("D")) != 0
if new_day
day_start_bar := bar_index
// Only update lines on the current active day (keeps chart clean, no infinite lines)
if show_pd and barstate.islast and not na(pdh_val)
line.delete(l_pdh)
line.delete(l_pdl)
line.delete(l_pdc)
label.delete(lbl_pdh)
label.delete(lbl_pdl)
label.delete(lbl_pdc)
int x1 = not na(day_start_bar) ? day_start_bar : bar_index - 50
int x2 = bar_index + line_len
// PDH (Red Line)
l_pdh := line.new(x1, pdh_val, x2, pdh_val, color=color.new(#ef4444, 20), width=1, style=line.style_solid)
lbl_pdh := label.new(x2, pdh_val, "PDH $" + str.tostring(pdh_val, "#.##"), color=color.new(color.black, 100), textcolor=color.new(#ef4444, 0), style=label.style_label_left, size=size.small)
// PDL (Green Line)
l_pdl := line.new(x1, pdl_val, x2, pdl_val, color=color.new(#10b981, 20), width=1, style=line.style_solid)
lbl_pdl := label.new(x2, pdl_val, "PDL $" + str.tostring(pdl_val, "#.##"), color=color.new(color.black, 100), textcolor=color.new(#10b981, 0), style=label.style_label_left, size=size.small)
if show_pdc
l_pdc := line.new(x1, pdc_val, x2, pdc_val, color=color.new(#94a3b8, 40), width=1, style=line.style_dashed)
lbl_pdc := label.new(x2, pdc_val, "PDC $" + str.tostring(pdc_val, "#.##"), color=color.new(color.black, 100), textcolor=color.new(#94a3b8, 20), style=label.style_label_left, size=size.tiny)
// =============================================================================
// 2. CLEAN PRE-MARKET HIGH & LOW (PMH / PML)
// =============================================================================
t_pm = time(timeframe.period, "0400-0930:23456")
is_pm = not na(t_pm)
var float cur_pmh = na
var float cur_pml = na
if is_pm
if not is_pm[1] or new_day
cur_pmh := high
cur_pml := low
else
cur_pmh := math.max(cur_pmh, high)
cur_pml := math.min(cur_pml, low)
var line l_pmh = na
var line l_pml = na
var label lbl_pmh = na
var label lbl_pml = na
if show_pm and barstate.islast and not na(cur_pmh) and not na(cur_pml)
line.delete(l_pmh)
line.delete(l_pml)
label.delete(lbl_pmh)
label.delete(lbl_pml)
int x1 = not na(day_start_bar) ? day_start_bar : bar_index - 30
int x2 = bar_index + line_len
// PMH (Orange Line)
l_pmh := line.new(x1, cur_pmh, x2, cur_pmh, color=color.new(#f97316, 20), width=1, style=line.style_dashed)
lbl_pmh := label.new(x2, cur_pmh, "PMH $" + str.tostring(cur_pmh, "#.##"), color=color.new(color.black, 100), textcolor=color.new(#f97316, 0), style=label.style_label_left, size=size.small)
// PML (Cyan Line)
l_pml := line.new(x1, cur_pml, x2, cur_pml, color=color.new(#06b6d4, 20), width=1, style=line.style_dashed)
lbl_pml := label.new(x2, cur_pml, "PML $" + str.tostring(cur_pml, "#.##"), color=color.new(color.black, 100), textcolor=color.new(#06b6d4, 0), style=label.style_label_left, size=size.small)
// =============================================================================
// 3. CLEAN SWING SUPPORT / RESISTANCE (LINES ONLY, NO CLUTTERED BOXES)
// =============================================================================
ph = ta.pivothigh(high, pivot_len, pivot_len)
pl = ta.pivotlow(low, pivot_len, pivot_len)
var float last_res = na
var float last_sup = na
if not na(ph)
last_res := ph
if not na(pl)
last_sup := pl
var line l_res = na
var line l_sup = na
var label lbl_res = na
var label lbl_sup = na
if show_sr and barstate.islast and not na(last_res) and not na(last_sup)
line.delete(l_res)
line.delete(l_sup)
label.delete(lbl_res)
label.delete(lbl_sup)
int x1 = bar_index - 40
int x2 = bar_index + line_len
// Resistance Line
l_res := line.new(x1, last_res, x2, last_res, color=color.new(#f43f5e, 50), width=1, style=line.style_dotted)
lbl_res := label.new(x2, last_res, "Key Res $" + str.tostring(last_res, "#.##"), color=color.new(color.black, 100), textcolor=color.new(#f43f5e, 20), style=label.style_label_left, size=size.tiny)
// Support Line
l_sup := line.new(x1, last_sup, x2, last_sup, color=color.new(#10b981, 50), width=1, style=line.style_dotted)
lbl_sup := label.new(x2, last_sup, "Key Sup $" + str.tostring(last_sup, "#.##"), color=color.new(color.black, 100), textcolor=color.new(#10b981, 20), style=label.style_label_left, size=size.tiny)
// =============================================================================
// 4. NEAREST PSYCHOLOGICAL WHOLE NUMBER LEVEL
// =============================================================================
f_psych_step(p) =>
if p > 500
10.0
else if p > 100
5.0
else if p > 25
1.0
else
0.50
float p_step = f_psych_step(close)
float psych_level = math.round(close / p_step) * p_step
var line l_psych = na
var label lbl_psych = na
if show_psych and barstate.islast
line.delete(l_psych)
label.delete(lbl_psych)
int x1 = bar_index - 20
int x2 = bar_index + line_len
l_psych := line.new(x1, psych_level, x2, psych_level, color=color.new(#64748b, 60), width=1, style=line.style_dotted)
lbl_psych := label.new(x2, psych_level, "Round $" + str.tostring(psych_level, "#.##"), color=color.new(color.black, 100), textcolor=color.new(#94a3b8, 30), style=label.style_label_left, size=size.tiny)
// =============================================================================
// 5. CLEAN EMAS
// =============================================================================
ema9 = ta.ema(close, 9)
ema21 = ta.ema(close, 21)
ema50 = ta.ema(close, 50)
plot(show_ema9 ? ema9 : na, "9 EMA (Fast)", color=color.new(#06b6d4, 0), linewidth=1)
plot(show_ema21 ? ema21 : na, "21 EMA (Base)", color=color.new(#10b981, 0), linewidth=2)
plot(show_ema50 ? ema50 : na, "50 EMA (S/R)", color=color.new(#f59e0b, 0), linewidth=1)
atr14 = ta.atr(14)
ema_bull_cross = ta.crossover(ema9, ema21)
ema_bear_cross = ta.crossunder(ema9, ema21)
// =============================================================================
// 6. HIGH-PROBABILITY BREAKOUT-RETEST LOGIC
// =============================================================================
f_near(lvl) =>
not na(lvl) and math.abs(close - lvl) <= (0.75 * atr14)
bool near_key_level = f_near(pdh_val) or f_near(pdl_val) or f_near(cur_pmh) or f_near(cur_pml) or f_near(last_res) or f_near(last_sup) or f_near(psych_level)
// Bullish Retest: Prior breakout of PDH or PMH, retest holding with 9 >= 21
bool broke_pdh_prior = (high[1] > pdh_val or high[2] > pdh_val) and (close[2] < pdh_val or close[3] < pdh_val)
bool holding_pdh = low <= (pdh_val + 0.35 * atr14) and close >= pdh_val and close > open and (ema9 >= ema21)
bool pdh_retest_long = broke_pdh_prior and holding_pdh
bool broke_pmh_prior = (high[1] > cur_pmh or high[2] > cur_pmh) and (close[2] < cur_pmh or close[3] < cur_pmh)
bool holding_pmh = low <= (cur_pmh + 0.35 * atr14) and close >= cur_pmh and close > open and (ema9 >= ema21)
bool pmh_retest_long = broke_pmh_prior and holding_pmh
bool bull_retest = pdh_retest_long or pmh_retest_long
// Bearish Retest: Prior breakdown of PDL or PML, retest rejecting with 9 <= 21
bool broke_pdl_prior = (low[1] < pdl_val or low[2] < pdl_val) and (close[2] > pdl_val or close[3] > pdl_val)
bool reject_pdl = high >= (pdl_val - 0.35 * atr14) and close <= pdl_val and close < open and (ema9 <= ema21)
bool pdl_retest_short = broke_pdl_prior and reject_pdl
bool broke_pml_prior = (low[1] < cur_pml or low[2] < cur_pml) and (close[2] > cur_pml or close[3] > cur_pml)
bool reject_pml = high >= (cur_pml - 0.35 * atr14) and close <= cur_pml and close < open and (ema9 <= ema21)
bool pml_retest_short = broke_pml_prior and reject_pml
bool bear_retest = pdl_retest_short or pml_retest_short
// =============================================================================
// 7. CLEAN SIGNALS (DISCREET ARROWS & CLEAN LABELS, NO GIANT BILLBOARDS)
// =============================================================================
// Breakout-Retest Signals
plotshape(show_retest_sig and bull_retest, "Retest Long", shape.triangleup, location.belowbar, color.new(#10b981, 0), text="RETEST BUY", textcolor=color.new(#10b981, 0), size=size.tiny, display=display.pane)
plotshape(show_retest_sig and bear_retest, "Retest Short", shape.triangledown, location.abovebar, color.new(#ef4444, 0), text="RETEST SELL", textcolor=color.new(#ef4444, 0), size=size.tiny, display=display.pane)
// EMA 9/21 Cross Signals (Discreet dots, filtered near key levels to kill chop)
bool valid_bull_cross = ema_bull_cross and (filter_cross ? near_key_level : true) and not bull_retest
bool valid_bear_cross = ema_bear_cross and (filter_cross ? near_key_level : true) and not bear_retest
plotshape(show_cross_sig and valid_bull_cross, "EMA Bull Cross", shape.circle, location.belowbar, color.new(#06b6d4, 20), size=size.tiny, display=display.pane)
plotshape(show_cross_sig and valid_bear_cross, "EMA Bear Cross", shape.circle, location.abovebar, color.new(#f43f5e, 20), size=size.tiny, display=display.pane)
// =============================================================================
// 8. MINIMALIST HUD PILL (Clean 2x3 Corner Badge)
// =============================================================================
var table hud = table.new(position.top_right, 2, 4, bgcolor=color.new(#020617, 30), border_color=color.new(#1e293b, 50), border_width=1)
if show_hud_pill and barstate.islast
string bias_t = ema9 >= ema21 ? "BULLISH (9>21)" : "BEARISH (9<21)"
color bias_c = ema9 >= ema21 ? color.new(#10b981, 0) : color.new(#ef4444, 0)
table.cell(hud, 0, 0, "TREND BIAS", text_color=color.new(#94a3b8, 0), text_size=size.tiny, bgcolor=color.new(#0f172a, 30))
table.cell(hud, 1, 0, bias_t, text_color=color.white, text_size=size.tiny, bgcolor=bias_c)
table.cell(hud, 0, 1, "PDH / PDL", text_color=color.new(#94a3b8, 0), text_size=size.tiny, bgcolor=color.new(#0f172a, 30))
table.cell(hud, 1, 1, "$" + str.tostring(pdh_val, "#.##") + " / $" + str.tostring(pdl_val, "#.##"), text_color=color.white, text_size=size.tiny, bgcolor=color.new(#020617, 30))
table.cell(hud, 0, 2, "PMH / PML", text_color=color.new(#94a3b8, 0), text_size=size.tiny, bgcolor=color.new(#0f172a, 30))
string pm_str = not na(cur_pmh) ? ("$" + str.tostring(cur_pmh, "#.##") + " / $" + str.tostring(cur_pml, "#.##")) : "Calculating..."
table.cell(hud, 1, 2, pm_str, text_color=color.white, text_size=size.tiny, bgcolor=color.new(#020617, 30))
table.cell(hud, 0, 3, "PSYCH LEVEL", text_color=color.new(#94a3b8, 0), text_size=size.tiny, bgcolor=color.new(#0f172a, 30))
table.cell(hud, 1, 3, "$" + str.tostring(psych_level, "#.##"), text_color=color.new(#38bdf8, 0), text_size=size.tiny, bgcolor=color.new(#020617, 30))
// =============================================================================
// 9. ALERTS
// =============================================================================
alertcondition(bull_retest, "Breakout-Retest BUY", "High-Probability Bullish Breakout-Retest confirmed on {{ticker}} at {{close}}!")
alertcondition(bear_retest, "Breakdown-Retest SELL", "High-Probability Bearish Breakdown-Retest confirmed on {{ticker}} at {{close}}!")
alertcondition(valid_bull_cross, "EMA Bull Cross (Near Key Level)", "9 EMA crossed above 21 EMA near Key Support on {{ticker}} at {{close}}")
alertcondition(valid_bear_cross, "EMA Bear Cross (Near Key Level)", "9 EMA crossed below 21 EMA near Key Resistance on {{ticker}} at {{close}}")