Overview

Input parameters in MQL5 let you configure a program without editing its source code. By declaring variables with the input keyword, you expose them in the EA/Script settings dialog and in the Strategy Tester, making your logic flexible, testable, and ready for optimization.

Ad

What You Will Be Able to Do

  • Declare configurable settings using the input keyword.
  • Modify parameters from the EA properties and the Strategy Tester.
  • Understand differences between input, extern (legacy), and normal variables.
  • Use typed inputs (numbers, bools, enums) safely inside trading logic.

Code Example


//+------------------------------------------------------------------+
//|                                           ParametrizedEA.mq5     |
//+------------------------------------------------------------------+
#property strict
#property version   "1.00"

// ---- User-configurable inputs (visible in EA settings & Strategy Tester)
input double    Lots            = 0.10;                 // Trade lot size
input int       MAPeriod        = 20;                   // Moving Average period
input ENUM_TIMEFRAMES MaTf      = PERIOD_M5;            // MA timeframe
input ENUM_MA_METHOD MaMethod   = MODE_SMA;             // MA method
input ENUM_APPLIED_PRICE MaPrice= PRICE_CLOSE;          // Applied price
input bool      LogEveryTick    = true;                 // Verbose logging

// Example of using an enum to constrain choices
enum TradeDirection { DIR_Buy = 0, DIR_Sell = 1, DIR_None = 2 };
input TradeDirection Direction  = DIR_None;             // Planned direction

// ---- Internal state
int    maHandle = INVALID_HANDLE;

int OnInit()
  {
   // Create an MA indicator handle using current symbol, selected TF, etc.
   maHandle = iMA(_Symbol, MaTf, MAPeriod, 0, MaMethod, MaPrice);
   if(maHandle == INVALID_HANDLE)
     {
      Print("Failed to create iMA handle. Check parameters.");
      return(INIT_FAILED);
     }

   // Basic parameter sanity checks
   if(Lots <= 0.0)      { Print("Lots must be > 0");       return(INIT_PARAMETERS_INCORRECT); }
   if(MAPeriod <= 1)     { Print("MAPeriod must be > 1");  return(INIT_PARAMETERS_INCORRECT); }

   Print(StringFormat("EA initialized. Lots=%.2f, MAPeriod=%d, TF=%s, Dir=%d",
         Lots, MAPeriod, EnumToString(MaTf), Direction));
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   if(maHandle != INVALID_HANDLE) IndicatorRelease(maHandle);
   Print("EA deinitialized.");
  }

void OnTick()
  {
   // Optionally log every tick (controlled by input)
   if(LogEveryTick)
      Print("Tick at ", TimeToString(TimeCurrent(), TIME_SECONDS));

   // Read latest MA value
   double buf[];
   if(CopyBuffer(maHandle, 0, 0, 1, buf) == 1)
     {
      double ma = buf[0];
      Print(StringFormat("Latest MA(%d) on %s = %.5f", MAPeriod, EnumToString(MaTf), ma));
     }
   else
     {
      Print("CopyBuffer failed (no data yet on this timeframe?).");
     }

   // Example: use Direction input to branch logic (no live orders here)
   if(Direction == DIR_Buy)
     {
      // ... evaluate buy conditions using inputs
     }
   else if(Direction == DIR_Sell)
     {
      // ... evaluate sell conditions using inputs
     }
  }
Ad

Execution Steps

  1. Open MetaEditorFile → New → Expert Advisor (template) and name it ParametrizedEA.
  2. Paste the code above and press F7 to compile.
  3. Attach the EA to a chart in MetaTrader 5.
  4. Open the EA’s Inputs tab to change values (e.g., MAPeriod, MaTf, Direction).
  5. Observe effects in the Experts log; try different inputs and verify behavior.
  6. (Optional) Open the Strategy Tester, set input ranges, and run optimizations.

Key Point

Use input for user-facing, configurable settings; use normal variables for internal state. Typed inputs (including enums) make your EA safer and easier to test, and they integrate seamlessly with the Strategy Tester for optimization.

Ad

Next Section

→ Next: Reading the Log