//+------------------------------------------------------------------+
//|                                              Value Charts XT.mq4 |
//|                                              NsTrader & X-Trader |
//|                                          http://www.x-trader.net |
//+------------------------------------------------------------------+
#property copyright "NsTrader & X-Trader"
#property link      "http://www.x-trader.net"


#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1 LimeGreen
#property indicator_color2 LimeGreen
#property indicator_color3 Red
#property indicator_color4 Red
#property indicator_color5 Black

#property indicator_color6 Gray
#property indicator_color7 Gray
#property indicator_color8 Black

#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 3
#property indicator_width4 3
#property indicator_width5 3
#property indicator_width6 1
#property indicator_width7 1
#property indicator_width8 1



SetLevelValue(1,2.0);
//SetLevelStyle (1,DRAW_LINE, STYLE_SOLID,1, Red);


SetLevelValue(2,1.5);

SetLevelValue(3,1.0);

SetLevelValue(4,-1.0);

SetLevelValue(5,-1.5);

SetLevelValue(6,-2);

//#property indicator_level1 2.0 
//#property indicator_level2 1.5
//#property indicator_level3 1.0
//#property indicator_level4 -1.0
//#property indicator_level5 -1.5
//#property indicator_level6 -2.0
//----
extern color colora = ForestGreen;
extern color colorb = Red;
extern color colorc = Gray;
extern color colord = Black;

//---- buffers
double ExtMapBuffer1[];// > 0 Green
double ExtMapBuffer2[];// < 0 Green
double ExtMapBuffer3[];// > 0 Red
double ExtMapBuffer4[];// < 0 Red

double ExtMapBuffer5[];// Black 3
double ExtMapBuffer6[];// High
double ExtMapBuffer7[];// Low
double ExtMapBuffer8[];// Black 1
//----
int ExtCountedBars=0;
string windowName = "Value Charts (H,L)";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {
//---- indicators
   
   IndicatorShortName(windowName);

   SetIndexStyle(0,DRAW_HISTOGRAM, 0, 3, colora);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexLabel(0,NULL);
   SetIndexStyle(1,DRAW_HISTOGRAM, 0, 3, colora);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexLabel(1,NULL);
   SetIndexStyle(2,DRAW_HISTOGRAM, 0, 3, colorb);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexLabel(2,NULL);
   SetIndexStyle(3,DRAW_HISTOGRAM, 0, 3, colorb);
   SetIndexBuffer(3, ExtMapBuffer4);
   SetIndexLabel(3,NULL);
   SetIndexStyle(4,DRAW_HISTOGRAM, 0, 3, colord);
   SetIndexBuffer(4, ExtMapBuffer5);
   SetIndexLabel(4,NULL);
   
   SetIndexStyle(5,DRAW_HISTOGRAM, 0, 1, colorc);
   SetIndexBuffer(5, ExtMapBuffer6);
   SetIndexStyle(6,DRAW_HISTOGRAM, 0, 1, colorc);
   SetIndexBuffer(6, ExtMapBuffer7);
   SetIndexStyle(7,DRAW_HISTOGRAM, 0, 1, colord);
   SetIndexBuffer(7, ExtMapBuffer8);
   SetIndexLabel(7,NULL);
//----
   SetIndexDrawBegin(0,10);
   SetIndexDrawBegin(1,10);
   SetIndexDrawBegin(2,10);
   SetIndexDrawBegin(3,10);
   SetIndexDrawBegin(4,10);
   
   SetIndexDrawBegin(5,10);
   SetIndexDrawBegin(6,10);
   SetIndexDrawBegin(7,10);
   
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexBuffer(4,ExtMapBuffer5);
   
   SetIndexBuffer(5,ExtMapBuffer6);
   SetIndexBuffer(6,ExtMapBuffer7);
   SetIndexBuffer(7,ExtMapBuffer8);
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {

   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   double haOpen, haHigh, haLow, haClose;
   if(Bars<=10) return(0);
   ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
   int pos=Bars-ExtCountedBars-1;
   while(pos>=0)
     {

      double MA = iMA(NULL,0,5,0,MODE_SMA,PRICE_MEDIAN,pos);
      double TR = iATR(NULL,0,5,pos);
      
      if(TR != 0)
     {
      haOpen=(Open[pos] - MA)/TR;
      haClose=(Close[pos] - MA)/TR;
      haHigh=(High[pos] - MA)/TR;
      haLow=(Low[pos] - MA)/TR;
     }
     
     else
 	   {
       haOpen = 0;
       haClose = 0;
       haHigh = 0;
       haLow = 0;
      }
      
      ExtMapBuffer1[pos]=EMPTY_VALUE;
      ExtMapBuffer2[pos]=EMPTY_VALUE;
      ExtMapBuffer3[pos]=EMPTY_VALUE;
      ExtMapBuffer4[pos]=EMPTY_VALUE;
      ExtMapBuffer5[pos]=EMPTY_VALUE;
      ExtMapBuffer6[pos]=EMPTY_VALUE;
      ExtMapBuffer7[pos]=EMPTY_VALUE;
      ExtMapBuffer8[pos]=EMPTY_VALUE;
      
      
      if (haOpen<haClose) 
      {

            if(haOpen < 0)
            {
               ExtMapBuffer1[pos]=haClose;
               ExtMapBuffer2[pos]=haOpen;
               if(haClose < 0) ExtMapBuffer5[pos]=haClose;
            }
            else
            {
               ExtMapBuffer1[pos]=haClose;
               ExtMapBuffer5[pos]=haOpen;
            }
      } 
      else
      {
            if(haOpen > 0)
            {
               ExtMapBuffer3[pos]=haOpen;
               ExtMapBuffer4[pos]=haClose;
               if(haClose > 0) ExtMapBuffer5[pos]=haClose;
            }
            else
            {
               ExtMapBuffer4[pos]=haClose;
               ExtMapBuffer5[pos]=haOpen;
            }
      }
        
      
      //-----------  
      ExtMapBuffer6[pos]=haHigh;
      ExtMapBuffer7[pos]=haLow;
        
      
      if(haLow > 0) ExtMapBuffer8[pos] = haLow;
      if(haHigh < 0) ExtMapBuffer8[pos] = haHigh;  
        
      
 	   pos--;
     
     }
     OBJ_LABEL_("VC",DoubleToStr(haClose,3),10,10,White,10);
//----
   return(0);
  }
//+------------------------------------------------------------------+

void OBJ_LABEL_(string str="",string TEXT="",int X=0, int Y=0,color COLOR=Blue,int FONT_SIZE=10)
{
   int Win_ind = WindowFind(windowName);
   if (Win_ind != -1)
   {
      if(ObjectFind(str) == -1)
      {
         ObjectCreate(str,OBJ_LABEL,Win_ind,0,0);
      }
      ObjectSetText(str,TEXT,FONT_SIZE,"",COLOR);
      ObjectSet(str,OBJPROP_CORNER, 1);  
      ObjectSet(str,OBJPROP_XDISTANCE,X);
      ObjectSet(str,OBJPROP_YDISTANCE,Y);
   }

}