The colour won't change?

To add comments, please log in or register
fxsquirrel
22
fxsquirrel  

Hi, I created this price ticker as I wanted to use the Market Watch prices and to reduce the quote to one less decimal place but still see the fractional element. It works great but the only snag is the colour doesn't change. What have a i missed? Thanks in advance.

#property indicator_chart_window

int init()
  {
   return(0);
  }

int deinit()
  {
   ObjectDelete("Symbol_Label");
   ObjectDelete("Bid_Price_Label");
   ObjectDelete("Fraction_Price_Label");
   ObjectDelete("Spread_Label");
   ObjectDelete("Spread_Fraction_Label");

   return(0);
  }

int start()
  {
   double Old_Price,Old_Spread;
   string FontType="Arial";
   string Bid_Price,Fraction_Price,Spread_Price,Spread_Fraction;
   color  FontColour,FontColour2;

   if(MarketInfo(NULL,MODE_BID) > Old_Price) FontColour = Blue;
   if(MarketInfo(NULL,MODE_BID) < Old_Price) FontColour = Red;
   Old_Price=MarketInfo(NULL,MODE_BID);

   if(MarketInfo(NULL,MODE_SPREAD) > Old_Spread) FontColour2 = Blue;
   if(MarketInfo(NULL,MODE_SPREAD) < Old_Spread) FontColour2 = Red;
   Old_Spread=MarketInfo(NULL,MODE_SPREAD);

   if(Digits==3)
     {
      //XX.XXX eg.AUDJPY
      if(StringLen(MathFloor(MarketInfo(NULL,MODE_BID)))==2)
        {
         Bid_Price      = DoubleToStr(MathFloor(MarketInfo(NULL,MODE_BID)*100)/100,2);
         Fraction_Price = StringSubstr(DoubleToStr(MarketInfo(NULL,MODE_BID),Digits),Digits+2);
        }

      //XXX.XXX eg.CHFJPY
      if(StringLen(MathFloor(MarketInfo(NULL,MODE_BID)))==3)
        {
         Bid_Price      = DoubleToStr(MathFloor(MarketInfo(NULL,MODE_BID)*100)/100,2);
         Fraction_Price = StringSubstr(DoubleToStr(MarketInfo(NULL,MODE_BID),Digits),Digits+3);
        }
     }

   if(Digits==5)
     {
      //X.XXXXX eg.EURGBP
      if(StringLen(MathFloor(MarketInfo(NULL,MODE_BID)))==1)
        {
         Bid_Price      = DoubleToStr((MathFloor(MarketInfo(NULL,MODE_BID)*10000))/10000,4);
         Fraction_Price = StringSubstr(DoubleToStr(MarketInfo(NULL,MODE_BID),Digits),Digits+1);
        }

      //XX.XXXXX eg.EURSEK
      if(StringLen(MathFloor(MarketInfo(NULL,MODE_BID)))==2)
        {
         Bid_Price      = DoubleToStr((MathFloor(MarketInfo(NULL,MODE_BID)*10000))/10000,4);
         Fraction_Price = StringSubstr(DoubleToStr(MarketInfo(NULL,MODE_BID),Digits),Digits+2);
        }
     }

   Spread_Price=MathFloor(DoubleToStr(MarketInfo(NULL,MODE_SPREAD)/10,1));
   Spread_Fraction=StringSubstr(DoubleToStr(MarketInfo(NULL,MODE_SPREAD)/10,1),2);

   ObjectCreate("Symbol_Label",OBJ_LABEL,0,0,0);
   ObjectSetText("Symbol_Label",Symbol(),27,FontType,FontColour);
   ObjectSet("Symbol_Label",OBJPROP_CORNER,1);
   ObjectSet("Symbol_Label",OBJPROP_XDISTANCE,1);
   ObjectSet("Symbol_Label",OBJPROP_YDISTANCE,1);

   ObjectCreate("Bid_Price_Label",OBJ_LABEL,0,0,0);
   ObjectSetText("Bid_Price_Label",Bid_Price,27,FontType,FontColour);
   ObjectSet("Bid_Price_Label",OBJPROP_CORNER,1);
   ObjectSet("Bid_Price_Label",OBJPROP_XDISTANCE,16);
   ObjectSet("Bid_Price_Label",OBJPROP_YDISTANCE,32);

   ObjectCreate("Fraction_Price_Label",OBJ_LABEL,0,0,0);
   ObjectSetText("Fraction_Price_Label",Fraction_Price,16,FontType,FontColour);
   ObjectSet("Fraction_Price_Label",OBJPROP_CORNER,1);
   ObjectSet("Fraction_Price_Label",OBJPROP_XDISTANCE,1);
   ObjectSet("Fraction_Price_Label",OBJPROP_YDISTANCE,36);

   ObjectCreate("Spread_Label",OBJ_LABEL,0,0,0);
   ObjectSetText("Spread_Label",Spread_Price,27,FontType,FontColour2);
   ObjectSet("Spread_Label",OBJPROP_CORNER,1);
   ObjectSet("Spread_Label",OBJPROP_XDISTANCE,16);
   ObjectSet("Spread_Label",OBJPROP_YDISTANCE,66);

   ObjectCreate("Spread_Fraction_Label",OBJ_LABEL,0,0,0);
   ObjectSetText("Spread_Fraction_Label",Spread_Fraction,16,FontType,FontColour2);
   ObjectSet("Spread_Fraction_Label",OBJPROP_CORNER,1);
   ObjectSet("Spread_Fraction_Label",OBJPROP_XDISTANCE,1);
   ObjectSet("Spread_Fraction_Label",OBJPROP_YDISTANCE,66);

   return(0);
  }
//+------------------------------------------------------------------+
dalmolin
5
dalmolin  
fxsquirrel:

Hi, I created this price ticker as I wanted to use the Market Watch prices and to reduce the quote to one less decimal place but still see the fractional element. It works great but the only snag is the colour doesn't change. What have a i missed? Thanks in advance.

William Roeder
20210
William Roeder  
int start(){
   double Old_Price,Old_Spread;
Shouldn't Old_Price be remembered between calls? Currently (since you're not using strict) it is always zero.
Petr Nosek
3024
Petr Nosek  
fxsquirrel:

Hi, I created this price ticker as I wanted to use the Market Watch prices and to reduce the quote to one less decimal place but still see the fractional element. It works great but the only snag is the colour doesn't change. What have a i missed? Thanks in advance.

  1. You should stop to use obsolete functions init(), deinit(), start().
  2. You should start to use #property strict.
  3. You should create objects in OnInit().
  4. Then you just change color and text in the loop OnCalculate()
  5. This indicator has no sense on metals, indexes...

See the updated code as an example:

#property strict
#property indicator_chart_window

int OnInit()
  {
   string FontType="Arial";
   color  FontColour=clrBlue;
   ObjectCreate(0,"Symbol_Label",OBJ_LABEL,0,0,0);
   ObjectSetString(0,"Symbol_Label",OBJPROP_TEXT,_Symbol);
   ObjectSetString(0,"Symbol_Label",OBJPROP_FONT,FontType);
   ObjectSetInteger(0,"Symbol_Label",OBJPROP_FONTSIZE,27);
   ObjectSetInteger(0,"Symbol_Label",OBJPROP_COLOR,FontColour);
   ObjectSetInteger(0,"Symbol_Label",OBJPROP_CORNER,CORNER_RIGHT_UPPER);
   ObjectSetInteger(0,"Symbol_Label",OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
   ObjectSetInteger(0,"Symbol_Label",OBJPROP_XDISTANCE,1);
   ObjectSetInteger(0,"Symbol_Label",OBJPROP_YDISTANCE,1);
   ObjectSetInteger(0,"Symbol_Label",OBJPROP_SELECTABLE,false);

   ObjectCreate(0,"Bid_Price_Label",OBJ_LABEL,0,0,0);
   ObjectSetString(0,"Bid_Price_Label",OBJPROP_FONT,FontType);
   ObjectSetInteger(0,"Bid_Price_Label",OBJPROP_FONTSIZE,27);
   ObjectSetInteger(0,"Bid_Price_Label",OBJPROP_COLOR,FontColour);
   ObjectSetInteger(0,"Bid_Price_Label",OBJPROP_CORNER,CORNER_RIGHT_UPPER);
   ObjectSetInteger(0,"Bid_Price_Label",OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
   ObjectSetInteger(0,"Bid_Price_Label",OBJPROP_XDISTANCE,14);
   ObjectSetInteger(0,"Bid_Price_Label",OBJPROP_YDISTANCE,35);
   ObjectSetInteger(0,"Bid_Price_Label",OBJPROP_SELECTABLE,false);

   ObjectCreate(0,"Fraction_Price_Label",OBJ_LABEL,0,0,0);
   ObjectSetString(0,"Fraction_Price_Label",OBJPROP_FONT,FontType);
   ObjectSetInteger(0,"Fraction_Price_Label",OBJPROP_FONTSIZE,16);
   ObjectSetInteger(0,"Fraction_Price_Label",OBJPROP_COLOR,FontColour);
   ObjectSetInteger(0,"Fraction_Price_Label",OBJPROP_CORNER,CORNER_RIGHT_UPPER);
   ObjectSetInteger(0,"Fraction_Price_Label",OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
   ObjectSetInteger(0,"Fraction_Price_Label",OBJPROP_XDISTANCE,1);
   ObjectSetInteger(0,"Fraction_Price_Label",OBJPROP_YDISTANCE,38);
   ObjectSetInteger(0,"Fraction_Price_Label",OBJPROP_SELECTABLE,false);

   ObjectCreate(0,"Spread_Label",OBJ_LABEL,0,0,0);
   ObjectSetString(0,"Spread_Label",OBJPROP_FONT,FontType);
   ObjectSetInteger(0,"Spread_Label",OBJPROP_FONTSIZE,27);
   ObjectSetInteger(0,"Spread_Label",OBJPROP_COLOR,FontColour);
   ObjectSetInteger(0,"Spread_Label",OBJPROP_CORNER,CORNER_RIGHT_UPPER);
   ObjectSetInteger(0,"Spread_Label",OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
   ObjectSetInteger(0,"Spread_Label",OBJPROP_XDISTANCE,14);
   ObjectSetInteger(0,"Spread_Label",OBJPROP_YDISTANCE,66);
   ObjectSetInteger(0,"Spread_Label",OBJPROP_SELECTABLE,false);

   ObjectCreate(0,"Spread_Fraction_Label",OBJ_LABEL,0,0,0);
   ObjectSetString(0,"Spread_Fraction_Label",OBJPROP_FONT,FontType);
   ObjectSetInteger(0,"Spread_Fraction_Label",OBJPROP_FONTSIZE,16);
   ObjectSetInteger(0,"Spread_Fraction_Label",OBJPROP_COLOR,FontColour);
   ObjectSetInteger(0,"Spread_Fraction_Label",OBJPROP_CORNER,CORNER_RIGHT_UPPER);
   ObjectSetInteger(0,"Spread_Fraction_Label",OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
   ObjectSetInteger(0,"Spread_Fraction_Label",OBJPROP_XDISTANCE,1);
   ObjectSetInteger(0,"Spread_Fraction_Label",OBJPROP_YDISTANCE,66);
   ObjectSetInteger(0,"Spread_Fraction_Label",OBJPROP_SELECTABLE,false);
   
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   ObjectDelete("Symbol_Label");
   ObjectDelete("Bid_Price_Label");
   ObjectDelete("Fraction_Price_Label");
   ObjectDelete("Spread_Label");
   ObjectDelete("Spread_Fraction_Label");
  }

int OnCalculate (const int rates_total,      // size of input time series 
                 const int prev_calculated,  // bars handled in previous call 
                 const datetime& time[],     // Time 
                 const double& open[],       // Open 
                 const double& high[],       // High 
                 const double& low[],        // Low 
                 const double& close[],      // Close 
                 const long& tick_volume[],  // Tick Volume 
                 const long& volume[],       // Real Volume 
                 const int& spread[]         // Spread 
   )
  {
   static double Old_Price=0.0;
   static long Old_Spread=0;
   
   string Bid_Price,Fraction_Price,Spread_Price,Spread_Fraction;
   color  FontColour,FontColour2;
   double M=pow(10,Digits-1);

   FontColour=Bid>Old_Price?clrBlue:clrRed;
   Old_Price=Bid;

   FontColour2=SymbolInfoInteger(_Symbol,SYMBOL_SPREAD)>Old_Spread?clrBlue:clrRed;
   Old_Spread=SymbolInfoInteger(_Symbol,SYMBOL_SPREAD);

   Bid_Price=DoubleToString(int(Bid*M)/M,Digits-1);
   Fraction_Price=DoubleToString((Bid-int(Bid*M)/M)*pow(10,Digits),0);

   Spread_Price=DoubleToString(Old_Spread/10.0,0);
   Spread_Fraction=StringSubstr(DoubleToStr(MarketInfo(NULL,MODE_SPREAD)/10,1),2);
   
   ObjectSetString(0,"Symbol_Label",OBJPROP_TEXT,_Symbol);
   ObjectSetInteger(0,"Symbol_Label",OBJPROP_COLOR,FontColour);
   
   ObjectSetString(0,"Bid_Price_Label",OBJPROP_TEXT,Bid_Price);
   ObjectSetInteger(0,"Bid_Price_Label",OBJPROP_COLOR,FontColour);
   
   ObjectSetString(0,"Fraction_Price_Label",OBJPROP_TEXT,Fraction_Price);
   ObjectSetInteger(0,"Fraction_Price_Label",OBJPROP_COLOR,FontColour);
   
   ObjectSetString(0,"Spread_Label",OBJPROP_TEXT,Spread_Price);
   ObjectSetInteger(0,"Spread_Label",OBJPROP_COLOR,FontColour);
   
   ObjectSetString(0,"Spread_Fraction_Label",OBJPROP_TEXT,Spread_Fraction);
   ObjectSetInteger(0,"Spread_Fraction_Label",OBJPROP_COLOR,FontColour);
   
   return(rates_total);
  }
fxsquirrel
22
fxsquirrel  
Petr Nosek:
  1. You should stop to use obsolete functions init(), deinit(), start().
  2. You should start to use #property strict.
  3. You should create objects in OnInit().
  4. Then you just change color and text in the loop OnCalculate()
  5. This indicator has no sense on metals, indexes...

See the updated code as an example:

Thanks very much for your explanation. I didn't expect you to code it for me but appreciate the help. I don't think I've seen "?" used before so I'll use this as a lesson, thanks.
To add comments, please log in or register