Predefined and Custom Functions in MQL4

In MQL4, functions play a pivotal role in structuring and organizing code. Functions allow you to group code into reusable blocks, making your programs cleaner, more efficient, and easier to debug. There are two main types of functions in MQL4: predefined and custom.

Predefined Functions

These are built-in functions provided by MQL4, designed to perform specific tasks. They are ready to use without needing any further definition. For instance:

There are many predefined functions in MQL4, each tailored to specific trading-related tasks.

Hint: You can call upon any indicator that is listed in your indicators folder, using the "i" followed by the start of the indicators name, in uppercase letters.

Here are some examples:

Each indicator is defined by a specific set of parameters tailored to its unique function and characteristics.

When you commence inputting these parameters into your Expert Advisor (EA), the MetaEditor will facilitate the process by offering suggestions for each segment.

For instance, in the aforementioned examples, 'NULL' and '0' are frequently utilized as the initial parameters. These represent the "Symbol" and "Chart Timeframe" respectively. If you aim to restrict the use to the EURUSD symbol exclusively, it's feasible. However, for versatility across various symbols, it's advisable to employ 'NULL', enabling the EA to operate across any chart and symbol.

The "Chart Timeframe" parameter is invaluable when designing an EA to evaluate multiple timeframes for potential signals. To target specific timeframes, you can use '1' for 1-minute, '5' for 5 minutes, '15' for 15 minutes, '30' for 30 minutes, '60' for 1 hour, '240' for 4 hours, and '1440' for a daily chart (You can also use PERIOD_M1, PERIOD_M5, PERIOD_H1 and so on). If the intent is for the indicator to adapt to any open chart, simply input '0', as demonstrated in the provided examples.

Detailed information on each indicator can be accessed at the MQL5 INDICATORS PAGE.

Some Extra Predefined Functions and Arrays

Close[1]: Represents the closing price of the previous bar (1 bar ago) on the chart. Similarly, Close[2] would refer to the closing price two bars ago, and so forth.

Open[2]: Refers to the opening price two bars ago. You can use this array to access the opening price of any historical bar by specifying its index, where Open[0] denotes the current bar's opening price.

High[1]: Points to the highest price of the previous bar. Using different indices with the High[] array, you can access the highest price of any past bar on the chart.

iClose(Symbol(), TimeFrame, shift): A function that retrieves the closing price for a specified symbol and timeframe. For instance, iClose("EURUSD", PERIOD_H1, 1) would return the closing price of the last completed 1-hour bar for EURUSD.

iHigh(Symbol(), TimeFrame, shift): This function provides the highest price for a given symbol and timeframe. An example could be iHigh("EURUSD", PERIOD_H4, 2), which fetches the highest price of the second to last 4-hour bar for EURUSD.

iHighest(Symbol(), TimeFrame, type, count, start): Finds the bar with the highest value (of the type specified) over a particular count of bars. For example, iHighest("EURUSD", PERIOD_D1, MODE_HIGH, 10, 0) would identify the bar with the highest high value over the last 10 daily bars for EURUSD.

These predefined functions and arrays play a vital role in analyzing price data and implementing trading logic. Familiarity with them can significantly streamline the coding process and improve the efficiency of an Expert Advisor.

Custom Functions

Custom functions are bespoke routines you create to meet specific requirements. These are instrumental in streamlining repetitive tasks by encapsulating distinct code blocks, which can then be invoked seamlessly across your program. An example of a custom function declaration is as follows:

double CalculateProfit(double openPrice, double closePrice) {
    return closePrice - openPrice;


This custom function, CalculateProfit, takes two parameters, the opening and closing prices, and returns their difference, representing the profit (or loss if negative). It would be declared in your global variables section of your E, before the Ontick function but after any input or exern functions.

Calling Functions

To use a function, such as the above (CalculateProfit), you call it by its name(Within the Ontick function) followed by its parameters in parentheses:

double profit = CalculateProfit(1.1000, 1.1050);
Print(profit);  // Outputs: 0.0050


Of course in the above example you probably have something like:

double MYOPENPRICE=iOpen(NULL,0,1);

This would give you:

double profit = CalculateProfit(MYOPENPRICE, MYCLOSEPRICE);
Print(profit);  // Outputs: "SOME NUMBER HERE"

Whether predefined or custom, functions provide a way to modularize and reuse code effectively.


Functions, whether predefined or custom, are essential tools in a programmer's toolkit. They allow for code organization, modularity, and reuse, enhancing the efficiency and clarity of your trading algorithms in MQL4.

NEXT UP: Event Driven Programming in MQL4