Back to MetaTrader 5 MQL5 for CFD Traders
Part 1

Inputs, Parameters, and the Strategy Tester

Turn magic numbers into inputs, group them, and let the tester search for values instead of you.

Beginner7 min readBeginner → AdvancedLesson 05 / 17
Try it in MetaEditor
Grouped inputs and an enum-driven moving average

Save as MQL5/Indicators/Lesson5Inputs.mq5. Open the inputs dialog to see the groups and the dropdowns; then optimise Period in the Strategy Tester.

//+------------------------------------------------------------------+
//| Lesson 5 - Grouped inputs, enum inputs, optimisation             |
//+------------------------------------------------------------------+
#property copyright "Strategist Academy"
#property version   "1.00"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_width1  2

input group "Moving average"
input int                Period    = 50;          // Period
input ENUM_APPLIED_PRICE Applied   = PRICE_CLOSE; // Applied price

input group "Display"
input color              LineColor = clrOrange;   // Line colour

double Buffer[];

int OnInit()
{
   if(Period < 2) return(INIT_FAILED);

   SetIndexBuffer(0, Buffer, INDICATOR_DATA);
   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, Period - 1);
   PlotIndexSetInteger(0, PLOT_LINE_COLOR, 0, LineColor);
   PlotIndexSetString(0, PLOT_LABEL, "MA(" + IntegerToString(Period) + ")");
   IndicatorSetString(INDICATOR_SHORTNAME, "Lesson 5 MA(" + IntegerToString(Period) + ")");
   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[])
{
   if(rates_total < Period) return(0);

   // Pick the source array based on the enum input.
   // (Applied price handling is simplified here; built-in iMA does this properly
   //  and is covered in Part 4.)
   int start = (prev_calculated == 0) ? Period - 1 : prev_calculated - 1;
   if(start < Period - 1) start = Period - 1;

   for(int i = start; i < rates_total && !IsStopped(); i++)
   {
      double sum = 0.0;
      for(int k = 0; k < Period; k++)
         sum += close[i - k];
      Buffer[i] = sum / Period;
   }
   return(rates_total);
}
To run it
  1. 1. Open MetaEditor from MT5 (press F4), and pick the folder this program belongs in — Experts, Indicators, or Scripts.
  2. 2. Create a new file, paste this over it, then press F7 to compile. Fix anything the Errors tab reports.
  3. 3. Back in MT5, drag it onto a chart — or open the Strategy Tester if it is an Expert Advisor.

input versus const

A const is fixed at compile time. An input appears in the program's dialog box and — critically — the Strategy Tester can iterate over it. Anything you might reasonably want to tune should be an input.

Grouping keeps the dialog usable

Once an EA has twenty parameters the dialog becomes hostile. Use input group "..." to section them. It costs nothing and saves you from mis-clicking your own risk settings.

Enums as inputs

Declaring an input as an ENUM_* gives the user a dropdown instead of a free-text box. Use them for timeframes, MA methods, applied price, and order filling modes.

Optimisation is a trap as much as a tool

The tester can brute-force every combination of your inputs. It will always find a beautiful curve — on history. A parameter set that only works on the exact window you optimised over has no predictive value. If a result looks too good, shrink the optimisation window, re-test on data the optimiser never saw, and check that neighbours of the winning values are also profitable. A single sharp spike in parameter space is curve-fitting; a broad plateau is a real effect.

What to change in this lesson

Load the indicator, open its dialog, and notice the grouped inputs. Then run it in the tester with the period input set to optimise from 10 to 200 — and watch how easy it is to produce a flattering result on past data.

What you just did

Lesson 05 of 17 in MetaTrader 5 MQL5 for CFD Traders. When you have run the examples or read the section, tick it off and move to the next lesson.