Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1413

 
Alexey Viktorov:

The IntegerToString() and DoubleToString() functions just represent the number as a string that is passed to the function. In the example

it is the conversion of r into a string and the strings are "added". But before that, the value of r changes.

I don't quite understand the question, but if you need to select only even-numbered elements from the array, the loop should be built this way


Thanks for the explanation.

 
Alexey Viktorov:

The IntegerToString() and DoubleToString() functions just represent the number as a string that is passed to the function. In the example

it is the conversion of r into a string and the strings are "added". But before this the value of r changes.



I cannot understand the process of "adding" values of string variables. I see 2 variants of "adding" strings in my code.

1. They add up the value of the current iteration's string variable with the value of the same string variable from the previous iteration.

2. At the current iteration, the values of the two term variables are added, i.e. IntegerToString(r) + DoubleToString(LoY[r],4).

It seems to me that you mean the first variant.

But then .... The value of IntegerToString(r) on the previous iteration = "0" and on the current iteration = "1" . The result of the addition should be "01".

If in the current iteration before summing up the IntegerToString(r) value changes from previous 0 to the current 1, we still get a two-digit string representation of the number, i.e. "11".

If we use the same algorithm to add the values of the string variable DoubleToString(LoY[r],4), then the result of adding two strings should be values like 1.61041.6105

But Print() prints the addition results as other numbers (such as they should be)
I do not understand it.
Here's your example of how values of string variables are summed up "...... and if you sum up stringsDoubleToString(2.3, 1)+DoubleToString(3.6, 1), it will be 2.33.6 as a string." That is, before addition, the string representation of the number of each variable was 2 characters, but after addition of two string variables, the result of addition is a string representation of the number of 4 characters. Why doesn't the addition in your code result in a number with twice the number of characters?

 
Alexey Viktorov:


I don't quite understand the question, but if you want to select only even-numbered elements from an array, then the loop should be built like this


Actually, this is what I meant by.... Here is the original code completed with BOX 2

int P1=0;
double LoY[31];
void OnTick()
{
if (P1==0)
{
for(int r=0; r<31;r++)
{
LoY[r]=1.6104+(r*2)*0.0001;
Print("-------------------------------------LoY[r]--------------=",  LoY[r]);
P1=1;
}
}
//*************************************************************  Б Л О К    N 2
for(int x=0; x<31;x++)
{
if (Bid < LoY[x] )
{
LoY[x]=Bid;
ArraySort(LoY,WHOLE_ARRAY,0,MODE_ASCEND);
Print("---LoY[0]--",  X, "---LoY[1]--" ,  X,   "---LoY[2]--" ,  X,  "---LoY[3]--" ,  X,  "---LoY[4]--" ,  X, ................  "---LoY[30]--" ,  X );
}
}
}

In the second block, the array element values no longer differ by the same delta as in the first block. The delta between adjacent elements can be different and therefore the algorithm for connecting strings from the first block is not suitable for the second block.... it seems to me.

QUESTION With which MQL4 language construct or function in the second block of the above code, the Print() function, at the end of the loop, will printonly once, with all values of the array elements filled in, arranged in one line, i.e. it will have the following form

Print("---LoY[0]--" , X,"---LoY[1]--", X,"---LoY[2]--", X,"---LoY[3]--", X,"---LoY[4]--", X................"---LoY[30]--", X );

Don't judge me harshly, but I'm not sure I can understand the explanation in words, because the second block is much more complicated than the first.... it seems to me.
Thanks for your help.

 

When programming, there is a lot of routine work, at least at the beginning. You think through the idea, then write the code, and then debug it. Sometimes debugging time takes longer than everything else.

It often happens that the initial idea doesn't work. You start looking from top to bottom in order of what's wrong.

Insert printf() everywhere, and see if this is what you expect or not. This will give you a quicker understanding of how the code works and where you have an error. Otherwise, understanding will take a long time.


Not my business, but try to write beautifully. Choose a style for yourself and stick to it. Place brackets in brackets in steps to make the attachments visible. If you use spaces between statements, then use them everywhere, or conversely, do not use them everywhere.

for(int x=0; x<31;x++)

if (Bid < LoY[x] )

Then it will be easier for you to read your own code.

-----------------

On the second block:

In the second block, lowercase x and uppercase X are different variables, the code will not work the way you want.

It would be better to sort the array outside the loop.

In Print you use X many times, you will print the same number of times.

-----------------

As for the code. It's difficult to understand the question, so I'll tell you what I've understood.

If you write Print in a loop, you will get as many printouts as that loop iterated. If you need to print the output once, place Print after the loop.

In this case, use a variable, into which you will write the value of each iteration, to collect the information. Our variable is a string, so all numbers must be converted to string.

We have a string variable because the Print function accepts a string.

What's going on:

First pass. Before the first pass before the loop, we have an empty variable: string str="";
Next, we assign an empty value to the variable plus "--LoY["+IntegerToString(r)+"]--, "+DoubleToString(LoY[r],4)+",";
here r is 0 and we actually add some strings:

"--LoY["+

IntegerToString(0)+

"]--, "+

DoubleToString(LoY[0],4)+

", ";

Second pass.

To all this we add the information of the second element

"--LoY["+IntegerToString(1)+"]--, "+DoubleToString(LoY[1],4)+", ";

This does not change the string from the first element.



You'd better explain the purpose in words, what you need rather than how you do it. Otherwise we may discuss your code for so long and it will turn out that your goal can be solved in another way.

 
ANDREY:

I can't understand the process of "adding" values of string variables. I see 2 options for "adding" strings in my code.

1. The value of the string variable of the current iteration is added to the value of the same string variable from the previous iteration.

2. At the current iteration, the values of the two term variables are added, i.e. IntegerToString(r) + DoubleToString(LoY[r],4).

It seems to me that you mean the first variant.

But then .... The value of IntegerToString(r) on the previous iteration = "0" and on the current iteration = "1" . The result of the addition should be "01".

If in the current iteration before summing up the IntegerToString(r) value changes from the previous value 0 to the current value 1, we still get a two-digit string representation of the number, i.e. "11".

If we add values of the string variable DoubleToString(LoY[r],4) by the same algorithm , then the result of adding two strings must be values like 1.61041.6105

But Print() for some reason prints addition results as other numbers (such as they should be)
I don't understand it.
Here's your example of adding values of string variables "...... and if you add stringsDoubleToString(2.3, 1)+DoubleToString(3.6, 1), it will be 2.33.6 as a string." That is, before addition, the string representation of the number of each variable was 2 characters, but after addition of two string variables, the result of addition is a string representation of the number of 4 characters. Why doesn't the addition in your code result in a number with twice the number of characters?

In fact, this is just like writing any text. We start with a blank sheet of paper, or should I say blank line. Then we write the first word, append the second word to it and so on.......... And when we see numbers in the text, we write them as a line symbol. That is, if we add 2 + 5, you get 7, and if we just write these same numbers, then first write 2 and then 5... so we get two numbers 25, written next to each other. I don't know how else to explain it.

Actually if you ignore the compiler's warnings, conversion of r-values to a string in your sample is not necessary. Here you may check these two options

Print("Test 1 " + (string)1 + (string)2);
Print("Test 2 " + 1 + 2);
 
Aleksei Stepanenko:


Thank you for your help and advice.

 
Alexey Viktorov:

In fact, it is exactly the same as writing any text. First we have a blank sheet of paper, or should I say a blank line. Then we write the first word, add the second word to it and so on.......... And when we see numbers in the text, we write them as a line symbol. That is, if we add 2 + 5, you get 7, and if we just write these same numbers, then first write 2 and then 5... so we get two numbers 25, written next to each other. I don't know how else to explain it.

Actually if you ignore the compiler's warnings, conversion of r-values to a string in your sample is not necessary. Here you are and check two such variants

Thanks for everything

 
ANDREY:

This is actually what I meant by.... Here is the original code, supplemented by BOX 2

In the second block, the array element values don't differ by the same delta as in the first block. The delta between adjacent elements may be different and therefore the algorithm for connecting strings from the first block is not suitable for the second block.... it seems to me.

QUESTION With which MQL4 language construct or function in the second block of the above code, the Print() function will printonly 1 time after the loop is finished, with all values of the array elements filled in one line, i.e. it will have the following form

Print("---LoY[0]--" , X,"---LoY[1]--", X,"---LoY[2]--", X,"---LoY[3]--", X,"---LoY[4]--", X................"---LoY[30]--", X );

Don't judge me harshly, but I'm not sure I can understand the explanation in words, because the second block is much more complicated than the first.... it seems to me.
Thanks for the help.

And why sort the array on each iteration of the loop? Because if you replaced value of the 0-th element of the array and after sorting it went to the end of the array, then zero will be the value that was the first, and the first will be the value that was the second. Thus, the value of the first array item will be skipped and you get "rubbish". All in all, all your code is something out of place.

What for do you need the first block in which you are filling the array? Fill the array right away with the values you need and then sort it. True, you'll have to run one more loop to fill the string and print. I wish mql4 had ArrayPrint()... But it's only experiments, and you won't actually need to print the array values.

 
Aleksei Stepanenko:

Better explain your goal in words, what you need, not how you do it. Otherwise, we can discuss your code for so long, and it will turn out that your goal can be solved in another way.

While studying mql4 (and a little bit of mql5), I am writing various codes. This includes codes that use arrays. It's often necessary to check the array items in some place of the code. I have learned how to do it using Print() in a loop. But in this case, Print() is printed at each iteration. When there are many iterations, it is inconvenient both for the program and for reading. You have correctly said that Print( ) must be removed outside the loop for Print() with values of the array items to be printed only once. I had guessed it before your words.

I learned very quickly to put Print() outside the loop. And now I'm trying to learn how to make Print() print all the values of array elements in one line.

I have been explained how to do it with respect to the first block of my code. So far, in general terms, I have understood this technology. I will continue racking my brains to understand the details of this method.
But in the second block of my code, as it seems to me, the method from the first block is not suitable because, unlike the first block, the delta between the values of the array elements in the second block is different.
If I'm right and the method from the first block doesn't fit to the second one, I would be grateful if you could show me how to do the same thing that was done in the first block, but with reference to the second block.

I corrected the second block. I put Print() and ArraySort() outside the loop. I have removed the X variable from Print() so as not to mislead you.

int P1=0;
double LoY[31];
void OnTick()
{
if (P1==0)
{
for(int r=0; r<31;r++)
{
LoY[r]=1.6104+(r*2)*0.0001;
P1=1;
}
}
//*************************************************************  Б Л О К    N 2
for(int x=0; x<31;x++)
{
if (Bid < LoY[x] )
{
LoY[x]=Bid;
}
}
ArraySort(LoY,WHOLE_ARRAY,0,MODE_ASCEND);
Print("-LoY[0]-", (знач.эл.масс с инд.0), "---LoY[1]--" ,  (знач.эл.масс с инд.1),   "---LoY[2]--" ,  (знач.эл.масс с инд.2),  "---LoY[3]--" ,  (знач.эл.масс с инд.3),................  "---LoY[30]--" ,  (знач.эл.масс с инд.30) );
}
 
Print("-LoY[0]-", DoubleToString(LoY[0],4), "---LoY[1]--", DoubleToString(LoY[1],4), "---LoY[2]--" , DoubleToString(LoY[2],4), "---LoY[3]--" , DoubleToString(LoY[3],4),................  "---LoY[30]--" ,  DoubleToString(LoY[30],4) );

or make a separate array print function:

void PrintArray(double &eArray)
   {
   string eStr="";
   int eSize=ArraySize(eArray);
   for(int i=0; i<eSize; i++)
      {
      eStr+=IntegerToString(i)+": "+DoubleToString(eArray[i],4)+", ";
      }
   Print(str);
   }

And call it from anywhere

PrintArray(LoY);
Reason: