MQL4 Book  About Complex Programs

Upgrade to
Book in One File

General Characteristics of Complex Programs

There is no single formal feature that distinguishes a customary program from a complex one. In general, complex programs positively differ in a variety of tools provided and quantity of information processed. Only some of qualitative adjectives that are peculiar to complex programs can be denoted.

Program Execution Order

As a rule, a usual program contains its code in the special function start() that is started for execution by the client terminal. In most cases function start() execution time is considerably less than the tick period. This means that most of the time the program is waiting for a tick to come. This kind of processes are characterized by the on-off ratio term. On-off ratio is the ratio of a repeating process period to the duration of the process itself. The execution time of start() Т1 is nearly from 10 to 100 milliseconds, and the Т2 time between ticks is 5 seconds at the average. Thus, the on-off ratio of a working EA is reaching Т2/Т1=1000 and more (see fig. 159). That is to say that the duration of a usual operating EA's effective capacity is 0,1% of the whole time, the rest of the time it is standing.

Sometimes complex calculations can be executed by an EA and as a result the duration of start() execution can be longer and reach tens of seconds. In these cases start() function will not be started on every tick but only on the ticks that came while the start() is waiting for them. The fig. 159 shows that the tick that came at the execution of the start() function period (at the t4 moment) will not cause a new special function's start. The next time the start() function will start at the t5 moment. The pause between the end of the current execution and the beginning of the next execution of the start() function will appear with this provision.

Fig. 159 Different on-off ratio while the start() function is executed by the client terminal
and the cycled start() function.

There is a method to increase the effective capacity of the program essentially, thus decreasing on-off ratio of the trade management process. For this purpose let us implement an algorithm, according to which the main code is many times (infinitely) repeated during start() execution (allowed only in EAs and scripts). The example of the looped start() is shown below:

start() // Special function start()
while(!IsStopped()) // Until user..
{ // ..stops execution of the program
RefreshRates(); // Data renewal
//......................The main code of the program is specified here
Sleep(5); // Short pause
return; // Control is returned to the terminal

The whole code is specified in the body of the "while" cycle operator, and the only way to exit the cycle is to receive a command from the client terminal to finish the program execution. If the start() function built on this specified principle is started, it will be executed infinitely long and will return control to the client terminal only when a user manually removes the EA from a security window or providing some other conditions (see Special Functions).

The execution of the cycled start() function is running uninterruptedly, that is why there is no period when the program is in the mode of waiting for a new tick (see Fig. 159), so the on-off ratio of the process of the cycled program execution is equal to 1. The start() function that is based on the specified principle is started by the client terminal only once. This means that the information updating (e.g. market quotes) must be compulsory performed using the RefreshRates() function. In order to avoid the large consumption of much resources a short pause at the end of the cycle can be specified.

The development of the looped program requires much attention while composing the algorithm. For example, the reaction of a usual program on a received critical error is breaking the start() function execution and returning control to the client terminal. A cycled program keeps control permanently while running so the other reaction must be anticipated, for example the prohibition of trade orders generation over some period. Yet the temporary prohibition should not hinder the program execution. During the whole execution period, the program should process all the available information about events, including the controlling actions of a user. In general, such a program has incommensurable power as compared to a usual one.

Available Tools

Use of lopped programs makes sense only if the continuity of a program execution is effectively used. For example, such a program can process a trader's control actions. The modification of coordinates of graphical objects or the fact of attaching other programs - scripts and indicators can be considered as controlling actions.

A simple program may respond to some events (including user-initiated) at a regular start of the special function start() on the nearest tick, as a rule. While a cycled program can process all events immediately (!). In this case the lag can only be for a short time, no more than the time of execution of one cycle of the start() function (nearly no more than 10-100 ms).

A complex program may use graphical objects to display order characteristics or rules of its modification. For example, orders of all type are shown in a security window in green lines, stop orders - in red lines. And if several orders are shown on the screen simultaneously, it is quite difficult to detect what line belongs to this or that order. But if we apply the graphical object "horizontal line" of a necessary color and style to each order line, it will be much easier to differentiate between orders and their stop orders.

Besides, the fact of changing the coordinates of such objects can be perceived by a program as a guide to action. For example, if a user shifts a horizontal line showing a pending order several points up, as a result of this action the program may form and send to the server a trade request, according to which the order should be modified, i.e. the preset open price should be increased by several points (for an instant execution the use of a looped program is obligatory). Thus a complex program may offer the possibility to manage trading using a mouse.

Function used for modification of separate stop-orders or a declared open price of an order can be used in complex programs as well. If such a function is used applicable to one of order lines, a graphical object, for example "an arrow" can be displayed near the order line, indicative of the function activity.

Using graphical objects you can set up trading scenario. For example, setting the "Pivot" flag at some distance from the current price, you can inform the program that it is necessary to close the order and open a new one in the opposite direction when the specified price is reached. Similarly you can specify modification level limits, the order closing price, etc. in the program. The use of graphical objects that display the settings of a program considerably increases the awareness of the trader about present and planned events.

Sound signals associated with events are also used in complex programs. Using sounds in a program allows the trader to leave the PC and orientate through events by the sound signal types (melodies, vocalized text, etc.).

The effectiveness of the trading often depends on the fact whether the time of important economical and political news release is taken into account. Large majority of dealing centers provide traders with the list of the news for the nearest week with the denotement of release time and importance. The information about coming events is recorded into a file. During the operation a program reads the information from the file and performs one or another trade scenario depending on the importance of a coming event. For example, a program can delete pending orders, modify stop-orders of market orders, etc. Shortly before the important news coming the program can terminate trading - close all orders preliminarily informing a trader.

Automated and Manual Mode of Program Operation

A complex program is characterized by a more complex algorithm of events processing. Particularly, this kind of program presupposes some reaction to a trader's interference with the trading process. For example, if a trading strategy allows only one market order and a trader opens one more order, a complex program, first, monitors this kind of event and then starts the execution of the algorithm part provided for such an occasion. The program can warn a trader about an unauthorized interference, at first, and offer to delete the odd order independently. If it does not happen, the program (depending on the settings) can delete the odd order or exit the automated trading mode informing the trader previously.

If a program is started for execution when there are already multiple orders placed, necessary actions will be performed depending on its settings. For example, the program can close all opposite orders without a trader's agreement. If a trading strategy does not allow pending orders, they will be deleted in the priority sequence - firstly, nearest to the market quotation, then more expensive ones, etc.

Once the trader has set the quantity and the quality limits of orders in a trading strategy, the program (working all the time and monitoring the events) can propose a trader to activate the automated trading mode, and if a trader agrees, designate the pursuing trading scenario using graphical objects.

Every trader has its own set of preferences when working with a program. Some traders admit only the automated trading, other traders - half-automated, thirds prefer only the manual mode. A correctly designed program must cover all the requirements, i.e. must have a number of settings that provide different usage mode. For example, a program can act as an adviser in the manual mode of working - display a text containing direct recommendations and also graphical objects displaying a trend direction, forecast pivot points, etc. A program can ask a trader to permit order opening, admit trader interference in orders management (e.g, manual stop-orders modification) while working in the half-automated mode. In case the program is running in the automated mode any trader's interference in the trading process can be considered as a signal for changing the mode to half-automated or manual.

All the described properties of a program can be embodied on the MQL4 programming language basis that is specially designed for this purpose. A correctly designed complex program has incontestable number of advantages, a trader quickly gets used to them and starts using them in trading.