本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Savan- Multi Moving Average 10/20/50/100/200", overlay=true)
//====================================================
// MOVING AVERAGE FUNCTION
//====================================================
maFunction(src, length, maType) =>
switch maType
"EMA" => ta.ema(src, length)
"SMA" => ta.sma(src, length)
"WMA" => ta.wma(src, length)
"VWMA" => ta.vwma(src, length)
"HMA" => ta.hma(src, length)
//====================================================
// MA 10 SETTINGS
//====================================================
showMA10 = input.bool(true, "Show MA 10")
len10 = input.int(10, "MA 10 Length", minval=1)
type10 = input.string("EMA", "MA 10 Type", options=["EMA","SMA","WMA","VWMA","HMA"])
color10 = input.color(color.red, "MA 10 Color")
width10 = input.int(2, "MA 10 Width", minval=1, maxval=5)
//====================================================
// MA 20 SETTINGS
//====================================================
showMA20 = input.bool(true, "Show MA 20")
len20 = input.int(20, "MA 20 Length", minval=1)
type20 = input.string("EMA", "MA 20 Type", options=["EMA","SMA","WMA","VWMA","HMA"])
color20 = input.color(color.orange, "MA 20 Color")
width20 = input.int(2, "MA 20 Width", minval=1, maxval=5)
//====================================================
// MA 50 SETTINGS
//====================================================
showMA50 = input.bool(true, "Show MA 50")
len50 = input.int(50, "MA 50 Length", minval=1)
type50 = input.string("EMA", "MA 50 Type", options=["EMA","SMA","WMA","VWMA","HMA"])
color50 = input.color(color.yellow, "MA 50 Color")
width50 = input.int(2, "MA 50 Width", minval=1, maxval=5)
//====================================================
// MA 100 SETTINGS
//====================================================
showMA100 = input.bool(true, "Show MA 100")
len100 = input.int(100, "MA 100 Length", minval=1)
type100 = input.string("EMA", "MA 100 Type", options=["EMA","SMA","WMA","VWMA","HMA"])
color100 = input.color(color.green, "MA 100 Color")
width100 = input.int(2, "MA 100 Width", minval=1, maxval=5)
//====================================================
// MA 200 SETTINGS
//====================================================
showMA200 = input.bool(true, "Show MA 200")
len200 = input.int(200, "MA 200 Length", minval=1)
type200 = input.string("EMA", "MA 200 Type", options=["EMA","SMA","WMA","VWMA","HMA"])
color200 = input.color(color.blue, "MA 200 Color")
width200 = input.int(2, "MA 200 Width", minval=1, maxval=5)
//====================================================
// CALCULATE MOVING AVERAGES
//====================================================
ma10 = maFunction(close, len10, type10)
ma20 = maFunction(close, len20, type20)
ma50 = maFunction(close, len50, type50)
ma100 = maFunction(close, len100, type100)
ma200 = maFunction(close, len200, type200)
//====================================================
// PLOT MOVING AVERAGES
//====================================================
plot(showMA10 ? ma10 : na, "MA 10", color=color10, linewidth=width10)
plot(showMA20 ? ma20 : na, "MA 20", color=color20, linewidth=width20)
plot(showMA50 ? ma50 : na, "MA 50", color=color50, linewidth=width50)
plot(showMA100 ? ma100 : na, "MA 100", color=color100, linewidth=width100)
plot(showMA200 ? ma200 : na, "MA 200", color=color200, linewidth=width200)
//====================================================
// BUY / SELL SETTINGS
//====================================================
showSignals = input.bool(true, "Show BUY / SELL Signals")
useTrendFilter = input.bool(false, "Enable Trend Filter")
trendMA = input.string("MA 50", "Trend Filter MA",
options=["MA 50","MA 100","MA 200"])
confirmedOnly = input.bool(true, "Confirmed Candle Only")
trendLine = switch trendMA
"MA 50" => ma50
"MA 100" => ma100
"MA 200" => ma200
//====================================================
// BUY / SELL LOGIC
//====================================================
buyCross = ta.crossover(ma10, ma20)
sellCross = ta.crossunder(ma10, ma20)
buyTrend = not useTrendFilter or close > trendLine
sellTrend = not useTrendFilter or close < trendLine
candleConfirmed = not confirmedOnly or barstate.isconfirmed
buySignal = buyCross and buyTrend and candleConfirmed
sellSignal = sellCross and sellTrend and candleConfirmed
//====================================================
// BUY / SELL LABELS
//====================================================
plotshape(showSignals and buySignal,
title="BUY Signal",
style=shape.labelup,
location=location.belowbar,
color=color.green,
text="BUY",
textcolor=color.white,
size=size.small)
plotshape(showSignals and sellSignal,
title="SELL Signal",
style=shape.labeldown,
location=location.abovebar,
color=color.red,
text="SELL",
textcolor=color.white,
size=size.small)
//====================================================
// ALERT CONDITIONS
//====================================================
alertcondition(buySignal, title="BUY Alert",
message="BUY Signal: MA 10 crossed above MA 20")
alertcondition(sellSignal, title="SELL Alert",
message="SELL Signal: MA 10 crossed below MA 20")