MQL5 Programming Articles

icon

Study the MQL5 language for programming trading strategies in numerous published articles mostly written by you - the community members. The articles are grouped into categories to help you quicker find answers to any questions related to programming: Integration, Tester, Trading Strategies, etc.

Follow our new publications and discuss them on the Forum!

Add a new article
latest | best
Library for easy and quick development of MetaTrader programs (part XXVIII): Closure, removal and modification of pending trading requests
Library for easy and quick development of MetaTrader programs (part XXVIII): Closure, removal and modification of pending trading requests

Library for easy and quick development of MetaTrader programs (part XXVIII): Closure, removal and modification of pending trading requests

This is the third article about the concept of pending requests. We are going to complete the tests of pending trading requests by creating the methods for closing positions, removing pending orders and modifying position and pending order parameters.
preview
SQLite: Native handling of SQL databases in MQL5

SQLite: Native handling of SQL databases in MQL5

The development of trading strategies is associated with handling large amounts of data. Now, you are able to work with databases using SQL queries based on SQLite directly in MQL5. An important feature of this engine is that the entire database is placed in a single file located on a user's PC.
preview
Continuous Walk-Forward Optimization (Part 2): Mechanism for creating an optimization report for any robot

Continuous Walk-Forward Optimization (Part 2): Mechanism for creating an optimization report for any robot

The first article within the Walk-Through Optimization series described the creation of a DLL to be used in our auto optimizer. This continuation is entirely devoted to the MQL5 language.
Library for easy and quick development of MetaTrader programs (part XXVII): Working with trading requests - placing pending orders
Library for easy and quick development of MetaTrader programs (part XXVII): Working with trading requests - placing pending orders

Library for easy and quick development of MetaTrader programs (part XXVII): Working with trading requests - placing pending orders

In this article, we will continue the development of trading requests, implement placing pending orders and eliminate detected shortcomings of the trading class operation.
Library for easy and quick development of MetaTrader programs (part XXVI): Working with pending trading requests - first implementation (opening positions)
Library for easy and quick development of MetaTrader programs (part XXVI): Working with pending trading requests - first implementation (opening positions)

Library for easy and quick development of MetaTrader programs (part XXVI): Working with pending trading requests - first implementation (opening positions)

In this article, we are going to store some data in the value of the orders and positions magic number and start the implementation of pending requests. To check the concept, let's create the first test pending request for opening market positions when receiving a server error requiring waiting and sending a repeated request.
Exploring Seasonal Patterns of Financial Time Series with Boxplot
Exploring Seasonal Patterns of Financial Time Series with Boxplot

Exploring Seasonal Patterns of Financial Time Series with Boxplot

In this article we will view seasonal characteristics of financial time series using Boxplot diagrams. Each separate boxplot (or box-and-whiskey diagram) provides a good visualization of how values are distributed along the dataset. Boxplots should not be confused with the candlestick charts, although they can be visually similar.
Library for easy and quick development of MetaTrader programs (part XXV): Handling errors returned by the trade server
Library for easy and quick development of MetaTrader programs (part XXV): Handling errors returned by the trade server

Library for easy and quick development of MetaTrader programs (part XXV): Handling errors returned by the trade server

After we send a trading order to the server, we need to check the error codes or the absence of errors. In this article, we will consider handling errors returned by the trade server and prepare for creating pending trading requests.
Extending Strategy Builder Functionality
Extending Strategy Builder Functionality

Extending Strategy Builder Functionality

In the previous two articles, we discussed the application of Merrill patterns to various data types. An application was developed to test the presented ideas. In this article, we will continue working with the Strategy Builder, to improve its efficiency and to implement new features and capabilities.
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.
preview
Continuous Walk-Forward Optimization (Part 1): Working with Optimization Reports

Continuous Walk-Forward Optimization (Part 1): Working with Optimization Reports

The first article is devoted to the creation of a toolkit for working with optimization reports, for importing them from the terminal, as well as for filtering and sorting the obtained data. MetaTrader 5 allows downloading optimization results, however our purpose is to add our own data to the optimization report.
Library for easy and quick development of MetaTrader programs (part XXIII): Base trading class - verification of valid parameters
Library for easy and quick development of MetaTrader programs (part XXIII): Base trading class - verification of valid parameters

Library for easy and quick development of MetaTrader programs (part XXIII): Base trading class - verification of valid parameters

In the article, we continue the development of the trading class by implementing the control over incorrect trading order parameter values and voicing trading events.
Library for easy and quick development of MetaTrader programs (part XXII): Trading classes - Base trading class, verification of limitations
Library for easy and quick development of MetaTrader programs (part XXII): Trading classes - Base trading class, verification of limitations

Library for easy and quick development of MetaTrader programs (part XXII): Trading classes - Base trading class, verification of limitations

In this article, we will start the development of the library base trading class and add the initial verification of permissions to conduct trading operations to its first version. Besides, we will slightly expand the features and content of the base trading class.
Library for easy and quick development of MetaTrader programs (part XXI): Trading classes - Base cross-platform trading object
Library for easy and quick development of MetaTrader programs (part XXI): Trading classes - Base cross-platform trading object

Library for easy and quick development of MetaTrader programs (part XXI): Trading classes - Base cross-platform trading object

In this article, we will start the development of the new library section - trading classes. Besides, we will consider the development of a unified base trading object for MetaTrader 5 and MetaTrader 4 platforms. When sending a request to the server, such a trading object implies that verified and correct trading request parameters are passed to it.
Library for easy and quick development of MetaTrader programs (part XX): Creating and storing program resources
Library for easy and quick development of MetaTrader programs (part XX): Creating and storing program resources

Library for easy and quick development of MetaTrader programs (part XX): Creating and storing program resources

The article deals with storing data in the program's source code and creating audio and graphical files out of them. When developing an application, we often need audio and images. The MQL language features several methods of using such data.
Building an Expert Advisor using separate modules
Building an Expert Advisor using separate modules

Building an Expert Advisor using separate modules

When developing indicators, Expert Advisors and scripts, developers often need to create various pieces of code, which are not directly related to the trading strategy. In this article, we consider a way to create Expert Advisors using earlier created blocks, such as trailing, filtering and scheduling code, among others. We will see the benefits of this programming approach.
Library for easy and quick development of MetaTrader programs (part XIX): Class of library messages
Library for easy and quick development of MetaTrader programs (part XIX): Class of library messages

Library for easy and quick development of MetaTrader programs (part XIX): Class of library messages

In this article, we will consider the class of displaying text messages. Currently, we have a sufficient number of different text messages. It is time to re-arrange the methods of their storage, display and translation of Russian or English messages to other languages. Besides, it would be good to introduce convenient ways of adding new languages to the library and quickly switching between them.
Library for easy and quick development of MetaTrader programs (part XVIII): Interactivity of account and any other library objects
Library for easy and quick development of MetaTrader programs (part XVIII): Interactivity of account and any other library objects

Library for easy and quick development of MetaTrader programs (part XVIII): Interactivity of account and any other library objects

The article arranges the work of an account object on a new base object of all library objects, improves the CBaseObj base object and tests setting tracked parameters, as well as receiving events for any 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

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.
Strategy builder based on Merrill patterns
Strategy builder based on Merrill patterns

Strategy builder based on Merrill patterns

In the previous article, we considered application of Merrill patterns to various data, such as to a price value on a currency symbol chart and values of standard MetaTrader 5 indicators: ATR, WPR, CCI, RSI, among others. Now, let us try to create a strategy construction set based on Merrill patterns.
Library for easy and quick development of MetaTrader programs (part XVI): Symbol collection events
Library for easy and quick development of MetaTrader programs (part XVI): Symbol collection events

Library for easy and quick development of MetaTrader programs (part XVI): Symbol collection events

In this article, we will create a new base class of all library objects adding the event functionality to all its descendants and develop the class for tracking symbol collection events based on the new base class. We will also change account and account event classes for developing the new base object functionality.
Developing a cross-platform grid EA (Last part): Diversification as a way to increase profitability
Developing a cross-platform grid EA (Last part): Diversification as a way to increase profitability

Developing a cross-platform grid EA (Last part): Diversification as a way to increase profitability

In previous articles within this series, we tried various methods for creating a more or less profitable grid Expert Advisor. Now we will try to increase the EA profitability through diversification. Our ultimate goal is to reach 100% profit per year with the maximum balance drawdown no more than 20%.
MQL5 Cookbook: Trading strategy stress testing using custom symbols
MQL5 Cookbook: Trading strategy stress testing using custom symbols

MQL5 Cookbook: Trading strategy stress testing using custom symbols

The article considers an approach to stress testing of a trading strategy using custom symbols. A custom symbol class is created for this purpose. This class is used to receive tick data from third-party sources, as well as to change symbol properties. Based on the results of the work done, we will consider several options for changing trading conditions, under which a trading strategy is being tested.
Developing Pivot Mean Oscillator: a novel Indicator for the Cumulative Moving Average
Developing Pivot Mean Oscillator: a novel Indicator for the Cumulative Moving Average

Developing Pivot Mean Oscillator: a novel Indicator for the Cumulative Moving Average

This article presents Pivot Mean Oscillator (PMO), an implementation of the cumulative moving average (CMA) as a trading indicator for the MetaTrader platforms. In particular, we first introduce Pivot Mean (PM) as a normalization index for timeseries that computes the fraction between any data point and the CMA. We then build PMO as the difference between the moving averages applied to two PM signals. Some preliminary experiments carried out on the EURUSD symbol to test the efficacy of the proposed indicator are also reported, leaving ample space for further considerations and improvements.
Parsing HTML with curl
Parsing HTML with curl

Parsing HTML with curl

The article provides the description of a simple HTML code parsing library using third-party components. In particular, it covers the possibilities of accessing data which cannot be retrieved using GET and POST requests. We will select a website with not too large pages and will try to obtain interesting data from this site.
Library for easy and quick development of MetaTrader programs (part XV): Collection of symbol objects
Library for easy and quick development of MetaTrader programs (part XV): Collection of symbol objects

Library for easy and quick development of MetaTrader programs (part XV): Collection of symbol objects

In this article, we will consider creation of a symbol collection based on the abstract symbol object developed in the previous article. The abstract symbol descendants are to clarify a symbol data and define the availability of the basic symbol object properties in a program. Such symbol objects are to be distinguished by their affiliation with groups.
A New Approach to Interpreting Classic and Hidden Divergence. Part II
A New Approach to Interpreting Classic and Hidden Divergence. Part II

A New Approach to Interpreting Classic and Hidden Divergence. Part II

The article provides a critical examination of regular divergence and efficiency of various indicators. In addition, it contains filtering options for an increased analysis accuracy and features description of non-standard solutions. As a result, we will create a new tool for solving the technical task.
Merrill patterns
Merrill patterns

Merrill patterns

In this article, we will have a look at Merrill patterns' model and try to evaluate their current relevance. To do this, we will develop a tool to test the patterns and apply the model to various data types such as Close, High and Low prices, as well as oscillators.
Optimization management (Part II): Creating key objects and add-on logic
Optimization management (Part II): Creating key objects and add-on logic

Optimization management (Part II): Creating key objects and add-on logic

This article is a continuation of the previous publication related to the creation of a graphical interface for optimization management. The article considers the logic of the add-on. A wrapper for the MetaTrader 5 terminal will be created: it will enable the running of the add-on as a managed process via C#. In addition, operation with configuration files and setup files is considered in this article. The application logic is divided into two parts: the first one describes the methods called after pressing a particular key, while the second part covers optimization launch and management.
Library for easy and quick development of MetaTrader programs (part XIV): Symbol object
Library for easy and quick development of MetaTrader programs (part XIV): Symbol object

Library for easy and quick development of MetaTrader programs (part XIV): Symbol object

In this article, we will create the class of a symbol object that is to be the basic object for creating the symbol collection. The class will allow us to obtain data on the necessary symbols for their further analysis and comparison.
Library for easy and quick development of MetaTrader programs (part XIII): Account object events
Library for easy and quick development of MetaTrader programs (part XIII): Account object events

Library for easy and quick development of MetaTrader programs (part XIII): Account object events

The article considers working with account events for tracking important changes in account properties affecting the automated trading. We have already implemented some functionality for tracking account events in the previous article when developing the account object collection.
Library for easy and quick development of MetaTrader programs (part XII): Account object class and collection of account objects
Library for easy and quick development of MetaTrader programs (part XII): Account object class and collection of account objects

Library for easy and quick development of MetaTrader programs (part XII): Account object class and collection of account objects

In the previous article, we defined position closure events for MQL4 in the library and got rid of the unused order properties. Here we will consider the creation of the Account object, develop the collection of account objects and prepare the functionality for tracking account events.
Library for easy and quick development of MetaTrader programs (part XI). Compatibility with MQL4 - Position closure events
Library for easy and quick development of MetaTrader programs (part XI). Compatibility with MQL4 - Position closure events

Library for easy and quick development of MetaTrader programs (part XI). Compatibility with MQL4 - Position closure events

We continue the development of a large cross-platform library simplifying the development of programs for MetaTrader 5 and MetaTrader 4 platforms. In the tenth part, we resumed our work on the library compatibility with MQL4 and defined the events of opening positions and activating pending orders. In this article, we will define the events of closing positions and get rid of the unused order properties.
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.
Developing a cross-platform grider EA (part III): Correction-based grid with martingale
Developing a cross-platform grider EA (part III): Correction-based grid with martingale

Developing a cross-platform grider EA (part III): Correction-based grid with martingale

In this article, we will make an attempt to develop the best possible grid-based EA. As usual, this will be a cross-platform EA capable of working both with MetaTrader 4 and MetaTrader 5. The first EA was good enough, except that it could not make a profit over a long period of time. The second EA could work at intervals of more than several years. Unfortunately, it was unable to yield more than 50% of profit per year with a maximum drawdown of less than 50%.
Library for easy and quick development of MetaTrader programs (part X): Compatibility with MQL4 - Events of opening a position and activating pending orders
Library for easy and quick development of MetaTrader programs (part X): Compatibility with MQL4 - Events of opening a position and activating pending orders

Library for easy and quick development of MetaTrader programs (part X): Compatibility with MQL4 - Events of opening a position and activating pending orders

In the previous articles, we started creating a large cross-platform library simplifying the development of programs for MetaTrader 5 and MetaTrader 4 platforms. In the ninth part, we started improving the library classes for working with MQL4. Here we will continue improving the library to ensure its full compatibility with MQL4.
Extract profit down to the last pip
Extract profit down to the last pip

Extract profit down to the last pip

The article describes an attempt to combine theory with practice in the algorithmic trading field. Most of discussions concerning the creation of Trading Systems is connected with the use of historic bars and various indicators applied thereon. This is the most well covered field and thus we will not consider it. Bars represent a very artificial entity; therefore we will work with something closer to proto-data, namely the price ticks.
Arranging a mailing campaign by means of Google services
Arranging a mailing campaign by means of Google services

Arranging a mailing campaign by means of Google services

A trader may want to arrange a mailing campaign to maintain business relationships with other traders, subscribers, clients or friends. Besides, there may be a necessity to send screenshotas, logs or reports. These may not be the most frequently arising tasks but having such a feature is clearly an advantage. The article deals with using several Google services simultaneously, developing an appropriate assembly on C# and integrating it with MQL tools.
Developing a cross-platform Expert Advisor to set StopLoss and TakeProfit based on risk settings
Developing a cross-platform Expert Advisor to set StopLoss and TakeProfit based on risk settings

Developing a cross-platform Expert Advisor to set StopLoss and TakeProfit based on risk settings

In this article, we will create an Expert Advisor for automated entry lot calculation based on risk values. Also the Expert Advisor will be able to automatically place Take Profit with the select ratio to Stop Loss. That is, it can calculate Take Profit based on any selected ratio, such as 3 to 1, 4 to 1 or any other selected value.
Library for easy and quick development of MetaTrader programs (part IX): Compatibility with MQL4 - Preparing data
Library for easy and quick development of MetaTrader programs (part IX): Compatibility with MQL4 - Preparing data

Library for easy and quick development of MetaTrader programs (part IX): Compatibility with MQL4 - Preparing data

In the previous articles, we started creating a large cross-platform library simplifying the development of programs for MetaTrader 5 and MetaTrader 4 platforms. In the eighth part, we implemented the class for tracking order and position modification events. Here, we will improve the library by making it fully compatible with MQL4.
Developing a cross-platform grider EA (part II): Range-based grid in trend direction
Developing a cross-platform grider EA (part II): Range-based grid in trend direction

Developing a cross-platform grider EA (part II): Range-based grid in trend direction

In this article, we will develop a grider EA for trading in a trend direction within a range. Thus, the EA is to be suited mostly for Forex and commodity markets. According to the tests, our grider showed profit since 2018. Unfortunately, this is not true for the period of 2014-2018.