input, sinput and enum inputs in Pine Script: input.*()
Short answer
Each input is a typed call that returns its value: input int Len = 20; // Length becomes input.int(20, "Length"). An enum input becomes input.enum or input.string(…, options = […]), and input group "Name" becomes the group= argument.
In MQL5 an input is a global variable declared with input (or sinput), its trailing comment becomes the label in the Inputs tab, an enum-typed input becomes a dropdown, and input group "Name" starts a collapsible section. Pine Script declares each input with a typed function call that returns its value: input.int, input.float, input.bool, input.string, input.source, input.timeframe, input.color, input.enum. The title is an argument, a dropdown is input.enum or input.string(…, options = […]), and sections are the group = argument.
What MQL5 inputs compute
An input variable is read-only inside the program and is set from the properties dialog before OnInit. Its type decides the editor: numbers get a text field, bool a true/false list, color a palette, datetime a date picker, and any enum (built-in like ENUM_TIMEFRAMES, ENUM_APPLIED_PRICE, ENUM_MA_METHOD, or your own) a dropdown whose entries show the comment after each enumerator. The comment after the declaration replaces the variable name as the label. sinput ("static input") behaves the same in the dialog but cannot be optimised in the Strategy Tester. input group "Title" adds a heading above the following inputs. MQL5 has no built-in bounds: a negative length is accepted and has to be rejected in OnInit.
A Pine input call returns a value with the input qualifier, which can be used where a constant is not required. Arguments like minval, maxval, step, tooltip, inline and confirm shape the editor, group creates a section, and display controls whether the value is shown in the status line and Data Window. Pine v6 supports user-defined enum types, and input.enum turns one into a dropdown that shows each field's title.
The Pine Script equivalent
A moving-average indicator with a custom enum, a built-in price enum, two groups and an sinput.
//@version=6
indicator("Selectable MA", overlay = true)
// A Pine enum gives a dropdown like an MQL5 enum input; the field titles are what the user sees.
enum MaKind
sma = "Simple"
ema = "Exponential"
wma = "Weighted"
const string G1 = "Moving average" // input group "Moving average"
const string G2 = "Display"
kind = input.enum(MaKind.ema, "Type", group = G1)
len = input.int(20, "Length", minval = 1, group = G1)
src = input.source(close, "Source", group = G1) // ENUM_APPLIED_PRICE
offset = input.float(0.0, "Vertical offset, price units", group = G2)
show = input.bool(true, "Show line", group = G2)
note = input.string("v1.2", "Build tag", group = G2, display = display.none) // sinput
// The same choice with a string list, as in Pine v5:
// kindStr = input.string("Exponential", "Type", options = ["Simple", "Exponential", "Weighted"])
ma = switch kind
MaKind.sma => ta.sma(src, len)
MaKind.ema => ta.ema(src, len)
MaKind.wma => ta.wma(src, len)
plot(show ? ma + offset : na, "MA", color = color.rgb(220, 20, 60), linewidth = 2)#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrCrimson
#property indicator_width1 2
enum ENUM_MA_KIND
{
MA_KIND_SMA, // Simple
MA_KIND_EMA, // Exponential
MA_KIND_WMA // Weighted
};
input group "Moving average"
input ENUM_MA_KIND InpKind = MA_KIND_EMA; // Type
input int InpLen = 20; // Length
input ENUM_APPLIED_PRICE InpPrice = PRICE_CLOSE; // Source
input group "Display"
input double InpOffset = 0.0; // Vertical offset, price units
input bool InpShow = true; // Show line
sinput string InpNote = "v1.2"; // Build tag (not optimised)
double MaBuffer[];
int maH;
int OnInit()
{
SetIndexBuffer(0, MaBuffer, INDICATOR_DATA);
ENUM_MA_METHOD method = (InpKind == MA_KIND_SMA) ? MODE_SMA
: (InpKind == MA_KIND_EMA) ? MODE_EMA : MODE_LWMA;
maH = iMA(_Symbol, _Period, InpLen, 0, method, InpPrice);
return(maH == INVALID_HANDLE ? INIT_FAILED : INIT_SUCCEEDED);
}
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
int toCopy = (prev_calculated == 0) ? rates_total : rates_total - prev_calculated + 1;
if(CopyBuffer(maH, 0, 0, toCopy, MaBuffer) <= 0)
return(0);
int start = rates_total - toCopy;
for(int i = start; i < rates_total; i++)
MaBuffer[i] = InpShow ? MaBuffer[i] + InpOffset : EMPTY_VALUE;
return(rates_total);
}| MQL5 | Pine |
|---|---|
input int Len = 20; // Length | len = input.int(20, "Length") |
input double | input.float(0.0, "…") (the default must be a float literal) |
input bool, input string, input color | input.bool, input.string, input.color |
input datetime | input.time(timestamp("2024-01-01"), "…") (ms) |
Custom enum input | enum type + input.enum, or input.string(…, options = […]) |
ENUM_APPLIED_PRICE | input.source(close, "…") |
ENUM_TIMEFRAMES | input.timeframe("", "…") (empty = chart timeframe) |
ENUM_MA_METHOD | input.string("EMA", options = ["SMA", "EMA", "SMMA (RMA)", "WMA"]) + switch |
input group "X" | group = "X" on each following input |
sinput | Same input.*; display = display.none to keep it off the status line |
Range check in OnInit | minval, maxval, step |
Where the two differ
Optimisation. The MQL5 Strategy Tester can sweep any input and skips sinput. TradingView has no built-in optimiser, so the distinction has no effect; sinput maps to an ordinary input.
Enum values. MQL5 enumerators are integers, and code often does arithmetic on them or casts them. Pine enum fields are not numbers; compare them with == or switch, and use a map or switch if a numeric value is needed.
Where inputs can be used. Some Pine parameters require simple or const values. Inputs are fine for ta.* lengths and request.security timeframes, but not for indicator(title = …) or alertcondition messages.
Order and layout. Pine shows inputs in the order the calls are executed; inline = "id" puts several on one line, which MQL5 cannot do.
Changing inputs. Changing an MQL5 input reinitialises the program (OnDeinit then OnInit); changing a Pine input reruns the whole script from the first bar.
What the Stratlate converter emits
The MQL5 to Pine engine turns each input, sinput and extern into a typed input.int, input.float, input.bool or input.string titled with the trailing comment (or the variable name when there is none), and adds display=display.none for sinput (PineEmitter input emission). An ENUM_APPLIED_PRICE input becomes input.source(close, …). A custom enum input is emitted as input.int holding the enumerator's index, with each enumerator declared as an integer constant, so the dropdown becomes a number field without the option names; ENUM_TIMEFRAMES inputs are likewise emitted as integers (minutes). input group lines produce no group= argument, and no minval/maxval is added. A default that cannot be lowered is replaced by a typed zero so the script still loads. In the program above the moving-average type selected at runtime was not resolved (MQLW0501, "unknown dispatch value"), so iMA fell back to ta.sma on close whatever the inputs said; the hand-written switch is the fix. Rebuild dropdowns and groups by hand after reviewing the report.
Pitfalls
- Giving
input.inta fractional default, as when an MQL5doubleinput is declared with the wrong function. It is a compile error; useinput.floatwith a float literal such as1.0. - Using the option string in calculations without matching it exactly.
options = ["EMA", "SMA"]with aswitchon"Ema"silently takes the default branch. - Translating
ENUM_TIMEFRAMESasinput.int. Useinput.timeframe;request.securityexpects timeframe strings like"60"or"D". - Forgetting that
groupmust be repeated on every input of a section. Inputs without it move to the top, outside any group. - Keeping range checks that call
runtime.error()whereminvalwould do. The dialog check is friendlier and runs before the script.
FAQ
Does Pine Script have enum inputs like MQL5?
Yes, since v6: declare an enum with titled fields and pass a field to input.enum. The dropdown shows the titles. In v5 and earlier the usual substitute is input.string with options.
What is the Pine equivalent of sinput?
An ordinary input.* call. TradingView has no built-in optimiser that the "static" flag would affect. Add display = display.none if the value should not appear next to the indicator name.
How do I add a tooltip like the MQL5 comment?
The MQL5 comment becomes the input's title. For a longer explanation, pass tooltip = "…"; TradingView shows it as an info icon next to the input.
Convert the whole program
Paste an MQL4 or MQL5 indicator or Expert Advisor and get Pine Script v6 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.
Open the converter