Self-learning the MQL5 language from scratch - page 48

 
If the immediate purpose is to implement a simple trailing stop, you should continue writing the script by adding the for and while loops.

I'm not sure that the "patience enough" script has a logical extension. It is probably better to move on to a new idea that requires having:

1. An if-else condition tree.
2. Calculation functions.
3. Cycles.

Since we will have to work in the field of algorithmic trading, it would be more convenient if the script were related to the trading strategy. Think about it.

 
For a better understanding of loops:

A loop allows you to pass a section of code closed in its body over and over again. The results of each pass will be different from the results of other passes, because the values of the variables/called functions may be different at each pass. The number of loop iterations is set by the programmer, the value of the variable or function - it depends on the specific code.
 
MrBrooklin:

Alexei, are you kidding? Yes, I'd like to learn the basics first!

Respectfully, Vladimir.


Not a lot. Judging by the dynamics and your base, theory and practice - it's your profile from start to finish. So why not... Not now, but later... When you get the hang of it and are on a first-name basis with the code.
 
Реter Konow:
If your immediate goal is to implement a simple trailing stop, continue writing the script by adding the for and while loops.

I'm not sure that the "patience enough" script has a logical extension. It is probably better to move on to a new idea that requires having:

1. An if-else condition tree.
2. Calculation functions.
3. Cycles.

Since we will have to work in the field of algorithmic trading, it would be more convenient if the script were related to the trading strategy. Think about it.

Thank you, Peter, for supporting my intention to equip New7.mq5 script with trailing stops, especially now, when I have started studying cycles. By the way, I have already tried the Sleep function in the script. It was recommended to use this function when writing the trailing stop. Where do I start? Probably, it would be better to describe the entire trailing stop algorithm in words first, and then proceed to writing the code?

Sincerely, Vladimir.

 
Aleksey Masterov:

Not much. Judging by the dynamics and your base and theory and practice, this is your profile from start to finish. So why not... not now, but later... When you get the hang of it and are on a first-name basis.

Thank you, Alexey, for your faith in me. All that remains is not to pump!

Sincerely, Vladimir.

 
MrBrooklin:

Thank you, Peter, for supporting my desire to equip New7.mq5 script with trailing stops, especially now, when I started studying cycles. By the way, I have already tried the Sleep function in the script. It was recommended to use this function when writing the trailing stop. Where do I start? Probably, it would be better to describe the entire trailing stop algorithm in words first, and then proceed to writing the code?

Sincerely, Vladimir.

Objectively speaking, a simple trailing stop cannot be written in the script. Let me explain: trailing stops do not exist by themselves, in a "vacuum", they are "tied" to an open position, which, in its turn, is "tied" to a strategy, and the strategy is implemented only in an EA.

The problem and the complexity of trailing in the script lies in the fact that you need to collect information about open positions and their orders in the loop, and then select the required order on the required symbol and calculate its modification. It's complicated and confusing, but for an EA, everything is much easier. First, you will already know which order to modify, and second, you will know when to modify, because the OnTick event comes.

So, you put an "order pending" in the script, then it triggers, a position is opened and you can trail the stop... What you need for this:

1. To loop the script.
2. Write the function of price change event fixation on the text symbol.
3. write function of stop order modification.
4. Write the condition of script unloading (exit from the loopless cycle) on position closing.

I have sketched the script outline approximately, but I will need to think about it more seriously.

P.S. The Sleep function is used to delay code execution when it's needed. For example, when making a request to the server or waiting for an event. In a trailing script this function will definitely be needed.
 
Реter Konow:
Programmers are afraid to use global variables because of errors occurring when changing their values. It creates a situation when an error is difficult to localize because each function can change them. Of course, only those variables must exist in the global scope which all the program functions must see. It cannot be otherwise.

I've always liked using global variables because they provided a fast growth of functionality, and the programme turned into a huge, active construction site. I've often been criticised for the way I write code, but that's why it's a construction site: it's cleaned up after the basic building work is done, and when the house is built, you can start tiling, painting, and cleaning up. Until then, the priority is to assemble the formwork and pour concrete).

However, programmers think differently. They will 'clean' and 'scrub' their code, even if it is two and a half lines. They will scrub their code even if it is two and a half lines long, but it will shine like... like a new coin.) This attitude to code is justified by their profession which they live by, but from the creative viewpoint, they are stiff and poorly developed. That's the way it is...

My advice: learn to write properly, but sometimes allow yourself to move away from the rules and experiment to gain a more varied experience. It will help you learn and you'll learn it faster.

There's an observation that once you start crutching, it's hard to stop, and as a consequence, the project code turns into what they call dre...code.

Let me explain:

  1. You have a project with an intermediate working solution and the number of implemented features counts=0.
  2. Our task is to implement the ++count feature.
  3. To add the features we need:
    • to write methods of object tree and connect all these things to event handlers with logic (estimated time 3 hours *count; count=0).
    • write a crutch in the form of global variable and use it in several methods, where we need it (estimated time 15 min *count.).
  4. Autonumbering bug (this is a bug report for meta-quotes).
  5. Naturally, we have chosen a crutch (it's really hard to make ourselves work in this case)
  6. if (we did it) goto 2
  7. else everything goes to hell, shouting help-mi and reading hilarious comments that it's wrong to do that.

I hope you paid attention to the fact, that the counter of implemented features, increases time of implementation of the next feature, but when implemented correctly, it resets to zero?

This is a very exaggerated idea, but that's how it works in real life.

What I mean is if you don't rewrite the project after you implement all the features it will go into production as an unreadable spoiler. And then, the life cycle of any project leads to a headache for the management: either to put the entire team on a global refactoring of all those things that were spun (and the competitors don't sleep, they, evil guys, write new features), or keep writing crutches and patching bugs, leaking in torrents.

 
Реter Konow:
Objectively speaking, a simple trailing stop will not work in the script. Let me explain: trailing stops do not exist by themselves, in a "vacuum", they are "bound" to an open position, which is in turn "bound" to the strategy, and the strategy is implemented only in an Expert Advisor.

The problem and the complexity of implementing trailing in the script is that you need to collect information about open positions and their orders in the loop, and then select the right order on the right symbol and calculate how to modify it. It's complicated and confusing, but for an EA, everything is much easier. First, you will already know which order to modify, and second, you will know when to modify, because the OnTick event comes.

So, you put an "order pending" in the script, then it triggers, a position is opened and you can trail the stop... What you need for this:

1. I want to loop the script.
2. The function of price change event fixation on the symbol.
3. Write function of stop order modification.
4. Write the condition of script unloading (exit from the loop) on position closing.

I have sketched the script outline approximately, but I will need to think about it more seriously.

P.S. The Sleep function is used to delay code execution when it's needed. For example, when making a request to the server or waiting for an event. In a trailing script this function will definitely be needed.

Peter, do we create trailing code in the script? Perfect! Now I take what you've listed as basic sections and begin to describe them in words, so it's clear how I should write functions, loops, etc. later. Is this correct?

Regards, Vladimir.

 
MrBrooklin:

Peter, so we create the trailing code in the script? Great! What you have listed I now take as basic sections and start to describe them in words, so that it is clear how to write functions, loops, etc. later. Is this correct?

Regards, Vladimir.

Yes, correct.
 
Vladimir Simakov:

There's an observation that once you start crunching, it's hard to stop, and as a consequence, the project code turns into what's known as d.c.

Let me explain:

  1. You have a project with an intermediate working solution and the number of implemented features counts=0.
  2. Our task is to implement the ++count feature.
  3. To add the features we need:
    • to write methods of object tree and connect all these things to event handlers with logic (estimated time 3 hours *count; count=0).
    • write a crutch in the form of global variable and use it in several methods, where we need it (estimated time 15 min *count.).
  4. Autonumbering bug (this is a bug report for meta-quotes).
  5. Naturally, the crutch version was chosen (it's really hard to make ourselves work in this case)
  6. if (we did it) goto 2
  7. else everything goes to hell, shouting help-mi and reading hilarious comments that it's wrong to do that.

I hope you paid attention to the fact, that the counter of implemented features, increases time of implementation of the next feature, but when implemented correctly, it resets to zero?

This is very exaggerated, but that's how it works in real life.

What I mean is if you don't rewrite the project after you implement all the features it will go into production as an unreadable spoiler. And then, the life cycle of any project leads to a headache for the management: either to put the entire team on a global refactoring of all those things they have spun (and competitors don't sleep, they, evil sons of bitches, write new features), or keep writing crutches and patching bugs, leaking in torrents.

Even though this message is mainly addressed to Peter, I ask you to write it without any slang, in order to fully understand your messages, in a language accessible to the 1st year pupil of programming school, as the topic is for beginners from scratch.

Regards, Vladimir.