本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AlgoAlpha
//@version=6
indicator("EQH/EQL Liquidity Zones (MTF) [AlgoAlpha]", "AlgoAlpha - EQH/EQL MTF", true, max_lines_count = 500, max_labels_count = 250, max_bars_back = 600)
// Detection
use_val = input.bool(true, "Filter by Overbought/Oversold", inline = "rsi", group = "Detection")
state_thresh = input.int(5, "Strength", minval = 1, maxval = 30, inline = "rsi", tooltip = "Enable the RSI filter and set the minimum distance from neutral RSI (50). EQH requires RSI above 50 plus Strength. EQL requires RSI below 50 minus Strength. Disable the filter to allow all matching levels.", group = "Detection")
tolerance = input.float(0.05, "Equal Level Tolerance", minval = 0.001, maxval = 0.1, step = 0.001, tooltip = "Maximum difference between matching highs or lows, measured against the 500-bar EMA of their average bar-to-bar movement. Lower values require a closer match. Calculated separately on each timeframe.", group = "Detection")
allow_extended = input.bool(true, "Separated Peaks/Troughs", inline = "extended", group = "Detection")
search_bars = input.int(20, "Lookback", minval = 2, maxval = 500, inline = "extended", tooltip = "When disabled, only consecutive candles can form zones. When enabled, also search for separated peaks and troughs within this many source-timeframe bars. Each peak or trough must exceed its two neighboring candles and is confirmed one candle later. All intervening highs must stay below both EQH endpoints. All intervening lows must stay above both EQL endpoints. The nearest valid match is used.", group = "Detection")
// Timeframes
use_tf1 = input.bool(true, "Timeframe 1", inline = "tf1", group = "Timeframes")
tf1 = input.timeframe("60", "", inline = "tf1", tooltip = "Enable this higher timeframe and select its interval. It must be strictly higher than the chart timeframe. The chart timeframe is always active. Higher-timeframe results use closed candles and are processed at the close of the first chart candle in the next source period. Rejection signals use chart candles, independently of the zone timeframe.", group = "Timeframes")
use_tf2 = input.bool(true, "Timeframe 2", inline = "tf2", group = "Timeframes")
tf2 = input.timeframe("180", "", inline = "tf2", tooltip = "Enable this higher timeframe and select its interval. It must be strictly higher than the chart timeframe. Detection, RSI filtering, sweep checks and zone age use this interval. Rejection signals always use chart candles.", group = "Timeframes")
use_tf3 = input.bool(true, "Timeframe 3", inline = "tf3", group = "Timeframes")
tf3 = input.timeframe("1D", "", inline = "tf3", tooltip = "Enable this higher timeframe and select its interval. It must be strictly higher than the chart timeframe. Detection, RSI filtering, sweep checks and zone age use this interval. Rejection signals always use chart candles.", group = "Timeframes")
use_tf4 = input.bool(false, "Timeframe 4", inline = "tf4", group = "Timeframes")
tf4 = input.timeframe("1W", "", inline = "tf4", tooltip = "Enable this higher timeframe and select its interval. It must be strictly higher than the chart timeframe. Detection, RSI filtering, sweep checks and zone age use this interval. Rejection signals always use chart candles.", group = "Timeframes")
use_tf5 = input.bool(false, "Timeframe 5", inline = "tf5", group = "Timeframes")
tf5 = input.timeframe("1M", "", inline = "tf5", tooltip = "Enable this higher timeframe and select its interval. It must be strictly higher than the chart timeframe. Detection, RSI filtering, sweep checks and zone age use this interval. Rejection signals always use chart candles.", group = "Timeframes")
// Management
expiry_age = input.int(1000, "Max Zone Age", minval = 1, tooltip = "Number of source-timeframe bars from the first matching candle after which a zone stops extending. Swept and expired zones remain as history until the stored-zone limit is reached.", group = "Management")
mitigate = input.string("body", "Sweep Type", options = ["wick", "body"], inline = "sweep", group = "Management")
allow_rejection = input.bool(false, "Allow Rejection", inline = "sweep", tooltip = "Body: stop a zone when a source-timeframe candle closes beyond its outer level. Allow Rejection requires two consecutive source-timeframe closes beyond that level. Wick: stop a zone when a source candle's high or low crosses the level. All sweep checks use closed source candles. Expiry does not trigger a sweep alert. Rejection signals are separate and always use chart candles.", group = "Management")
max_zones = input.int(200, "Max Stored Zones", minval = 10, maxval = 240, tooltip = "Maximum number of zones stored across all timeframes, including active and historical zones. The oldest zones are deleted first when this limit is reached.", group = "Management")
// Appearance
colBull = input.color(#00ffbb, "Bull Color", inline = "colors", group = "Appearance")
colBear = input.color(#ff1100, "Bear Color", inline = "colors", tooltip = "Colors for bullish EQL support zones and bearish EQH resistance zones.", group = "Appearance")
show_labels = input.bool(true, "Timeframe Labels", inline = "display", group = "Appearance")
show_signals = input.bool(true, "Rejection Signals", inline = "display", tooltip = "Show the source timeframe beside active zones. Triangles confirm at the current chart candle close. EQL: the candle touches the zone and closes above its upper edge. EQH: the candle touches the zone and closes below its lower edge. The candle must open, or the previous candle must close, inside the zone or on its support/resistance side. Only zones confirmed by the candle opening time can signal.", group = "Appearance")
// Variables
var zone_lines = array.new_line()
var zone_bodies = array.new_line()
var zone_fills = array.new_linefill()
var zone_labels = array.new_label()
var zone_bulls = array.new_bool()
var zone_slots = array.new_int()
var zone_births = array.new_int()
var zone_active = array.new_bool()
var zone_confirmed_at = array.new_int()
var zone_swept_at = array.new_int()
var last_bars = array.new_int(6, int(na))
// Functions
validate_tf(enabled, tf, slot) =>
if enabled and timeframe.in_seconds(tf) <= timeframe.in_seconds()
runtime.error("Timeframe " + str.tostring(slot) + " (" + tf + ") must be strictly higher than the chart timeframe (" + timeframe.period + "). Select a higher interval or disable this row.")
tf_text(tf) =>
minutes = str.tonumber(tf)
na(minutes) ? tf : minutes >= 60 and minutes % 60 == 0 ? str.tostring(int(minutes / 60)) + "H" : tf + "m"
detect_zones(shift) =>
varian = math.avg(math.abs(high - high[1]), math.abs(low - low[1]))
smoothvar = ta.ema(varian, 500)
state = ta.rsi(close, 14)
threshold = smoothvar * tolerance
eqh = math.abs(high - high[1]) < threshold
eql = math.abs(low - low[1]) < threshold
rsi_filtered_eqh = not use_val or state > 50 + state_thresh
rsi_filtered_eql = not use_val or state < 50 - state_thresh
create_eqh = eqh and rsi_filtered_eqh and not eqh[1]
create_eql = eql and rsi_filtered_eql and not eql[1]
h_time = create_eqh ? time[1] : int(na)
h_level = create_eqh ? math.max(high, high[1]) : float(na)
h_body = create_eqh ? math.max(open, open[1], close, close[1]) : float(na)
h_birth = create_eqh ? bar_index - 1 : int(na)
l_time = create_eql ? time[1] : int(na)
l_level = create_eql ? math.min(low, low[1]) : float(na)
l_body = create_eql ? math.min(open, open[1], close, close[1]) : float(na)
l_birth = create_eql ? bar_index - 1 : int(na)
peak = high[1] > high[2] and high[1] > high
trough = low[1] < low[2] and low[1] < low
if allow_extended
if peak and not create_eqh and not create_eqh[1] and rsi_filtered_eqh[1]
between_high = high[2]
for i = 3 to search_bars + 1
if i + 1 > bar_index or between_high >= high[1]
break
first_peak = high[i] > high[i + 1] and high[i] > high[i - 1]
equal_peak = math.abs(high[1] - high[i]) < threshold[1]
clear_path = between_high < math.min(high[1], high[i])
if first_peak and equal_peak and clear_path
h_time := time[i]
h_level := math.max(high[1], high[i])
h_body := math.max(open[1], close[1], open[i], close[i])
h_birth := bar_index - i
break
between_high := math.max(between_high, high[i])
if trough and not create_eql and not create_eql[1] and rsi_filtered_eql[1]
between_low = low[2]
for i = 3 to search_bars + 1
if i + 1 > bar_index or between_low <= low[1]
break
first_trough = low[i] < low[i + 1] and low[i] < low[i - 1]
equal_trough = math.abs(low[1] - low[i]) < threshold[1]
clear_path = between_low > math.max(low[1], low[i])
if first_trough and equal_trough and clear_path
l_time := time[i]
l_level := math.min(low[1], low[i])
l_body := math.min(open[1], close[1], open[i], close[i])
l_birth := bar_index - i
break
between_low := math.min(between_low, low[i])
[h_time[shift], h_level[shift], h_body[shift], h_birth[shift], l_time[shift], l_level[shift], l_body[shift], l_birth[shift], high[shift], low[shift], close[shift], close[shift + 1], bar_index[shift], time_close[shift]]
add_zone(slot, tf, bullish, left_time, level, body, birth, confirmed_at) =>
if zone_lines.size() >= max_zones
linefill.delete(zone_fills.shift())
line.delete(zone_lines.shift())
line.delete(zone_bodies.shift())
label.delete(zone_labels.shift())
zone_bulls.shift()
zone_slots.shift()
zone_births.shift()
zone_active.shift()
zone_confirmed_at.shift()
zone_swept_at.shift()
col = bullish ? colBull : colBear
right_time = nz(time_close, time)
outer_line = line.new(left_time, level, right_time, level, xloc = xloc.bar_time, color = col, width = 1)
inner_line = line.new(left_time, body, right_time, body, xloc = xloc.bar_time, color = color.new(col, 80), width = 1)
zone_fill = linefill.new(outer_line, inner_line, color.new(col, 80))
zone_label = show_labels ? label.new(time, level, (bullish ? "EQL " : "EQH ") + tf_text(tf), xloc = xloc.bar_time, style = label.style_label_left, color = color.new(col, 100), textcolor = col, size = size.tiny) : na
zone_lines.push(outer_line)
zone_bodies.push(inner_line)
zone_fills.push(zone_fill)
zone_labels.push(zone_label)
zone_bulls.push(bullish)
zone_slots.push(slot)
zone_births.push(birth)
zone_active.push(true)
zone_confirmed_at.push(nz(confirmed_at, nz(time_close, time + 1)))
zone_swept_at.push(int(na))
process_data(slot, tf, h_time, h_level, h_body, h_birth, l_time, l_level, l_body, l_birth, source_high, source_low, source_close, previous_close, source_bar, source_end) =>
eqh_formed = false
eql_formed = false
eqh_mitigated = false
eql_mitigated = false
last_bar = last_bars.get(slot)
new_bar = not na(source_bar) and (na(last_bar) or source_bar > last_bar)
if barstate.isconfirmed and new_bar
if not na(h_time) and source_bar - h_birth <= expiry_age
add_zone(slot, tf, false, h_time, h_level, h_body, h_birth, source_end)
eqh_formed := true
if not na(l_time) and source_bar - l_birth <= expiry_age
add_zone(slot, tf, true, l_time, l_level, l_body, l_birth, source_end)
eql_formed := true
if zone_lines.size() > 0
for i = zone_lines.size() - 1 to 0
if zone_active.get(i) and zone_slots.get(i) == slot
outer_line = zone_lines.get(i)
inner_line = zone_bodies.get(i)
level = outer_line.get_y1()
bullish = zone_bulls.get(i)
cross_body = bullish ? source_close < level : source_close > level
previous_cross = bullish ? previous_close < level : previous_close > level
cross_wick = bullish ? source_low < level : source_high > level
is_swept = mitigate == "body" ? cross_body and (not allow_rejection or previous_cross) : cross_wick
is_expired = source_bar - zone_births.get(i) > expiry_age
if is_swept or is_expired
outer_line.set_x2(nz(source_end, time))
inner_line.set_x2(nz(source_end, time))
label.delete(zone_labels.get(i))
zone_labels.set(i, na)
zone_active.set(i, false)
if is_swept and not is_expired
zone_swept_at.set(i, nz(source_end, time))
eqh_mitigated := eqh_mitigated or not bullish
eql_mitigated := eql_mitigated or bullish
last_bars.set(slot, source_bar)
[eqh_formed, eql_formed, eqh_mitigated, eql_mitigated]
process_htf(enabled, tf, slot) =>
eqh_formed = false
eql_formed = false
eqh_mitigated = false
eql_mitigated = false
if enabled
[h_time, h_level, h_body, h_birth, l_time, l_level, l_body, l_birth, source_high, source_low, source_close, previous_close, source_bar, source_end] = request.security(syminfo.tickerid, tf, detect_zones(1), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
[new_eqh, new_eql, swept_eqh, swept_eql] = process_data(slot, tf, h_time, h_level, h_body, h_birth, l_time, l_level, l_body, l_birth, source_high, source_low, source_close, previous_close, source_bar, source_end)
eqh_formed := new_eqh
eql_formed := new_eql
eqh_mitigated := swept_eqh
eql_mitigated := swept_eql
[eqh_formed, eql_formed, eqh_mitigated, eql_mitigated]
// Chart-Timeframe Rejections
chart_rejections() =>
eqh_rejected = false
eql_rejected = false
if barstate.isconfirmed and zone_lines.size() > 0
chart_end = nz(time_close, time)
for i = 0 to zone_lines.size() - 1
known_at_open = time >= zone_confirmed_at.get(i)
swept_on_bar = not na(zone_swept_at.get(i)) and zone_swept_at.get(i) == chart_end
if known_at_open and (zone_active.get(i) or swept_on_bar)
level = zone_lines.get(i).get_y1()
body = zone_bodies.get(i).get_y1()
zone_top = math.max(level, body)
zone_bottom = math.min(level, body)
bullish = zone_bulls.get(i)
touched = low <= zone_top and high >= zone_bottom
correct_side = bullish ? open >= zone_bottom or close[1] >= zone_bottom : open <= zone_top or close[1] <= zone_top
rejected = touched and correct_side and (bullish ? close > zone_top : close < zone_bottom)
eqh_rejected := eqh_rejected or (not bullish and rejected)
eql_rejected := eql_rejected or (bullish and rejected)
[eqh_rejected, eql_rejected]
// Validation
if barstate.isfirst
validate_tf(use_tf1, tf1, 1)
validate_tf(use_tf2, tf2, 2)
validate_tf(use_tf3, tf3, 3)
validate_tf(use_tf4, tf4, 4)
validate_tf(use_tf5, tf5, 5)
// Calculations
[h_time, h_level, h_body, h_birth, l_time, l_level, l_body, l_birth, source_high, source_low, source_close, previous_close, source_bar, source_end] = detect_zones(0)
[h0, l0, hm0, lm0] = process_data(0, timeframe.period, h_time, h_level, h_body, h_birth, l_time, l_level, l_body, l_birth, source_high, source_low, source_close, previous_close, source_bar, source_end)
[h1, l1, hm1, lm1] = process_htf(use_tf1, tf1, 1)
[h2, l2, hm2, lm2] = process_htf(use_tf2, tf2, 2)
[h3, l3, hm3, lm3] = process_htf(use_tf3, tf3, 3)
[h4, l4, hm4, lm4] = process_htf(use_tf4, tf4, 4)
[h5, l5, hm5, lm5] = process_htf(use_tf5, tf5, 5)
eqh_formed = h0 or h1 or h2 or h3 or h4 or h5
eql_formed = l0 or l1 or l2 or l3 or l4 or l5
eqh_mitigated = hm0 or hm1 or hm2 or hm3 or hm4 or hm5
eql_mitigated = lm0 or lm1 or lm2 or lm3 or lm4 or lm5
// Signals
[eqh_rejected, eql_rejected] = chart_rejections()
// Visuals
if zone_lines.size() > 0
for i = 0 to zone_lines.size() - 1
if zone_active.get(i)
zone_lines.get(i).set_x2(nz(time_close, time))
zone_bodies.get(i).set_x2(nz(time_close, time))
if show_labels
zone_labels.get(i).set_x(time)
plotchar(show_signals and eqh_rejected, "EQH Rejection", "▼", location.abovebar, size = size.tiny, color = colBear)
plotchar(show_signals and eql_rejected, "EQL Rejection", "▲", location.belowbar, size = size.tiny, color = colBull)
// Alerts
alertcondition(eqh_formed, "New Equal High Formed")
alertcondition(eql_formed, "New Equal Low Formed")
alertcondition(eqh_mitigated, "Equal High Mitigated")
alertcondition(eql_mitigated, "Equal Low Mitigated")
alertcondition(eqh_rejected, "Equal High Rejection")
alertcondition(eql_rejected, "Equal Low Rejection")