The Forex market is fast-paced and frequently necessitates making multiple decisions at the same time. Fortunately, MQL4 exists to make things a lot easier. But what exactly is MQL4, and how does it work in Forex?
What is MQL4?
MetaQuotes Language 4 (MQL4) is a programming language for trading strategies that is built into MetaQuotes. MetaQuotes Software Corp. created this language based on their extensive experience in developing online trading platforms. Traders can use this language to create their own Expert Advisors, which automate trading management and are ideal for implementing to their own trading strategies. In addition, MQL4 allows them to create their own technical indicators (custom indicators), scripts, and libraries.
MQL4 includes a large number of functions for analyzing current and previously received quotes, as well as basic indicators and functions for managing and controlling trade orders. For writing program code, the MetaEditor (text editor) that highlights different MQL4 language constructions is used. It makes it simple for users to find their way around the expert system text. It is difficult to keep up with all of the action in the Forex market. However, an algorithmic trading system can help traders make better decisions and gain an advantage.
The brief guide categorizes functions, operations, reserved words, and other language constructions and allows traders to find a description of every used element of the language.
Programs written in MetaQuotes Language 4 have a variety of features and functions:
An expert advisor is a mechanical trading system that is linked to a specific chart. An Expert Advisor begins to run when one of the following events occurs: initialization and denationalization events, a new tick receipt event, a timer event, depth of market changing event, chart event, and custom events. An Expert Advisor can both inform traders about trading opportunities and automatically trade on their behalf by sending orders directly to a trade server. Expert Advisors are saved in the terminal directoryMQL4Experts directory.
Custom Indicator is a technical indicator that is written independently of those already integrated into the client terminal. They, like built-in indicators, cannot trade automatically and are only intended to perform analytical functions. Custom indicators are saved in the terminal directoryMQL4Indicators directory.
A script is a program that is designed to perform a single set of actions. Unlike Expert Advisors, scripts do not perform any actions other than the start event (this requires the OnStart handler function in a script). Scripts are kept in the terminal directoryMQL4Scripts.
A library is a collection of custom functions designed to store and distribute frequently used blocks of custom code. Libraries cannot begin to execute on their own. Terminal directory MQL4Libraries is where libraries are kept.
Include File is a text file that contains the source code for the most frequently used blocks of custom programs. During the compilation process, such files can be included in the source texts of Expert Advisors, scripts, custom indicators, and libraries. The use of included files is preferable to the use of libraries due to the additional burden that occurs when calling library functions. Include files can be stored in the same directory as source files using the #include directive with double quotes. Terminal directoryMQL4Include is another location for include files; in this case, the #include directive is used with angle brackets.
Why traders should learn MQL4?
Knowing how MQL4 works gives a traders a good understanding of how to automate or make certain aspects of their trading more efficient. This means they can design a system that is tailored to their specific requirements.
MQL4's Importance and Benefits, as well as learning Algorithmic Trading
MQL4 is a powerful tool that, when used correctly, can greatly expand the trading capabilities. Let's look at some more benefits of MQL4 and algorithmic trading.
The psychological benefit. An algorithmic trading system promotes a systematic trading pattern. This not only helps to keep the emotions of traders in check, but it also helps them maintain their discipline and achieve trading consistency.
The ability to test your strategy in the past. A trading system that is automated is based on a specific set of rules. Traders can put these rules to the test using historical data. This allows them to evaluate and fine-tune their trading strategy before putting real money at risk.
Increased speed. Computer programs can react to changing market conditions in real time. A few seconds can mean the difference between a winning and a losing trade in Forex trading. An automated system can assist the traders in keeping up with the volatile Forex market.
Opportunities should be maximized. Multiple strategies can be run by an automated system, putting them in a better position to identify excellent opportunities. It also allows them to take a break from their trading screen, allowing them to explore other markets or develop new strategies.
Although an automated system has obvious advantages, manual trading continues to exist for a reason.
Manual Trading vs Automated Trading: Getting the best of both worlds
Although automated trading promises efficiency and opportunities, a computer lacks a brain.
A computer can only carry out scenarios that have been pre-programmed. A human, on the other hand, can consider everything that is going on. A human, for example, can get a sense of the market and account for unexpected events or awkward market movements. There is also the issue of automated systems' fallibility. Because the systems are prone to mechanical failure, they still necessitate some monitoring.
Both manual and automated trading have advantages and disadvantages. Traders can get the best of both worlds by using a semi-automated Forex system. This type of system looks for trading opportunities in the market but leaves it up to them to decide whether or not to trade. They can combine their intelligence and experience with the analytical capabilities of an automated system with semi-automated trading. Nonetheless, a semi-automated system will necessitate some coding, so they must be familiar with MQL4.
How does MQL4 works?
MQL4 is based on the C++ programming language concepts. As a result, it makes extensive use of C++ concepts such as object-oriented programming (OOP).
The programming language executes various applications based on the rules defined for each type of program (e.g. trade management or market analysis). MQL4 allows the traders to create and run three types of applications: scripts, custom indicators, and expert advisors.
Scripts are used to analyze relatively simple but frequently performed actions such as closing orders or removing objects from a chart. Custom indicators are tools for market analysis. Finally, traders have knowledgeable advisors.
What is Expert Advisors in MQL4?
An expert advisor (EA) in MQL4 is a program that monitors and trades the markets using the language's algorithms. An EA searches the market for opportunities based on the parameters that the traders specify. When it discovers an opportunity, it either notifies them or automatically opens a position.
The platform for creating, editing, and compiling MQL4 expert advisors, indicators, and scripts is included with MT4. MetaEditor is the name of this program.
Timecurrent in MQL4
Returns the last known server time, the time of one of the selected Symbols in the "Market Watch" window for the last quotation receipt. This function returns the time of the handled tick received in the OnTick handler. If, for instance, the time of the last quote receipt for any icon available in the "Market Watch" window OnInit, OnDeinit, OnTimer and so on, the time shown in the title of this window is other cases. The time value on a business server is not based upon the computer's time settings. The function contains two variants; Call without parameters and the Call with MqlDateTime type parameter.
If the MqlDateTime structure type variable was passed as a parameter, it is filled with the appropriate data.
Use the GetTickCount function to arrange high-resolution counters and timers, which returns values in milliseconds.
TimeCurrent is simulated using historical data during Strategy Tester testing.
The function returns the last known value of the server time (the time of the last quote arriving) in seconds since 00:00 on January 1, 1970. Before launching special functions for execution, the client terminal updates the time of the last quote received (along with other environment variables). Each tick is distinguished by its own server time value, which can be obtained using the TimeCurrent function. This value can only be changed during execution as a result of the Refresh Rates function call and only if the information has been updated since the last execution of the Refresh Rates function, i.e., if the server has returned new values for some environment variables.
In general, the time of bar opening, Time[i], does not coincide with the time of new tick coming. Timeframe bar opening times have always been divisible by timeframe. Any first tick received within a timeframe forms a bar; if no tick receipt occurs within a timeframe, the bar does not form within the timeframe.
For example, a tick arriving at the terminal at time (server) t0 results in the formation of a bar with an opening time equal to Time[i+2]. In general, the moment specified as the start of the timeframe does not coincide with moment t0, though it may coincide by accident. Following ticks that arrive at the terminal within the same timeframe (at t1 and t2) can change the parameters of the bar, such as maximum price or open price, but they have no effect on the time of bar opening. MetaTrader 4 is an online trading system that does not take into account the closing time of bars (formally,it can be considered as the bar closing time when the time of the previous tick coming within a timeframe or the start time of the next timeframe, see the image below).
As shown in the image above, it is possible that bars do not form at some time periods equal to the timeframe. As a result, the full timeframe is packed between time t5 of the next tick coming and time t6 of the next tick coming, so the new bar hasn't formed at that time. In this manner, the time of bar opening may differ by more than a whole timeframe from the time of opening of an adjacent bar, but it is always divisible by a timeframe. To show the sequence of bar formation, traders can use the EA timebars.mq4, which outputs the time of tick arrival and the time of bar opening.
The output of the EA timebars.mq4 is depicted in the image below. The first tick at the regular time period of one minute duration occurred at 14:29:12, and a new bar was formed with the opening time of 14:29:00. Please keep in mind that the server time is displayed in the right column of the message box, while the local time is displayed in the left column.
If the ticks appear infrequently (for example, between the end of the European session and the start of the Asian session), traders may notice another phenomenon during the execution of timebars. mq4: the opening times of adjacent bars can differ by more than one minute (for one-minute timeframe). Simultaneously, the indexing of bars is saved in sequence, without spaces.
Server times in different dealing centers may differ. The time at which trades begin and end is set on each server individually, and it may differ from the start and end of the regular day. Some dealing centers, for example, have settings that initiate trades on Sunday at 23:00 server time. As a result, incomplete daily bars are formed, with a practical duration of one hour (see the picture below).
MQL4 makes it relatively simple to use date and time functions. Some of them convert the server and the local time in seconds since 00:00 on January 1, 1970 into an integer number that corresponds to an hour, a day, and so on. Other functions return an integer number representing the current hour, day, minute, and so on.
Date and Time Functions
Day. It returns the current month's day, i.e. the month's day of the last known server time.
DayOfWeek. It returns the weekday index number (Sunday-0,1,2,3,4,5,6) of the last known server time.
DayOfYear. It returns the current day of the year (1 is January 1st,...,365(6) is December 31st), i.e. the day when the year of the steadily for the past recognized server time.
Hour. It returns the current hour (0,1,2,...,23) of the last known server time at the time the program is launched ( during in the accomplishment of the program the value is not changed).
Minute. It returns the current minute (0,1,2,...,59) of the last known server time at the time the program is launched (the value is still the same during the execution of the program).
Month. It returns the current month's number (1-January,2,3,4,5,6,7,8,9,10,11,12), i.e. the month of the last known server time.
Seconds. It returns the number of seconds that have passed since the start of the current minute of the last known server time at the time the program was launched (the value is still the same during the accomplishment of the program).
TimeCurrent. It returns the last known server time (the time of the last quote coming) expressed in seconds since 00:00 January 1st, 1970.
TimeDay. It returns the month's day (1 - 31) for the given date.
TimeDayOfWeek. The weekday is return (0-Sunday,1,2,3,4,5,6) for the given date.
TimeDayOfYear. It returns the day of the year (1 is the first of January,...,365(6) is the 31st of December) for the specified date.
TimeHour. It returns the current time's hour.
TimeLocal. It returns the local PC time, expressed in seconds since 00:00 on January 1, 1970.
TimeMinute. It returns the number of minutes elapsed since the specified time.
TimeMonth. It returns the month number for the specified time period (1-January,2,3,4,5,6,7,8,9,10,11,12).
TimeSeconds. It returns the number of seconds elapsed since the start of the specified time.
TimeYear. It gives the year for the given date. The returned value can be between 1970 and 2037.
Year. It returns the current year, which is the same as the year of the last known server time.