MQL4 Book  Simple Programs in MQL4

Upgrade to
Book in One File

Simple Programs in MQL4

This section contains several simple programs ready for practical use. We will discuss general principles of creating a simple Expert Advisor and a simple custom indicator, as well as the shared usage of an Expert Advisor and different indicators.

Trading criteria used in programs are applied for educational purpose and should not be considered as a guide for action in trading on a real account.

  • Usage of Technical Indicators.
    There are several dozens of indicators in MetaTrader 4. Such indicators are called technical. The name "technical" originates from two types of market analysis: fundamental analysis (FA) which is the analysis of macroeconomic indexes in the context of a traded security, market, country, etc.; and technical analysis (TA) which is the analysis of price using charts and different price transformations. MQL4 allows to get values of technical indicators via corresponding functions. When calling functions of technical indicators, required parameters must be specified.
  • Simple Expert Advisor.
    When writing an Expert Advisor (trading robot) it is necessary to conduct preliminary works: define a trading strategy, set criteria, and on the bases of all this create a structure. Trading criteria are usually set in one or several functions, which are blocks of producing trade signals. The size of an opened trade position is often a separate task and can be written in a separate function. Orders to open, close and modify orders can result in errors that should be processed. These operations are also usually included into corresponding user-defined functions.
  • Creation of Custom Indicators.
    It is not difficult to write a custom indicator if we know its arrangement. Each custom indicator may contain from 1 to 8 indicator buffers, using which terminal shoes the information on charts. Necessary buffers are declared in the form of arrays of double type on the program global level, further in init() each buffer parameter is specified/set up: drawing style, color and width of lines, etc. Since start() is launched in the indicator at each received tick, organizing reasonable calculations is extremely important. For creating an optimal indicator algorithm IndicatorCounted() function is used, this function contains data about amount of bars that hasn't change since the last start() call.
  • Custom Indicator ROC (Price Rate of Change).
    Creation of a custom indicator is better understood on an example with detailed explanations. Detailed comments in the indicator text will be useful for you further, when you decide to modify the indicator. Good programs are well documented programs.
  • Combined Use of Programs.
    For using values of a custom indicator in other indicators, scripts or Expert Advisors, add into a program code custom indicator call using the function iCustom(). The physical presence of the called custom indicator in a corresponding directory is not checked during compilation. That is why parameters of custom indicator call must be set up correctly, otherwise calculated values may differ from expected ones. The possibility of calling a custom indicator helps to considerably simplify an Expert Advisor code.