Edgoro
Compile error 199 · MQL5

MQL5 error: wrong parameters count (OrderSend, iMA)

Fix "wrong parameters count" in MetaEditor: MQL4's OrderSend() and iMA() calls in MQL5 code, and how to rewrite them with CTrade and indicator handles.

MetaEditor · Errors tab · error 199 · line 3
wrong parameters count
built-in: bool OrderSend(const MqlTradeRequest&,MqlTradeResult&)

What it means

A function was called with a number of arguments that matches none of its versions. MetaEditor stops with error 199 and, on the next line, prints the signature it expected. Older builds print 'OrderSend' - wrong parameters count. In practice it almost always means MQL4 code: OrderSend(), iMA(), iRSI() and many others exist in both languages with different parameters.

Usual causes

  • MQL4's OrderSend(). In MQL4 it takes up to 11 arguments (symbol, operation, volume, price, slippage, stop, target, comment, magic, expiration, colour). In MQL5 it takes exactly two: an MqlTradeRequest and an MqlTradeResult.
  • MQL4's indicator calls. MQL4's iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, 0) has a last argument, the bar shift, and returns a value. MQL5's iMA() has six parameters and returns a handle; values come from CopyBuffer().
  • Your own function called with an argument too many or too few after its declaration changed.

How to detect it

  • MetaEditor's Errors tab shows wrong parameters count on the line of the call, followed by an information line with the expected signature, such as built-in: bool OrderSend(const MqlTradeRequest&,MqlTradeResult&).
  • The same MQL4 line usually triggers undeclared identifier errors too, for OP_BUY, Ask and the like.

Broken code

MetaEditor reports wrong parameters count on line 3 for OrderSend, plus undeclared identifier for 'OP_BUY' and 'Ask'.

MQL5
void OnTick()
  {
   int ticket = OrderSend(Symbol(), OP_BUY, 0.1, Ask, 3, 0, 0, "EA", 1234, 0, clrGreen);
  }

Fixed code

The same program, corrected. It compiles in MetaEditor with 0 errors and 0 warnings.

MQL5
#include <Trade/Trade.mqh>

input double InpLots  = 0.10; // Volume
input ulong  InpMagic = 1234; // Magic number

CTrade trade;

int OnInit()
  {
   trade.SetExpertMagicNumber(InpMagic);   // MQL4's magic argument
   trade.SetDeviationInPoints(3);          // MQL4's slippage argument
   return INIT_SUCCEEDED;
  }

void OnTick()
  {
   if(PositionSelect(_Symbol))
      return;
   // Market buy at the current Ask, no stop or target, with a comment
   if(!trade.Buy(InpLots, _Symbol, 0.0, 0.0, 0.0, "EA"))
      PrintFormat("Buy failed: retcode %u (%s)", trade.ResultRetcode(), trade.ResultRetcodeDescription());
  }

Why the fix works

The standard library's CTrade class does what MQL4's OrderSend() did. The magic number and the slippage become settings of the CTrade object, and Buy() takes the volume, the symbol, the price (0 means the current Ask), the stop, the target and the comment. Success isn't a ticket number any more: Buy() returns true or false, and the result is in ResultRetcode() and ResultDeal().

The same error with iMA() and other indicators

In MQL5, indicator functions create the indicator once and return a handle: iMA(_Symbol, PERIOD_CURRENT, 20, 0, MODE_SMA, PRICE_CLOSE), with no shift argument. Create the handle in OnInit, check it against INVALID_HANDLE, and read values with CopyBuffer(handle, 0, shift, count, array). Passing a plain number where MQL5 expects an enumeration, such as a timeframe, gives a different error: cannot convert enum.

Questions

Why does OrderSend give wrong parameters count in MT5?

Because the code is written for MT4. MQL5's OrderSend() takes two structures, an MqlTradeRequest and an MqlTradeResult, instead of MQL4's list of up to 11 values. Rewrite the call with CTrade's Buy() and Sell(), or fill the two structures yourself.

Why does iMA give wrong parameters count in MQL5?

MQL4's iMA() has a seventh argument, the bar shift, and returns the average's value. MQL5's iMA() has six parameters and returns a handle; read the values with CopyBuffer().

Can MetaEditor compile MQL4 code for MetaTrader 5?

No. MQL4 code has to be converted to MQL5. MetaTrader 5's MetaEditor compiles .mq5 files, and MetaQuotes' list of compile errors even has one (413) that tells you to compile MQL4 programs with MetaTrader 4's MetaEditor.

The programs on this page are minimal examples written to show the error and its fix, not trading systems. Some of them send orders when they run: try them on a demo account. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.

Working code that avoids it

All MQL5 examples

Rather not write the error handling yourself?

Edgoro's MT5 EA generator writes complete MQL5 Expert Advisors from plain English and compiles each one in MetaEditor before you get it. A clean build proves the code compiles, not that a strategy works: test it on a demo account.