Articles with examples of trading robots developed in MQL5

icon

An Expert Advisor is the 'pinnacle' of programming and the desired goal of every automated trading developer. Read the articles in this section to create your own trading robot. By following the described steps you will learn how to create, debug and test automated trading systems.

The articles not only teach MQL5 programming, but also show how to implement trading ideas and techniques. You will learn how to program a trailing stop, how to apply money management, how to get the indicator values, and much more.

Add a new article
latest | best
Trading signals module using the system by Bill Williams
Trading signals module using the system by Bill Williams

Trading signals module using the system by Bill Williams

The article describes the rules of the trading system by Bill Williams, the procedure of application for a developed MQL5 module to search and mark patterns of this system on the chart, automated trading with found patterns, and also presents the results of testing on various trading instruments.
The Use of ORDER_MAGIC for Trading with Different Expert Advisors on a Single Instrument
The Use of ORDER_MAGIC for Trading with Different Expert Advisors on a Single Instrument

The Use of ORDER_MAGIC for Trading with Different Expert Advisors on a Single Instrument

This article considers the questions of information coding, using the magic-identification, as well as the division, assembly, and synchronization of automatic trading of different Expert Advisors. This article will be interesting to beginners, as well as to more experienced traders, because it tackles the question of virtual positions, which can be useful in the implementation of complex systems of synchronization of Expert Advisors and various strategies.
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.
Order Strategies. Multi-Purpose Expert Advisor
Order Strategies. Multi-Purpose Expert Advisor

Order Strategies. Multi-Purpose Expert Advisor

This article centers around strategies that actively use pending orders, a metalanguage that can be created to formally describe such strategies and the use of a multi-purpose Expert Advisor whose operation is based on those descriptions
Graphical Interfaces X: The Multiline Text box control (build 8)
Graphical Interfaces X: The Multiline Text box control (build 8)

Graphical Interfaces X: The Multiline Text box control (build 8)

The Multiline Text box control is discussed. Unlike the graphical objects of the OBJ_EDIT type, the presented version will not have restrictions on the number of input characters. It also adds the mode for turning the text box into a simple text editor, where the cursor can be moved using the mouse or keys.
EA remote control methods
EA remote control methods

EA remote control methods

The main advantage of trading robots lies in the ability to work 24 hours a day on a remote VPS server. But sometimes it is necessary to intervene in their work, while there may be no direct access to the server. Is it possible to manage EAs remotely? The article proposes one of the options for controlling EAs via external commands.
Universal Expert Advisor: Trading Modes of Strategies (Part 1)
Universal Expert Advisor: Trading Modes of Strategies (Part 1)

Universal Expert Advisor: Trading Modes of Strategies (Part 1)

Any Expert Advisor developer, regardless of programming skills, is daily confronted with the same trading tasks and algorithmic problems, which should be solved to organize a reliable trading process. The article describes the possibilities of the CStrategy trading engine that can undertake the solution of these tasks and provide a user with convenient mechanism for describing a custom trading idea.
Processing of trade events in Expert Advisor using the OnTrade() function
Processing of trade events in Expert Advisor using the OnTrade() function

Processing of trade events in Expert Advisor using the OnTrade() function

MQL5 gave a mass of innovations, including work with events of various types (timer events, trade events, custom events, etc.). Ability to handle events allows you to create completely new type of programs for automatic and semi-automatic trading. In this article we will consider trade events and write some code for the OnTrade() function, that will process the Trade event.
How to copy signals using an EA by your rules?
How to copy signals using an EA by your rules?

How to copy signals using an EA by your rules?

When you subscribe to signals, such situation may occur: your trade account has a leverage of 1:100, the provider has a leverage of 1:500 and trades using the minimal lot, and your trade balances are virtually equal — but the copy ratio will comprise only 10% to 15%. This article describes how to increase the copy rate in such cases.
A Virtual Order Manager to track orders within the position-centric MetaTrader 5 environment
A Virtual Order Manager to track orders within the position-centric MetaTrader 5 environment

A Virtual Order Manager to track orders within the position-centric MetaTrader 5 environment

This class library can be added to an MetaTrader 5 Expert Advisor to enable it to be written with an order-centric approach broadly similar to MetaTrader 4, in comparison to the position-based approach of MetaTrader 5. It does this by keeping track of virtual orders at the MetaTrader 5 client terminal, while maintaining a protective broker stop for each position for disaster protection.
Applying Monte Carlo method in reinforcement learning
Applying Monte Carlo method in reinforcement learning

Applying Monte Carlo method in reinforcement learning

In the article, we will apply Reinforcement learning to develop self-learning Expert Advisors. In the previous article, we considered the Random Decision Forest algorithm and wrote a simple self-learning EA based on Reinforcement learning. The main advantages of such an approach (trading algorithm development simplicity and high "training" speed) were outlined. Reinforcement learning (RL) is easily incorporated into any trading EA and speeds up its optimization.
preview
Websockets for MetaTrader 5

Websockets for MetaTrader 5

Before the introduction of the network functionality provided with the updated MQL5 API, MetaTrader programs have been limited in their ability to connect and interface with websocket based services. But of course this has all changed, in this article we will explore the implementation of a websocket library in pure MQL5. A brief description of the websocket protocol will be given along with a step by step guide on how to use the resulting library.
How to Develop an Expert Advisor using UML Tools
How to Develop an Expert Advisor using UML Tools

How to Develop an Expert Advisor using UML Tools

This article discusses creation of Expert Advisors using the UML graphical language, which is used for visual modeling of object-oriented software systems. The main advantage of this approach is the visualization of the modeling process. The article contains an example that shows modeling of the structure and properties of an Expert Advisor using the Software Ideas Modeler.
Universal Expert Advisor: Custom Strategies and Auxiliary Trade Classes (Part 3)
Universal Expert Advisor: Custom Strategies and Auxiliary Trade Classes (Part 3)

Universal Expert Advisor: Custom Strategies and Auxiliary Trade Classes (Part 3)

In this article, we will continue analyzing the algorithms of the CStrategy trading engine. The third part of the series contains the detailed analysis of examples of how to develop specific trading strategies using this approach. Special attention is paid to auxiliary algorithms — Expert Advisor logging system and data access using a conventional indexer (Close[1], Open[0] etc.)
Creating an Expert Advisor, which Trades on a Number of Instruments
Creating an Expert Advisor, which Trades on a Number of Instruments

Creating an Expert Advisor, which Trades on a Number of Instruments

The concept of diversification of assets on financial markets is quiet old, and has always attracted beginner traders. In this article, the author proposes a maximally simple approach to a construction of a multi-currency Expert Advisor, for an initial introduction to this direction of trading strategies.
Graphical Interfaces IV: the Multi-Window Mode and System of Priorities (Chapter 2)
Graphical Interfaces IV: the Multi-Window Mode and System of Priorities (Chapter 2)

Graphical Interfaces IV: the Multi-Window Mode and System of Priorities (Chapter 2)

In this chapter, we will extend the library implementation to the possibility of creating multi-window interfaces for the MQL applications. We will also develop a system of priorities for left mouse clicking on graphical objects. This is required to avoid problems when elements do not respond to the user's actions.
Cross-Platform Expert Advisor: Time Filters
Cross-Platform Expert Advisor: Time Filters

Cross-Platform Expert Advisor: Time Filters

This article discusses the implementation of various methods of time filtering a cross-platform expert advisor. The time filter classes are responsible for checking whether or not a given time falls under a certain time configuration setting.
Graphical Interfaces I: Form for Controls (Chapter 2)
Graphical Interfaces I: Form for Controls (Chapter 2)

Graphical Interfaces I: Form for Controls (Chapter 2)

In this article we will create the first and main element of the graphical interface - a form for controls. Multiple controls can be attached to this form anywhere and in any combination.
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.
Trading DiNapoli levels
Trading DiNapoli levels

Trading DiNapoli levels

The article considers one of the variants for Expert Advisor practical realization to trade DiNapoli levels using MQL5 standard tools. Its performance is tested and conclusions are made.
Universal Expert Advisor: Accessing Symbol Properties (Part 8)
Universal Expert Advisor: Accessing Symbol Properties (Part 8)

Universal Expert Advisor: Accessing Symbol Properties (Part 8)

The eighth part of the article features the description of the CSymbol class, which is a special object that provides access to any trading instrument. When used inside an Expert Advisor, the class provides a wide set of symbol properties, while allowing to simplify Expert Advisor programming and to expand its functionality.
Using indicators for optimizing Expert Advisors in real time
Using indicators for optimizing Expert Advisors in real time

Using indicators for optimizing Expert Advisors in real time

Efficiency of any trading robot depends on the correct selection of its parameters (optimization). However, parameters that are considered optimal for one time interval may not retain their effectiveness in another period of trading history. Besides, EAs showing profit during tests turn out to be loss-making in real time. The issue of continuous optimization comes to the fore here. When facing plenty of routine work, humans always look for ways to automate it. In this article, I propose a non-standard approach to solving this issue.
Optimization management (Part I): Creating a GUI
Optimization management (Part I): Creating a GUI

Optimization management (Part I): Creating a GUI

This article describes the process of creating an extension for the MetaTrader terminal. The solution discussed helps to automate the optimization process by running optimizations in other terminals. A few more articles will be written concerning this topic. The extension has been developed using the C# language and design patterns, which additionally demonstrates the ability to expand the terminal capabilities by developing custom modules, as well as the ability to create custom graphical user interfaces using the functionality of a preferred programming language.
Regular expressions for traders
Regular expressions for traders

Regular expressions for traders

A regular expression is a special language for handling texts by applying a specified rule, also called a regex or regexp for short. In this article, we are going to show how to handle a trade report with the RegularExpressions library for MQL5, and will also demonstrate the optimization results after using it.
Parallel Calculations in MetaTrader 5
Parallel Calculations in MetaTrader 5

Parallel Calculations in MetaTrader 5

Time has been a great value throughout the history of mankind, and we strive not to waste it unnecessarily. This article will tell you how to accelerate the work of your Expert Advisor if your computer has a multi-core processor. Moreover, the implementation of the proposed method does not require the knowledge of any other languages besides MQL5.
preview
Creating an EA that works automatically (Part 01): Concepts and structures

Creating an EA that works automatically (Part 01): Concepts and structures

Today we'll see how to create an Expert Advisor that simply and safely works in automatic mode.
preview
Useful and exotic techniques for automated trading

Useful and exotic techniques for automated trading

In this article I will demonstrate some very interesting and useful techniques for automated trading. Some of them may be familiar to you. I will try to cover the most interesting methods and will explain why they are worth using. Furthermore, I will show what these techniques are apt to in practice. We will create Expert Advisors and test all the described techniques using historic quotes.
Deep Neural Networks (Part VI). Ensemble of neural network classifiers: bagging
Deep Neural Networks (Part VI). Ensemble of neural network classifiers: bagging

Deep Neural Networks (Part VI). Ensemble of neural network classifiers: bagging

The article discusses the methods for building and training ensembles of neural networks with bagging structure. It also determines the peculiarities of hyperparameter optimization for individual neural network classifiers that make up the ensemble. The quality of the optimized neural network obtained in the previous article of the series is compared with the quality of the created ensemble of neural networks. Possibilities of further improving the quality of the ensemble's classification are considered.
Practical evaluation of the adaptive market following method
Practical evaluation of the adaptive market following method

Practical evaluation of the adaptive market following method

The main difference of the trading system proposed in the article is the use of mathematical tools for analyzing stock quotes. The system applies digital filtering and spectral estimation of discrete time series. The theoretical aspects of the strategy are described and a test Expert Advisor is created.
Universal Expert Advisor: Pending Orders and Hedging Support (Part 5)
Universal Expert Advisor: Pending Orders and Hedging Support (Part 5)

Universal Expert Advisor: Pending Orders and Hedging Support (Part 5)

This article provides further description of the CStrategy trading engine. By popular demand of users, we have added pending order support functions to the trading engine. Also, the latest version of the MetaTrader 5 now supports accounts with the hedging option. The same support has been added to CStrategy. The article provides a detailed description of algorithms for the use of pending orders, as well as of CStrategy operation principles on accounts with the hedging option enabled.
Graphical Interfaces I: Animating the Graphical Interface (Chapter 3)
Graphical Interfaces I: Animating the Graphical Interface (Chapter 3)

Graphical Interfaces I: Animating the Graphical Interface (Chapter 3)

In the previous article, we started developing a form class for controls. In this article, we are going to continue doing that by filling this class with methods for moving a form over the chart area. We will then integrate this interface component into the core of the library. Also, we will ensure that the color of a form control changes when the mouse cursor is hovering over it.
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: Using Indicators to Set Trading Conditions in Expert Advisors
MQL5 Cookbook: Using Indicators to Set Trading Conditions in Expert Advisors

MQL5 Cookbook: Using Indicators to Set Trading Conditions in Expert Advisors

In this article, we will continue to modify the Expert Advisor we have been working on throughout the preceding articles of the MQL5 Cookbook series. This time, the Expert Advisor will be enhanced with indicators whose values will be used to check position opening conditions. To spice it up, we will create a drop-down list in the external parameters to be able to select one out of three trading indicators.
Visualizing optimization results using a selected criterion
Visualizing optimization results using a selected criterion

Visualizing optimization results using a selected criterion

In the article, we continue to develop the MQL application for working with optimization results. This time, we will show how to form the table of the best results after optimizing the parameters by specifying another criterion via the graphical interface.
Graphical Interfaces III: Simple and Multi-Functional Buttons (Chapter 1)
Graphical Interfaces III: Simple and Multi-Functional Buttons (Chapter 1)

Graphical Interfaces III: Simple and Multi-Functional Buttons (Chapter 1)

Let us consider the button control. We will discuss examples of several classes for creating a simple button, buttons with extended functionality (icon button and split button) and interconnected buttons (button groups and radio button). Added to that, we will introduce some additions to existing classes for controls to broaden their capability.
MQL5 Wizard: How to Create a Risk and Money Management Module
MQL5 Wizard: How to Create a Risk and Money Management Module

MQL5 Wizard: How to Create a Risk and Money Management Module

The generator of trading strategies of the MQL5 Wizard greatly simplifies testing of trading ideas. The article describes how to develop a custom risk and money management module and enable it in the MQL5 Wizard. As an example we've considered a money management algorithm, in which the size of the trade volume is determined by the results of the previous deal. The structure and format of description of the created class for the MQL5 Wizard are also discussed in the article.
Creating Expert Advisors Using Expert Advisor Visual Wizard
Creating Expert Advisors Using Expert Advisor Visual Wizard

Creating Expert Advisors Using Expert Advisor Visual Wizard

Expert Advisor Visual Wizard for MetaTrader 5 provides a highly intuitive graphical environment with a comprehensive set of predefined trading blocks that let you design Expert Advisors in minutes. The click, drag and drop approach of Expert Advisor Visual Wizard allows you to create visual representations of forex trading strategies and signals as you would with pencil and paper. These trading diagrams are analyzed automatically by Molanis’ MQL5 code generator that transforms them into ready to use Expert Advisors. The interactive graphical environment simplifies the design process and eliminates the need to write MQL5 code.
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.
Graphical Interfaces I: Functions for the Form Buttons and Deleting Interface Elements (Chapter 4)
Graphical Interfaces I: Functions for the Form Buttons and Deleting Interface Elements (Chapter 4)

Graphical Interfaces I: Functions for the Form Buttons and Deleting Interface Elements (Chapter 4)

In this article, we are going to continue developing the CWindow class by adding methods, which will allow managing the form by clicking on its controls. We will enable the program to be closed by a form button as well as implement a minimizing and maximizing feature for the form.
Creating a trading robot for Moscow Exchange. Where to start?
Creating a trading robot for Moscow Exchange. Where to start?

Creating a trading robot for Moscow Exchange. Where to start?

Many traders on Moscow Exchange would like to automate their trading algorithms, but they do not know where to start. The MQL5 language offers a huge range of trading functions, and it additionally provides ready classes that help users to make their first steps in algo trading.