MQL4 Program Types

Now that you have a better understanding about what MQL4 is, we can introduce the main categories of programs that you can create with this powerful language.

With MQL4, it is possible to create five types of programs, and each type is quite different from the others. Also, interesting that one type of programs can be used by the other types in different ways, meaning for example, an indicator can be used in a script or in an expert advisor. You will see this in more detail when reading about the individual types.

Here are the main types of programs that you can find and create with MQL4 coding language:

  • Include files
  • Libraries
  • Scripts
  • Indicators
  • Expert advisors

Types of MQL4 Programs

Include Files

Include files are text source files containing code that you want to use in other programs. Include files have an MQH extension and are stored in the MQL4/Include/ subfolder or in the same subfolder as the source files that are using the include file. Include files cannot run by themselves — they can only be included in other types of programs.

It is worth to expand this concept in case you are not familiar with programming. Assume that you have strict rules of risk management — all your orders will have a position size calculated following your risk management rules. What you can do is create a function to calculate your position size and save it in an include file. You can then include this file in a script or expert advisor and use the included function without having to write the code again. Basically, the advantage of include files is to keep the code clean and the ability to re-use portions of code.

Libraries

Libraries have the same purpose of include files, but they have an EX4 extension (which means that they are pre-compiled) and they are saved in the MQL4/Libraries/ subfolder. As include files, you cannot run libraries on their own, they can only be included in other types of files.

Scripts

Scripts are programs that execute a specific action only once when you attach them to a chart. The source file of a script has an MQ4 extension while a compiled executable version has an EX4 extension. Scripts are saved in the MQL4/Scripts/ subfolder.

Scripts and expert advisors are very similar. They can both execute actions, however, there is a fundamental difference between the two: scripts execute the task only once and then they exit, while expert advisors repeat the action as new events arrive.

Scripts can perform a simple action or more complex tasks, some examples can be:

  • Open an order with a default or calculated position size, stop-loss, and take-profit.
  • Create a report and save to a file.
  • Send a report through email.
  • Close all open orders immediately.
  • Calculate some values and returning the result.

Scripts can load values from default and custom indicators and can also include libraries and include files, but remember, they only run once when executed.

Indicators

Indicators are graphical representations of prices or manipulation of prices. To provide an example, think about a simple moving average — it is probably the most common and popular indicator and it represents the average price over a period of time.

Source files of indicators have an MQ4 extension while compiled executable versions for MetaTrader 4 have an EX4 extension. Indicators files are stored in the MQL4/Indicators/ subfolder.

MetaTrader 4 comes with quite a few built-in indicators but sometimes they are insufficient to satisfy your technical analysis needs. The web is full of enhanced indicators which may suit better your trading style, and if that is still not what you are looking for, with MQL4 you can create your own custom indicator.

Again, an indicator is a graphical representations of prices or manipulation of prices. You attach it to a chart and it shows you the information it is programmed to show.

Some examples of standard indicators are:

Custom indicators can be enhanced versions of existing ones or completely new ones. You can see some examples of custom indicators that we have created and provide in a free-to-use open-source form.

Examples of Standard MetaTrader 4 Indicators - MA, ATR, and Stoch

One very important difference of indicators compared to scripts and expert advisors is that indicators cannot execute trade actions. Indicators cannot open, modify, or close orders. At the same time, indicators can get information about existing or closed orders.

Expert Advisors

Expert advisors are probably the most powerful tool you can develop with MQL4. They are programs that you attach to a chart and they continue running until you manually stop them. To understand how an expert advisor works, you must understand the concept of a tick. A tick is an event when a new price quote arrives to the chart. A tick is what triggers the action of the expert advisor. Having an expert advisor running allows MetaTrader to continuously monitor prices and indicators and perform specific programmed actions when conditions are verified.

Expert advisors' source files have an MQ4 extension while executable compiled versions have an EX4 extension. The folder where these files are saved is MQL4/Experts/.

Expert advisors are known in the trading industry under different names, and even though there are minor differences in their meanings, they mostly refer to the tools used to automate trading activities. Expert advisors are also known as black boxes, trading robots, and bots.

You can find expert advisors on the web, for free and as commercial products. However, always be careful as there are many scams around. As an alternative you can create your own robot.

Expert Advisors can be very versatile. Some examples of actions that they can perform are:

  • Monitor prices and indicator values, placing or closing orders when prices or indicators reach a certain values.
  • Send alerts or notifications via email or to the screen when some conditions are met.
  • Move stop-loss or take-profit level automatically, applying trailing stop.
  • Send automatic reports.
  • Perform an action at a scheduled time.

As you can see, expert advisors can be very handy and you will see other examples and advantages of custom expert advisors as you start using them more.

You should now have a better idea about the types of programs you can find and create with MQL4, and it is probably time for you to meet MetaEditor.


If you want to get news of the most recent updates to our guides or anything else related to Forex trading, you can subscribe to our monthly newsletter.

© 2005–2021

EarnForex.com

Design — Mart Studio

Forex trading bears intrinsic risks of loss. You must understand that Forex trading, while potentially profitable, can make you lose your money. Never trade with the money that you cannot afford to lose! Trading with leverage can wipe your account even faster.

CFDs are leveraged products and as such loses may be more than the initial invested capital. Trading in CFDs carry a high level of risk thus may not be appropriate for all investors.