Quick Answer
The MQL5 error 4014 (ERR_FUNCTION_NOT_ALLOWED, shown on the forum as "System function is not allowed to call") means that your program called a function that its program type is not allowed to use. The case that appears most in forum threads is the same one: a trading function such as OrderSend() or OrderCalcMargin() inside a custom indicator. A forum moderator answered one poster in one line: you can't call trading related functions in an indicator.
The fix is to move the trading code into an Expert Advisor or a script. A second thread shows that putting the call in OnTimer() does not help: the poster's OrderSend and even MessageBox returned 4014 from the timer, because the code was still in an indicator. A third thread shows a different trap: a 4014 that you read with GetLastError() without ResetLastError() first may be a stale value from earlier. This post shows a broken indicator, the EA version, a safe way to read the error, and the MQL4 versus MQL5 mix-up.
From the author: I convert indicators and strategies between platforms. The 4014 case that I meet most is an indicator that "also trades" or "also checks the margin". The cleanest solution is a split: the indicator calculates and plots, the EA reads it and trades.
Key Facts
| What you see | Cause reported in the forum | What to do |
|---|---|---|
| OrderCalcMargin() returns false, error 4014, in an indicator's OnInit() | Trading related functions can not be called in an indicator (moderator reply) | Move the call to an EA or script |
| OrderSend() in OnTimer() of an indicator gives 4014 | The timer is not the cause, the program type is | Move the logic to an EA |
| MessageBox() in the same timer also gives 4014 | Indicators can not do blocking calls (moderator reply) | Avoid blocking calls in indicators |
| 4014 after PlotIndexSetInteger() calls | The value was probably stale, and the file was an MQL4 file with MQL5 functions | Use ResetLastError() first, and use an .mq5 file |
| The same code works in an EA | The EA is allowed to trade | Keep the trading code in the EA |
The messages and answers in the table come from threads on the MQL5 forum, not from the reference page for error codes, which I could not check.
A Broken Indicator
This indicator tries to check the margin of a one-lot buy when it starts:
#property indicator_chart_window
#property indicator_plots 0
int OnInit()
{
double margin = 0.0;
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
if(!OrderCalcMargin(ORDER_TYPE_BUY, _Symbol, 1.0, ask, margin))
Print("OrderCalcMargin failed, error ", GetLastError()); // 4014 in the forum thread
return(INIT_SUCCEEDED);
}
int OnCalculate(const int rates_total, const int prev_calculated,
const int begin, const double &price[])
{
return(rates_total);
}
In the forum thread that this follows, the poster's indicator printed the error 4014 and no margin. I have not compiled this exact code, so check the output in your terminal.
The Fix: Trade From an EA or Script
The same check works in an Expert Advisor:
int OnInit()
{
double margin = 0.0;
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
ResetLastError();
if(OrderCalcMargin(ORDER_TYPE_BUY, _Symbol, 1.0, ask, margin))
Print("Margin for 1.0 lot: ", margin);
else
Print("OrderCalcMargin failed, error ", GetLastError());
return(INIT_SUCCEEDED);
}
void OnTick()
{
}
If you need the indicator's data in the EA, create the indicator handle in the EA's OnInit() and read it with CopyBuffer() in OnTick(). See MQL5 CopyBuffer guide for the full pattern, and convert an MT5 indicator to an EA for the case where you want one program.
OnTimer Does Not Change the Program Type
One poster asked whether OrderSend() can be used in OnTimer(). He wanted to watch for a file and send an order when it appeared. The call returned 4014, and so did MessageBox(). The answers were:
- One user said that
OrderSend()is not allowed in an indicator, and that no other trading function is either. - A moderator pointed to the documentation on how programs run.
- A second moderator added that indicators can not do blocking calls, such as
Sleep, server calls and web requests.
The result: the event handler does not matter, the program type does. Move the same OnTimer() code to an EA (set the timer in OnInit() with EventSetTimer()):
int OnInit()
{
EventSetTimer(1);
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
EventKillTimer();
}
void OnTimer()
{
Print("Timer in an EA: trading functions are allowed to be called here");
}
void OnTick()
{
}
Whether an order is actually accepted then depends on the usual conditions: the terminal's Algo Trading button, the account and the symbol. See Algo trading disabled, error 10027 and OrderSend failed, error 4756 for the errors that come next.
Read the Error Correctly: ResetLastError First
One thread has a 4014 that was not caused by the call that the poster blamed. A moderator wrote that you must not assume that the error variable is yours if your execution path is coming from the terminal, and that you should always wrap your own calls with ResetLastError() and GetLastError(). The pattern:
ResetLastError();
bool ok = SomeCall();
int err = GetLastError();
if(!ok)
PrintFormat("SomeCall failed, error %d", err);
Without ResetLastError() before the call, GetLastError() can return an old code, and you look for a problem in the wrong line.
The MQL4 and MQL5 Mix-Up
In that same thread, a reply said that the code used MQL5 functions (PlotIndexSetInteger and others) in a file named as an MQL4 indicator (.mq4), and that these functions do not exist in MQL4. A file with the wrong platform gives errors that look confusing. Check the extension and the platform of the code that you paste: .mq5 for MetaTrader 5. See MQL5 OnCalculate not found for another indicator mistake.
What Is Allowed Where (Summary From the Threads)
| Program type | Trading functions (OrderSend, OrderCalcMargin, ...) | Blocking calls (Sleep, requests) |
|---|---|---|
| Expert Advisor | Allowed | Allowed, but they delay the next event |
| Script | Allowed | Allowed |
| Custom indicator | Not allowed (error 4014) | Not allowed |
This table is a summary of the forum answers. Check the MQL5 reference on program running for the full rules.
Similar Messages
| Message | What it means | Where to look | |---|---|---| | 4014 System function is not allowed to call | The program type can not use this function | This post | | 10027 Autotrading disabled | The terminal or EA is not allowed to trade | Autotrading disabled | | 4756 OrderSend failed | The request could not be sent | OrderSend failed | | Return value should be checked | A result is ignored | OrderSend return value | | Implicit conversion from 'number' to 'string' | A number is used where text is expected | Number to string warning | | Trade retcodes | The server's answer to a request | Trade retcodes guide |
Checklist: What to Check When You See Error 4014
- Check the program type: is the code in an indicator?
- Find the trading call (
OrderSend,OrderCalcMarginand similar) and move it to an EA or script. - Do not use
Sleepor web requests in an indicator. - Do not rely on
OnTimerto change the rules. - Call
ResetLastError()before the call that you test. - Check the file extension and the platform.
- Let the EA read the indicator with
CopyBuffer(). - Test on a demo account.
Common Mistakes
- Putting
OrderSendin an indicator "to get a signal". - Moving the call to
OnTimerand expecting a change. - Reading
GetLastError()without resetting it first. - Mixing
.mq4and MQL5 functions. - Blocking calls in an indicator.
- Ignoring the result of the call.
FAQ
Related Guides
- MQL5 Algo trading disabled, error 10027: the terminal is not allowed to trade.
- MQL5 OrderSend failed, error 4756: the next error after an EA sends an order.
- MQL5 OrderSend return value should be checked: read the result.
- MQL5 implicit conversion from number to string: a warning that often points to a logic bug.
- MQL5 trade retcodes guide: the server's answers.
- MQL5 CopyBuffer guide: read an indicator from an EA.
- Convert an MT5 indicator to an EA: one program that calculates and trades.
- MQL5 OnCalculate function not found: an indicator's entry point.
- What to check in a trading bot before going live: a pre-live checklist.
Fix Compile Errors Without Guessing
If an Expert Advisor or indicator does not compile, paste the code and the error messages into the CodeFlowOS Error Fixer. It suggests a minimal fix, explains each error and shows what changed. A fix uses 1 credit, with up to 3 attempts included, and the compiler check is optional. Outputs are not guaranteed, and code that compiles can still have wrong trading logic, so always review the result. The Code Review is free during the early-bird period. CodeFlowOS also converts strategies between Pine Script, MQL5 and cTrader C#: try the converter page.
This article is educational. It does not describe trading performance and is not financial advice. Always compile and test code in MetaEditor and the Strategy Tester before using it on a live account.