Color change in button and decimals.

 

Hi! I need help. I have made these buttons with the intention of informing me of the current benefits as well as that they change color depending on whether the benefit is "0" with gains or losses (gray, green or red) and with only two decimal places. In principle it works for me but it does not change color and the decimals of two become 9 or 10. Can someone correct the code so that it works? Thank you very much in advance.


//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//------------------------------------------------------
//+--- Boton Beneficio Actual € 

double Beneficio = 0;
color clrProfit;

for(int b= OrdersTotal()-1; b>=0; b--)
    {
    
    Beneficio = DoubleToString(AccountProfit(),2);
    
    if (AccountProfit() == 0)
      clrProfit = clrSilver;
      else if (AccountProfit() > 0)
        clrProfit = clrLimeGreen;
        if (AccountProfit() < 0)
          clrProfit = clrCrimson;
          
    CrearBoton("Boton6", 796, 2, 60, 18, clrBlack, clrProfit, clrBlack,"ORBITRON",8,"Profit :");
    ButtonTextChange(0,"Boton7",Beneficio);      
    CrearBoton("Boton7", 855, 2, 70, 18, clrBlack, clrProfit, clrBlack,"ORBITRON",8,Beneficio);
              
    }
 
//------------------------------------------------------
//===================================================================//
//+------------------------------------------------------------------+
//| Cambio de valor                                                  |
//+------------------------------------------------------------------+
bool ButtonTextChange(const long   chart_ID=0,    // chart's ID 
                      const string name="Button", // button name 
                      const string text="Text")   // text 
  { 
//--- reset the error value 
   ResetLastError(); 
//--- change object text 
   if(!ObjectSetString(chart_ID,name,OBJPROP_TEXT,text)) 
     { 
      Print(__FUNCTION__, 
            ": ¡no se pudo cambiar el texto! Código de error =" ,GetLastError()); 
      return(false); 
     } 
//--- successful execution 
   return(true); 
  } 

//+------------------------------------------------------------------+
//===================================================================//
 

Take a standard example ( CButton ) and add this code for button #1:

//+------------------------------------------------------------------+
//| Event handler                                                    |
//+------------------------------------------------------------------+
void CControlsDialog::OnClickButton1(void)
  {
   Comment(__FUNCTION__);
   static bool reverse=false;
   reverse=!reverse;
   if(reverse)
     {
      m_button1.ColorBackground(clrBlue);
     }
   else
     {
      m_button1.ColorBackground(clrRed);
     }
  }

Result:

color

Documentation on MQL5: Standard Library / Panels and Dialogs / CButton
Documentation on MQL5: Standard Library / Panels and Dialogs / CButton
  • www.mql5.com
CButton - Panels and Dialogs - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Vladimir Karputov:

Take a standard example ( CButton ) and add this code for button #1:

Result:


Hello Vladimir Karputov!
         Thank you very much for the help but I think the information is in mql5 and what I need must be Mql4, I do not know if it is compatible or not. On the other hand I think I have explained myself wrong, I do not need that when pressing the button it changes color, I use the buttons as information windows that give me the current profit or losses and that also change color depending on whether it is in profit , losses or to "0". Can you help me? The code that I have posted sometimes works and the majority is always in GRAY color, the other problem is the decimals of the Profit or loss, I need the profit or loss to be in this format "25.35" but I continually get this format "23.3499999999" and I can't figure out how to remove those annoying "99999999".
Greetings and a thousand thanks in advance.
 
xyon126 :
Hello Vladimir Karputov!
         Thank you very much for the help but I think the information is in mql5 and what I need must be Mql4, I do not know if it is compatible or not. On the other hand I think I have explained myself wrong, I do not need that when pressing the button it changes color, I use the buttons as information windows that give me the current profit or losses and that also change color depending on whether it is in profit , losses or to "0". Can you help me? The code that I have posted sometimes works and the majority is always in GRAY color, the other problem is the decimals of the Profit or loss, I need the profit or loss to be in this format "25.35" but I continually get this format "23.3499999999" and I can't figure out how to remove those annoying "99999999".
Greetings and a thousand thanks in advance.

You are on the MQL5 forum. You get all the answers about MQL5.

If you have been using the old terminal until now, please do not waste our time, write strictly in one section of the forum: MQL4 and MetaTrader 4


I'll move your topic.

 
xyon126: I need the profit or loss to be in this format "25.35" but I continually get this format "23.3499999999" and I can't figure out how to remove those annoying "99999999".

Floating-point has an infinite number of decimals, it's you, not understanding floating-point and that some numbers can't be represented exactly. (like 1/10.s)
          Double-precision floating-point format - Wikipedia

See also The == operand. - MQL4 programming forum 2013.06.07

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

Reason: