Multiple Exponential Moving Averages

DFYT · study · 67 行 · 点赞 2 · TradingView 原页

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

Pine Script

//@version=6
indicator(title="Multiple Exponential Moving Averages", shorttitle="Multi EMA", overlay=true, timeframe="", timeframe_gaps=true)
 
// Input for source
src = input.source(close, title="Source")
offset = input.int(title="Offset", defval=0, minval=-500, maxval=500, display=display.data_window)
 
// EMA lengths
len10 = input.int(10, minval=1, title="EMA 10 Length")
len20 = input.int(20, minval=1, title="EMA 20 Length")
len50 = input.int(50, minval=1, title="EMA 50 Length")
len150 = input.int(150, minval=1, title="EMA 150 Length")
len200 = input.int(200, minval=1, title="EMA 200 Length")
 
// Calculate EMAs
out10 = ta.ema(src, len10)
out20 = ta.ema(src, len20)
out50 = ta.ema(src, len50)
out150 = ta.ema(src, len150)
out200 = ta.ema(src, len200)
 
// Plot EMAs with different colors
plot(out10, title="EMA 10", color=color.red, offset=offset)
plot(out20, title="EMA 20", color=color.orange, offset=offset)
plot(out50, title="EMA 50", color=color.blue, offset=offset)
plot(out150, title="EMA 150", color=color.green, offset=offset)
plot(out200, title="EMA 200", color=color.purple, offset=offset)
 
// Smoothing MA inputs
GRP = "Smoothing"
TT_BB = "Only applies when 'SMA + Bollinger Bands' is selected. Determines the distance between the SMA and the bands."
maTypeInput = input.string("None", "Type", options=["None", "SMA", "SMA + Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group=GRP, display=display.data_window)
var isBB = maTypeInput == "SMA + Bollinger Bands"
maLengthInput = input.int(14, "Length", group=GRP, display=display.data_window, active=maTypeInput != "None")
bbMultInput = input.float(2.0, "BB StdDev", minval=0.001, maxval=50, step=0.5, tooltip=TT_BB, group=GRP, display=display.data_window, active=isBB)
var enableMA = maTypeInput != "None"
 
// Select which EMA to apply smoothing to
smoothOn = input.string("EMA 50", "Apply Smoothing To", options=["EMA 10", "EMA 20", "EMA 50", "EMA 150", "EMA 200"], group=GRP, active=enableMA)
 
// Get the selected out for smoothing
selectedOut = switch smoothOn
    "EMA 10" => out10
    "EMA 20" => out20
    "EMA 50" => out50
    "EMA 150" => out150
    "EMA 200" => out200
    => na  // Fallback
 
// Smoothing MA Calculation
ma(source, length, MAtype) =>
    switch MAtype
        "SMA"                   => ta.sma(source, length)
        "SMA + Bollinger Bands" => ta.sma(source, length)
        "EMA"                   => ta.ema(source, length)
        "SMMA (RMA)"            => ta.rma(source, length)
        "WMA"                   => ta.wma(source, length)
        "VWMA"                  => ta.vwma(source, length)
        => na
 
// Smoothing MA plots
smoothingMA = enableMA ? ma(selectedOut, maLengthInput, maTypeInput) : na
smoothingStDev = isBB ? ta.stdev(selectedOut, maLengthInput) * bbMultInput : na
plot(smoothingMA, "Smoothing MA", color=color.yellow, display=enableMA ? display.all : display.none, editable=enableMA)
bbUpperBand = plot(smoothingMA + smoothingStDev, title="Upper Bollinger Band", color=color.green, display=isBB ? display.all : display.none, editable=isBB)
bbLowerBand = plot(smoothingMA - smoothingStDev, title="Lower Bollinger Band", color=color.green, display=isBB ? display.all : display.none, editable=isBB)
fill(bbUpperBand, bbLowerBand, color=isBB ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill", display=isBB ? display.all : display.none, editable=isBB)

← 返回列表