Learning logic - page 5

 
Vinin:


That's not my line. It is the author's

Optimal start() code

These 2 lines look strange, to put it mildly:

   limit = Bars - counted_bars-1;
   if(Bars - counted_bars > 2) limit = Bars - nPeriod-1;

better like this:

   if(Bars - counted_bars > 2) limit = Bars - nPeriod-1;
   else limit = Bars - counted_bars-1;
 
PapaYozh:

I don't understand why we have to dance around boolean variables when we can write it this way:

I liked it, thank you for reminding me, PapaYozh. I've created such constructions myself - and forgot...
 
Vinin:


And logic too, the logic of thinking.

The third option is to abandon the logical conditions in the indicator completely. The question arises - is it possible.

Let's try .

For this we will add a couple of logical variables

The entire code of the start function now looks like this


It seems to me that it can be simplified

for(int i = 0; i < limit; i++)
{
// Option three_v1

high=iMA(NULL,0,period,0,MODE_EMA,PRICE_HIGH,i);
low=iMA(NULL,0,period,0,MODE_EMA,PRICE_LOW,i);
atr=high-low;
MaTDn[i] = low - atr;
MaTUp[i] = high + atr;
}

 
Mathemat:
I liked it, thank you for reminding me, PapaYozh. I myself created such constructions - and forgot...

In my opinion, there is excessive use of the Symbol() function call.

I would write it that way:

   string smbl=Symbol();
   showEUR  = ( StringFind(smbl, "EUR", 0) != -1);
   showUSD  = ( StringFind(smbl, "USD", 0) != -1);
   showGBP  = ( StringFind(smbl, "GBP", 0) != -1);
   showCHF  = ( StringFind(smbl, "CHF", 0) != -1);
   showJPY  = ( StringFind(smbl, "JPY", 0) != -1);
But that's not about logic :)
 
Vinin:

It's really quite simple. There is a desire to help others do the best code, and to learn from others.

It's no secret that one eventually develops quite certain stereotypes which sometimes (most often) help and sometimes hinder.

I will prepare a variant of the indicator and try to explain what has been done and why. If you are interested, of course.


There is no way to explain by example how to make optimal code - there will be too many omissions.

The point is that there are a lot of rules of creating an optimal code. For example, not many of those who are familiar with the MQL4 language know that it is much easier for a processor to perform addition and subtraction operations than multiplication and division. Not many people know that from the viewpoint of code optimization it is better to write a=b+b than a=b*2. So I think that to understand the logic of programming and optimization it is much more useful to send those interested to works on constructing an optimal code and studying algorithmic languages as well as to make users accustomed to creating flow-charts of future programs to avoid omissions and minimize rework.

I enclose a link to a very good book on code building for this post.

С. S. McConnel "The Perfect Code".


You may download the book here: http://depositfiles.com/files/ebk6t6t5h (file size 21 Mbytes. Format - djvu)

So, generally speaking, this thread can only deal with special cases of the source and optimized code sections with explanations of why the optimized code section is better than the source one and why it is optimized this way. I think that's exactly what you wanted originally.

 
drknn:


Attached to this post is a download link to a great book on code building.

С. S. McConnel "The Perfect Code".

+1, very useful book.
 
drknn:

So, in general, in this thread we can only discuss special cases of the source and optimized code sections with explanations of why the optimized code section is better than the source and why it is optimized in such a way. I think that's exactly what you wanted from the very beginning.

Not bad either, but still it would be good to either clarify a branch name or start to study logic.

I really don't know how to do the second one myself...))

 
denis_orlov:

It's not bad either, but it would be good to either clarify the name of the branch or start studying logic.

I really don't know how to do the second one myself...))


Logic(Greek λογική - "science of reasoning", "art of reasoning" from λόγος - "speech", "reasoning", "thought")- the science of forms, methods and laws of intellectual cognitive activity, formalized with logical language. Since this knowledge is derived from reason, logic is also defined as the science of the forms and laws of correct thinking. Because reasoning takes the form of reasoning in language, of which proof and disproof are special cases, logic is sometimes defined as the science of modes of reasoning or the science of ways to prove and disprove. Logic as a science studies ways of reaching the truth in the process of cognition in an indirect way, not from sense experience, but from knowledge obtained earlier, so it can also be defined as a science about ways of deriving inference knowledge.

The deductive knowledge, obtained through the application of the laws of logic and methods of logical thinking, - the purpose of any logical action, aimed at achieving the truth and the application of the resulting knowledge to a deeper knowledge of the phenomena and events of the world.

One of the main tasks of logic is to determine how to arrive at a conclusion from a premise(proper reasoning) and how to gain true knowledge about the subject of thought in order to gain a deeper understanding of the nuances of the thought object under study and its relations to other aspects of the phenomenon in question.

Logic serves as one of the tools of almost every science.

 
denis_orlov:

It's not bad either, but it would be good to either clarify the name of the branch or start studying logic.

I don't really know how to do the second one myself...)


I can share what it was like for me.

It took me three years to try to understand logic as a science - I still could not grasp the essence, e.g. why do I need logic at all. It was just a set of almost useless patterns, but I felt that something was wrong, so I continued to read through it, to try to reason with its schemes, to solve all sorts of logical problems with the help of formulas. And then, out of the blue, it suddenly dawned on me... Then, when I finally realized the value of this discipline, I began to actively involve my friends in the study of logic. I was literally "on fire" with logic. My friends were actively drawn into all the logic humor, the beauty of hard conclusions, the insolubility of paradoxes, and while I was around, they learned at least something. But when left alone with themselves, they faded away like the embers of a fire. None of them had ever lit the fire of his own spirit. Here I understood that they were interested in me, but not more - none of them had the kicking power behind them that would compel them to enter into this science. And then all those who had actually studied Logic, for myself, I had divided into two categories. They were all forced to study it. There was no getting away from it. Some professors who teach logic at universities say that it is very hard for students. On the contrary, they are surprised at how hard it was for them and how easy it is to apply. So, for myself I have split all those forced into two classes - those who fell in love with logic while studying it, and those who passed their credits and forgot about it. I always find it pleasant to talk to those who have fallen in love, because they understand you in a heartbeat. But sometimes it is hard to deal with the forgetful - such people play Russian roulette with their own death. The fact that consciously can only act on a pre-made plan. This is the only way to restore order to their lives. Making a plan of action is a thought process. If it is not done correctly, the result will not be good. He who has chaos in his head, he also has a mess in his affairs. So it turns out that thinking controls our lives. And to abandon Logic here is a quick road to the grave.

:) Enough is enough, otherwise I can go on for hours talking about such things and it will lead us away from the topic of this branch in the end.

P.S.

I can only add that I was in such a difficult situation in my life that I didn't know what to do - all my plans and hopes had crumbled. It hurt like hell. I was suffocating with hopelessness. I decided that until I understood the situation, I could not decide what to do with my life and how to relate to those around me. I knew from books that there is such a science as Logic. At this point, I decided that I had to learn it, so that my conclusions were correct. The complexity and pain of my life situation was the force that spurred me on, making me seek answers to my questions not only in logic, but also in many other sciences. That's all in the past now. Why am I telling you this? I just wanted you to understand what drove me to study logic. So to speak, I showed you by example that I am not an exception and I too had to study this discipline from under the stick. But I didn't study it at uni, but on my own.

 
nikost:


It seems to me we can simplify it even further

for(int i = 0; i < limit; i++)
{
// variant three_v1

high=iMA(NULL,0,period,0,MODE_EMA,PRICE_HIGH,i);
low=iMA(NULL,0,period,0,MODE_EMA,PRICE_LOW,i);
atr=high-low;
MaTDn[i] = low - atr;
MaTUp[i] = high + atr;
}


I tried to keep the rendering as in the original code. The ATR is calculated differently. If you compare the two indicators, you will see the difference.