Pine Script inputs: turn any number into a setting
Every number in a script is a guess until you test it. Inputs turn those numbers into settings, so you can try 20, then 50, without touching the code.
What an input is
An input is a setting. It shows up when you click the gear icon next to your script's name on the chart, under Inputs.
Without inputs, changing a number means opening the code, editing it and saving. With inputs, you just type a new value in the settings box. The chart updates right away.
It also makes your script easy to share. Someone else can change the settings without reading a line of code.
New to Pine Script? Start with Pine Script for beginners. This page picks up from there.
The basic pattern
Every input follows the same shape:
length = input.int(20, "Length")
In plain words:
- length is the name you give it. You use this name later in the code.
- input.int says "this setting is a whole number".
- 20 is the starting value.
- "Length" is the label people see in the settings box.
The 6 input types you'll use most
Pine Script version 6 has a different input for each kind of value:
| Input | What it holds | Example use |
|---|---|---|
| input.int | A whole number | How many candles to average |
| input.float | A number with decimals | 1.5 times the average range |
| input.bool | On or off (a tick box) | Show or hide a line |
| input.source | A price to use | Close, open, high or low |
| input.string | A choice from a list | "SMA" or "EMA" |
| input.color | A color picker | The line's color |
There are others, like input.timeframe for picking a chart timeframe. That one gets its own page: Pine Script request.security.
A full example with every type
Here's a whole indicator. It draws a Moving average (the average price of the last few candles) with 5 settings:
//@version=6
indicator("Average with settings", overlay=true)
src = input.source(close, "Price", group="Average")
length = input.int(20, "Length", minval=1, maxval=500, group="Average", tooltip="How many candles to average")
maType = input.string("SMA", "Type", options=["SMA", "EMA"], group="Average")
showLine = input.bool(true, "Show the line", group="Look")
lineColor = input.color(color.orange, "Color", group="Look")
smaLine = ta.sma(src, length)
emaLine = ta.ema(src, length)
avg = maType == "SMA" ? smaLine : emaLine
plot(showLine ? avg : na, "Average", color=lineColor)
Line by line:
- Line 1 says this is Pine Script version 6.
- Line 2 names the indicator and draws it on the price chart.
- Line 3 lets you pick which price to average. It starts on the closing price.
- Line 4 is the number of candles. minval and maxval stop anyone typing 0 or 10,000. tooltip adds a small "i" with a hint.
- Line 5 gives a drop-down list with 2 choices. options is the list.
- Line 6 is a tick box, ticked to start.
- Line 7 is a color picker, orange to start.
- Lines 8 and 9 work out both kinds of average every candle.
- Line 10 picks one. Read it as: "if the Type is SMA, use smaLine, otherwise use emaLine."
- Line 11 draws it. If the box is unticked, it draws na (nothing) instead.
group puts settings under a small heading. Here, "Average" and "Look" become 2 neat sections.
Why lines 8 and 9 work out both averages
You might wonder why we don't only work out the one we need.
Functions like ta.sma and ta.ema keep a memory of past candles. If they only run on some candles, that memory has gaps, and the numbers can come out wrong. Pine Script warns you about this.
The safe habit: work out these values on every candle, then choose between them.
input.float: a setting with decimals
Use it when a whole number isn't enough:
mult = input.float(1.5, "Range multiplier", minval=0.1, step=0.1)
- 1.5 is the starting value.
- minval=0.1 is the smallest allowed.
- step=0.1 is how much the little up and down arrows change it each click.
More useful extras
These work on most input types:
- tooltip adds a hint next to the setting. Use it to explain what the number does.
- group puts settings under a heading.
- inline puts 2 settings on the same row. Give both the same inline name.
- confirm=true asks for the value when you first add the script to a chart.
How to use inputs to test, not to tune
Here's the catch. Inputs make it easy to try 50 settings until the past looks perfect. That feels like progress. It usually isn't.
A setting picked because it fit the past is called overfitting. It's tuned to old candles that will never repeat exactly.
A better habit:
- Pick a setting for a reason you can say in one sentence. For example: "20 candles is about one month of trading days."
- Test it on past charts with bar replay, one candle at a time.
- Log what you see in a trading journal.
- Only change the setting if you can say why, then test again.
A better number won't fix a missing plan. No setting tells you where your stop goes or how big to trade. That comes from your rules, not your inputs.
Try it today
- Paste the full example into the Pine Editor, on a computer.
- Save it and add it to a chart.
- Click the gear icon. Find the 2 groups, "Average" and "Look".
- Switch Type from SMA to EMA. Watch the line react faster.
- Hover over the "i" next to Length. That's your tooltip.
Learning from a real script
Inputs are how a finished indicator stays flexible. Studying one shows you how many settings are useful, and how many are too many.
Our course includes the full source code of the StructureX chart tool, to learn from and customize, settings and all. It's source code, not a ready-made trading bot.
Fewer settings, clearer chart. If a setting has never changed your decision, think about removing it.
Common mistakes
- Wrong type. input.int won't accept 1.5. Use input.float for decimals.
- No limits. Without minval, someone can type 0 and break the script.
- Labels that mean nothing. "Len2" helps nobody. Write "Slow average length".
- Settings scattered everywhere. Put inputs at the top of the script, so they're easy to find.
- Tuning until it looks perfect. That's overfitting. Pick for a reason, then test.
Questions
How do I add a setting to a Pine Script indicator?
Replace the number with an input, like length = input.int(20, "Length"), then use length in the code. It appears under Inputs in the script's settings.
What is the difference between input.int and input.float?
input.int holds whole numbers like 20. input.float holds numbers with decimals like 1.5.
How do I make a drop-down menu in Pine Script?
Use input.string with an options list, for example input.string("SMA", "Type", options=["SMA", "EMA"]).
How do I group inputs in Pine Script?
Add group="Name" to each input. Inputs with the same group name appear together under that heading.
Want the next step? We'll walk you through it.
Four free lessons by email, one every few days — each with a 10-minute exercise on a real chart.
Send Me the Free Lessons 4 short emails · No spam · Unsubscribe anytimeOr see the whole method, step by step:
See What’s Inside the Course Try everything free first. Decide when you're ready. · Open the free chart tool (works in the TradingView phone app)One chart idea a day, in plain English. Follow along: