Quick Answer
An MQL5 indicator is driven by one event handler: OnCalculate. If MetaTrader cannot find a handler with a form it accepts, the indicator does not work, and people search for the message "OnCalculate function not found in custom indicator". The most common reason is not that the function is missing, but that its parameter list is not one of the two forms the language allows.
According to the MQL5 documentation, OnCalculate has two forms: one with rates_total, prev_calculated, begin and a single price[] array, and one with rates_total, prev_calculated and the arrays time, open, high, low, close, tick_volume, volume and spread. Only one of them can be used in a single indicator. A forum answer to exactly this problem says the same thing: a user who added a time[] parameter to the short form got a call that is "neither" of the two documented forms. The fix is to copy one form exactly and not to change its parameters.
From the author: I build and convert MQL5 code, including indicators that become Expert Advisors. When code is ported from another platform, or when a function is written from memory, one parameter is often added, dropped or given another type. The compiler may accept the file, and the terminal still does not treat the function as the indicator handler.
Key Facts
| What you see | Usual cause | Fix |
|---|---|---|
| "OnCalculate function not found" | The parameter list matches neither documented form | Copy one form exactly |
| It worked, then you added time[] to the short form | The short form has price[] only | Use the long form, or remove time[] |
| Two OnCalculate functions in one file | Only one form may be used | Keep one |
| No OnCalculate at all | The file has only OnStart or OnTick | Write OnCalculate, or make it a script or EA |
| Same message with iCustom in an EA | The called indicator has the problem | Fix the indicator, then call it |
| Indicator draws once and never updates | The return value is wrong | Return rates_total |
The Two Valid Forms
From the MQL5 documentation, the short form works on one array of prices:
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &price[])
The long form gives you the full time series:
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[])
Note the types: tick_volume and volume are long, and spread is int. The names of the parameters can be your own, but their number, order and types must stay the same.
Cause 1: A Parameter List That Is Neither Form
A typical mistake:
// Not valid: the short form plus an extra time[]
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const datetime &time[],
const double &price[])
It looks reasonable, but it is not a documented form. Replace it with one of the two forms above. If you need the time of the bars, use the long form, where time[] is given.
Other changes that break the form: a missing const, a missing & on an array, double instead of long for tick_volume, a different order, or a return type other than int.
Cause 2: Two Forms in One Indicator
The documentation says that only one form can be used in a single indicator. If you pasted an example with the short form into a file that already has the long form, remove one of them.
Cause 3: There Is No OnCalculate
A file that has OnStart is a script, a file that has OnTick is an Expert Advisor, and a file that has OnCalculate is an indicator. If you wrote indicator code with #property indicator_... lines but the handler is missing or misspelled (OnCalculat, onCalculate), the terminal cannot call it. MQL5 is case sensitive.
Cause 4: The Return Value
The documentation says that OnCalculate returns an int that is passed as prev_calculated in the next call. In the sample code, the function returns rates_total. If you return 0, the indicator recalculates everything on each call; if you return a value that is too large, bars are skipped. Return rates_total unless you have a reason not to.
A Working Indicator
This indicator draws the middle of the high and the low. It uses the long form:
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_label1 "Mid"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
double MidBuffer[];
int OnInit()
{
SetIndexBuffer(0, MidBuffer, INDICATOR_DATA);
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[])
{
int start = (prev_calculated > 0) ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
MidBuffer[i] = (high[i] + low[i]) / 2.0;
return rates_total;
}
Compile it in MetaEditor, add it to a chart on a demo account, and compare the line with the middle of the high and the low of some candles. The code assumes the arrays are indexed from the oldest bar. Check the direction with ArrayGetAsSeries if you change it, and see CopyBuffer in MQL5 for the indexing rules of buffers.
Why prev_calculated Matters
On the first call, prev_calculated is 0 and the indicator must calculate all bars. On later calls, it holds the value your function returned before, so you can start from the last bar again (prev_calculated - 1) and skip the history. A loop that always starts at 0 works on short charts and gets slow on long ones.
If an EA Calls the Indicator
When an Expert Advisor loads the indicator with iCustom, the EA can show the same complaint, because the problem is in the indicator file. Compile the indicator on its own first, attach it to a chart, and only then call it from the EA. The conversion in the other direction is described in convert an MT5 indicator to an EA.
Similar Problems
| Problem | What it means | Where to look |
|---|---|---|
| Wrong parameters count | A call with the wrong number of arguments | MQL5 wrong parameters count |
| Undeclared identifier | A name that the compiler does not know | MQL5 undeclared identifier |
| Array out of range | An index outside the array | MQL5 array out of range |
| Possible loss of data | A type conversion warning | MQL5 possible loss of data |
| cTrader indicator | The same job on cTrader: Calculate(int index) | cTrader CS0534 |
Checklist: What to Check When OnCalculate Is Not Found
- Compare the parameter list with one of the two forms, character by character.
- Check the types:
longfor the volumes,intfor the spread. - Check
constand&on every array. - Keep only one
OnCalculatein the file. - Check the spelling and the case of the name.
- Return an
int, normallyrates_total. - Set the buffers in
OnInitwithSetIndexBuffer. - Compile in MetaEditor, then test on a demo chart.
Common Mistakes
- Adding
time[]to the short form. - Mixing the two forms in one file.
- Using
intfortick_volume. - Returning
0or a wrong number. - Writing
OnTickin an indicator and expecting it to run. - Looping from 0 on each call.
FAQ
Related Guides
- Convert an MT5 indicator to an EA: move indicator logic into an Expert Advisor.
- CopyBuffer in MQL5: read indicator buffers and get the indexing right.
- MQL5 wrong parameters count: a call with the wrong number of arguments.
- MQL5 undeclared identifier: a name the compiler does not know.
- MQL5 array out of range: an index outside the array.
- MQL5 possible loss of data: a type conversion warning.
- Pine Script to MQL5 guide: converting a script step by step.
- MT5 every tick vs real ticks: how the tester feeds data to your code.
- What to check in a trading bot before going live: a pre-live checklist for stops, lot size, spread and trade results.
Fix Compile Errors Without Guessing
If an indicator or an Expert Advisor does not compile, paste the code and the message 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 logic, so always review the result. 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 before using it on a live account.