Refer a friend
Title Icon

Advanced

Identify hidden opportunities, master risk management,
and become an expert Forex trader.

The Beginner’s Guide to MQL5

MetaTrader, as a platform, has built-in functions that assist in technical analysis and trade management while also allowing traders to develop their own indicators and trading strategies. MQL5 is the integrated programming language used in MetaTrader for developing automatic trading strategies, creating custom technical indicators, and more. In our comprehensive guide, we will be understanding MQL5 in-depth.

What is MQL5?

MetaQuotes Language 5 (MQL5) is a programming language that helps traders with automated forex trading, creating their own indicators and testing trading strategies on the MetaTrader platform.

Features of MQL5

Price chart and indicator management

MQL5 allows traders to directly manage price charts. It also allows them to adjust colours, display custom symbols, move charts, and develop control panels. The trading robots (Expert Advisors) can be debugged on both price charts and multi-currency testers. The indicators can also be managed with 12 drawing styles and future predictive values through 512 buffers and direct calculations. You can also use more than one indicator to create a combined forex trading strategy through the program.

Graphic

Event-based Expert Advisors

MQL5 is event-driven and follows an event-based trading logic. It has the OnTick() symbol, which is used to handle events. You can use the tick flow (update of a currency pair’s prince) of any currency pair and receive entry or exit signals. The tick flow generates market activity signals, providing traders with ideal prices to enter or exit the market. The program executes within seconds, and you can move ahead to open another trade. Each trading operations data is handled with another handler called OnTradeTransaction(). So, you do not have to wait between trades to receive information. Hence, you are able to write trading algorithms efficiently by analysing different forex events.

Graphic

Multi-currency Expert Advisors

You can develop Expert Advisors with MQL5 that can trade on multiple symbols like EUR/USD, USD/GBP, and AUD/GBP. You can simultaneously test them on the strategy tester, which is a built-in tool that helps traders evaluate the efficiency of their EAs before they implement it in real-time. Traders can then debug their Expert Advisors in the price charts and use it with multiple currency pairs together to manage their portfolio having different types of currency pairs.

Graphic

MQL5 cloud network

The MQL5 cloud network enables fast trading optimisation and the creation of trading robots. It also unites remote MT5 agents. Then, it distributes all computation tasks among them to ensure each process is done quickly. With other processes of the programming language, traders are able to run thousands of tests to analyse and monitor the success rate of their strategies in a few hours. The more tests they are able to run with this feature, the faster they can judge if their trading strategy needs any changes or not.

MQL5 community

The MQL5 community helps forex traders connect and resolve each others’ queries. It also provides additional features like the WebTerminal, social trading platform, hosting services, trading application market, and more.

Graphic

CodeBase

CodeBase is the largest application library to exist for automatic trading. It consists of applications that can be used in MetaTrader 5 and MetaTrader 4 with the app’s source codes, expert advisors, scripts, indicators, and much more. The library is completely free, and anybody with access to MetaTrader can use the same. Traders can also post their personal developments here for others to use them.

Functions

MQL5 consists of hundreds of functions that can be used for generalised trading. These functions help in identifying market directions, deleting ticks from price history, creating databases, and much more. Each function on MQL5 serves a different purpose, from working with economic calendar events to creating technical indicators. Some of the widely used functions are:

  • FileReadString: Reads a file’s string from the current position of a trade
  • iAO to use the Awesome Oscillator
  • iBearsPower to understand the bear’s power in the market
  • MathSqrt to find a return’s square root
  • iCustom to develop a custom indicator

What does MQL5 help with?

Expert Advisors (EAs)

MQL5 enables traders to create several Expert Advisors (trading robots) that help in automating trades. Expert Advisors are written with the help of this language and use specific conditions to fit the trading needs of each trader.

Technical Indicators

Technical forex indicators are small program codes written in the language. These are displayed on the price chart or below them. They help in conducting technical analysis and placing an order accordingly. With the help of MQL5, all indicators can be managed together, and new custom indicators can also be created.

All orders, trades and positions

MQL5 also helps in placing ideal stop loss and take profit orders, managing overall trading orders, trades and positions in the forex market. The programming language helps modify existing positions and minimises the risk associated with each trade. It automates each function by rejecting invalid requests and processing the valid ones. The programming language, with the help of trading robots, identifies the best possible trade in the existing market, executes a trade and monitors the position to reap gains.

Different tabs available on MQL5

  • Journal tab: Displays all the events that occur during the strategy testing.
  • Agents tab: Helps in testing the strategies.
  • Graph tab: Displays the price chart graph after orders are sent to the Journal tab.
  • Inputs tab: Specifies all the input parameters that the Expert Advisors use.
  • Results tab: The tab is displayed after the strategy testing is completed, and it provides you with the summary of the test results.
  • Settings tab: Provides you with everything about the trade, like date, executing type, deposit, optimisation, and more.
  • Debug tab: Provides you with information regarding files, functions, types, lines and expressions to start the debugging process of the Expert Advisors.

What are the MQL5 data types available?

Each program needs to operate with data. Data types differ according to their purposes and help in different processes. Here is a list of the most important data types in MQL5 –

  • Int data type is 4 bytes (32 bits) in size and is used to access array components.
  • Char data type only takes a single byte of memory and helps in expressing in binary notation values, containing both negative and positive values.
  • Uchar integer data type’s size is also 1 byte but only provides positive values between 0 to 225.
  • Uint or unsigned integer data type takes 4 bytes in memory and helps traders express integer values 0 to 295.
  • Short data type is sized at 2 bytes and expressed in a range that is equal to 2 to the power of 16. It contains both positive and negative values.
  • Short or unsigned short data type is also sized at 2 bytes but only provides positive values.
  • The long data type is 8 bytes, giving both positive and negative values in size, whereas the ulong is also 8 bytes providing only positive values.
  • Bool data type helps in storing logical values in true or false forms and provides answers in the numerical representation of 0 and 1, respectively.
  • String data type helps in strong texts strings and takes the longest type to process as they take one of the biggest memory spaces.
  • Float data type provides values in fractions, and double data type is the same but only has twice the accuracy of float numbers.
  • Color data type stores information about colors and is 4 bytes in size.
  • Datetime data type stores the date and time in seconds about trades.

How to write and test an EA with MQL5?

1. Launch MQL5

Launch the MetaQuotes Language Editor 5 and press the ‘new’ button on the menu bar. An MQL5 Wizard Window will open. Select expert Advisor from this window and click ‘next’.

2. Give your EA a name

Enter the name of your EA in the name box that appears next. You can also provide the dialogue box with your email address or website link.

3. Change the parameters

When building your EA, you can customise the parameters per your trading preferences. You can also set multiple parameters to test which values give the best result. Add each parameter in the MQL Wizard dialogue box by clicking on the ‘add’ button. Add as many parameters as you may like.

4. Initialise the EA

Next, you can initialise your Expert Advisor by using an indicator function. You can add technical indicators like Moving Average or the Average Directional Index as parameters for the EA to react accordingly. These indicators are obtained by using functions specific to the MQL5 programming language.

5. De-initialise the EA

If you feel that your EA is not performing as per expectations, you can De-initialise it and remove it from your trading charts. All indicators released during the initialisation phase are de-initialised. However, this function can be skipped if your EA has started automating your trades successfully.

6. Check the number of bars on the chart

This step is possible by checking the EA’s ontick section. This checks if enough bars are present on the currency pair’s price chart. The EA can relax if there are 60 or fewer bars available on the chart until more bars are added so that the EA is able to provide accurate signals. If there are over 60 bars already present on the price chart, you can start using your EA to generate trading signals.

7. Debug the EA

The last step before testing the EA is to debug it. This will help identify code errors and improve it by checking how it performs line by line. Here is how you can debug the EA:

  • Open the tools bar on the Editor menu Bar and click on Options
  • Select the currency pair you are trading and its timeframe
  • Click on the ‘OK’ button
  • Insert a breakpoint to the code where you want the debugger to monitor the code’s performance
  • Press F5 and slick on the green button that appears on MetaEditor’s toolbar
  • Identify the error on the code, if any
  • Visit the trading terminal with the EA attached to the trading chart
  • Click ‘OK’ if all the errors are sorted or improve the code from where the debugger has noticed errors

8. Test the EA

The last step is to test the EA through the strategy tester. Press Ctrl+R to start the testing process. Thereafter, follow these steps to test your EA strategy:

  • Select the Expert Advisor that you want to test
  • Select the currency pair and timeframe for the test
  • Select a custom period
  • Set dates for the customer period, a minimum of 5
  • Select ‘normal’ in the execution tab
  • Select whatever deposit amount you want to use in USD
  • Set the optimisation option to disable, as you are only testing for now
  • Press the ‘start’ button

Start using MQL5 today

Start using MQL5 today to get hold of the impressive set of built-in indicators, custom tools, and functions that help you with automatic forex trading. You can build your own trading robots with MQL5 that seamlessly analyse quotes and trade forex on your behalf. Start forex trading with our platform to get access to MQL5’s trading platform, MT4 and MT5. Sign up for a live trading account or try a risk-free demo account.

Recommended Topics

Guide to Forex
Trading indicators.

Enter your details to get a copy of our
free eBook

Thank you, please check your inbox for your ebook.

Ads BG

Start a risk free
demo account

News & Analysis

Catch up on what you might
have missed in the market.

Runner graphic

Ready to trade at
Blueberry Markets?

Your highly-rated trading experience
is a click away