本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator('MTF Intraday Fractal CHoCH Signals', overlay = true, max_lines_count = 500)
// ==========================================
// 1. INPUTS
// ==========================================
grp_str = 'Structure Settings'
fractal_tf = input.timeframe('', 'Fractal Timeframe (Blank = Chart TF)', group = grp_str)
fractal_len = input.int(2, 'Fractal Length (Bars Left/Right)', minval = 1, group = grp_str)
show_line = input.bool(false, 'Show CHoCH Reference Line', group = grp_str)
grp_sess = 'Intraday Session Filter'
use_session = input.bool(true, 'Restrict Signals to Session', group = grp_sess)
trade_sess = input.session('0915-1510', 'Trading Session Time', group = grp_sess)
grp_col = 'Colors'
bull_col = input.color(#089981, 'Buy Signal Color', group = grp_col)
bear_col = input.color(#f23645, 'Sell Signal Color', group = grp_col)
// ==========================================
// 2. HTF NON-REPAINTING FRACTAL FUNCTION
// ==========================================
f_htf_swings(int len) =>
bool new_d = ta.change(time('D')) != 0
var int d_bar = 0
var float s_h = na
var int s_ht = na
var float s_l = na
var int s_lt = na
if new_d
d_bar := bar_index
s_h := na
s_ht := na
s_l := na
s_lt := na
s_lt
float _ph = ta.pivothigh(high, len, len)
float _pl = ta.pivotlow(low, len, len)
bool valid_f = bar_index - 2 * len >= d_bar
if not na(_ph) and valid_f
s_h := _ph
s_ht := time[len]
s_ht
if not na(_pl) and valid_f
s_l := _pl
s_lt := time[len]
s_lt
// Return [1] (last closed HTF bar) for zero-repainting MTF accuracy
[s_h[1], s_ht[1], s_l[1], s_lt[1]]
string req_tf = fractal_tf == '' ? timeframe.period : fractal_tf
bool is_htf = fractal_tf != '' and timeframe.in_seconds(req_tf) > timeframe.in_seconds(timeframe.period)
[htf_sh, htf_sh_t, htf_sl, htf_sl_t] = request.security(syminfo.tickerid, req_tf, f_htf_swings(fractal_len), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
// ==========================================
// 3. SESSION & NEW DAY RESET (CHART TF)
// ==========================================
bool in_session = not use_session or not na(time(timeframe.period, trade_sess))
bool is_new_day = ta.change(time('D')) != 0
var int day_start_bar = 0
var int day_start_time = time
var int trend = 0 // 1 = Uptrend, -1 = Downtrend, 0 = Neutral
var float last_sh = na
var int last_sh_time = na
var bool sh_active = false
var float last_sl = na
var int last_sl_time = na
var bool sl_active = false
if is_new_day
day_start_bar := bar_index
day_start_time := time
trend := 0
last_sh := na
last_sh_time := na
sh_active := false
last_sl := na
last_sl_time := na
sl_active := false
sl_active
// ==========================================
// 4. UNIFIED NEW-DAY FRACTAL UPDATES
// ==========================================
float chart_ph = ta.pivothigh(high, fractal_len, fractal_len)
float chart_pl = ta.pivotlow(low, fractal_len, fractal_len)
bool valid_chart_fractal = bar_index - 2 * fractal_len >= day_start_bar
bool new_sh = false
bool new_sl = false
float sh_val = na
int sh_t = na
float sl_val = na
int sl_t = na
if is_htf
// Accept HTF fractal only if it formed today and is newer than the last recorded one
if not na(htf_sh) and htf_sh_t >= day_start_time and (na(last_sh_time) or htf_sh_t != last_sh_time)
new_sh := true
sh_val := htf_sh
sh_t := htf_sh_t
sh_t
if not na(htf_sl) and htf_sl_t >= day_start_time and (na(last_sl_time) or htf_sl_t != last_sl_time)
new_sl := true
sl_val := htf_sl
sl_t := htf_sl_t
sl_t
else
if not na(chart_ph) and valid_chart_fractal
new_sh := true
sh_val := chart_ph
sh_t := time[fractal_len]
sh_t
if not na(chart_pl) and valid_chart_fractal
new_sl := true
sl_val := chart_pl
sl_t := time[fractal_len]
sl_t
bool buy_signal = false
bool sell_signal = false
if new_sh
last_sh := sh_val
last_sh_time := sh_t
sh_active := true
// Retroactive lag check (Chart TF only)
if not is_htf and trend == -1 and fractal_len > 1
for i = fractal_len - 1 to 1 by 1
if close[i] > last_sh and sh_active
trend := 1
sh_active := false
if in_session
buy_signal := true
if show_line
line.new(last_sh_time, last_sh, time, last_sh, xloc = xloc.bar_time, color = bull_col, style = line.style_dashed)
break
if new_sl
last_sl := sl_val
last_sl_time := sl_t
sl_active := true
// Retroactive lag check (Chart TF only)
if not is_htf and trend == 1 and fractal_len > 1
for i = fractal_len - 1 to 1 by 1
if close[i] < last_sl and sl_active
trend := -1
sl_active := false
if in_session
sell_signal := true
if show_line
line.new(last_sl_time, last_sl, time, last_sl, xloc = xloc.bar_time, color = bear_col, style = line.style_dashed)
break
// Initialize intraday trend on first available fractal break of the day
if trend == 0
if not na(last_sh) and close > last_sh
trend := 1
sh_active := false
sh_active
else if not na(last_sl) and close < last_sl
trend := -1
sl_active := false
sl_active
// ==========================================
// 5. REAL-TIME CHoCH ENTRY TRIGGERS (CHART TF)
// ==========================================
if trend == -1 and sh_active and not na(last_sh)
if close > last_sh
trend := 1
sh_active := false
if in_session
buy_signal := true
if show_line
line.new(last_sh_time, last_sh, time, last_sh, xloc = xloc.bar_time, color = bull_col, style = line.style_dashed)
else if trend == 1 and sl_active and not na(last_sl)
if close < last_sl
trend := -1
sl_active := false
if in_session
sell_signal := true
if show_line
line.new(last_sl_time, last_sl, time, last_sl, xloc = xloc.bar_time, color = bear_col, style = line.style_dashed)
// ==========================================
// 6. CLEAN BUY / SELL SIGNALS & ALERTS
// ==========================================
plotshape(buy_signal, title = 'Buy Signal', text = 'BUY', style = shape.labelup, location = location.belowbar, color = bull_col, textcolor = color.white, size = size.small)
plotshape(sell_signal, title = 'Sell Signal', text = 'SELL', style = shape.labeldown, location = location.abovebar, color = bear_col, textcolor = color.white, size = size.small)
alertcondition(buy_signal, 'CHoCH Buy Signal', 'Bullish CHoCH detected - BUY Signal')
alertcondition(sell_signal, 'CHoCH Sell Signal', 'Bearish CHoCH detected - SELL Signal')