color switching

 

Hi!

How to change the color of the label depending on the spread value ?


//+------------------------------------------------------------------+

//|                                              SpreadIndikator.mq4 |

//|                               Copyright ? 2010 MeinMetatrader.de |

//|                                                                  |

//+------------------------------------------------------------------+

#property copyright "Copyright ? 2010 MeinMetatrader.de"

#property link      ""



#property indicator_chart_window

/#property indicator_buffers 0



extern color LabelColor = Yellow;

 double spread;



#define OBJ_NAME "SpreadIndikatorObj"

int init()

{

   ShowSpread();

}

int start()

{

   ShowSpread();

}



int deinit()

{

   ObjectDelete(OBJ_NAME);

}



void ShowSpread()

{



  

   spread = MarketInfo(Symbol(), MODE_SPREAD);

   

  

   DrawSpreadOnChart(spread);

}



void DrawSpreadOnChart(double spread)

{

   string s = "Spread: "+DoubleToStr(spread, 0)+" points";

   

   if(ObjectFind(OBJ_NAME) < 0)

   {

      if(spread>12){LabelColor = Red;}

      else{LabelColor = Yellow;}

      ObjectCreate(OBJ_NAME, OBJ_LABEL, 0, 0, 0);

      ObjectSet(OBJ_NAME, OBJPROP_CORNER, 0);

      ObjectSet(OBJ_NAME, OBJPROP_YDISTANCE, 12);

      ObjectSet(OBJ_NAME, OBJPROP_XDISTANCE, 24);

      ObjectSetText(OBJ_NAME, s, 10, "FixedSys", LabelColor);

   }

     ObjectSetText(OBJ_NAME, s);

     WindowRedraw();

}


The above solution does not work

What's wrong with it ?

Thanks for the help

 
mqlbandi: How to change the color of the label depending on the spread value ?

When does your code set the color? Why do you expect that to change?

 
William Roeder # :

Mikor állítja be a kódod a színt? Miért várod, hogy ezzon?

if it is greater than 12, the color is red, otherwise it is yellow
 
mqlbandi #:
if it is greater than 12, the color is red, otherwise it is yellow

and : 

if(ObjectFind(OBJ_NAME) < 0)
 
mqlbandi #: if it is greater than 12, the color is red, otherwise it is yellow

Irrelevant. You didn't answer my questions. “When does your code set the color? Why do you expect any color change?”

 
mqlbandi #if it is greater than 12, the color is red, otherwise it is yellow
Mladen Rakic #: and : 
if(ObjectFind(OBJ_NAME) < 0)
William Roeder #: Irrelevant. You didn't answer my questions. “When does your code set the color? Why do you expect any color change?

@mqlbandi, to help make William's question more clear to you since you might be having difficulty understanding the logic ...

Your code is only setting the colour when you first create the object, hence why Mladen called your attention to "if(ObjectFind(OBJ_NAME) < 0)".

After that, you never update the colour ever again nor check the spread's value to make the decision to change the colour.

Do you understand William's question now?

EDIT: Here is a hint — you are correctly updating the text with the current value, but you are not updating the colour.

 
Fernando Carreiro # :

@mqlbandi , hogy határozottbbé tegye William kérdését, mivel előfordulhat, hogy nehezen érti meg a logikát...

A kód csak az objektum létrehozását állítja be a színt , ezért Mladen felhívta a figyelmet az "if(ObjectFind(OBJ_NAME) < 0)"-ra.

Ezt követően soha többé nem frissíti a színt, és nem ellenőrzi a szórás értékét, hogy döntést hozzon a szín megváltoztatásáról.

Érted most William kérdését?

   Thank you, I didn't know that, the color change is already working