input.int, input.float, input.bool in MQL5: input variables

Short answer

Declare a global with the input keyword: input int InpLength = 20; the trailing comment becomes its displayed name and input group starts a section. MQL5 has no minval, maxval or step, so check the range in OnInit().

input.int(), input.float() and input.bool() put a setting on the script's Inputs tab and return its value. MQL5 does the same with a global variable declared with the input keyword: input int InpLength = 20; appears on the indicator's Inputs tab, the trailing comment becomes its displayed name, and input group "Core" starts a titled section. What MQL5 does not have is minval, maxval and step: MetaTrader accepts any value of the right type, so the range has to be checked in OnInit().

What input.int() computes

input.int(defval, title, minval, maxval, step, tooltip, inline, group, confirm, display, active) returns a constant of qualifier input int: it is fixed for the whole run, can be used where a simple value is required (lengths of ta.* functions, for example), and cannot be assigned to. TradingView's dialog refuses values below minval or above maxval, and the arrow buttons move in increments of step. input.float is the same with a float, and it can also take options, a list of allowed values. input.bool(defval, title, tooltip, inline, group, confirm) draws a checkbox. group puts inputs under a heading, inline puts several on one row, tooltip adds a hover note, and confirm=true asks for the value when the script is added to a chart.

The MQL5 equivalent

The Pine version has three inputs in two groups, with a range on the length and the multiplier.

//@version=6
indicator("Inputs demo", overlay=true)
len = input.int(20, "Length", minval=1, maxval=500, step=1, group="Core")
mult = input.float(2.0, "Multiplier", minval=0.1, step=0.1, group="Core")
showBand = input.bool(true, "Show band", group="Display")
basis = ta.sma(close, len)
dev = mult * ta.stdev(close, len)
plot(basis, "Basis")
plot(showBand ? basis + dev : na, "Upper")

In MQL5 each becomes an input variable. int stays int, float becomes double, bool stays bool, and the comment after the declaration is what the user sees in place of the variable name. The checks in OnInit() return INIT_PARAMETERS_INCORRECT, which stops the indicator from loading and prints the reason in the Experts log.

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_label1  "Basis"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDodgerBlue
#property indicator_label2  "Upper"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrOrange

input group "Core"
input int    InpLength = 20;     // Length (1 to 500)
input double InpMult   = 2.0;    // Multiplier (at least 0.1)
input group "Display"
input bool   InpShowBand = true; // Show band

double BufBasis[], BufUpper[];

int OnInit()
{
   // Pine's minval / maxval: MetaTrader accepts any value, so refuse bad ones here
   if(InpLength < 1 || InpLength > 500)
     {
      Print("Length must be between 1 and 500");
      return INIT_PARAMETERS_INCORRECT;
     }
   if(InpMult < 0.1)
     {
      Print("Multiplier must be at least 0.1");
      return INIT_PARAMETERS_INCORRECT;
     }
   SetIndexBuffer(0, BufBasis, INDICATOR_DATA);
   SetIndexBuffer(1, BufUpper, INDICATOR_DATA);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   return 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 start = prev_calculated > 0 ? prev_calculated - 1 : 0;
   for(int i = start; i < rates_total; i++)
     {
      if(i < InpLength - 1)
        {
         BufBasis[i] = EMPTY_VALUE;
         BufUpper[i] = EMPTY_VALUE;
         continue;
        }
      double sum = 0.0, sq = 0.0;
      for(int k = i - InpLength + 1; k <= i; k++)
         sum += close[k];
      double mean = sum / InpLength;
      for(int k = i - InpLength + 1; k <= i; k++)
         sq += (close[k] - mean) * (close[k] - mean);
      double dev = InpMult * MathSqrt(sq / InpLength);   // ta.stdev: population form
      BufBasis[i] = mean;
      BufUpper[i] = InpShowBand ? mean + dev : EMPTY_VALUE;
     }
   return rates_total;
}
Pine argumentMQL5
defvalThe initialiser: input int InpLength = 20;
titleThe comment after the declaration: // Length
minval, maxvalAn if in OnInit returning INIT_PARAMETERS_INCORRECT
stepNone in the Inputs dialog; the Strategy Tester has its own start/step/stop per input for optimisation
groupinput group "Name" before the inputs it covers
tooltip, inline, confirmNone
input.float(..., options=[...])An enum input, see input.string options
Not shown in optimisationsinput instead of input

Where the two differ

TopicPine ScriptMQL5
Range checksEnforced by the dialogNot enforced; your OnInit code decides
Writing to the valueCompile errorAlso a compile error: input variables are read-only; copy into a normal global if the script changes it
Name shown to the usertitle stringTrailing comment; without it, the variable name
Where it can be declaredAnywhere at global scope, even mid-scriptGlobal scope only, before use; order on the tab follows declaration order
Type of a float inputfloatdouble; an integer literal default such as 2 is fine
Changing a valueThe script reruns from bar 0The indicator is reinitialised: OnDeinit, OnInit, then OnCalculate with prev_calculated == 0
Reading from another programNot possibleiCustom passes inputs positionally, in declaration order; input group lines are skipped

What the Stratlate converter emits

Each input.int, input.float and input.bool with a constant default becomes an input int, input double or input bool with the same name, and the Pine title is written as the trailing comment when it differs from the variable name. Constant minval and maxval on integer and float inputs are turned into checks at the top of OnInit that print the input's title and return INIT_PARAMETERS_INCORRECT, exactly as in the example above. step, group and tooltip are dropped and listed in the report as PM221 ("extra input options ... have no MQL equivalent"), so the converted Inputs tab is one flat list. An input the script later writes to is emitted as an input under a shadow name plus a plain global that is reset from it on every bar, and an input whose default is not a compile-time constant becomes ordinary runtime state. The sample above converts CLEAN; compile it and try an out-of-range value to see the check fire.

Pitfalls

  • Trusting MetaTrader to stop a length of 0. Without an OnInit check the indicator divides by zero or reads past an array end on the first bar and stops with a runtime error.
  • Assigning to an input variable. MetaEditor refuses it ("constant cannot be modified"); declare a working copy if the converted script needs to change the value.
  • Reordering inputs after an EA uses iCustom on the indicator. iCustom passes values by position, so a new input in the middle shifts every later value into the wrong parameter.
  • Using an int for what Pine declared with input.float. A multiplier of 1.5 silently becomes 1.
  • Expecting input group to change behaviour. It is cosmetic, like Pine's group; it adds a heading and nothing else.

FAQ

Why did my indicator fail with "init failed" after conversion?

OnInit returned INIT_PARAMETERS_INCORRECT because an input is outside the minval/maxval the Pine script declared. The Experts tab prints which input and which bound; change the value and the indicator loads.

What is the difference between input and sinput?

Both appear on the Inputs tab. sinput marks a value that the Strategy Tester must not optimise, such as a colour or a display switch. Pine has no optimiser, so either is a correct translation; use sinput for purely visual settings.

Can I show a tooltip on an MQL5 input?

No. The only text MetaTrader shows is the trailing comment, so fold a short hint into it, for example // Length (bars, 1 to 500).

Convert the whole script

Paste a Pine Script indicator or strategy and get MQL5 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

Related