![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Pay attention to the filter by symbol and by wizard in the position loop. If there is no filter, but you trawl all open positions on all symbols, and this is bad.
So, at first glance, everything seems to be fine.
Pay attention to the filter by symbol and by wizard in the position loop. If there is no filter, but you trawl all open positions on all symbols, and this is bad.
So, everything seems to be OK at first sight.
Thank you very much, Andrey! I understand everything about Magic because several positions may be opened for one symbol, but another question has arisen. Will the Expert Advisor go through the open positions for all symbols at once, if it is not explicitly pointed to the current symbol? And this despite the fact that it is set for a certain currency pair, for example, EURUSD? Honestly, I do not quite understand this point.
Regards, Vladimir.
Thank you very much, Andrey! I understand everything about Magic, because several positions may be opened on one symbol, but I have another question. Will the EA look through the open positions for all symbols at once, if it is not explicitly pointed to the current symbol? And this despite the fact that it is set for a certain currency pair, for example, EURUSD? Honestly, I do not quite understand this point.
Sincerely, Vladimir.
So, on the basis of the read literature, I wrote a short algorithm for creating an Expert Advisor with the trailing stop function:
Please review the algorithm and give me some hints on what points have been missed.
Sincerely, Vladimir.
The theory is not bad, now let's focus on the practice. Will it work?
Good on the theory, now the practice. Can you do it?
I'll try. But you understand that this requires an entirely different level of knowledge, and I don't have it yet.
Regards, Vladimir.
Yes, Alexey, I've already seen this code. It's in the form of an include file. To be honest, I haven't found anything about the symbol in it, though I've viewed it several times. Perhaps I misunderstood something or am just searching poorly.
Sincerely, Vladimir.
For now, let's continue with the functions.
As I wrote earlier, functions are everywhere, you have to love them and you have to know how to write them. Functions, are our little fighters in solving global problems. If we were generals in an army, what kind of fighters would we want to control? Here's a rough list:
But we got sidetracked, let's move on to functions again.
If a function solves too many tasks as a whole - then following the analogy, it's a very clever fighter who, if something goes wrong with it, could ruin the whole enterprise. If you ask what such a function does, the answer could be long. If the result of this function suddenly stops being correct, it will be very difficult to find out what causes an error in it (because there are many tasks, much code, a lot of calls to subprocedures and where exactly the error is difficult to understand).
If a function calculates correct results on Mondays, Wednesdays and Sundays and on the rest days depending on our "mood", can we rely on this function? Imagine that the OrderSend function, say, opens positions only on Thursdays and if some magic parameter with value 13 is defined. And this is not nonsense or fantasy at all. This behavior can be arranged at a click of fingers - it's enough to make the function dependent on some parameters in the external environment.
Suppose the function:
will always return the sum of two values, regardless of the external environment. It means that even if we copy this function into another script or Expert Advisor, it will work perfectly well there. This function can be written once and used in many of our programs by just obtuse copying. We will always be able to rely on its result knowing that its operation does not depend on anything. Such functions, the result of which does not depend on their environment, are called side-effect-free or pure functions. If we strive to write pure functions, we soon get a lot of them. This means you can combine them into a file and include them in your new projects. This is called code reuse. We don't do the work twice. Instead, we use already written functions that we know and whose reliability has been tested more than once.
Let's look at the anti-example now:
The result seems to be the same, because c is always zero. Or is it not always? What if someone changes c somewhere? What then? What if someone somewhere also uses the external variable c, but for his own purposes, and he has a variable c of a different type, let's say string? Combining these two functions is no longer possible (the compiler will not allow to declare two variables with the same name). Their common dependencies are also difficult to solve. I don't know what to do with it at all. For example, I still don't know a reliable and easy way to make such functions work together.
Even if there is no other function and only one function reads an external variable, it is not so easy to copy it elsewhere. We have to copy both this function and its dependency. But what if we copy these functions into a common file? We get 50 or 100 of these functions there. And each of them copies with itself a heap of its own dependent variables. We get a tangle of related variables with unclear functions. But what is it all for? What problems does it solve? Why create unnecessary dependencies when you can do without them in the vast majority of cases?
Functions have one more surprising feature. Functions are self-descriptive. In other words, you don't have to draw a scheme, just choose good names and divide the general algorithm into functions. Here is an example:
I don't know what this code does, because the functions are not even written. But if I were to read it, it would probably mean that if the first <first> pending order with direction ORDER_TYPE_BUY is successfully selected, it would be canceled (the first function selects, the second one cancels). Since the code would run every tick, no matter how many pending orders there were, each one would be cancelled sooner or later. This also means that any attempt to place a pending buy order would be suppressed - the order would be immediately removed. At the same time, the sell orders will be placed without any problems.
There are only two lines of code and two functions. And the algorithm is non-trivial, and what is more important, it is reliable.
When speaking of MCL, we should mention a bit more about pure functions. Since it is an application language, it is difficult to write anything without relying on data provided by the terminal. After all, this is the main task: to properly interact with the trading environment. Formally, any trading environment is changeable: prices, number of orders, balance changes, etc., etc. Therefore, any function interacting with such a changeable trading environment is not clear. Because the external trading environment can also be considered as some global variable, which is constantly changing. But when we write OrdersTotal(), we do not expect that this function will always return the same value. Instead, we expect it to return the number of pending orders that will naturally vary. Therefore, in MQL, we will consider the functions as clean and reusable, even in case they call functions of external API, like OrdersTotal(). It will be our reasonable indulgence.
Let's continue with the functions...
Thank you very much, Vasily, for the priceless knowledge you share not only with me, but also with those novice programmers who read or will read this topic!
With the same great respect, Vladimir.
I continue studying the MQL5 programming language. While there were no serious remarks on the code writing algorithm of Trailing_Stop Expert Advisor (I remember about the symbol and Magic, I will add it to the algorithm later!), I created input parameters for the EA and wrote the code of the loop that starts the search of open positions.
When I ran the Expert Advisor I saw a problem - in the "Experts" tab of the trading terminal 2 identical messages "A loop has started" appear on every tick, despite the fact that the trading terminal has only one chart of the currency pair EURUSD and only one position is opened on it. And these messages have the same exact time of output.
I fought till midnight but could not win. I cannot understand what the problem is.
The code of the Expert Advisor is written in English, while the comments are in Russian in order to make the process easier. In this EA, I tried to describe everything, as I promised earlier, in a way understandable to a 1st grade student of a programming school.
Regards, Vladimir.
i equals the number of open positions, so many cycles will be with printing
you need to remove the "=" sign in why do you need to go through the loop when the number of open positions is 0. this zero call is where the second print is coming from