input.string options in MQL5: enum inputs for drop-downs
Short answer
Declare an enum with one member per option and an input of that enum type; MetaTrader shows it as a drop-down, and the comment after each member is the text the user sees. The script then compares enum members instead of strings.
A Pine input.string() with an options list shows a drop-down instead of a text box, and scripts use it to pick a moving average type, a signal mode or a session. MQL5 builds drop-downs from enumerations: declare an enum with one member per option, declare an input of that enum type, and MetaTrader shows a list. The comment after each member is the text the user sees, so the drop-down can read exactly like the Pine one. The script then compares enum members instead of strings.
What input.string() with options computes
input.string(defval, title, options, tooltip, inline, group, confirm, display, active) returns an input string constant. With options=["SMA", "EMA", "WMA"] the dialog only offers those values, and defval must be one of them. The value is fixed for the run and is usually compared with ==, either in a ternary chain or in a switch. Without options the input is an open text box, which MQL5 handles with a plain input string. input.int and input.float accept options as well, producing a numeric drop-down.
The MQL5 equivalent
The Pine version picks the moving average from a three-item list.
//@version=6
indicator("Options demo", overlay=true)
maType = input.string("EMA", "MA type", options=["SMA", "EMA", "WMA"])
len = input.int(20, "Length")
ma = maType == "SMA" ? ta.sma(close, len) : maType == "EMA" ? ta.ema(close, len) : ta.wma(close, len)
plot(ma, "MA")In MQL5 the list is an enum; the drop-down shows the comments (SMA, EMA, WMA), and the code compares against the members. Because the choice is fixed for the run, the example resolves it once in OnInit and creates a single iMA handle, rather than computing all three averages on every bar as the Pine ternary does.
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_label1 "MA"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_width1 2
// input.string(..., options=["SMA", "EMA", "WMA"]) becomes an enum; the comment
// after each member is the text MetaTrader shows in the drop-down
enum ENUM_MA_KIND
{
MA_SMA, // SMA
MA_EMA, // EMA
MA_WMA // WMA
};
input ENUM_MA_KIND InpType = MA_EMA; // MA type
input int InpLength = 20; // Length
double BufMa[];
int hMa;
int OnInit()
{
SetIndexBuffer(0, BufMa, INDICATOR_DATA);
ENUM_MA_METHOD method = InpType == MA_SMA ? MODE_SMA
: InpType == MA_EMA ? MODE_EMA : MODE_LWMA;
hMa = iMA(_Symbol, _Period, InpLength, 0, method, PRICE_CLOSE);
// the label, when you need it as text: EnumToString gives "MA_EMA"
IndicatorSetString(INDICATOR_SHORTNAME, "MA(" + EnumToString(InpType) + ", "
+ IntegerToString(InpLength) + ")");
return hMa == 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[])
{
if(BarsCalculated(hMa) < rates_total)
return 0;
int count = prev_calculated > 0 ? rates_total - prev_calculated + 1 : rates_total;
if(CopyBuffer(hMa, 0, 0, count, BufMa) < 0)
return 0;
return rates_total;
}When the original script uses the selected text itself (in a label, an alert message or a string comparison spread through the code), keep a parallel array of labels, string Labels[] = {"SMA", "EMA", "WMA"};, and read Labels[(int)InpType]. Enum members are numbered from 0 in declaration order unless you assign values, so the cast gives the index. EnumToString() returns the member's identifier (MA_EMA), not its comment. Numeric options such as input.int(14, options=[7, 14, 21]) become an enum with explicit values: enum ENUM_LEN { LEN_7 = 7, LEN_14 = 14, LEN_21 = 21 };, and (int)InpLen is the number.
Where the two differ
| Topic | Pine Script | MQL5 |
|---|---|---|
| Value type | string | An enum member (an integer underneath) |
| Comparison | maType == "EMA" | InpType == MA_EMA; a typo is a compile error, not a silently false test |
| Display text | The option string itself | The member's trailing comment; without one, the identifier |
| Text at run time | The value is the text | Parallel label array, or EnumToString for the identifier |
| Default | defval, must be in the list | The initialiser, must be a member |
| Characters | Any text, spaces included | Identifiers are letters, digits and underscores; the comment can be anything |
| Optimisation | None | The Strategy Tester can iterate over all members |
Passing through iCustom | Not applicable | Pass the member or its integer value in position |
What the Stratlate converter emits
An input.string with options becomes a generated enum named ENUM_<NAME>_OPTS whose members are <name>_opt0, <name>_opt1 and so on, each with the Pine option text as its trailing comment, so the MetaTrader drop-down shows the same labels. The input is declared as input ENUM_..._OPTS <name>_Input with the default member chosen from defval, and a label array <name>_Labels[] holds the option strings. The original string variable is kept and set from <name>_Labels[(int)<name>_Input] in OnInit and at the top of each bar, which keeps every maType == "EMA" comparison in the script working unchanged. tooltip and group are dropped and noted as PM221. The sample converts CLEAN; compile it and switch the drop-down to confirm each option.
Pitfalls
- Leaving off the member comments. The user sees
MA_SMAinstead of "SMA", and options with spaces in Pine ("Above zero") become unreadable identifiers. - Reordering enum members after an EA reads the indicator with
iCustom. The EA passes an integer, and a new order maps it to a different option. - Using
EnumToStringto rebuild the Pine text for an alert. It returns the identifier, not the comment; keep a label array. - Translating the option list to an open
input string. It compiles, but the user can type "ema" or "EMA " and the comparisons quietly fall through to the last branch. - Giving two enums the same member name. Members share the global namespace in MQL5, so
MODE_FASTin two enums is a redefinition error; prefix them.
FAQ
Can an MQL5 enum input have spaces in its labels?
Yes. The identifier cannot, but the comment after the member is shown verbatim, so MODE_ABOVE, // Above zero appears as "Above zero" in the drop-down.
How do I use the selected option as text?
Declare string Labels[] = {"SMA", "EMA", "WMA"}; in the same order as the enum and read Labels[(int)InpType]. This is also what the converter does, so converted alert messages that include the option text still read the same.
Should I use a switch or a ternary on the enum?
Either works. A switch over the enum is clearer when there are more than two options and lets you create only the handle you need in OnInit, which is cheaper than computing every alternative per bar.
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