MA Shift

prbm30 · study · 61 行 · 点赞 7 · TradingView 原页

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

Pine Script

//@version=6
indicator("MA Shift", "MA Shift", overlay = true, behind_chart = false)
 
// ─────────────────────────────────────────────
// Inputs
// ─────────────────────────────────────────────
grpMain  = "Main MA"
mainType = input.string("SMA", "Type", options = ["SMA", "EMA", "RMA", "WMA", "VWMA"], group = grpMain)
mainLen  = input.int(200, "Length", minval = 1, inline = "main", group = grpMain)
mainSrc  = input.source(hl2, "", inline = "main", group = grpMain)
upColor  = input.color(color.white, "Up",   inline = "col", group = grpMain)
dnColor  = input.color(#262626,     "Down", inline = "col", group = grpMain)
 
grpFast  = "MA 50"
showFast = input.bool(true, "Show", group = grpFast)
fastType = input.string("SMA", "Type", options = ["SMA", "EMA", "RMA", "WMA", "VWMA"], group = grpFast)
fastLen  = input.int(50, "Length", minval = 1, inline = "fast", group = grpFast)
fastSrc  = input.source(close, "", inline = "fast", group = grpFast)
fastCol  = input.color(color.white, "Color", group = grpFast)
 
// ─────────────────────────────────────────────
// Functions
// ─────────────────────────────────────────────
// Returns the moving average selected by the user
getMA(series float src, simple int len, simple string kind) =>
    switch kind
        "EMA"  => ta.ema(src, len)
        "RMA"  => ta.rma(src, len)
        "WMA"  => ta.wma(src, len)
        "VWMA" => ta.vwma(src, len)
        => ta.sma(src, len)
 
// ─────────────────────────────────────────────
// Calculations
// ─────────────────────────────────────────────
mainMA = getMA(mainSrc, mainLen, mainType)
fastMA = getMA(fastSrc, fastLen, fastType)
 
// Bullish while price is above the main MA (or before the MA has enough data)
bullish = na(mainMA) or mainSrc >= mainMA
barCol  = bullish ? upColor : dnColor
 
// ─────────────────────────────────────────────
// Plots
// ─────────────────────────────────────────────
// Main MA with a soft glow behind it
plot(mainMA, "Main MA Glow", color = color.new(barCol, 80), linewidth = 7, display = display.pane)
plot(mainMA, "Main MA",      color = barCol, linewidth = 2)
 
// Fixed-color MA 50
plot(showFast ? fastMA : na, "MA 50", color = fastCol, linewidth = 2)
 
// Recolor the chart candles by trend
barcolor(barCol)
plotcandle(open, high, low, close, "Candles", color = barCol, wickcolor = barCol, bordercolor = barCol)
 
// ─────────────────────────────────────────────
// Alerts
// ─────────────────────────────────────────────
alertcondition(ta.crossover(mainSrc, mainMA),  "Price above MA", "Price crossed above the main MA")
alertcondition(ta.crossunder(mainSrc, mainMA), "Price below MA", "Price crossed below the main MA")

← 返回列表