本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator(title="VWAP EMA Signals [UgurTash]", shorttitle="VWAP EMA Signals [UgurTash]", overlay=true, timeframe="", timeframe_gaps=true)
hideonDWM = input(false, title="Hide VWAP on 1D or Above", group="VWAP Settings", display = display.none)
var anchor = input.string(defval = "Session", title="Anchor Period",
options=["Session", "Week", "Month", "Quarter", "Year", "Decade", "Century", "Earnings", "Dividends", "Splits"], group="VWAP Settings")
src = input(title = "Source", defval = ohlc4, group="VWAP Settings", display = display.none)
offset = input.int(0, title="Offset", group="VWAP Settings", display = display.none)
BANDS_GROUP = "Bands Settings"
CALC_MODE_TOOLTIP = "Determines the units used to calculate the distance of the bands. When 'Percentage' is selected, a multiplier of 1 means 1%."
calcModeInput = input.string("Standard Deviation", "Bands Calculation Mode", options = ["Standard Deviation", "Percentage"], group = BANDS_GROUP, tooltip = CALC_MODE_TOOLTIP, display = display.none)
showBand_1 = input(true, title = "", group = BANDS_GROUP, inline = "band_1", display = display.none)
bandMult_1 = input.float(1.0, title = "Bands Multiplier #1", group = BANDS_GROUP, inline = "band_1", step = 0.5, minval=0, display = display.none, active = showBand_1)
showBand_2 = input(false, title = "", group = BANDS_GROUP, inline = "band_2", display = display.none)
bandMult_2 = input.float(2.0, title = "Bands Multiplier #2", group = BANDS_GROUP, inline = "band_2", step = 0.5, minval=0, display = display.none, active = showBand_2)
showBand_3 = input(false, title = "", group = BANDS_GROUP, inline = "band_3", display = display.none)
bandMult_3 = input.float(3.0, title = "Bands Multiplier #3", group = BANDS_GROUP, inline = "band_3", step = 0.5, minval=0, display = display.none, active = showBand_3)
cumVolume = ta.cum(volume)
if barstate.islast and cumVolume == 0
runtime.error("No volume is provided by the data vendor.")
isNewPeriod = switch anchor
"Earnings" =>
new_earnings_actual = request.earnings(syminfo.tickerid, earnings.actual, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true)
new_earnings_standardized = request.earnings(syminfo.tickerid, earnings.standardized, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true)
not na(new_earnings_actual) or not na(new_earnings_standardized)
"Dividends" =>
new_dividends = request.dividends(syminfo.tickerid, dividends.gross, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true)
not na(new_dividends)
"Splits" =>
new_split = request.splits(syminfo.tickerid, splits.denominator, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true)
not na(new_split)
"Session" => timeframe.change("D")
"Week" => timeframe.change("W")
"Month" => timeframe.change("M")
"Quarter" => timeframe.change("3M")
"Year" => timeframe.change("12M")
"Decade" => timeframe.change("12M") and year % 10 == 0
"Century" => timeframe.change("12M") and year % 100 == 0
=> false
isEsdAnchor = anchor == "Earnings" or anchor == "Dividends" or anchor == "Splits"
if na(src[1]) and not isEsdAnchor
isNewPeriod := true
float vwapValue = na
float upperBandValue1 = na
float lowerBandValue1 = na
float upperBandValue2 = na
float lowerBandValue2 = na
float upperBandValue3 = na
float lowerBandValue3 = na
if not (hideonDWM and timeframe.isdwm)
[_vwap, _stdevUpper, _] = ta.vwap(src, isNewPeriod, 1)
vwapValue := _vwap
stdevAbs = _stdevUpper - _vwap
bandBasis = calcModeInput == "Standard Deviation" ? stdevAbs : _vwap * 0.01
upperBandValue1 := _vwap + bandBasis * bandMult_1
lowerBandValue1 := _vwap - bandBasis * bandMult_1
upperBandValue2 := _vwap + bandBasis * bandMult_2
lowerBandValue2 := _vwap - bandBasis * bandMult_2
upperBandValue3 := _vwap + bandBasis * bandMult_3
lowerBandValue3 := _vwap - bandBasis * bandMult_3
plot(vwapValue, title = "VWAP", color = #2962FF, offset = offset)
displayBand1 = showBand_1 ? display.all : display.none
upperBand_1 = plot(upperBandValue1, title="Upper Band #1", color = color.green, offset = offset, display = displayBand1, editable = showBand_1)
lowerBand_1 = plot(lowerBandValue1, title="Lower Band #1", color = color.green, offset = offset, display = displayBand1, editable = showBand_1)
fill(upperBand_1, lowerBand_1, title="Bands Fill #1", color = color.new(color.green, 95), display = displayBand1, editable = showBand_1)
displayBand2 = showBand_2 ? display.all : display.none
upperBand_2 = plot(upperBandValue2, title="Upper Band #2", color = color.olive, offset = offset, display = displayBand2, editable = showBand_2)
lowerBand_2 = plot(lowerBandValue2, title="Lower Band #2", color = color.olive, offset = offset, display = displayBand2, editable = showBand_2)
fill(upperBand_2, lowerBand_2, title="Bands Fill #2", color = color.new(color.olive, 95), display = displayBand2, editable = showBand_2)
displayBand3 = showBand_3 ? display.all : display.none
upperBand_3 = plot(upperBandValue3, title="Upper Band #3", color = color.teal, offset = offset, display = displayBand3, editable = showBand_3)
lowerBand_3 = plot(lowerBandValue3, title="Lower Band #3", color = color.teal, offset = offset, display = displayBand3, editable = showBand_3)
fill(upperBand_3, lowerBand_3, title="Bands Fill #3", color = color.new(color.teal, 95), display = displayBand3, editable = showBand_3)
// Original EMA and smoothing controls.
emaLength = input.int(9, minval=1, title="Length", group="EMA Settings")
emaSource = input(close, title="Source", group="EMA Settings")
emaOffset = input.int(title="Offset", defval=0, minval=-500, maxval=500, group="EMA Settings", display = display.none)
emaValue = ta.ema(emaSource, emaLength)
plot(emaValue, title="EMA", color=color.orange, offset=emaOffset)
// 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.none)
var isBB = maTypeInput == "SMA + Bollinger Bands"
maLengthInput = input.int(14, "Length", group = GRP, display = display.none, 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.none, active = isBB)
var enableMA = maTypeInput != "None"
// 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)
// Smoothing MA plots
smoothingMA = enableMA ? ma(emaValue, maLengthInput, maTypeInput) : na
smoothingStDev = isBB ? ta.stdev(emaValue, maLengthInput) * bbMultInput : na
plot(smoothingMA, "EMA-based 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)
// Signals use the base VWAP and EMA, independent of visual plot offsets.
// The calculation timeframe follows the chart by default.
SIGNALS_GROUP = "Signals"
showEntries = input.bool(true, "Show Entry Triangles", group=SIGNALS_GROUP)
showExits = input.bool(true, "Show Exit Markers", group=SIGNALS_GROUP)
longColor = input.color(color.lime, "Long Color", group=SIGNALS_GROUP)
shortColor = input.color(color.red, "Short Color", group=SIGNALS_GROUP)
crossDown = ta.crossunder(vwapValue, emaValue)
crossUp = ta.crossover(vwapValue, emaValue)
longEntry = barstate.isconfirmed and crossDown
shortEntry = barstate.isconfirmed and crossUp
// 0 = no prior entry, 1 = long, -1 = short.
var int position = 0
longExit = shortEntry and position == 1
shortExit = longEntry and position == -1
if longEntry
position := 1
else if shortEntry
position := -1
plotshape(showEntries and longEntry, title="Long Entry", style=shape.triangleup, location=location.belowbar, color=longColor, text="LONG", textcolor=longColor, size=size.small)
plotshape(showEntries and shortEntry, title="Short Entry", style=shape.triangledown, location=location.abovebar, color=shortColor, text="SHORT", textcolor=shortColor, size=size.small)
plotshape(showExits and longExit, title="Long Exit", style=shape.xcross, location=location.belowbar, color=longColor, text="EXIT L", textcolor=longColor, size=size.tiny)
plotshape(showExits and shortExit, title="Short Exit", style=shape.xcross, location=location.abovebar, color=shortColor, text="EXIT S", textcolor=shortColor, size=size.tiny)
alertcondition(longEntry, title="Long Entry", message="VWAP crossed below EMA on a closed signal bar: LONG. {{ticker}} {{interval}}")
alertcondition(shortEntry, title="Short Entry", message="VWAP crossed above EMA on a closed signal bar: SHORT. {{ticker}} {{interval}}")
alertcondition(longExit, title="Long Exit", message="VWAP crossed above EMA on a closed signal bar: EXIT LONG. {{ticker}} {{interval}}")
alertcondition(shortExit, title="Short Exit", message="VWAP crossed below EMA on a closed signal bar: EXIT SHORT. {{ticker}} {{interval}}")