Multi-Horizon Momentum Curve [BackQuant]

BackQuant · study · 165 行 · 点赞 399 · 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/
// © BackQuant
 
//@version=6
indicator("Multi-Horizon Momentum Curve [BackQuant]", shorttitle="Momentum Curve [BackQuant]", overlay=false)
 
// Constants
const string calc = "Momentum Calculation"
const string trend = "Trend Settings"
const string ui = "Plotting and Coloring"
const color GREEN = #00ff00
const color RED = #ff0000
 
// Inputs
float src = input.source(close, "Source", group=calc)
 
int len1 = input.int(5, "Horizon 1", minval=2, group=calc, inline="H1")
float w1 = input.float(1.0, "Weight", minval=0.0, step=0.1, group=calc, inline="H1")
 
int len2 = input.int(10, "Horizon 2", minval=2, group=calc, inline="H2")
float w2 = input.float(1.5, "Weight", minval=0.0, step=0.1, group=calc, inline="H2")
 
int len3 = input.int(20, "Horizon 3", minval=2, group=calc, inline="H3")
float w3 = input.float(2.25, "Weight", minval=0.0, step=0.1, group=calc, inline="H3")
 
int len4 = input.int(40, "Horizon 4", minval=2, group=calc, inline="H4")
float w4 = input.float(3.25, "Weight", minval=0.0, step=0.1, group=calc, inline="H4")
 
int volLen = input.int(30, "Volatility Length", minval=5, group=calc)
float maxMomentum = input.float(4.0, "Momentum Clamp", minval=1.0, maxval=10.0, step=0.25, group=calc)
int smoothLen = input.int(4, "Curve Smoothing", minval=1, group=calc)
 
float flipThreshold = input.float(0.20, "Trend Threshold", minval=0.0, maxval=3.0, step=0.05, group=trend)
 
bool showOsc = input.bool(true, "Show Oscillator", group=ui)
bool showHorizons = input.bool(false, "Show Individual Horizons", group=ui)
bool showStatic = input.bool(true, "Show Static Levels", group=ui)
bool paintCandles = input.bool(true, "Color Candles", group=ui)
 
// One-Bar Log Return
float logReturn = src > 0.0 and src[1] > 0.0 ? math.log(src / src[1]) : 0.0
 
// Base Realized Volatility
float sigma = ta.stdev(logReturn, volLen)
 
// Normalized Horizon Momentum
normalizedMomentum(float source, int length, float volatility, float clampValue) =>
    float horizonReturn = source > 0.0 and source[length] > 0.0 ? math.log(source / source[length]) : 0.0
    float expectedVol = volatility * math.sqrt(length)
    float momentum = expectedVol > 0.0 ? horizonReturn / expectedVol : 0.0
    math.max(-clampValue, math.min(clampValue, momentum))
 
float mom1 = normalizedMomentum(src, len1, sigma, maxMomentum)
float mom2 = normalizedMomentum(src, len2, sigma, maxMomentum)
float mom3 = normalizedMomentum(src, len3, sigma, maxMomentum)
float mom4 = normalizedMomentum(src, len4, sigma, maxMomentum)
 
// Weighted Multi-Horizon Curve
float totalWeight = w1 + w2 + w3 + w4
float rawCurve = totalWeight > 0.0 ? (mom1 * w1 + mom2 * w2 + mom3 * w3 + mom4 * w4) / totalWeight : 0.0
float curve = smoothLen > 1 ? ta.ema(rawCurve, smoothLen) : rawCurve
 
// Horizon Agreement
int bullCount = (mom1 > 0.0 ? 1 : 0) + (mom2 > 0.0 ? 1 : 0) + (mom3 > 0.0 ? 1 : 0) + (mom4 > 0.0 ? 1 : 0)
int bearCount = (mom1 < 0.0 ? 1 : 0) + (mom2 < 0.0 ? 1 : 0) + (mom3 < 0.0 ? 1 : 0) + (mom4 < 0.0 ? 1 : 0)
 
float agreement = math.max(bullCount, bearCount) / 4.0
 
// Persistent Trend State
var int trendState = 0
 
if trendState == 0
    trendState := curve >= 0.0 ? 1 : -1
else if trendState != 1 and curve > flipThreshold
    trendState := 1
else if trendState != -1 and curve < -flipThreshold
    trendState := -1
 
bool bullFlip = trendState == 1 and trendState[1] == -1
bool bearFlip = trendState == -1 and trendState[1] == 1
 
color trendCol = trendState == 1 ? GREEN : RED
 
// Oscillator Color Gradient
color plotCol = #ffffff
 
if curve >= 2.5 or curve <= -2.5
    plotCol := #ffffff
else if curve > 0.0
    if curve < 0.50
        plotCol := #1dcaff4d
    else if curve < 1.00
        plotCol := #1e9b254d
    else if curve < 1.50
        plotCol := #00ff004d
    else if curve < 2.00
        plotCol := #00ff0080
    else
        plotCol := #00ff00
else if curve < 0.0
    if curve > -0.50
        plotCol := #e651004d
    else if curve > -1.00
        plotCol := #7715154d
    else if curve > -1.50
        plotCol := #ff00004d
    else if curve > -2.00
        plotCol := #ff000080
    else
        plotCol := #ff0000
else
    plotCol := #ffffff4d
 
// Static Colors
color bullZone = #00ff003d
color bearZone = #ff00003d
color bullEdge = #00ff00fc
color bearEdge = #ff0000fc
color extremeCol = #ffffff
color midCol = #ffffff4d
 
// Oscillator
plot(showOsc ? curve : na, "Multi-Horizon Momentum", plotCol, 3, plot.style_columns)
 
// Individual Horizons
plot(showHorizons ? mom1 : na, "Horizon 1", color.new(chart.fg_color, 85), 1)
plot(showHorizons ? mom2 : na, "Horizon 2", color.new(chart.fg_color, 78), 1)
plot(showHorizons ? mom3 : na, "Horizon 3", color.new(chart.fg_color, 70), 1)
plot(showHorizons ? mom4 : na, "Horizon 4", color.new(chart.fg_color, 60), 1)
 
// Static Zones
pBullNormal = plot(showStatic ? 2.0 : na, "+2", bullEdge)
pBullExtreme = plot(showStatic ? 2.5 : na, "+2.5", bullEdge)
 
pBearNormal = plot(showStatic ? -2.0 : na, "-2", bearEdge)
pBearExtreme = plot(showStatic ? -2.5 : na, "-2.5", bearEdge)
 
fill(pBullNormal, pBullExtreme, bullZone, title="Bull Extreme Zone")
fill(pBearNormal, pBearExtreme, bearZone, title="Bear Extreme Zone")
 
hline(0.0, "Zero", midCol, hline.style_solid)
 
// Candles
plotcandle(
     open, high, low, close,
     "Trend Candles",
     trendCol,
     trendCol,
     true,
     bordercolor=trendCol,
     display=paintCandles ? display.all : display.none,
     force_overlay=true)
 
// Data Window
plot(rawCurve, "Raw Momentum Composite", display=display.data_window, editable=false)
plot(curve, "Smoothed Momentum Composite", display=display.data_window, editable=false)
plot(mom1, "Horizon 1 Momentum", display=display.data_window, editable=false)
plot(mom2, "Horizon 2 Momentum", display=display.data_window, editable=false)
plot(mom3, "Horizon 3 Momentum", display=display.data_window, editable=false)
plot(mom4, "Horizon 4 Momentum", display=display.data_window, editable=false)
plot(agreement * 100.0, "Horizon Agreement", display=display.data_window, editable=false)
 
// Alerts
alertcondition(bullFlip, "Momentum Curve Bullish", "Multi-Horizon Momentum Curve turned bullish on {{ticker}}")
alertcondition(bearFlip, "Momentum Curve Bearish", "Multi-Horizon Momentum Curve turned bearish on {{ticker}}")

← 返回列表