Trend Vector Momentum

mbedaiwi · study · 114 行 · 点赞 39 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mbedaiwi2
 
//@version=6
indicator("Trend Vector Momentum", overlay=false, max_labels_count=200, max_lines_count=200)
 
// -----------------------------------------------------------------------------
// INPUTS — same core lengths as Trend Vector Pro so colors and momentum agree
// -----------------------------------------------------------------------------
grp_core = "Momentum"
int   indLen    = input.int(32, "Indicator Line Length", minval=2, group=grp_core, tooltip="Long smoothing of price change (True Strength Index first smoothing).")
int   smooth    = input.int(5,  "Smooth",                minval=1, group=grp_core, tooltip="Second smoothing of the True Strength Index.")
int   signalLen = input.int(5,  "Signal Line",           minval=1, group=grp_core, tooltip="EMA length of the Fast Momentum line used for the dotted Slow Momentum line.")
float histScale = input.float(2, "Histogram Scale",      minval=0.1, step=0.5, group=grp_core)
float refLevel  = input.float(25, "Reference Lines (+/-)", minval=1, group=grp_core, tooltip="Scale reference only. No signals are taken from these lines.")
 
grp_div = "Divergences"
bool showDiv   = input.bool(true, "Show Divergences (momentum pane)", group=grp_div)
bool divOnMain = input.bool(true, "Also Draw on Price Chart", group=grp_div, tooltip="Draws the divergence lines between the price highs/lows on the main chart as well. Turn off 'Show Momentum Divergences' in Trend Vector Pro to avoid duplicate lines.")
int  divLen  = input.int(5, "Pivot Lookback", minval=2, group=grp_div, tooltip="Divergences are confirmed this many bars after the pivot.")
int  divMax  = input.int(60, "Max Bars Between Pivots", minval=5, group=grp_div, tooltip="Pivots further apart than this are not compared.")
bool divConfirm = input.bool(true, "Confirm with Momentum Turn", group=grp_div, tooltip="A bearish divergence is only shown if the fast line is falling when it is confirmed (rising for a bullish divergence).")
 
grp_col = "Colors"
color cBullUp   = input.color(#26A69A, "Histogram: Bullish Rising",  group=grp_col)
color cBullDown = input.color(#26A69A, "Histogram: Bullish Falling", group=grp_col)
color cBearDown = input.color(#FF1744, "Histogram: Bearish Falling", group=grp_col)
color cBearUp   = input.color(#FF1744, "Histogram: Bearish Rising",  group=grp_col)
color cFast     = input.color(#D1D4DC, "Fast Momentum Line",  group=grp_col)
color cSlow     = input.color(#787B86, "Slow Momentum Line",  group=grp_col)
color cRef      = input.color(color.new(#2962FF, 60), "Reference Lines", group=grp_col)
 
// -----------------------------------------------------------------------------
// CALCULATION
// -----------------------------------------------------------------------------
// Fast Momentum line: True Strength Index (double-smoothed price change, scaled to +/-100)
float chg      = ta.change(close)
float numer    = ta.ema(ta.ema(chg, indLen), smooth)
float denom    = ta.ema(ta.ema(math.abs(chg), indLen), smooth)
float fastLine = denom != 0 ? 100 * numer / denom : na
// Slow Momentum line: EMA of the fast line
float slowLine = ta.ema(fastLine, signalLen)
// Histogram: distance between the lines (energy behind the move)
float hist     = (fastLine - slowLine) * histScale
 
// Histogram: green above zero, red below; faded when the energy is fading
color histCol = hist >= 0 ? (hist >= nz(hist[1]) ? cBullUp : color.new(cBullDown, 55)) : (hist <= nz(hist[1]) ? cBearDown : color.new(cBearUp, 55))
 
// -----------------------------------------------------------------------------
// PLOTS
// -----------------------------------------------------------------------------
hline(0,         "Zero", color=color.new(color.gray, 50), linestyle=hline.style_solid)
hline(refLevel,  "+Ref", color=cRef, linestyle=hline.style_solid)
hline(-refLevel, "-Ref", color=cRef, linestyle=hline.style_solid)
 
plot(hist,     "Histogram",          color=histCol, style=plot.style_columns)
plot(slowLine, "Slow Momentum Line", color=cSlow,   style=plot.style_circles, linewidth=1)
plot(fastLine, "Fast Momentum Line", color=cFast,   linewidth=2)
 
// -----------------------------------------------------------------------------
// DIVERGENCES — price makes a new extreme that momentum does not confirm
// -----------------------------------------------------------------------------
float phM = ta.pivothigh(fastLine, divLen, divLen)
float plM = ta.pivotlow(fastLine, divLen, divLen)
 
var float prevPhM  = na
var float prevPhPx = na
var int   prevPhB  = na
var float prevPlM  = na
var float prevPlPx = na
var int   prevPlB  = na
 
bool bearDiv = false
bool bullDiv = false
 
if not na(phM)
    int   b  = bar_index - divLen
    float px = high[divLen]
    bool okBear = not divConfirm or fastLine < fastLine[1]
    if not na(prevPhM) and b - prevPhB <= divMax and okBear and px > prevPhPx and phM < prevPhM and phM > 0
        bearDiv := true
        if showDiv
            line.new(prevPhB, prevPhM, b, phM, color=cBearDown, width=2)
            label.new(b, phM, "Bear Div", style=label.style_label_down, color=color.new(cBearDown, 20), textcolor=color.white, size=size.tiny)
        if divOnMain
            line.new(prevPhB, prevPhPx, b, px, color=cBearDown, width=2, force_overlay=true)
            label.new(b, px, "Bear Div", style=label.style_label_down, color=color.new(cBearDown, 20), textcolor=color.white, size=size.tiny, force_overlay=true)
    prevPhM  := phM
    prevPhPx := px
    prevPhB  := b
 
if not na(plM)
    int   b  = bar_index - divLen
    float px = low[divLen]
    bool okBull = not divConfirm or fastLine > fastLine[1]
    if not na(prevPlM) and b - prevPlB <= divMax and okBull and px < prevPlPx and plM > prevPlM and plM < 0
        bullDiv := true
        if showDiv
            line.new(prevPlB, prevPlM, b, plM, color=cBullUp, width=2)
            label.new(b, plM, "Bull Div", style=label.style_label_up, color=color.new(cBullUp, 20), textcolor=color.white, size=size.tiny)
        if divOnMain
            line.new(prevPlB, prevPlPx, b, px, color=cBullUp, width=2, force_overlay=true)
            label.new(b, px, "Bull Div", style=label.style_label_up, color=color.new(cBullUp, 20), textcolor=color.white, size=size.tiny, force_overlay=true)
    prevPlM  := plM
    prevPlPx := px
    prevPlB  := b
 
// -----------------------------------------------------------------------------
// ALERTS
// -----------------------------------------------------------------------------
alertcondition(bearDiv, "Bearish Divergence", "Trend Vector Momentum: bearish divergence on {{ticker}} {{interval}}")
alertcondition(bullDiv, "Bullish Divergence", "Trend Vector Momentum: bullish divergence on {{ticker}} {{interval}}")
alertcondition(ta.crossover(fastLine, 0),  "Momentum Above Zero", "Trend Vector Momentum: fast line crossed above zero on {{ticker}} {{interval}}")
alertcondition(ta.crossunder(fastLine, 0), "Momentum Below Zero", "Trend Vector Momentum: fast line crossed below zero on {{ticker}} {{interval}}")

← 返回列表