![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
Igor, the desire to learn MQL5 language is not lost, on the contrary, it only increases. I'm just torn, why I'm so dumb and cannot understand simple things with this loop operator. The main thing is that without the loop operator the code works perfectly, but with the loop it is a complete mess. I will get to the bottom of this anyway. However I've got much less time for self-study since the quarantine period is over and I've got rare moments to study the language at work. Mostly I study the language in the evening and late at night.
I need to solve 2 tasks to finally get down to writing the Trailing Stop code:
Vladimir, you are getting into some kind of weird stuff. You have been given a template: https://www.mql5.com/ru/forum/352460/page58#comment_18726492 You ignored it and decided to go ahead with your wits. This way is difficult and may take years. On your way, study C, C++, Rust, C#, polymorphism, inheritance, dereferencing of pointers, dynamic type conversion: Check or drive. You must take the shortest way to the aim. If you want trailing, write trailing. You don't need to study C and C++ to do it. This all as an elective if you feel like it.
The situation with the for loop is illustrative. It's a powerful construction that must be applied to the right place. You have netting and an auxiliary algorithm. That's why there is no enumeration of positions, there are no mages, so you don't have this loop. Of course, everything will work without it, but the loop only produces oil. But you stubbornly try to use it, it seems to be an end in itself: to use as much and as much as possible, to have as much as possible.
Vladimir, you are getting into a bit of a rut. You were given a template: https://www.mql5.com/ru/forum/352460/page58#comment_18726492 You ignored it and decided to go ahead with your mind. This road is difficult and may take years. On your way, study C, C++, Rust, C#, polymorphism, inheritance, dereferencing of pointers, dynamic type conversion: Check or drive. You must take the shortest way to the aim. If you want trailing, write trailing. You don't need to study C and C++ to do it. This all as an elective if you feel like it.
The situation with the for loop is illustrative. It's a powerful construction that must be applied to the right place. You have netting and an auxiliary algorithm. That's why there is no enumeration of positions, there are no mages, so you don't have this loop. Of course, everything will work without it, but the loop only makes you feel like oil. But you are obstinately trying to use it, so it seems to be an end in itself: you may use as much of everything and anything to keep in stock as possible.
Hello, Vasily!
Thank you for timely joining the topic and directing me on the right way as always. Now let me explain everything in one order:
Vasily, I am extremely grateful to you for guidance and constructive criticism! I also express my gratitude to all the participants of this theme who help me in my self-study of MQL5 programming language from scratch.
Self-study continues. To be continued.
Regards, Vladimir.
Good afternoon everyone!
If I wanted to add something interesting, I think the idea of "self-training in MQL5 from scratch" is not quite right. If a person is 0 in programming, he has to learn how to program first. If the aim is to learn how to program in MQL from the scratch, a person must first learn howto program in C, at least the very basics, and then learn to write in MQL, because MQL is in fact C, but it is specialized for a certain task, a person doesn't understand how loops work, and already trying to write programs. It's like trying to learn Latin without knowing Russian...
Another thing is that C is not a very friendly language to learn programming, it is simple, but it is too concise, it is difficult for a beginner to read and understand code without having basic understanding of algorithms, data types, Boolean algebra. In C three characters in a row may mean several strings in another language.
To learn to program simply from scratch, for the base, to learn to talk to a computer in one (no matter what) language, I would recommend starting with Pascal, the easiest book to start with it (2-3 weeks at ease), then C (there after Pascal max week with diligence, master the syntax, I mean the basic language skills!) and then take up specialized languages like MQL, because MQL is C, which has a little twist and simplification, since it is designed for a single task and the compiler knows the obvious. And C in turn is a kind of RAR-compressed Pascal, but Pascal is almost English =).
Now about loops:
When I learned programming, the analogy with sums in algebra helped me, where you specify the initial value of n, the final value of n and the formula with this n to calculate.
Hello Andrey! I completely agree with you about the basis. I don't have it and never had it. But as the saying goes - Moscow wasn't built at once! Thank you for your advices and tips!
Regards, Vladimir.
Yeah. I agree - his codes are good too! You're getting yourself worked up over nothing. Take your time sorting out the code without rushing and that's it. It's all elementary here - select and trawl: (how much easier - just your topic of choosing the NEED position... :-))
Besides that, include files - it means that they are in your code as functions as well and that's it.
Thanks, Roman, for the codes and links! I am dealing with the codes calmly and surely do not hurry anywhere!
Sincerely, Vladimir.
Good day and good mood everyone!
I continue studying the MQL5 programming language . Today, as promised, I'm posting for everyone to see the code of the Expert Advisor template designed for writing a trailing stop. Due to the fact that the EA template code turned out to be quite cumbersome, so we had to reduce the comments as much as possible. The full version of the Expert Advisor with detailed comments on each line of code, in a presentation accessible to a student of the 1st grade of a programming school, will be published later in the form of a file with the working name Trailing_Stop.mq5 so as not to produce another "footcloth" on the site.
Before publishing the code, the EA was tested in the strategy tester . No problems have been identified. The EA uses a netting position accounting system. This accounting system implies that at one time there can be only one open position on the account for the same symbol (financial instrument).
At the moment, the Expert Advisor has the ability to:
In addition, the EA has a pre-installed template, kindly provided by Vasily Sokolov, which will be filled with code during the self-learning process.
A WARNING!!!
The Expert Advisor is developed according to the self-study plan for a demo account and for educational purposes only! Not intended for trading on a real account and making a profit!
With best regards, Vladimir.
Good day and good mood everyone!
I continue studying the MQL5 programming language. Yesterday I wrote the code of Stop Loss at Breakeven for Sell and Buy positions. I am pasting the code without any comments yet. I will publish the full version of the Expert Advisor with a detailed description of each code line, in a form accessible for 1st year pupils of the programming school, later in the file with the working title Trailing_Stop.mq5. Before publishing the code, we checked the performance of the Expert Advisor in the strategy tester. No problems were found.
Regards, Vladimir.
Good day and good mood everyone!
I continue studying the MQL5 programming language. Today, as promised, I am pasting the code of the template of my Expert Advisor designed for writing a trailing stop. Due to the fact that the code of the template EA is quite cumbersome, I had to cut the comments as short as possible. The full version of the Expert Advisor with detailed comments on each line of the code, in a form accessible for a 1st grade programming school student, will be published later as a file with the working name Trailing_Stop.mq5, to avoid creating another spoilage on the website.
...
Very good. The main idea is well captured. Especially I like the development process:
Good day and good mood everyone!
I continue studying the MQL5 programming language. Yesterday I wrote the code for Stop Loss at Breakeven for Buy and Sell positions.
I.e. everything is correct. You do not have to solve the problem all at once in one place. You should gradually solve it, just like you did. First, you describe the basic logic in the OnTick function and basic functions like OpenBUY, OpenSELL, TickUP, TickDown.
Then, when this basic code is debugged, compiled and works within its capabilities, you start describing the other functions. This may be done the next day or even a week. This means that you don't have to change the main code. I think you can see why.
Now you need to hone this manner of development: continue writing functions, linking functionality through them to the existing code. The existing program should work correctly. The complexity should not increase. The functions to be added must return simple, clear results. Their names should be as simple and clear to you. If you can't do this, it may be because you need to write not one but two or even three functions to solve the problem. At first, the overall framework of the application will be hard to define. That's why you'd better ask advice of more experienced colleagues. Over time, you will learn to develop such templates on your own.
As time goes by,you will need to use more and more language constructs within functions to make them fit your needs.At this point, you will gradually and most importantly, organically extend your knowledge of language constructs, learn loops, arrays and even working with pointers.
In fact, you have already learned the hardest part of programming, although you have not yet consolidated the material you have learned. All you need to do now is to repeat it many times to consolidate it. From here there will be nothing fundamentally new, everything is the same: General template -> Description of the names of functions and their parameters-> Writing the contents of these functions -> Arrangement of these functions in the central unit. That's it. All that remains is to perfect the skill and be consistent. Various additional constructions you start using like loops and arrays are just details, sometimes cleverly done, but nothing more than details.
As there is no limit to perfection, I will add a few more comments on the code:
I have highlighted two non-trivial places in yellow.
1) Note that the code is repeated in the first if and in the next else. The only difference is in the last line and end action (OpenBUY, OpenSell).
2) Conditions for getting into the else block are not obvious. They are not visible because of the abundance of ??. In fact, they only depend on the last line:
This is a sure sign that a function is missing here.
We need to write a function that returns true if the time to open the position corresponds to the given one (I will write it later)
Very good. The main point is captured correctly. Especially liked the development process:
I.e. Everything is right. You don't have to solve the problem all at once in one place. You do it gradually, as it happened in your case. First, you describe the basic logic in the OnTick function and basic functions like OpenBUY, OpenSELL, TickUP, TickDown.
Then, when this basic code is debugged, compiled and works within its capabilities, you start describing the other functions. This may be done the next day or even a week. This means that you don't have to change the main code. I think you can see why.
Now you need to hone this manner of development: continue writing functions, linking functionality through them to the existing code. The existing program should work correctly. The complexity should not increase. The functions to be added must return simple, clear results. Their names should be as simple and clear to you. If you can not do this, it may be that you need to write not one but two or even three functions to solve the problem.
In order for a function to perform its intended tasks,you will eventually need to use more and more language constructs within such functions. That's when you will gradually and most importantly, organically extend your knowledge of language constructions, learn loops, arrays and even work with pointers.
Hello, Vasily! Thank you very much for your timely suggestions and support. Your commentaries on the role of functions and principles of program code construction have really helped me in learning the MQL5 programming language:
Now that the information in my head is structured, it is easier to understand the code written not only by me, but also by other programmers. I hope this topic will be a good help for those who are starting to learn the MQL5 programming language from the scratch.
Regards, Vladimir.