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
Error Handling and Logging in MQL5
Error Handling and Logging in MQL5

Error Handling and Logging in MQL5

This article focuses on general issues linked to handling software errors. Furthermore, the logging term is brought up and the examples of logging implementation with MQL5 tools are shown.
MQL5 Cookbook - Programming moving channels
MQL5 Cookbook - Programming moving channels

MQL5 Cookbook - Programming moving channels

This article presents a method of programming the equidistant channel system. Certain details of building such channels are being considered here. Channel typification is provided, and a universal type of moving channels' method is suggested. Object-oriented programming (OOP) is used for code implementation.
Timeseries in DoEasy library (part 37): Timeseries collection - database of timeseries by symbols and periods
Timeseries in DoEasy library (part 37): Timeseries collection - database of timeseries by symbols and periods

Timeseries in DoEasy library (part 37): Timeseries collection - database of timeseries by symbols and periods

The article deals with the development of the timeseries collection of specified timeframes for all symbols used in the program. We are going to develop the timeseries collection, the methods of setting collection's timeseries parameters and the initial filling of developed timeseries with historical data.
Library for easy and quick development of MetaTrader programs (part XVII): Interactivity of library objects
Library for easy and quick development of MetaTrader programs (part XVII): Interactivity of library objects

Library for easy and quick development of MetaTrader programs (part XVII): Interactivity of library objects

In this article, we are going to finish the development of the base object of all library objects, so that any library object based on it is able to interact with a user. For example, users will be able to set the maximum acceptable size of a spread for opening a position and a price level, upon reaching which an event from a symbol object is sent to the program with the spread or price level-based signal.
Improving Panels: Adding transparency, changing background color and inheriting from CAppDialog/CWndClient
Improving Panels: Adding transparency, changing background color and inheriting from CAppDialog/CWndClient

Improving Panels: Adding transparency, changing background color and inheriting from CAppDialog/CWndClient

In this article, we continue studying the use of CAppDialog. Now we will learn how to set color for the background, borders and header of the dialog box. Also, this article provides a step-by-step description of how to add transparency for an application window when dragging it within the chart. We will consider how to create child classes of CAppDialog or CWndClient and analyze new specifics of working with controls. Finally, we will review new Projects from a new perspective.
preview
WebSockets for MetaTrader 5 — Using the Windows API

WebSockets for MetaTrader 5 — Using the Windows API

In this article, we will use the WinHttp.dll to create a WebSocket client for MetaTrader 5 programs. The client will ultimately be implemented as a class and also tested against the Deriv.com WebSocket API.
Graphical Interfaces X: Updates for Easy And Fast Library (Build 2)
Graphical Interfaces X: Updates for Easy And Fast Library (Build 2)

Graphical Interfaces X: Updates for Easy And Fast Library (Build 2)

Since the publication of the previous article in the series, Easy And Fast library has received some new features. The library structure and code have been partially optimized slightly reducing CPU load. Some recurring methods in many control classes have been moved to the CElement base class.
OOP in MQL5 by Example: Processing Warning and Error Codes
OOP in MQL5 by Example: Processing Warning and Error Codes

OOP in MQL5 by Example: Processing Warning and Error Codes

The article describes an example of creating a class for working with the trade server return codes and all the errors that occur during the MQL-program run. Read the article, and you will learn how to work with classes and objects in MQL5. At the same time, this is a convenient tool for handling errors; and you can further change this tool according to your specific needs.
Charts and diagrams in HTML
Charts and diagrams in HTML

Charts and diagrams in HTML

Today it is difficult to find a computer that does not have an installed web-browser. For a long time browsers have been evolving and improving. This article discusses the simple and safe way to create of charts and diagrams, based on the the information, obtained from MetaTrader 5 client terminal for displaying them in the browser.
Library for easy and quick development of MetaTrader programs (part XXIV): Base trading class - auto correction of invalid parameters
Library for easy and quick development of MetaTrader programs (part XXIV): Base trading class - auto correction of invalid parameters

Library for easy and quick development of MetaTrader programs (part XXIV): Base trading class - auto correction of invalid parameters

In this article, we will have a look at the handler of invalid trading order parameters and improve the trading event class. Now all trading events (both single ones and the ones occurred simultaneously within one tick) will be defined in programs correctly.
Developing graphical interfaces based on .Net Framework and C# (part 2): Additional graphical elements
Developing graphical interfaces based on .Net Framework and C# (part 2): Additional graphical elements

Developing graphical interfaces based on .Net Framework and C# (part 2): Additional graphical elements

The article is a follow-up of the previous publication "Developing graphical interfaces for Expert Advisors and indicators based on .Net Framework and C#". It introduces new graphical elements for creating graphical interfaces.
Better Programmer (Part 06): 9 habits that lead to effective coding
Better Programmer (Part 06): 9 habits that lead to effective coding

Better Programmer (Part 06): 9 habits that lead to effective coding

It's not always all about writing the code that leads to effective coding. There are certain habits that I have found in my experience that lead to effective coding. We are going to discuss some of them in detail in this article. This is a must-read article for every programmer who wants to improve their ability to write complex algorithms with less hassle.
MQL5 Cookbook - Creating a ring buffer for fast calculation of indicators in a sliding window
MQL5 Cookbook - Creating a ring buffer for fast calculation of indicators in a sliding window

MQL5 Cookbook - Creating a ring buffer for fast calculation of indicators in a sliding window

The ring buffer is the simplest and the most efficient way to arrange data when performing calculations in a sliding window. The article describes the algorithm and shows how it simplifies calculations in a sliding window and makes them more efficient.
Graphical Interfaces X: Text Edit box, Picture Slider and simple controls (build 5)
Graphical Interfaces X: Text Edit box, Picture Slider and simple controls (build 5)

Graphical Interfaces X: Text Edit box, Picture Slider and simple controls (build 5)

This article will consider new controls: Text Edit box, Picture Slider, as well as additional simple controls: Text label and Picture. The library continues to grow, and in addition to introduction of new controls, the previously created ones are also being improved.
Library for easy and quick development of MetaTrader programs (part II). Collection of historical orders and deals
Library for easy and quick development of MetaTrader programs (part II). Collection of historical orders and deals

Library for easy and quick development of MetaTrader programs (part II). Collection of historical orders and deals

In the first part, we started creating a large cross-platform library simplifying the development of programs for MetaTrader 5 and MetaTrader 4 platforms. We created the COrder abstract object which is a base object for storing data on history orders and deals, as well as on market orders and positions. Now we will develop all the necessary objects for storing account history data in collections.
Timeseries in DoEasy library (part 35): Bar object and symbol timeseries list
Timeseries in DoEasy library (part 35): Bar object and symbol timeseries list

Timeseries in DoEasy library (part 35): Bar object and symbol timeseries list

This article starts a new series about the creation of the DoEasy library for easy and fast program development. In the current article, we will implement the library functionality for accessing and working with symbol timeseries data. We are going to create the Bar object storing the main and extended timeseries bar data, and place bar objects to the timeseries list for convenient search and sorting of the objects.
Prices and Signals in DoEasy library (Part 65): Depth of Market collection and the class for working with MQL5.com Signals
Prices and Signals in DoEasy library (Part 65): Depth of Market collection and the class for working with MQL5.com Signals

Prices and Signals in DoEasy library (Part 65): Depth of Market collection and the class for working with MQL5.com Signals

In this article, I will create the collection class of Depths of Market of all symbols and start developing the functionality for working with the MQL5.com Signals service by creating the signal object class.
preview
Manual charting and trading toolkit (Part III). Optimization and new tools

Manual charting and trading toolkit (Part III). Optimization and new tools

In this article, we will further develop the idea of drawing graphical objects on charts using keyboard shortcuts. New tools have been added to the library, including a straight line plotted through arbitrary vertices, and a set of rectangles that enable the evaluation of the reversal time and level. Also, the article shows the possibility to optimize code for improved performance. The implementation example has been rewritten, allowing the use of Shortcuts alongside other trading programs. Required code knowledge level: slightly higher than a beginner.
Cross-Platform Expert Advisor: Custom Stops, Breakeven and Trailing
Cross-Platform Expert Advisor: Custom Stops, Breakeven and Trailing

Cross-Platform Expert Advisor: Custom Stops, Breakeven and Trailing

This article discusses how custom stop levels can be set up in a cross-platform expert advisor. It also discusses a closely-related method by which the evolution of a stop level over time can be defined.
preview
Using AutoIt With MQL5

Using AutoIt With MQL5

Short description. In this article we will explore scripting the MetraTrader 5 terminal by integrating MQL5 with AutoIt. In it we will cover how to automate various tasks by manipulating the terminals' user interface and also present a class that uses the AutoItX library.
Universal Expert Advisor: the Event Model and Trading Strategy Prototype (Part 2)
Universal Expert Advisor: the Event Model and Trading Strategy Prototype (Part 2)

Universal Expert Advisor: the Event Model and Trading Strategy Prototype (Part 2)

This article continues the series of publications on a universal Expert Advisor model. This part describes in detail the original event model based on centralized data processing, and considers the structure of the CStrategy base class of the engine.
Graphics in DoEasy library (Part 74): Basic graphical element powered by the CCanvas class
Graphics in DoEasy library (Part 74): Basic graphical element powered by the CCanvas class

Graphics in DoEasy library (Part 74): Basic graphical element powered by the CCanvas class

In this article, I will rework the concept of building graphical objects from the previous article and prepare the base class of all graphical objects of the library powered by the Standard Library CCanvas class.
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: How to Avoid Errors When Setting/Modifying Trade Levels
MQL5 Cookbook: How to Avoid Errors When Setting/Modifying Trade Levels

MQL5 Cookbook: How to Avoid Errors When Setting/Modifying Trade Levels

In continuation of our work on the Expert Advisor from the previous article of the series called "MQL5 Cookbook: Analyzing Position Properties in the MetaTrader 5 Strategy Tester", we will enhance it with a whole lot of useful functions, as well as improve and optimize the existing ones. The Expert Advisor will this time have external parameters that can be optimized in the MetaTrader 5 Strategy Tester and will in some ways resemble a simple trading system.
preview
Multibot in MetaTrader: Launching multiple robots from a single chart

Multibot in MetaTrader: Launching multiple robots from a single chart

In this article, I will consider a simple template for creating a universal MetaTrader robot that can be used on multiple charts while being attached to only one chart, without the need to configure each instance of the robot on each individual chart.
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.
Cross-Platform Expert Advisor: The CExpertAdvisor and CExpertAdvisors Classes
Cross-Platform Expert Advisor: The CExpertAdvisor and CExpertAdvisors Classes

Cross-Platform Expert Advisor: The CExpertAdvisor and CExpertAdvisors Classes

This article deals primarily with the classes CExpertAdvisor and CExpertAdvisors, which serve as the container for all the other components described in this article-series regarding cross-platform expert advisors.
Building interactive semi-automatic drag-and-drop Expert Advisor based on predefined risk and R/R ratio
Building interactive semi-automatic drag-and-drop Expert Advisor based on predefined risk and R/R ratio

Building interactive semi-automatic drag-and-drop Expert Advisor based on predefined risk and R/R ratio

Some traders execute all their trades automatically, and some mix automatic and manual trades based on the output of several indicators. Being a member of the latter group I needed an interactive tool to asses dynamically risk and reward price levels directly from the chart. This article will present a way to implement an interactive semi-automatic Expert Advisor with predefined equity risk and R/R ratio. The Expert Advisor risk, R/R and lot size parameters can be changed during runtime on the EA panel.
Graphical Interfaces V: The Combobox Control (Chapter 3)
Graphical Interfaces V: The Combobox Control (Chapter 3)

Graphical Interfaces V: The Combobox Control (Chapter 3)

In the first two chapters of the fifth part of the series, we developed classes for creating a scrollbar and a view list. In this chapter, we will speak about creating a class for the combobox control. This is also a compound control containing, among others, elements considered in the previous chapters of the fifth part.
preview
Multilayer perceptron and backpropagation algorithm (Part II): Implementation in Python and integration with MQL5

Multilayer perceptron and backpropagation algorithm (Part II): Implementation in Python and integration with MQL5

There is a Python package available for developing integrations with MQL, which enables a plethora of opportunities such as data exploration, creation and use of machine learning models. The built in Python integration in MQL5 enables the creation of various solutions, from simple linear regression to deep learning models. Let's take a look at how to set up and prepare a development environment and how to use use some of the machine learning libraries.
Graphical Interfaces X: Text selection in the Multiline Text box (build 13)
Graphical Interfaces X: Text selection in the Multiline Text box (build 13)

Graphical Interfaces X: Text selection in the Multiline Text box (build 13)

This article will implement the ability to select text using various key combinations and deletion of the selected text, similar to the way it is done in any other text editor. In addition, we will continue to optimize the code and prepare the classes to move on to the final process of the second stage of the library's evolution, where all controls will be rendered as separate images (canvases).
MQL5 Cookbook: Developing a Framework for a Trading System Based on the Triple Screen Strategy
MQL5 Cookbook: Developing a Framework for a Trading System Based on the Triple Screen Strategy

MQL5 Cookbook: Developing a Framework for a Trading System Based on the Triple Screen Strategy

In this article, we will develop a framework for a trading system based on the Triple Screen strategy in MQL5. The Expert Advisor will not be developed from scratch. Instead, we will simply modify the program from the previous article "MQL5 Cookbook: Using Indicators to Set Trading Conditions in Expert Advisors" which already substantially serves our purpose. So the article will also demonstrate how you can easily modify patterns of ready-made programs.
Graphical Interfaces VI: the Slider and the Dual Slider Controls (Chapter 2)
Graphical Interfaces VI: the Slider and the Dual Slider Controls (Chapter 2)

Graphical Interfaces VI: the Slider and the Dual Slider Controls (Chapter 2)

In the previous article, we have enriched our library with four controls frequently used in graphical interfaces: checkbox, edit, edit with checkbox and check combobox. The second chapter of the sixth part will be dedicated to the slider and the dual slider controls.
The Order of Object Creation and Destruction in MQL5
The Order of Object Creation and Destruction in MQL5

The Order of Object Creation and Destruction in MQL5

Every object, whether it is a custom object, a dynamic array or an array of objects, is created and deleted in MQL5-program in its particular way. Often, some objects are part of other objects, and the order of object deleting at deinitialization becomes especially important. This article provides some examples that cover the mechanisms of working with objects.
Universal Expert Advisor: CUnIndicator and Use of Pending Orders (Part 9)
Universal Expert Advisor: CUnIndicator and Use of Pending Orders (Part 9)

Universal Expert Advisor: CUnIndicator and Use of Pending Orders (Part 9)

The article describes the work with indicators through the universal CUnIndicator class. In addition, new methods of working with pending orders are considered. Please note: from this point on, the structure of the CStrategy project has undergone substantial changes. Now all its files are located in a single directory for the convenience of users.
Using MetaTrader 5 as a Signal Provider for MetaTrader 4
Using MetaTrader 5 as a Signal Provider for MetaTrader 4

Using MetaTrader 5 as a Signal Provider for MetaTrader 4

Analyse and examples of techniques how trading analysis can be performed on MetaTrader 5 platform, but executed by MetaTrader 4. Article will show you how to create simple signal provider in your MetaTrader 5, and connect to it with multiple clients, even running MetaTrader 4. Also you will find out how you can follow participants of Automated Trading Championship in your real MetaTrader 4 account.
MQL as a Markup Tool for the Graphical Interface of MQL Programs. Part 2
MQL as a Markup Tool for the Graphical Interface of MQL Programs. Part 2

MQL as a Markup Tool for the Graphical Interface of MQL Programs. Part 2

This paper continues checking the new conception to describe the window interface of MQL programs, using the structures of MQL. Automatically creating GUI based on the MQL markup provides additional functionality for caching and dynamically generating the elements and controlling the styles and new schemes for processing the events. Attached is an enhanced version of the standard library of controls.
LifeHack for Traders: Indicators of Balance, Drawdown, Load and Ticks during Testing
LifeHack for Traders: Indicators of Balance, Drawdown, Load and Ticks during Testing

LifeHack for Traders: Indicators of Balance, Drawdown, Load and Ticks during Testing

How to make the testing process more visual? The answer is simple: you need to use one or more indicators in the Strategy Tester, including a tick indicator, an indicator of balance and equity, an indicator of drawdown and deposit load. This solution will help you visually track the nature of ticks, balance and equity changes, as well as drawdown and deposit load.
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.
Graphical Interfaces II: the Menu Item Element (Chapter 1)
Graphical Interfaces II: the Menu Item Element (Chapter 1)

Graphical Interfaces II: the Menu Item Element (Chapter 1)

In the second part of the series, we will show in detail the development of such interface elements as main menu and context menu. We will also mention drawing elements and create a special class for it. We will discuss in depth such question as managing program events including custom ones.