本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator(title="VortexT", shorttitle="Vt", format=format.price, precision=4, overlay=false, max_lines_count=500, max_labels_count=500)
// === Parameters ===
period_ = input.int(14, title="Length", minval=2, tooltip="Period for Vortex calculation. 14 is standard")
showHistogram = input.bool(true, title="Show Difference Histogram", tooltip="Display VI+ minus VI- as colored columns")
showBackground = input.bool(true, title="Show Trend Background", tooltip="Color background based on dominant Vortex line")
dynamicView = input.bool(true, title="Dynamic Trend View", tooltip="Show only the dominant Vortex line (VI+ when above VI-, VI- when above VI+)")
showDiv = input.bool(true, title="Show Divergences", tooltip="Show or hide divergence lines and labels")
lb = input.int(7, title="Pivot Lookback", minval=2, tooltip="Bars lookback for pivot detection. Higher values = fewer but stronger signals")
maxDist = input.int(60, title="Max Bars Between Pivots", minval=5, tooltip="Divergences are drawn only if the two pivots are at most this many bars apart")
// === Vortex Calculation ===
VMP = math.sum(math.abs(high - low[1]), period_)
VMM = math.sum(math.abs(low - high[1]), period_)
STR = math.sum(ta.tr, period_)
VIP = STR > 0 ? VMP / STR : na
VIM = STR > 0 ? VMM / STR : na
delta = VIP - VIM
// === Logic for Dynamic Display ===
plotVIP = dynamicView ? (VIP >= VIM ? VIP : na) : VIP
plotVIM = dynamicView ? (VIM > VIP ? VIM : na) : VIM
// === Plotting Vortex Lines ===
plot(plotVIP, title="VI + (Bullish Momentum)", color=color.new(color.green, 0), linewidth=2, style=plot.style_linebr)
plot(plotVIM, title="VI - (Bearish Momentum)", color=color.new(color.red, 0), linewidth=2, style=plot.style_linebr)
// === Bearish Divergence ===
// Price makes higher high, but VI+ makes lower high
phVIP = ta.pivothigh(VIP, lb, lb)
var float lastPricePH = na
var float lastVIPPH = na
var int lastBarPH = na
if not na(phVIP)
pivotBar = bar_index - lb
if showDiv and not na(lastVIPPH) and high[lb] > lastPricePH and phVIP < lastVIPPH and pivotBar - lastBarPH <= maxDist
line.new(lastBarPH, lastVIPPH, pivotBar, phVIP, xloc=xloc.bar_index, color=color.new(color.red, 30), width=2, style=line.style_dotted)
label.new(pivotBar, phVIP, "Bear Div", color=color.new(color.red, 20), style=label.style_label_down, textcolor=color.white, size=size.small)
lastPricePH := high[lb]
lastVIPPH := phVIP
lastBarPH := pivotBar
// === Bullish Divergence ===
// Price makes lower low, but VI- makes lower high
phVIM = ta.pivothigh(VIM, lb, lb)
var float lastPricePL = na
var float lastVIMPH = na
var int lastBarPL = na
if not na(phVIM)
pivotBar = bar_index - lb
if showDiv and not na(lastVIMPH) and low[lb] < lastPricePL and phVIM < lastVIMPH and pivotBar - lastBarPL <= maxDist
line.new(lastBarPL, lastVIMPH, pivotBar, phVIM, xloc=xloc.bar_index, color=color.new(color.green, 30), width=2, style=line.style_dotted)
label.new(pivotBar, phVIM, "Bull Div", color=color.new(color.green, 20), style=label.style_label_up, textcolor=color.white, size=size.small)
lastPricePL := low[lb]
lastVIMPH := phVIM
lastBarPL := pivotBar
// === Zero Line ===
hline(0.0, title="Zero Line", color=color.new(color.gray, 70), linestyle=hline.style_dashed)
// === Trend Background ===
bgcolor(showBackground ? (VIP > VIM ? color.new(color.green, 92) : color.new(color.red, 92)) : na)
// === Difference Histogram ===
plot(showHistogram ? delta : na, title="VI+ minus VI- Difference", style=plot.style_columns,
color=delta >= 0 ? color.new(color.lime, 60) : color.new(color.maroon, 60))
// === Data Window ===
plot(VIP, title="VI+ (full)", display=display.data_window)
plot(VIM, title="VI- (full)", display=display.data_window)