//+------------------------------------------------------------------+
//|                                                       ay-R&V.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
//--- input parameters
extern int       NumBars         = 12;
extern color     IncreaseColor   = LimeGreen;
extern color     DecreaseColor   = Red;
extern color     EqualColor      = Goldenrod;
extern int       WindowId        = 1;


string gsPref = "ay.rv.";
int    giDigits;
double gdPoint;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   if (Point == 0.001 || Point == 0.00001) 
   { gdPoint = Point*10; giDigits = Digits - 1; }
   else 
   { gdPoint = Point; giDigits = Digits; }  
   
   layout();
//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----
   delObjs();
//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int    i, counted_bars=IndicatorCounted();
   color  clr;
   double currange, prevrange;
   int    maxbar = 1;
   if (newBar()) maxbar = NumBars;
//----
   for (i=0; i<maxbar; i++)
   {
      if (Volume[i] > Volume[i+1]) clr = IncreaseColor; else 
      if (Volume[i] < Volume[i+1]) clr = DecreaseColor; else
      clr = EqualColor;
      ObjectSetText(gsPref + "vol." + i, DoubleToStr(Volume[i],0), 8, "Tahoma", clr);
      
      currange  = (High[i]-Low[i])/gdPoint;
      prevrange = (High[i+1]-Low[i+1])/gdPoint; 
      if (currange > prevrange) clr = IncreaseColor; else 
      if (currange < prevrange) clr = DecreaseColor; else
      clr = EqualColor;
      ObjectSetText(gsPref + "range." + i, DoubleToStr(currange,0), 8, "Tahoma", clr);
      
      
   }
//----
   return(0);
}

void layout()
{
   int i, x=5, y=5;
   lblCreate("vol",   x, y, "Vol",   8, "Tahoma", EqualColor); x+=40;
   lblCreate("range", x, y, "Range", 8, "Tahoma", EqualColor); x+=40;
   lblCreate("bar",   x, y, "Bar#",  8, "Tahoma", EqualColor); 
           
   for (i=0; i<NumBars; i++)
   {
      x=5; y+=12;
      lblCreate("vol."+i,   x, y, "00000", 8, "Tahoma", EqualColor); x+=40;
      lblCreate("range."+i, x, y, "00000", 8, "Tahoma", EqualColor); x+=40;
      lblCreate("bar."+i,   x, y, i,       8, "Tahoma", EqualColor); 
      
   }
   
}

bool newBar()
{
   static datetime LastBar;
   datetime CurrBar = Time[0];

   if(LastBar!=CurrBar)
   {
      LastBar=CurrBar;
      return (true);
   }

   return(false);    
} 

void lblCreate(string name,int x,int y,string text="-",int size=8,
               string font="Tahoma",color c=CLR_NONE)
{
   
   name = gsPref + name;
   ObjectCreate (name,OBJ_LABEL,WindowId,0,0);
   ObjectSet    (name,OBJPROP_CORNER,1);
   ObjectSet    (name,OBJPROP_XDISTANCE,x);
   ObjectSet    (name,OBJPROP_YDISTANCE,y);
   ObjectSetText(name,text,size,font,c);
} 

void delObjs()
{
   int objs = ObjectsTotal();
   string name;
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)
   {
      name=ObjectName(cnt);
      if (StringSubstr(name,0,StringLen(gsPref)) == gsPref) ObjectDelete(name); 
   }   
}
//+------------------------------------------------------------------+