MQL4 Code Tutorials for Metatrader 4

Should you learn MQL4? Every trader needs to know at least a little MQL4 to edit Metatrader scripts, change expert advisor settings and to understand MT4 indicator structure. This page will contain links to MQL tutorials, code examples and other material that should help Metatrader 4 traders. It is my opinion that everyone who uses MT4 ought to learn enough MQL4 to be able to read and understand the main components of the MT4 EAs (Expert Advisors) and at a minimum to be able to do minor code customizations when necessary. If you've spent any time programming in other languages you will find that MQL4 is fairly straightforward, but there are some gotchas with regards to the MT4 platform and how orders and handled that need to be taken into account.

How To Write a Close All Script in MQL4? I've created a simple MQL4 code sample that looks at the always popular question, how do you create a script that closes all orders? There are many sample MQL4 scripts available on the internet. The goal of this MQL4 code sample is to help traders understand exactly how the code is put together piece by piece, so that more Metatrader traders may have the knowledge and desire to tinker with the code and create their own custom scripts. This CloseThis and CloseAll MQL4 sample covers simple concepts such as how to make a loop in MQL4, how to close out only the current symbol, or only symbols that have a given Magic Number. Functions covered include OrderSelect(), OrderType(), OrderClose(), OrdersTotal(), OrderTicket(), OrderLots(), NormalizeDouble(), MarketInfo(), RefreshRates(), IsTradeContextBusy(), Sleep() and OrderSymbol() among others.

How to place only one trade per bar on a forex MT4 Expert Advisor? Learn the simple method for preventing multiple trades per bar within your MT4 EA. The code is super simple to understand and can be implemented into any MT4 expert advisor. The MQL4 predefined variable Bars will be discussed in this short tutorial.

How to create a simple MT4 expert advisor template that trades once per bar? Have you ever wished you had a simple template to create expert advisors? Building on the previous MQL4 tutorial, this two segment sample shows how to put together a simple expert advisor that uses the RSI indicator to give trade signals. This tutorial provides a simple generalized template that may be followed for future expert advisor development. Download is available.

How to delete all objects from a MT4 chart? If you're like me, then you try out a lot of different indicators, expert advisors and scripts for MetaTrader. Some of them are great but some of them I'd like to forget about. Unfortunately many of these programs leave behind loads of objects on a single chart, slowing down MetaTrader and creating a nuisance. Fight back with this simple tutorial that describes how to use the ObjectsTotal(), ObjectDelete(), ObjectName() and Comments() MQL4 functions to clean your chart, as well as how to use a simple for loop to get the job done within your code.

How Do You Use the MarketInfo MQL4 function? MarketInfo is one of the most useful and can be the most difficult MQL4 functions to understand for some traders. This simple set of examples explores how to put MarketInfo to use in referencing data on other charts for use in the OrderClose() function. Functions described include OrderSelect(), OrderClose(), OrderSymbol(), NormalizeDouble() and MarketInfo().

If you have suggestions for a tutorial or code sample or questions about any of the code on this site, please leave me a comment.

Follow patrickmwhite on Twitter

For updates