Multiple SMA, EMA, VWAP, and Crossovers

thisgirl · study · 122 行 · 点赞 224 · 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/
// © thisgirl
 
//@version=5
indicator("Multiple SMA, EMA, and VWAP Crossovers", overlay=true)
 
// Input for SMAs and EMA
sma5 = ta.sma(close, 5)
sma10 = ta.sma(close, 10)
sma20 = ta.sma(close, 20)
sma50 = ta.sma(close, 50)
sma100 = ta.sma(close, 100)
sma200 = ta.sma(close, 200)
sma325 = ta.sma(close, 325)
ema9 = ta.ema(close, 9)
 
// VWAP
vwap = ta.vwap(close)
 
// Input options for crossovers
showCrossover_20_50 = input.bool(false, title="20 SMA / 50 SMA Crossover")
showCrossover_20_100 = input.bool(false, title="20 SMA / 100 SMA Crossover")
showCrossover_20_200 = input.bool(false, title="20 SMA / 200 SMA Crossover")
showCrossover_20_325 = input.bool(false, title="20 SMA / 325 SMA Crossover")
showCrossover_50_100 = input.bool(false, title="50 SMA / 100 SMA Crossover")
showCrossover_50_200 = input.bool(false, title="50 SMA / 200 SMA Crossover")
showCrossover_50_325 = input.bool(false, title="50 SMA / 325 SMA Crossover")
showCrossover_100_200 = input.bool(false, title="100 SMA / 200 SMA Crossover")
showCrossover_100_325 = input.bool(false, title="100 SMA / 325 SMA Crossover")
showCrossover_200_325 = input.bool(false, title="200 SMA / 325 SMA Crossover")
showCrossover_VWAP_20 = input.bool(false, title="VWAP / 20 SMA Crossover")
showCrossover_VWAP_50 = input.bool(false, title="VWAP / 50 SMA Crossover")
showCrossover_VWAP_100 = input.bool(false, title="VWAP / 100 SMA Crossover")
showCrossover_VWAP_200 = input.bool(false, title="VWAP / 200 SMA Crossover")
showCrossover_VWAP_325 = input.bool(false, title="VWAP / 325 SMA Crossover")
 
// Input options for additional indicators
showVWAP = input.bool(false, title="Show VWAP")
showEMA9 = input.bool(false, title="Show 9 EMA")
showSMA5 = input.bool(false, title="Show 5 SMA")
showSMA10 = input.bool(false, title="Show 10 SMA")
showSMA20 = input.bool(false, title="Show 20 SMA")
showSMA50 = input.bool(false, title="Show 50 SMA")
showSMA100 = input.bool(false, title="Show 100 SMA")
showSMA200 = input.bool(false, title="Show 200 SMA")
showSMA325 = input.bool(false, title="Show 325 SMA")
 
// Plotting SMAs, EMA, and VWAP based on crossover selections
plot(showCrossover_20_50 or showCrossover_20_100 or showCrossover_20_200 or showCrossover_20_325 or showCrossover_VWAP_20 ? sma20 : na, title="20 SMA", color=color.green)
plot(showCrossover_50_100 or showCrossover_50_200 or showCrossover_50_325 or showCrossover_VWAP_50 or showCrossover_20_50 ? sma50 : na, title="50 SMA", color=color.orange)
plot(showCrossover_100_200 or showCrossover_100_325 or showCrossover_VWAP_100 or showCrossover_50_100 or showCrossover_20_100 ? sma100 : na, title="100 SMA", color=color.purple)
plot(showCrossover_200_325 or showCrossover_VWAP_200 or showCrossover_100_200 or showCrossover_50_200 or showCrossover_20_200 ? sma200 : na, title="200 SMA", color=color.yellow)
plot(showCrossover_20_325 or showCrossover_50_325 or showCrossover_100_325 or showCrossover_200_325 or showCrossover_VWAP_325 ? sma325 : na, title="325 SMA", color=color.fuchsia)
plot(showCrossover_VWAP_20 or showCrossover_VWAP_50 or showCrossover_VWAP_100 or showCrossover_VWAP_200 or showCrossover_VWAP_325 ? vwap : na, title="VWAP", color=color.white)
 
// Plotting additional indicators based on user input
plot(showVWAP ? vwap : na, title="VWAP", color=color.white)
plot(showEMA9 ? ema9 : na, title="9 EMA", color=color.blue)
plot(showSMA5 ? sma5 : na, title="5 SMA", color=color.red)
plot(showSMA10 ? sma10 : na, title="10 SMA", color=color.orange)
plot(showSMA20 ? sma20 : na, title="20 SMA", color=color.green)
plot(showSMA50 ? sma50 : na, title="50 SMA", color=color.purple)
plot(showSMA100 ? sma100 : na, title="100 SMA", color=color.yellow)
plot(showSMA200 ? sma200 : na, title="200 SMA", color=color.fuchsia)
plot(showSMA325 ? sma325 : na, title="325 SMA", color=color.aqua)
 
// Creating alerts for each crossover
alertcondition(ta.crossover(sma20, sma50), title="20 SMA / 50 SMA Long", message="20 SMA / 50 SMA Long")
alertcondition(ta.crossunder(sma20, sma50), title="20 SMA / 50 SMA Short", message="20 SMA / 50 SMA Short")
alertcondition(ta.crossover(sma20, sma100), title="20 SMA / 100 SMA Long", message="20 SMA / 100 SMA Long")
alertcondition(ta.crossunder(sma20, sma100), title="20 SMA / 100 SMA Short", message="20 SMA / 100 SMA Short")
alertcondition(ta.crossover(sma20, sma200), title="20 SMA / 200 SMA Long", message="20 SMA / 200 SMA Long")
alertcondition(ta.crossunder(sma20, sma200), title="20 SMA / 200 SMA Short", message="20 SMA / 200 SMA Short")
alertcondition(ta.crossover(sma20, sma325), title="20 SMA / 325 SMA Long", message="20 SMA / 325 SMA Long")
alertcondition(ta.crossunder(sma20, sma325), title="20 SMA / 325 SMA Short", message="20 SMA / 325 SMA Short")
alertcondition(ta.crossover(sma50, sma100), title="50 SMA / 100 SMA Long", message="50 SMA / 100 SMA Long")
alertcondition(ta.crossunder(sma50, sma100), title="50 SMA / 100 SMA Short", message="50 SMA / 100 SMA Short")
alertcondition(ta.crossover(sma50, sma200), title="50 SMA / 200 SMA Long", message="50 SMA / 200 SMA Long")
alertcondition(ta.crossunder(sma50, sma200), title="50 SMA / 200 SMA Short", message="50 SMA / 200 SMA Short")
alertcondition(ta.crossover(sma50, sma325), title="50 SMA / 325 SMA Long", message="50 SMA / 325 SMA Long")
alertcondition(ta.crossunder(sma50, sma325), title="50 SMA / 325 SMA Short", message="50 SMA / 325 SMA Short")
alertcondition(ta.crossover(sma100, sma200), title="100 SMA / 200 SMA Long", message="100 SMA / 200 SMA Long")
alertcondition(ta.crossunder(sma100, sma200), title="100 SMA / 200 SMA Short", message="100 SMA / 200 SMA Short")
alertcondition(ta.crossover(sma100, sma325), title="100 SMA / 325 SMA Long", message="100 SMA / 325 SMA Long")
alertcondition(ta.crossunder(sma100, sma325), title="100 SMA / 325 SMA Short", message="100 SMA / 325 SMA Short")
alertcondition(ta.crossover(vwap, sma20), title="VWAP / 20 SMA Long", message="VWAP / 20 SMA Long")
alertcondition(ta.crossunder(vwap, sma20), title="VWAP / 20 SMA Short", message="VWAP / 20 SMA Short")
alertcondition(ta.crossover(vwap, sma50), title="VWAP / 50 SMA Long", message="VWAP / 50 SMA Long")
alertcondition(ta.crossunder(vwap, sma50), title="VWAP / 50 SMA Short", message="VWAP / 50 SMA Short")
alertcondition(ta.crossover(vwap, sma100), title="VWAP / 100 SMA Long", message="VWAP / 100 SMA Long")
alertcondition(ta.crossunder(vwap, sma100), title="VWAP / 100 SMA Short", message="VWAP / 100 SMA Short")
alertcondition(ta.crossover(vwap, sma200), title="VWAP / 200 SMA Long", message="VWAP / 200 SMA Long")
alertcondition(ta.crossunder(vwap, sma200), title="VWAP / 200 SMA Short", message="VWAP / 200 SMA Short")
alertcondition(ta.crossover(vwap, sma325), title="VWAP / 325 SMA Long", message="VWAP / 325 SMA Long")
alertcondition(ta.crossunder(vwap, sma325), title="VWAP / 325 SMA Short", message="VWAP / 325 SMA Short")
 
// Function to create labels for crossovers
crossLabel(src1, src2, title, show) =>
    if (show)
        longCondition = ta.crossover(src1, src2)
        shortCondition = ta.crossunder(src1, src2)
        if (longCondition)
            label.new(bar_index, y=src1, text="Long", style=label.style_label_up, color=color.green, textcolor=color.white, size=size.small, yloc=yloc.price, xloc=xloc.bar_index)
        if (shortCondition)
            label.new(bar_index, y=src1, text="Short", style=label.style_label_down, color=color.red, textcolor=color.white, size=size.small, yloc=yloc.price, xloc=xloc.bar_index)
 
// Creating labels for each crossover
crossLabel(sma20, sma50, "20 SMA / 50 SMA", showCrossover_20_50)
crossLabel(sma20, sma100, "20 SMA / 100 SMA", showCrossover_20_100)
crossLabel(sma20, sma200, "20 SMA / 200 SMA", showCrossover_20_200)
crossLabel(sma20, sma325, "20 SMA / 325 SMA", showCrossover_20_325)
crossLabel(sma50, sma100, "50 SMA / 100 SMA", showCrossover_50_100)
crossLabel(sma50, sma200, "50 SMA / 200 SMA", showCrossover_50_200)
crossLabel(sma50, sma325, "50 SMA / 325 SMA", showCrossover_50_325)
crossLabel(sma100, sma200, "100 SMA / 200 SMA", showCrossover_100_200)
crossLabel(sma100, sma325, "100 SMA / 325 SMA", showCrossover_100_325)
crossLabel(sma200, sma325, "200 SMA / 325 SMA", showCrossover_200_325)
crossLabel(vwap, sma20, "VWAP / 20 SMA", showCrossover_VWAP_20)
crossLabel(vwap, sma50, "VWAP / 50 SMA", showCrossover_VWAP_50)
crossLabel(vwap, sma100, "VWAP / 100 SMA", showCrossover_VWAP_100)
crossLabel(vwap, sma200, "VWAP / 200 SMA", showCrossover_VWAP_200)
crossLabel(vwap, sma325, "VWAP / 325 SMA", showCrossover_VWAP_325)

← 返回列表