Pine inputs in MQL5: options, source, timeframe

Short answer

Each input.*() call becomes a global declared with the input keyword, with Pine's title in the trailing comment so MetaTrader shows it as the name. Dropdowns, price sources, timeframes and value limits each need a specific pattern; inline, tooltip, confirm and step are dropped.

Every Pine Script input.*() call becomes a line in the TradingView settings dialog. In MQL5 the same job is done by global variables declared with the input keyword, which MetaTrader shows on the Inputs tab. Most types map one to one, but dropdowns, price sources, timeframes and value limits each need a specific pattern. This guide is for developers converting an indicator or strategy who want the MetaTrader settings to offer the same choices as the original.

The basic mapping

Pine Script v6MQL5Notes
input.int(14, "Length")input int InpLen = 14; // LengthThe trailing comment is the label shown
input.float(2.0), input.price()input double
input.bool(true)input bool
input.color(color.red)input colorNo transparency in MetaTrader's colour picker
input.string(..., options = [...])enum + inputA dropdown
input.source(close)input ENUM_APPLIED_PRICEStandard prices only
input.timeframe("60")input ENUM_TIMEFRAMESPERIOD_CURRENT for ""
input.symbol("EURUSD")input stringBroker symbol names differ
input.time(timestamp(...))input datetimeSeconds, not milliseconds
input.enum(MyEnum.A)enum + inputMember comments become the labels
group = "Style"input group "Style"Applies to the inputs that follow

MetaTrader shows the comment after an input declaration as its name, so put Pine's title there; without it, users see the variable name. inline, tooltip, confirm and step have no MQL5 equivalent and are dropped. Use sinput for a setting that must never be optimised in the Strategy Tester.

A worked example

This Pine indicator uses five kinds of input in two groups:

//@version=6
indicator("Configurable MA", overlay = true)
len  = input.int(20, "Length", minval = 1, maxval = 500, group = "Moving average")
src  = input.source(close, "Source", group = "Moving average")
kind = input.string("SMA", "Type", options = ["SMA", "WMA"], group = "Moving average")
show = input.bool(true, "Show line", group = "Style")
col  = input.color(color.purple, "Line colour", group = "Style")
ma = kind == "SMA" ? ta.sma(src, len) : ta.wma(src, len)
plot(ma, "MA", color = col, display = show ? display.all : display.none)

The MQL5 version with the same settings:

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_label1  "MA"

enum ENUM_MA_KIND
{
   MA_KIND_SMA,   // SMA
   MA_KIND_WMA    // WMA
};

input group "Moving average"
input int                InpLen  = 20;              // Length
input ENUM_APPLIED_PRICE InpSrc  = PRICE_CLOSE;     // Source
input ENUM_MA_KIND       InpKind = MA_KIND_SMA;     // Type
input group "Style"
input bool               InpShow = true;            // Show line
input color              InpCol  = clrMediumPurple; // Line colour

double MaBuf[];

double Price(const ENUM_APPLIED_PRICE ap, const double &o[], const double &h[],
             const double &l[], const double &c[], const int i)
{
   switch(ap)
   {
      case PRICE_OPEN:     return o[i];
      case PRICE_HIGH:     return h[i];
      case PRICE_LOW:      return l[i];
      case PRICE_MEDIAN:   return (h[i] + l[i]) / 2.0;               // hl2
      case PRICE_TYPICAL:  return (h[i] + l[i] + c[i]) / 3.0;        // hlc3
      case PRICE_WEIGHTED: return (h[i] + l[i] + 2.0 * c[i]) / 4.0;  // hlcc4
      default:             return c[i];
   }
}

int OnInit()
{
   if(InpLen < 1 || InpLen > 500)          // Pine: minval=1, maxval=500
   {
      Print("Length must be between 1 and 500");
      return INIT_PARAMETERS_INCORRECT;
   }
   SetIndexBuffer(0, MaBuf, INDICATOR_DATA);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetInteger(0, PLOT_LINE_COLOR, InpCol);
   PlotIndexSetInteger(0, PLOT_DRAW_TYPE, InpShow ? DRAW_LINE : DRAW_NONE);
   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++)
   {
      MaBuf[i] = EMPTY_VALUE;
      if(i < InpLen - 1) continue;
      double sum = 0.0, wsum = 0.0;
      for(int k = 0; k < InpLen; k++)
      {
         double w = InpKind == MA_KIND_WMA ? InpLen - k : 1.0;
         sum  += w * Price(InpSrc, open, high, low, close, i - k);
         wsum += w;
      }
      MaBuf[i] = sum / wsum;
   }
   return rates_total;
}

Dropdowns: options become an enum

A string input with options becomes an enum. MetaTrader displays the comment on each enum member in the dropdown, so MA_KIND_SMA, // SMA shows as "SMA". Your code then compares against enum members instead of strings, which also removes a class of Pine bugs where a typo in a string comparison silently selects the else branch. If the rest of the script really needs the string, keep a parallel array of labels and index it with the enum value. Numeric options lists (for example input.int(14, options = [7, 14, 21])) can be handled the same way, mapping each member to its number.

Price sources: input.source

ENUM_APPLIED_PRICE gives the user a dropdown of close, open, high, low, median (hl2), typical (hlc3) and weighted. Pine's ohlc4 has no member of its own, and MetaTrader's PRICE_WEIGHTED is (high + low + 2 × close) / 4, which is Pine's hlcc4, not ohlc4. Because the full OnCalculate form receives the price arrays rather than a single applied price, you compute the chosen price yourself, as Price() does in the example. That is also where you decide what PRICE_WEIGHTED means.

Pine also lets a source input select the output of another indicator on the chart. MQL5 has no equivalent in the Inputs dialog. The usual replacement is to compute that indicator inside your own code, or to read it with iCustom and CopyBuffer.

Timeframe inputs

input.timeframe maps to ENUM_TIMEFRAMES, with PERIOD_CURRENT standing for Pine's empty string. MetaTrader's list is fixed (M1 to M6, M10, M12, M15, M20, M30, H1 to H4, H6, H8, H12, D1, W1, MN1), so a Pine default such as "45" or "3D" has no member and needs a decision. Reading the higher timeframe usually happens through iBarShift and the iClose family:

// Pine: tf = input.timeframe("240", "Higher timeframe")   ("" = chart timeframe)
input ENUM_TIMEFRAMES InpTf = PERIOD_H4;   // Higher timeframe

// Pine: request.security(syminfo.tickerid, tf, close[1], lookahead = barmerge.lookahead_on)
// inside the OnCalculate loop, for chart bar i:
int    shift  = iBarShift(_Symbol, InpTf, time[i]);      // the HTF bar containing bar i
double htfPrevClose = iClose(_Symbol, InpTf, shift + 1);  // the HTF bar before it

The full set of rules for request.security, including lookahead and repainting, is in the multi-timeframe guide linked below.

Limits, and inputs the script changes

Pine's minval and maxval stop the user from entering a value outside the range. MQL5 inputs have no bounds, so check them in OnInit and return INIT_PARAMETERS_INCORRECT; MetaTrader then refuses to start the indicator and the Print message explains why. Without the check, an out-of-range length can read past the start of an array and stop the indicator with an "array out of range" error.

MQL5 inputs are also constants. Pine allows len := len * 2 on an input variable; in MQL5 that is a compile error ("constant cannot be modified"). Copy the input into an ordinary global and modify the copy.

What Stratlate does with inputs

The converter emits each input as an MQL input with Pine's title as the trailing comment. A string input with options becomes an enum dropdown whose labels are the Pine option strings, and the script's string variable is set from it on each bar. input.source with a default of close, open, high, low, hl2, hlc3 or ohlc4 becomes an ENUM_APPLIED_PRICE input; Stratlate's runtime computes PRICE_WEIGHTED as ohlc4, so the default matches the Pine script even though MetaTrader's label says "Weighted". Any other source default, such as hlcc4 or a computed series, is not editable in MetaTrader and is reported as an approximation. Constant minval/maxval limits become OnInit checks, an input the script assigns to is split into an input and a working copy, and input.color(color.new(c, 80)) keeps the base colour. As always, compile the result and backtest it before trading with it.

Pitfalls

  • Assigning to an input. Symptom: "constant cannot be modified" in MetaEditor.
  • Treating PRICE_WEIGHTED as ohlc4 without computing it yourself. Built-in indicators such as iMA use MetaTrader's definition. Symptom: values that differ slightly from TradingView on every bar.
  • A Pine timeframe with no MetaTrader period. Symptom: a default that cannot be expressed, or a silent fall-back to the chart timeframe.
  • Milliseconds in a datetime input. Pine's timestamp() returns milliseconds; a datetime counts seconds. Symptom: an invalid date far in the future.
  • Missing range checks. Symptom: "array out of range" in the Experts log after a user types 0 or a very large length.
  • Hard-coded symbol names. A broker may call it EURUSD.m or XAUUSD instead of TradingView's ticker. Symptom: empty data from iClose and friends.

FAQ

How do I make a dropdown input in MQL5?

Declare an enum and an input of that enum type. MetaTrader shows each member's trailing comment as its label, so write the Pine option text in the comment.

What replaces input.source in MQL5?

An input of type ENUM_APPLIED_PRICE for the standard prices. MetaTrader has no ohlc4 member, and its PRICE_WEIGHTED is hlcc4, so compute the chosen price yourself if you need Pine's exact definitions.

Why does MetaEditor say constant cannot be modified?

MQL5 inputs are read-only. Pine lets a script reassign an input variable; in MQL5 copy the input into an ordinary global variable and change the copy.

Keep the settings when you convert

The converter carries titles, dropdowns, price sources and limits into the MetaTrader Inputs tab, and reports any input it could not make editable.

Open the converter

Related guides