OPEN-SOURCE SCRIPT

vika

//version=5
indicator("Auto Anchored Moving Average", "Anchored MA", overlay = true, max_bars_back = 5000, max_lines_count = 500)

// Inputs
auto = input.bool(true, "Enable Auto Anchor")
anchor = input.timeframe('D', 'Anchor Period')
source = input(hlc3, "Source")

showPP = input.bool(true, "Show Prev. Period MA")
highlightAnc = input.bool(true, "Highlight Anchor Change")

wma_show = input.bool(true, "WMA", inline = "WMA", group = "Styles")
sma_show = input.bool(true, "SMA", inline = "SMA", group = "Styles")
vwap_show = input.bool(true, "VWAP", inline = "VWAP", group = "Styles")
wma_style = input.string("──────", "", options = ["──────", "─ ─ ─ ─", "· · · · ·"], inline = "WMA", group = "Styles")
sma_style = input.string("──────", "", options = ["──────", "─ ─ ─ ─", "· · · · ·"], inline = "SMA", group = "Styles")
vwap_style = input.string("──────", "", options = ["──────", "─ ─ ─ ─", "· · · · ·"], inline = "VWAP", group = "Styles")
wma_color = input.color(color.lime, "WMA Color", inline = "WMA", group = "Styles")
sma_color = input.color(color.red, "SMA Color", inline = "SMA", group = "Styles")
vwap_color = input.color(color.blue, "VWAP Color", inline = "VWAP", group = "Styles")

// Determine anchor period automatically
autoAnchor = switch
timeframe.isintraday => timeframe.multiplier <= 15 ? "1D" : "1W"
timeframe.isdaily => "1M"
=> "12M"

// Override auto-anchor if 'auto' is off
if auto
anchor := autoAnchor

// Anchor change detection
isNewAnchor = timeframe.change(anchor)

var length = 1
length := isNewAnchor ? 1 : length + 1

// Calculate MAs
wma = ta.wma(source, length)
sma = ta.sma(source, length)
vwap = nz(ta.vwma(source, length))

// Previous MAs (for prev period plot)
p_wma = ta.valuewhen(isNewAnchor, wma[1], 0)
p_sma = ta.valuewhen(isNewAnchor, sma[1], 0)
p_vwap = ta.valuewhen(isNewAnchor, vwap[1], 0)

// Plot styles
get_style(style) =>
switch style
"──────" => line.style_solid
"─ ─ ─ ─" => line.style_dashed
"· · · · ·" => line.style_dotted

// Draw moving averages
draw(show, ma, _c, style) =>
var line _l = na
var label _lb = na
var label _lbc = na

_l.set_x2(bar_index)
if show and isNewAnchor and showPP
_l := line.new(bar_index - 1, ma[1], bar_index, ma[1], color = _c, style = style)

show = not isNewAnchor

plot(show and wma_show ? wma : na, "WMA", wma_color, style=plot.style_linebr)
plot(show and sma_show ? sma : na, "SMA", sma_color, style=plot.style_linebr)
plot(show and vwap_show ? vwap : na, "VWAP", vwap_color, style=plot.style_linebr)

// Highlight anchor change
hlight = isNewAnchor and highlightAnc
plotshape(hlight and wma_show ? wma[1] : na, 'WMA Highlight', shape.circle, location.absolute, color.new(wma_color, 50), -1)
plotshape(hlight and sma_show ? sma[1] : na, 'SMA Highlight', shape.circle, location.absolute, color.new(sma_color, 50), -1)
plotshape(hlight and vwap_show ? vwap[1] : na, 'VWAP Highlight', shape.circle, location.absolute, color.new(vwap_color, 50), -1)

// Draw lines for each moving average
draw(wma_show, wma, wma_color, get_style(wma_style))
draw(sma_show, sma, sma_color, get_style(sma_style))
draw(vwap_show, vwap, vwap_color, get_style(vwap_style))
Candlestick analysisChart patternsTrend Analysis

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer