why do i keep getting 16 digits in my orderprofit calc? [here's my code]!

 

Hello dear guardians, 

a part of my EA displays the sum of all open buy trades PL on the side of the chart, it works. but the problem is that every couple of seconds the normal 1.23 format profit turns into something like 1.2399999999and then very quickly goes back to 2 decimals(hence the lack of screenshot)

as you know this kind of number makes the displayed label visibly incorrect(still logically correct thou).  so I need to make sure it always displays the profit only with 2 decimals.

i appreicate all your help. here is the code: 

i made a function that calculates the PL of all buys if they exist: 

double getBuySum() {
double display;
if (OrderSelect(p1buy1, SELECT_BY_TICKET) == true)
                  {
                      for (int i= OrdersTotal()-1;i>=0;i--) { 
                              if (OrderSelect(i,SELECT_BY_POS) == true) {
                                    if (OrderTicket() == p1buy1) { display = display + OrderProfit(); } 
                                    if (OrderTicket() == p1buy2) { display = display + OrderProfit(); } 
                                    if (OrderTicket() == p1buy3) { display = display + OrderProfit(); } 
                                    if (OrderTicket() == p1buy4) { display = display + OrderProfit(); } 
                                    if (OrderTicket() == p1buy5) { display = display + OrderProfit(); } 
                              }
                      } return display;
	          } else { return 0; }
}

and this is the label: 

   ObjectCreate(ChartID(),"buyPL",OBJ_LABEL,0,0,0);
   ObjectSet("buyPL",OBJPROP_CORNER,CORNER_RIGHT_UPPER);
   ObjectSetText("buyPL","Buy Pack Profit: " + getBuySum() ,15,NULL,clrWhite);
   ObjectSet("buyPL",13,0);
   ObjectSetInteger(0,"buyPL",OBJPROP_YDISTANCE,10);
   ObjectSetInteger(0,"buyPL",OBJPROP_XDISTANCE,40);

i also have a problem with the label, at first the displayed number in front of " Buy Pack Profit" is 0, then when numbers come in, the words get pushed to the left, they are 4 labels so different labels start to move left and right as their values change. 

how can i fix their position ? 

also if you think my code could be enhanced in any way please feel free to point it out, i'm not very experienced and I'm always trying to enhance my code.

thanks a lot in advance. it's my first time asking online and i consider myself a begginer programmer and i apprieciate your help. 

(couldn't find solution in docs)

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 

Hello Mahdi 

Why do the words get pushed ? - In your object creation you have CORNER_RIGHT_UPPER as the anchor point , labels not having a static size (like edits) ,when the text grows the x axis size grows and so your label being anchored (locked) on its right upper point it appears to expand to the left . The program does this because it assumes you dont want anything to "flood" to the right .

The flashing numbers : - You can turn a double into a string with DoubleToString(value,digits)

  //creation
  double buyPL=12.653575156513151324381321351;
  bool cr=ObjectCreate(ChartID(),"buyPL",OBJ_LABEL,0,0,0);
  if(cr)
    {
    int width=(int)ChartGetInteger(ChartID(),CHART_WIDTH_IN_PIXELS,0);
    ObjectSetInteger(ChartID(),"buyPL",OBJPROP_COLOR,clrWhite);
    ObjectSetInteger(ChartID(),"buyPL",OBJPROP_FONTSIZE,14);
    ObjectSetInteger(ChartID(),"buyPL",OBJPROP_XDISTANCE,width-200);
    ObjectSetInteger(ChartID(),"buyPL",OBJPROP_YDISTANCE,40);
    ObjectSetInteger(ChartID(),"buyPL",OBJPROP_CORNER,CORNER_LEFT_UPPER);
    ObjectSetString(ChartID(),"buyPL",OBJPROP_TEXT,"buyPL : "+DoubleToString(buyPL,2));
    ObjectSetInteger(ChartID(),"buyPL",OBJPROP_SELECTABLE,false);
    ObjectSetInteger(ChartID(),"buyPL",OBJPROP_BACK,false);
    } 
 
Mahdi24w: the normal 1.23 format profit turns into something like 1.2399999999and then very quickly goes back to 2 decimals
Floating-point has infinite number of decimals, it's your not understanding floating-point and that some numbers can't be represented exactly. (like 1/10.)
          Double-precision floating-point format - Wikipedia, the free encyclopedia

See also The == operand. - MQL4 programming forum

Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

If you want to see the correct number of digits, convert it to a string with the correct/wanted accuracy.
          question about decima of marketinfo() - MQL4 programming forum 2016.05.18

 
Lorentzos Roussos:

Hello Mahdi 

Why do the words get pushed ? - In your object creation you have CORNER_RIGHT_UPPER as the anchor point , labels not having a static size (like edits) ,when the text grows the x axis size grows and so your label being anchored (locked) on its right upper point it appears to expand to the left . The program does this because it assumes you dont want anything to "flood" to the right .

The flashing numbers : - You can turn a double into a string with DoubleToString(value,digits)

Thank a lot for sharing your knowledge and the practical solution Lorentzos, I finally understood this and I know it will come to a lot of use in the future, thanks a lot again. 


William Roeder:
Floating-point has infinite number of decimals, it's your not understanding floating-point and that some numbers can't be represented exactly. (like 1/10.)
          Double-precision floating-point format - Wikipedia, the free encyclopedia

See also The == operand. - MQL4 programming forum

Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

If you want to see the correct number of digits, convert it to a string with the correct/wanted accuracy.
          question about decima of marketinfo() - MQL4 programming forum 2016.05.18

thank you too William, I looked around the refrenced links and it helped me better understand these concepts. 



honestly I wasn't expecting the help, I'm glad I got it. I'll definitly try to increase my contribution to this forum. 

Reason: