Articles with MQL5 programming examples

icon

Access a huge collection of articles with code examples showing how to create indicators and trading robots for the MetaTrader platform in the MQL5 language. Source codes are attached to the articles, so you can open them in MetaEditor and run them to see how the applications work.

These articles will be useful both for those who have just started exploring automated trading and for professional traders with programming experience. They feature not only examples, but also contain new ideas.

Add a new article
latest | best
Using Layouts and Containers for GUI Controls: The CGrid Class
Using Layouts and Containers for GUI Controls: The CGrid Class

Using Layouts and Containers for GUI Controls: The CGrid Class

This article presents an alternative method of GUI creation based on layouts and containers, using one layout manager — the CGrid class. The CGrid class is an auxiliary control that acts as a container for other containers and controls using a grid layout.
Drawing Dial Gauges Using the CCanvas Class
Drawing Dial Gauges Using the CCanvas Class

Drawing Dial Gauges Using the CCanvas Class

We can find dial gauges in cars and airplanes, in industrial production and everyday life. They are used in all spheres which require quick response to behavior of a controlled value. This article describes the library of dial gauges for MetaTrader 5.
Using Layouts and Containers for GUI Controls: The CBox Class
Using Layouts and Containers for GUI Controls: The CBox Class

Using Layouts and Containers for GUI Controls: The CBox Class

This article presents an alternative method of GUI creation based on layouts and containers, using one layout manager — the CBox class. The CBox class is an auxiliary control that acts as a container for essential controls in a GUI panel. It can make designing graphical panels easier, and in some cases, reduce coding time.
MQL5 Cookbook: Implementing an Associative Array or a Dictionary for Quick Data Access
MQL5 Cookbook: Implementing an Associative Array or a Dictionary for Quick Data Access

MQL5 Cookbook: Implementing an Associative Array or a Dictionary for Quick Data Access

This article describes a special algorithm allowing to gain access to elements by their unique keys. Any base data type can be used as a key. For example it may be represented as a string or an integer variable. Such data container is commonly referred to as a dictionary or an associative array. It provides easier and more efficient way of problem solving.
Studying the CCanvas Class. How to Draw Transparent Objects
Studying the CCanvas Class. How to Draw Transparent Objects

Studying the CCanvas Class. How to Draw Transparent Objects

Do you need more than awkward graphics of moving averages? Do you want to draw something more beautiful than a simple filled rectangle in your terminal? Attractive graphics can be drawn in the terminal. This can be implemented through the CСanvas class, which is used for creating custom graphics. With this class you can implement transparency, blend colors and produce the illusion of transparency by means of overlapping and blending colors.
Optimization. A Few Simple Ideas
Optimization. A Few Simple Ideas

Optimization. A Few Simple Ideas

The optimization process can require significant resources of your computer or even of the MQL5 Cloud Network test agents. This article comprises some simple ideas that I use for work facilitation and improvement of the MetaTrader 5 Strategy Tester. I got these ideas from the documentation, forum and articles.
MQL5 Cookbook: ОСО Orders
MQL5 Cookbook: ОСО Orders

MQL5 Cookbook: ОСО Orders

Any trader's trading activity involves various mechanisms and interrelationships including relations among orders. This article suggests a solution of OCO orders processing. Standard library classes are extensively involved, as well as new data types are created herein.
Programming EA's Modes Using Object-Oriented Approach
Programming EA's Modes Using Object-Oriented Approach

Programming EA's Modes Using Object-Oriented Approach

This article explains the idea of multi-mode trading robot programming in MQL5. Every mode is implemented with the object-oriented approach. Instances of both mode classes hierarchy and classes for testing are provided. Multi-mode programming of trading robots is supposed to take into account all peculiarities of every operational mode of an EA written in MQL5. Functions and enumeration are created for identifying the mode.
MQL5 Wizard: Placing Orders, Stop-Losses and Take Profits on Calculated Prices. Standard Library Extension
MQL5 Wizard: Placing Orders, Stop-Losses and Take Profits on Calculated Prices. Standard Library Extension

MQL5 Wizard: Placing Orders, Stop-Losses and Take Profits on Calculated Prices. Standard Library Extension

This article describes the MQL5 Standard Library extension, which allows to create Expert Advisors, place orders, Stop Losses and Take Profits using the MQL5 Wizard by the prices received from included modules. This approach does not apply any additional restrictions on the number of modules and does not cause conflicts in their joint work.
MQL5 Programming Basics: Global Variables of the Terminal
MQL5 Programming Basics: Global Variables of the Terminal

MQL5 Programming Basics: Global Variables of the Terminal

This article highlights object-oriented capabilities of the MQL5 language for creating objects facilitating work with global variables of the terminal. As a practical example I consider a case when global variables are used as control points for implementation of program stages.
Why Virtual Hosting On The MetaTrader 4 And MetaTrader 5 Is Better Than Usual VPS
Why Virtual Hosting On The MetaTrader 4 And MetaTrader 5 Is Better Than Usual VPS

Why Virtual Hosting On The MetaTrader 4 And MetaTrader 5 Is Better Than Usual VPS

The Virtual Hosting Cloud network was developed specially for MetaTrader 4 and MetaTrader 5 and has all the advantages of a native solution. Get the benefit of our free 24 hours offer - test out a virtual server right now.
MQL5 Cookbook: Handling BookEvent
MQL5 Cookbook: Handling BookEvent

MQL5 Cookbook: Handling BookEvent

This article considers BookEvent - a Depth of Market event, and the principle of its processing. An MQL program, handling states of Depth of Market, serves as an example. It is written using the object-oriented approach. Results of handling are displayed on the screen as a panel and Depth of Market levels.
MQL5 Cookbook: Handling Custom Chart Events
MQL5 Cookbook: Handling Custom Chart Events

MQL5 Cookbook: Handling Custom Chart Events

This article considers aspects of design and development of custom chart events system in the MQL5 environment. An example of an approach to the events classification can also be found here, as well as a program code for a class of events and a class of custom events handler.
MQL5 Cookbook: Handling Typical Chart Events
MQL5 Cookbook: Handling Typical Chart Events

MQL5 Cookbook: Handling Typical Chart Events

This article considers typical chart events and includes examples of their processing. We will focus on mouse events, keystrokes, creation/modification/removal of a graphical object, mouse click on a chart and on a graphical object, moving a graphical object with a mouse, finish editing of text in a text field, as well as on chart modification events. A sample of an MQL5 program is provided for each type of event considered.
MQL5 Cookbook: Processing of the TradeTransaction Event
MQL5 Cookbook: Processing of the TradeTransaction Event

MQL5 Cookbook: Processing of the TradeTransaction Event

This article considers capabilities of the MQL5 language from the point of view of the event-driven programming. The greatest advantage of this approach is that the program can receive information about phased implementation of a trade operation. The article also contains an example of receiving and processing information about ongoing trade operation using the TradeTransaction event handler. In my opinion, such an approach can be used for copying deals from one terminal to another.
How to Prepare a Trading Account for Migration to Virtual Hosting
How to Prepare a Trading Account for Migration to Virtual Hosting

How to Prepare a Trading Account for Migration to Virtual Hosting

MetaTrader client terminal is perfect for automating trading strategies. It has all tools necessary for trading robot developers ‒ powerful C++ based MQL4/MQL5 programming language, convenient MetaEditor development environment and multi-threaded strategy tester that supports distributed computing in MQL5 Cloud Network. In this article, you will find out how to move your client terminal to the virtual environment with all custom elements.
Indicator for Constructing a Three Line Break Chart
Indicator for Constructing a Three Line Break Chart

Indicator for Constructing a Three Line Break Chart

This article is dedicated to the Three Line Break chart, suggested by Steve Nison in his book "Beyond Candlesticks". The greatest advantage of this chart is that it allows filtering minor fluctuations of a price in relation to the previous movement. We are going to discuss the principle of the chart construction, the code of the indicator and some examples of trading strategies based on it.
MQL5 Cookbook - Multi-Currency Expert Advisor and Working with Pending Orders in MQL5
MQL5 Cookbook - Multi-Currency Expert Advisor and Working with Pending Orders in MQL5

MQL5 Cookbook - Multi-Currency Expert Advisor and Working with Pending Orders in MQL5

This time we are going to create a multi-currency Expert Advisor with a trading algorithm based on work with the pending orders Buy Stop and Sell Stop. This article considers the following matters: trading in a specified time range, placing/modifying/deleting pending orders, checking if the last position was closed at Take Profit or Stop Loss and control of the deals history for each symbol.
Tips for an Effective Product Presentation on the Market
Tips for an Effective Product Presentation on the Market

Tips for an Effective Product Presentation on the Market

Selling programs to traders effectively does not only require writing an efficient and useful product and then publishing it on the Market. It is vital to provide a comprehensive, detailed description and good illustrations. A quality logo and correct screenshots are equally as important as the "true coding". Bear in mind a simple formula: no downloads = no sales.
Indicator for Renko charting
Indicator for Renko charting

Indicator for Renko charting

The article describes an example of Renko charting and its implementation in MQL5 as an indicator. Modifications of this indicator distinguish it from a classic chart. It can be constructed both in the indicator window and on the main chart. Moreover, there is the ZigZag indicator. You can find a few examples of the chart implementation.
MQL5 Cookbook: Development of a Multi-Symbol Indicator to Analyze Price Divergence
MQL5 Cookbook: Development of a Multi-Symbol Indicator to Analyze Price Divergence

MQL5 Cookbook: Development of a Multi-Symbol Indicator to Analyze Price Divergence

In this article, we will consider the development of a multi-symbol indicator to analyze price divergence in a specified period of time. The core topics have been already discussed in the previous article on the programming of multi-currency indicators "MQL5 Cookbook: Developing a Multi-Symbol Volatility Indicator in MQL5". So this time we will dwell only on those new features and functions that have been changed dramatically. If you are new to the programming of multi-currency indicators, I recommend you to first read the previous article.
SQL and MQL5: Working with SQLite Database
SQL and MQL5: Working with SQLite Database

SQL and MQL5: Working with SQLite Database

This article is intended for developers who would be interested in using SQL in their projects. It explains the functionality and advantages of SQLite. The article does not require special knowledge of SQLite functions, yet minimum understanding of SQL would be beneficial.
MQL5 Programming Basics: Lists
MQL5 Programming Basics: Lists

MQL5 Programming Basics: Lists

The new version of the programming language for trading strategy development, MQL [MQL5], provides more powerful and effective features as compared with the previous version [MQL4]. The advantage essentially lies in the object-oriented programming features. This article looks into the possibility of using complex custom data types, such as nodes and lists. It also provides an example of using lists in practical programming in MQL5.
MQL5 Cookbook: Developing a Multi-Symbol Volatility Indicator in MQL5
MQL5 Cookbook: Developing a Multi-Symbol Volatility Indicator in MQL5

MQL5 Cookbook: Developing a Multi-Symbol Volatility Indicator in MQL5

In this article, we will consider the development of a multi-symbol volatility indicator. The development of multi-symbol indicators may present some difficulties for novice MQL5 developers which this article helps to clarify. The major issues arising in the course of development of a multi-symbol indicator have to do with the synchronization of other symbols' data with respect to the current symbol, the lack of some indicator data and the identification of the beginning of 'true' bars of a given time frame. All of these issues will be closely considered in the article.
Indicator for Kagi Charting
Indicator for Kagi Charting

Indicator for Kagi Charting

The article proposes Kagi chart indicator with various charting options and additional functions. Also, indicator charting principle and its MQL5 implementation features are considered. The most popular cases of its implementation in trading are displayed - Yin/Yang exchange strategy, pushing away from the trend line and consistently increasing "shoulders"/decreasing "waists".
Creating a Multi-Currency Multi-System Expert Advisor
Creating a Multi-Currency Multi-System Expert Advisor

Creating a Multi-Currency Multi-System Expert Advisor

The article introduces a structure for an Expert Advisor that trades multiple symbols and uses several trading systems simultaneously. If you already identified the optimal input parameters for all your EAs and got good backtesting results for each of them separately, ask yourself what results you would get if testing all EAs simultaneously, with all your strategies put together.
MQL5 Cookbook: Indicator Subwindow Controls - Scrollbar
MQL5 Cookbook: Indicator Subwindow Controls - Scrollbar

MQL5 Cookbook: Indicator Subwindow Controls - Scrollbar

Let's continue exploring various controls and this time turn our attention to scrollbar. Just like in the previous article entitled "MQL5 Cookbook: Indicator Subwindow Controls - Buttons", all operations will be performed in the indicator subwindow. Take a moment to read the above mentioned article as it provides a detailed description of working with events in the OnChartEvent() function, while this point will only be casually touched upon in this article. For illustrative purposes, this time around we will create a vertical scrollbar for a large list of all financial instrument properties that can be obtained using MQL5 resources.
MQL5 Cookbook: Indicator Subwindow Controls - Buttons
MQL5 Cookbook: Indicator Subwindow Controls - Buttons

MQL5 Cookbook: Indicator Subwindow Controls - Buttons

In this article, we will consider an example of developing a user interface with button controls. To convey the idea of interactivity to the user, buttons will change their colors when the cursor hovers over them. With the cursor being over a button, the button color will be slightly darkened, getting significantly darker when the button is clicked. Furthermore, we will add tooltips to each button, thus creating an intuitive interface.
MQL5 Cookbook: Monitoring Multiple Time Frames in a Single Window
MQL5 Cookbook: Monitoring Multiple Time Frames in a Single Window

MQL5 Cookbook: Monitoring Multiple Time Frames in a Single Window

There are 21 time frames available in MetaTrader 5 for analysis. You can take advantage of special chart objects that you can place on the existing chart and set the symbol, time frame and some other properties right there. This article will provide detailed information on such chart graphical objects: we will create an indicator with controls (buttons) that will allow us to set multiple chart objects in a subwindow at the same time. Furthermore, chart objects will accurately fit in the subwindow and will be automatically adjusted when the main chart or terminal window is resized.
MQL5 Cookbook: Sound Notifications for MetaTrader 5 Trade Events
MQL5 Cookbook: Sound Notifications for MetaTrader 5 Trade Events

MQL5 Cookbook: Sound Notifications for MetaTrader 5 Trade Events

In this article, we will consider such issues as including sound files in the file of the Expert Advisor, and thus adding sound notifications to trade events. The fact that the files will be included means that the sound files will be located inside the Expert Advisor. So when giving the compiled version of the Expert Advisor (*.ex5) to another user, you will not have to also provide the sound files and explain where they need to be saved.
Marvel Your MQL5 Customers with a Usable Cocktail of Technologies!
Marvel Your MQL5 Customers with a Usable Cocktail of Technologies!

Marvel Your MQL5 Customers with a Usable Cocktail of Technologies!

MQL5 provides programmers with a very complete set of functions and object-oriented API thanks to which they can do everything they want within the MetaTrader environment. However, Web Technology is an extremely versatile tool nowadays that may come to the rescue in some situations when you need to do something very specific, want to marvel your customers with something different or simply you do not have enough time to master a specific part of MT5 Standard Library. Today's exercise walks you through a practical example about how you can manage your development time at the same time as you also create an amazing tech cocktail.
MQL5 Wizard: How to Teach an EA to Open Pending Orders at Any Price
MQL5 Wizard: How to Teach an EA to Open Pending Orders at Any Price

MQL5 Wizard: How to Teach an EA to Open Pending Orders at Any Price

The article describes a method of modifying the code of a trading signal module for the implementation of the functionality allowing you to set pending orders at any distance from the current price: it may be the Close or Open price of the previous bar or the value of the moving average. There are plenty of options. Important is that you can set any opening price for a pending order. This article will be useful to traders who trade with pending orders.
MQL5 Cookbook: Reducing the Effect of Overfitting and Handling the Lack of Quotes
MQL5 Cookbook: Reducing the Effect of Overfitting and Handling the Lack of Quotes

MQL5 Cookbook: Reducing the Effect of Overfitting and Handling the Lack of Quotes

Whatever trading strategy you use, there will always be a question of what parameters to choose to ensure future profits. This article gives an example of an Expert Advisor with a possibility to optimize multiple symbol parameters at the same time. This method is intended to reduce the effect of overfitting parameters and handle situations where data from a single symbol are not enough for the study.
Extending MQL5 Standard Library and Reusing Code
Extending MQL5 Standard Library and Reusing Code

Extending MQL5 Standard Library and Reusing Code

MQL5 Standard Library makes your life as a developer easier. Nevertheless, it does not implement all the needs of all developers in the world, so if you feel that you need some more custom stuff you can take a step further and extend. This article walks you through integrating MetaQuotes' Zig-Zag technical indicator into the Standard Library. We get inspired by MetaQuotes' design philosophy to achieve our goal.
Trading Signal Generator Based on a Custom Indicator
Trading Signal Generator Based on a Custom Indicator

Trading Signal Generator Based on a Custom Indicator

How to create a trading signal generator based on a custom indicator? How to create a custom indicator? How to get access to custom indicator data? Why do we need the IS_PATTERN_USAGE(0) structure and model 0?
MQL5 Cookbook: Writing the History of Deals to a File and Creating Balance Charts for Each Symbol in Excel
MQL5 Cookbook: Writing the History of Deals to a File and Creating Balance Charts for Each Symbol in Excel

MQL5 Cookbook: Writing the History of Deals to a File and Creating Balance Charts for Each Symbol in Excel

When communicating in various forums, I often used examples of my test results displayed as screenshots of Microsoft Excel charts. I have many times been asked to explain how such charts can be created. Finally, I now have some time to explain it all in this article.
Another MQL5 OOP Class
Another MQL5 OOP Class

Another MQL5 OOP Class

This article shows you how to build an Object-Oriented Expert Advisor from scratch, from conceiving a theoretical trading idea to programming a MQL5 EA that makes that idea real in the empirical world. Learning by doing is IMHO a solid approach to succeed, so I am showing a practical example in order for you to see how you can order your ideas to finally code your Forex robots. My goal is also to invite you to adhere the OO principles.
MQL5 Cookbook: Developing a Multi-Currency Expert Advisor with Unlimited Number of Parameters
MQL5 Cookbook: Developing a Multi-Currency Expert Advisor with Unlimited Number of Parameters

MQL5 Cookbook: Developing a Multi-Currency Expert Advisor with Unlimited Number of Parameters

In this article, we will create a pattern that uses a single set of parameters for optimization of a trading system, while allowing for unlimited number of parameters. The list of symbols will be created in a standard text file (*.txt). Input parameters for each symbol will also be stored in files. This way we will be able to circumvent the restriction of the terminal on the number of input parameters of an Expert Advisor.
How to Install and Use OpenCL for Calculations
How to Install and Use OpenCL for Calculations

How to Install and Use OpenCL for Calculations

It has been over a year since MQL5 started providing native support for OpenCL. However not many users have seen the true value of using parallel computing in their Expert Advisors, indicators or scripts. This article serves to help you install and set up OpenCL on your computer so that you can try to use this technology in the MetaTrader 5 trading terminal.
MQL5 Cookbook: Multi-Currency Expert Advisor - Simple, Neat and Quick Approach
MQL5 Cookbook: Multi-Currency Expert Advisor - Simple, Neat and Quick Approach

MQL5 Cookbook: Multi-Currency Expert Advisor - Simple, Neat and Quick Approach

This article will describe an implementation of a simple approach suitable for a multi-currency Expert Advisor. This means that you will be able to set up the Expert Advisor for testing/trading under identical conditions but with different parameters for each symbol. As an example, we will create a pattern for two symbols but in such a way so as to be able to add additional symbols, if necessary, by making small changes to the code.