Back to TradingView PineScript for CFD Traders
Part 1

Inputs and parameters: making indicators reusable

Inputs let you expose tunables to whoever uses your script. Pick the right input type and Pine will give you a useful settings UI.

Beginner7 min readBeginner → AdvancedLesson 05 / 17
Try it in TradingView
A moving average with a real settings panel

Switch Type between SMA/EMA/WMA/RMA in the settings dialog. Change Source to hl2 or ohlc4 and watch the line move — that's input.source working.

//@version=5
indicator("Lesson 5 - Configurable MA", overlay=true)

// Four different input types, all exposed in the settings dialog.
src     = input.source(close, "Source")
length  = input.int(20, "Length", minval=1, maxval=500)
maType  = input.string("EMA", "Type", options=["SMA", "EMA", "WMA", "RMA"])
show200 = input.bool(true, "Show 200 reference")

// switch picks the matching built-in function.
ma = switch maType
    "SMA" => ta.sma(src, length)
    "EMA" => ta.ema(src, length)
    "WMA" => ta.wma(src, length)
    "RMA" => ta.rma(src, length)

plot(ma, "MA", color=color.orange, linewidth=2)
plot(show200 ? ta.ema(src, 200) : na, "200 EMA", color=color.gray)
To run it
  1. 1. Open any chart in TradingView and click Pine Editor at the bottom.
  2. 2. Select everything in the editor and paste this over it, then click Save.
  3. 3. Click Add to chart. Open the Strategy Tester tab if it is a strategy.

Why inputs matter

Hard-coded constants work for learning, but real indicators expose their tunables. Inputs become the settings panel any user of your script will see. The right input type gives you the right control — sliders, dropdowns, colour pickers, checkboxes.

Input types

input.int — integers, with optional min/max/step. input.float — same with decimals. input.bool — checkboxes for on/off. input.string — free text or a dropdown. input.source — lets users pick a series (close, high, low, hl2, ohlc4, etc.). input.timeframe — a timeframe picker.

String dropdowns

input.string can take a list of options:

maType = input.string("SMA", "Type", options=["SMA", "EMA", "WMA"])

The settings panel shows a dropdown. You read the selected value as a regular string.

Source inputs

src = input.source(close, "Source") lets the user pick which series feeds your indicator — close, high, hl2, ohlc4, or any other plot on the chart. This single line is what makes a moving-average indicator usable across different chart types and custom feeds.

Tooltips and groups

Inputs accept tooltip and group arguments. tooltip="Period over which the average is calculated" shows a question-mark icon in the settings panel. group="Visuals" clusters related inputs together. Use them once you have more than a handful of inputs.

Defaults and validation

Pick defaults that are sensible for the most common use case. If the user can shoot themselves in the foot (negative length, zero divisor), use minval or guard with if at the top of the script.

A worked example

//@version=5
indicator("Configurable MA", overlay=true)
src      = input.source(close, "Source")
length   = input.int(20, "Length", minval=1, maxval=500)
maType   = input.string("EMA", "Type", options=["SMA", "EMA", "WMA"])
show200  = input.bool(false, "Show 200 reference")

ma = switch maType
    "SMA" => ta.sma(src, length)
    "EMA" => ta.ema(src, length)
    "WMA" => ta.wma(src, length)

plot(ma, color=color.orange, linewidth=2)
plot(show200 ? ta.ema(src, 200) : na, color=color.gray)

What to take from this

Inputs are how you turn a script into a tool. Spend the time to expose the right tunables with sensible defaults and validation — your future self, when you come back to tune the indicator on a live market, will thank you.

What you just did

Lesson 05 of 17 in TradingView PineScript for CFD Traders. When you have run the examples or read the section, tick it off and move to the next lesson.