The script
A moving average, a VWAP that restarts each day, week or month, and a session highlight, with every kind of input arranged under three headings. Paste it into a new indicator in the Pine Editor, then save and add it to the chart.
//@version=6
indicator("Settings tab example", overlay = true)
const string G_MA = "Moving average"
const string G_VWAP = "VWAP"
const string G_SESS = "Session highlight"
enum MaKind
sma = "SMA"
ema = "EMA"
wma = "WMA"
// Moving average: the kind and the length share a row
src = input.source(close, "Source", group = G_MA)
maKind = input.enum(MaKind.ema, "Average", inline = "ma", group = G_MA)
maLen = input.int(50, "", minval = 1, maxval = 500, inline = "ma", group = G_MA, tooltip = "Kind of average and its length in bars")
maColor = input.color(color.orange, "Color", group = G_MA)
// VWAP that restarts every day, week or month
showVwap = input.bool(true, "Show VWAP", group = G_VWAP)
resetTf = input.timeframe("D", "Restart every", options = ["D", "W", "M"], group = G_VWAP, active = showVwap)
bandMult = input.float(1.0, "Band width (x stdev)", minval = 0, step = 0.5, group = G_VWAP, active = showVwap)
// Session highlight: its fields can only be edited when it's switched on
useSess = input.bool(false, "Highlight a session", group = G_SESS)
sess = input.session("0930-1600", "Session", group = G_SESS, active = useSess)
tzName = input.string("Exchange", "Time zone", options = ["Exchange", "UTC", "America/New_York", "Europe/London", "Asia/Tokyo"], group = G_SESS, active = useSess)
// All three averages run on every bar; the input only picks which one is shown
smaLine = ta.sma(src, maLen)
emaLine = ta.ema(src, maLen)
wmaLine = ta.wma(src, maLen)
float ma = switch maKind
MaKind.sma => smaLine
MaKind.wma => wmaLine
=> emaLine
[vwapLine, vwapUpper, vwapLower] = ta.vwap(hlc3, timeframe.change(resetTf), bandMult)
string tz = tzName == "Exchange" ? syminfo.timezone : tzName
bool inSess = not na(time(timeframe.period, sess, tz))
plot(ma, "Moving average", maColor, 2)
plot(showVwap ? vwapLine : na, "VWAP", color.blue)
plot(showVwap ? vwapUpper : na, "VWAP upper band", color.new(color.blue, 60))
plot(showVwap ? vwapLower : na, "VWAP lower band", color.new(color.blue, 60))
bgcolor(useSess and inSess ? color.new(color.yellow, 88) : na, title = "Session")How it works
- Groups. The three
const stringnames are reused asgrouparguments, so a typo can't split a section in two. Each group becomes a heading in the Inputs tab, in the order the inputs appear in the code. - One row, one title.
maKindandmaLenshareinline = "ma". Only the first has a title; the second has""and a tooltip that explains both. That's the layout TradingView aligns cleanly. input.enum()shows a dropdown with the enum's titles ("SMA", "EMA", "WMA"). Compared withinput.string(..., options = [...]), a misspelled option is caught when the script is saved, not silently ignored.input.source(close, ...)offers the price sources (open, high, low, close, hl2...) and also the outputs of other indicators on the same chart, so this average can be computed on another indicator's line.input.timeframe("D", ..., options = ["D", "W", "M"])limits the choice to three periods;timeframe.change(resetTf)is true on the first bar of each one, which restartsta.vwap().active = useSessgrays out the session fields while the checkbox is off: they keep their value but can't be edited.input.session("0930-1600", ...)is read withtime(timeframe.period, sess, tz), which returnsnaoutside the session. The"Exchange"option stands forsyminfo.timezone, the exchange's own time zone.
Pitfalls
- A title on every inline input. Several titled inputs on one
inlinerow may not line up, and TradingView warns about it (inputs inline might not align). Keep the title on the first input of the row. - Inputs don't change while the script runs. An input is fixed for the whole run: code can't change it, and it can't take a different value on each bar. Values calculated from bar data are series, which some arguments refuse (series int used but simple int expected).
- hline() needs a fixed value.
hline(input.float(70, "Level"))works;hline(ta.sma(close, 50))doesn't (series float vs input float). - ta. calls in the switch branches.* Picking the average with
switchand callingta.sma()orta.ema()inside the branches means the averages not chosen don't run on every bar, which TradingView flags (should be called on each calculation). Calculate all of them, then pick, as the script does. - Changing a default after people use the script. Instances already on a chart keep the value saved with them; a new default only applies to new instances.
Variations
Inputs that only show their value where it helps. display = display.none keeps an input out of the indicator's status line, useful for colors and long options. confirm = true asks for the value when the script is first added to a chart, which suits a price level:
//@version=6
indicator("Level from the chart", overlay = true)
level = input.price(0.0, "Level", confirm = true)
lineCol = input.color(color.orange, "Line color", display = display.none)
plot(level, "Level", lineCol, 2)
input.price() with confirm = true lets you click the level on the chart. input.time() works the same way for a date, as the strategy template does for its date range.
Questions
How do I make a dropdown input in Pine Script?
Pass a list to options, for example input.string("EMA", "Type", options = ["SMA", "EMA", "WMA"]), or declare an enum and use input.enum(). The enum version catches misspelled options when you save the script.
How do I put two inputs on the same line?
Give both the same inline string, such as inline = "ma", and the same group. Keep a title on the first input only and use "" for the others, with a tooltip if they need explaining.
How do I hide an input's value from the indicator's name on the chart?
Add display = display.none to the input call. The value still appears in the settings, but not in the status line next to the indicator's name.
Can one input disable another?
Yes, with active: input.session("0930-1600", "Session", active = useSess) is grayed out while the useSess checkbox is off. The disabled input keeps its value.
Related pages
Every script on this page was checked with Edgoro Pine Check (Pine Script v6 rules). Pine Check is a static checker, not TradingView's compiler: TradingView compiles the script when you save it in the Pine Editor. The scripts are examples to learn from, not trading systems. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.