How to change Value color to red when its <0

 

I need:


if PipValue<0 then Show PipValue color = Red,
else color = InfoColor

What shall I do?

{
   string Pip = "PIP";
   string PipValue = DoubleToStr(pipVal, 0);
   ObjectCreate(Pip, OBJ_LABEL, 0 , 0, 0);
   ObjectSet(Pip, OBJPROP_CORNER, 1);
   ObjectSet(Pip, OBJPROP_XDISTANCE, 10);
   ObjectSet(Pip, OBJPROP_YDISTANCE, 24);
   ObjectSetText(Pip, "PiP Count: "+PipValue, 8, "Tahoma", InfoColor);
}
 
Konnj:

I need:

if PipValue<0 then Show PipValue color = Red,
else color = InfoColor

What shall I do?


Don't create the object when it already exists or you will get and error and the object won't be created. Check if the object exists before you try and create it, if it already exists just change it . . .

color LabelColor;

LabelColor = InfoColor;

if (PipValue < 0) LabelColor = Red; 

{
   string Pip = "PIP";
   string PipValue = DoubleToStr(pipVal, 0);
   if(ObjectFind(Pip) < 0) ObjectCreate(Pip, OBJ_LABEL, 0 , 0, 0); // check if Object exists

   ObjectSet(Pip, OBJPROP_CORNER, 1);
   ObjectSet(Pip, OBJPROP_XDISTANCE, 10);
   ObjectSet(Pip, OBJPROP_YDISTANCE, 24);
   ObjectSetText(Pip, "PiP Count: "+PipValue, 8, "Tahoma", LabelColor);
}
 

Thanks a lot RaptorUK, you are so fast ;)

It works, but I just want to change PipValue color to Red, not all the object text.

Regards

 
Konnj:

Thanks a lot RaptorUK, you are so fast ;)

It works, but I just want to change PipValue color to Red, not all the object text.

Ah sorry . . . you need to create 2 separate labels, one for the text one for the numbers . . . my comment about ObjectCreate still stands though
 

You mean Pip Object?1

Is it Okay now?

string PPip = "PIP";
   string PipValue = DoubleToStr(pipVal, 0);
   if(ObjectFind(PPip ) < 0) ObjectCreate(PPip , OBJ_LABEL, 0 , 0, 0); // check if Object exists

   ObjectSet(PPip , OBJPROP_CORNER, 1);
   ObjectSet(PPip , OBJPROP_XDISTANCE, 10);
   ObjectSet(PPip , OBJPROP_YDISTANCE, 24);
   ObjectSetText(PPip , "PiP Count: "+PipValue, 8, "Tahoma", LabelColor);
 
Konnj:

You mean Pip Object?1

Is it Okay now?

If you want the text "PiP Count" in one colour and the PipValue in a different colour then you need one label Object for the "PiP Count" text and a different label Object for the numerical PipValue

Checking if an Object exists before you try to create it applies to all Objects where you are modifying them . . .

 

Thanks again man, I did it and it works so good.

One more question please, When I remove the indicator from chart, the object is still on chart ! How can be remove as long as indicator was remove of chart!

 
Konnj:

Thanks again man, I did it and it works so good.

One more question please, When I remove the indicator from chart, the object is still on chart ! How can be remove as long as indicator was remove of chart!

In deinit() use ObjectDelete(object_name) for your 2 label Objects.
 
Best Regards
 

The code shows position's profit, but when position is close, it's still shows the last profit! I want it to show "0" after close the position (or shows "text") . What should I do?

 
Konnj:

What should I do?

Open a new thread, explain your issue, post your code.
Reason: