本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator('MVWAP Bird\'s Beak + Pullback (Purple X)', overlay = true)
// ==========================================
// 1. 參數設定
// ==========================================
len5 = input.int(5, title = 'MVWAP 5')
len8 = input.int(8, title = 'MVWAP 8 (離場線)')
len20 = input.int(20, title = 'MVWAP 20')
len100 = input.int(100, title = 'MVWAP 100 (方向線)')
v20_slope_bars = input.int(3, title = 'MVWAP 20 持續斜行確認K線數', minval = 1)
consol_pct = input.float(0.3, title = '盤整糾結距離閾值 (%)', step = 0.1)
reentry_cooldown = input.int(20, title = 'Pullback 買入冷卻期 (K線數)', minval = 1)
// ==========================================
// 2. 計算 MVWAP (成交量加權移動平均)
// ==========================================
v5 = ta.vwma(close, len5)
v8 = ta.vwma(close, len8)
v20 = ta.vwma(close, len20)
v100 = ta.vwma(close, len100)
plot(v5, title = 'MVWAP 5', color = color.red, linewidth = 1)
plot(v8, title = 'MVWAP 8', color = color.gray, linewidth = 1)
plot(v20, title = 'MVWAP 20', color = color.green, linewidth = 2)
plot(v100, title = 'MVWAP 100', color = color.orange, linewidth = 2)
// ==========================================
// 3. 斜率、交叉與回踩重站條件
// ==========================================
v100_up = v100 > v100[1]
v100_down = v100 < v100[1]
v5_up = v5 > v5[1]
v5_down = v5 < v5[1]
v20_up = ta.lowest(ta.change(v20), v20_slope_bars) > 0
v20_down = ta.highest(ta.change(v20), v20_slope_bars) < 0
// (A) 原始鳥嘴交叉突破
cross_up_20 = ta.crossover(v5, v20)
cross_down_20 = ta.crossunder(v5, v20)
orig_long = v100_up and v20_up and v5_up and cross_up_20
orig_short = v100_down and v20_down and v5_down and cross_down_20
// (B) Pullback 回踩重站條件
was_below_5_or_20 = close[1] < v5[1] or close[1] < v20[1]
now_above_5_and_20 = close > v5 and close > v20
reentry_long = v100_up and v20_up and v5 > v20 and was_below_5_or_20 and now_above_5_and_20
was_above_5_or_20 = close[1] > v5[1] or close[1] > v20[1]
now_below_5_and_20 = close < v5 and close < v20
reentry_short = v100_down and v20_down and v5 < v20 and was_above_5_or_20 and now_below_5_and_20
// (C) 離場條件 (MVWAP 8)
exit_long_cond = ta.crossunder(close, v8)
exit_short_cond = ta.crossover(close, v8)
// (D) 盤整狀態判定
dist_pct = math.abs(v5 - v20) / v20 * 100
is_consolidating = dist_pct < consol_pct
// ==========================================
// 4. 狀態機與 20 支 K 線冷卻控制
// ==========================================
var int pos = 0 // 0 = 空手, 1 = 持多(L), -1 = 持空(S)
var bool consol_locked = false
var int last_reentry_bar = -999
if not is_consolidating
consol_locked := false
consol_locked
bool show_L = false
bool show_S = false
bool show_X_long = false
bool show_X_short = false
// 檢查離場 (X)
if pos == 1 and exit_long_cond
show_X_long := true
pos := 0
if is_consolidating
consol_locked := true
consol_locked
else if pos == -1 and exit_short_cond
show_X_short := true
pos := 0
if is_consolidating
consol_locked := true
consol_locked
// 檢查進場 (L / S)
if pos == 0
if not consol_locked and orig_long
show_L := true
pos := 1
consol_locked := true
consol_locked
else if not consol_locked and orig_short
show_S := true
pos := -1
consol_locked := true
consol_locked
else if not is_consolidating and bar_index - last_reentry_bar > reentry_cooldown
if reentry_long
show_L := true
pos := 1
last_reentry_bar := bar_index
last_reentry_bar
else if reentry_short
show_S := true
pos := -1
last_reentry_bar := bar_index
last_reentry_bar
// ==========================================
// 5. 圖面標示 (L / S 保持原樣,X 改為紫色)
// ==========================================
plotshape(show_L, title = 'Long Entry (L)', style = shape.labelup, location = location.belowbar, color = color.green, text = 'L', textcolor = color.white, size = size.small)
plotshape(show_S, title = 'Short Entry (S)', style = shape.labeldown, location = location.abovebar, color = color.red, text = 'S', textcolor = color.white, size = size.small)
// 紫色 X 離場標示 (多單離場在 K 線上方,空單離場在 K 線下方)
plotshape(show_X_long, title = 'Exit Long (X)', style = shape.labeldown, location = location.abovebar, color = color.purple, text = 'X', textcolor = color.white, size = size.small)
plotshape(show_X_short, title = 'Exit Short (X)', style = shape.labelup, location = location.belowbar, color = color.purple, text = 'X', textcolor = color.white, size = size.small)