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

 
-Aleks-:

Please help - I can't understand why I get zero when calculating the VesMass array in i>0 values ?

It turns out that I should write not 4 and 5, but 4.0 and 5.0 - what is the logic - it's not clear to me...
 
-Aleks-:

It turns out that you should write not 4 and 5, but 4.0 and 5.0 - what is the logic - it's not clear to me...
Read the documentation and it will become clear. It's good that you did it yourself.
 
Alexey Viktorov:
Read the documentation and it will become clear. It's good that you did it yourself.

Yes, I've read about something like this before - so I decided to try it, but it's not clear where the logic behind it is... and I couldn't find the answer in the documentation, by the way.
 
-Aleks-:

I've read about something like this before - so I decided to try it, but it's not clear where the logic behind it is... And I couldn't find the answer in the documentation, by the way.


This is a peculiarity not only in MCL, in C++ and other programming languages too - division of two integers or variables of type (int) will give truncated to integer result. In your case 4/5=0.

You can write 4.0/5 or (double)4/5 - then the result is a real number.
 
Vitalie Postolache:


This is not only a feature of ICL, in C++ and other programming languages it is the same - dividing two integers or variables of type (int) will give a truncated result to an integer. In your case 4/5=0.

You can write 4.0/5 or (double)4/5 - then the result is a real number.

I understand that, but I don't understand the logic of the solution - i.e. the need.

 
-Aleks-:

I understand that, but I don't understand the logic behind the solution - i.e. the need.


Well you don't argue "why 2*2=4 in arithmetic, what is the logic of the solution"? You just take it as an axiom and use it. That's how it is with dividing two integers.
 
Vitalie Postolache:

Well you don't argue "why 2*2=4 in arithmetic, what is the logic behind the solution"? You just take it as an axiom and use it. It's the same with dividing two integers.

I'll check with addition...

 


Kot:


The line is a trend line. I changed properties to a ray. It did not help. If I wanted it to output the price regardless of the zero bar position (above, below).

This code doesn't work either.

int start() {

double pr=ObjectGetValueByShift("line",0);

If (Bid==pr){

Alert (pr);}

return(0);}


Is the line name spelled correctly?

Try this script, and show what it says in the logs.

void OnStart()
  {
     double pr;
     int obj_total=ObjectsTotal(); 
     string name;
     int i; 
     
   for(i=0;i<obj_total;i++) 
     { 
      name=ObjectName(i); 
      if(ObjectType(name)!=OBJ_TREND) continue; 
      PrintFormat("Объект %d:     Имя объекта - %s",i,name); 
      pr=ObjectGetValueByShift(name,0);
      PrintFormat("Объект %d:     Цена - %.5f",i,pr);
      
     }  

}

The script detects the object, writes the name correctly. But the price is absolutely left-handed. It is marked on the photo with a horizontal line. It gives these prices regardless of whether the bars crossed the line or not.

Files:
23.png  62 kb
 
Kot:


The line is trending. Changed the properties to beam. It did not help. Ideally, the price should be displayed regardless of the position of the zero bar (top, bottom).

This code doesn't work either.

int start() {

double pr=ObjectGetValueByShift("line",0);

If (Bid==pr){

Alert (pr);}

return(0);}

Bid may easily jump your line, it is not obliged to stop at each point (i.e., you won't get the exact equality).

change the condition to for example if ((MathMin(Close[1],Open[0])<=pr && Bid>=pr) || (MathMax(Close[1],Open[0])>=pr && Bid<=pr)) or similar...

 
Maxim Kuznetsov:

Bid can easily jump your line, it is not obliged to stop at every point (i.e., the exact equality will not happen)

change the condition to for example if ((MathMin(Close[1],Open[0])<=pr && Bid>=pr) || (MathMax(Close[1],Open[0])>=pr && Bid<=pr)) or similar...


Thank you! got the idea.

What if the situation is different. There are horizontal lines with different names on the chart. You need the script to calculate which line corresponds to which price.

Reason: