//+------------------------------------------------------------------+
//|                                                                  |
//|                      Volumes Emphasized.mq4                      |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "NormalizedVolumeOscillator by Vadim Shumiloff"
#property link      "shumiloff@mail.ru"

/*--------------------------------------------------------------------
Volumes Emphasized:

First off, my expression of grateful thanks to MissPips for adding
the crown of glory to this effort....the real-time update of the
current volume bar percentage indicator.

Enhancements are added to the core coding of the copyright indicator 
to enable the normal volume histogram to be seen along with volume 
graduations generated by the core code.  The histogram can be resized 
down in the display window (shrink it) so that this indicator can 
co-exist with another in the same window, yet occupy only the lower 
regions of the window.  The ShrinkNumber "1" sets a normal sized 
display.  Using "2" cuts the display height in half, "3" into a third, 
"4" into a fourth, etc.  The graduation bars display as their normal 
height within the histogram. They can be emphasized with different 
colors and by varying their width from the normal bars they overlay.  
You can alter the graduations.  Originally they were bars below average, 
0-38.2% avg., 38.2%-61.8% avg., 61.8%-100% avg., and over 100% avg.  
The below average bars are now just part of the normal histogram.  
But the range from 0%-100% can be graduated differently by the user.  
Instead of using 38.2 and 61.8 to divide that range into three parts, 
you could use 33.33 and 66.66, or 25 and 50, etc.  This is useful if 
you want to NOT show so many different colors bars, maybe showing only 
those between 50-100 and over 100.  By selecting 25, 50 and then 
coloring the 0-25 and 25-50 the same as the normal bars, you will 
reduce the display of colored bars to just those in the 50-100 and the 
100+ graduations.  You can eliminate the display of selected graduations 
by setting their width and color to the same as the normal histogram. 
You can eliminate the display of the normal histogram and selected 
graduations by coloring them "CLR_NONE", of if this does not work 
satisfactorily, color them the same as the chart background color.
This will cause only the remaining graduated colored bars to be shown.

The Color tab of the Indicator's Properties Window allows you to set 
the color and width of the normal histogram, of the three graduations 
between 0-100, and the 100+ graduation.  In addition to these, the 
first two items on the list are for the zero line (normally not used) 
and an extra, "Phantom" normal histogram.  It is required to maintain
the indicator window height when the display is "shrunken" within the
window of another indicator.  This item should always be made invisible 
using "CLR_NONE" as the color choice.  If this does not work, then it 
should be colored the same color as the chart background.
                                                       - Traderathome
--------------------------------------------------------------------*/

#property indicator_separate_window
#property indicator_buffers 7

#property indicator_color1 CLR_NONE        // Zero/axis line (usually not necessary to show)
#property indicator_color2 CLR_NONE        // Phantom volume histogram to maintain normal window height
#property indicator_width1 1  
#property indicator_width2 1  

#property indicator_color3 DarkSlateBlue   // Normal volume bars
#property indicator_color4 Gold            // Volumes between 0% avg. & first graduation #
#property indicator_color5 LimeGreen       // Volumes between first & second graduation #
#property indicator_color6 CornflowerBlue  // Volumes between second graduation # and 100% of avg.
#property indicator_color7 MediumOrchid    // Volumes greater than 100% of average
#property indicator_width3 1  
#property indicator_width4 2  
#property indicator_width5 2 
#property indicator_width6 2 
#property indicator_width7 2

//---- External Inputs 
extern int    VolumePeriod                    = 10;
extern string Numbers_shrink_bars_in_display  = "2=1/2, 3=1/3, 4=1/4, 5=1/5, etc";
extern double Enter_ShrinkNumber              = 1.25;
extern string Need_2_numbers_dividing_range   = "0-100 into 3 parts (33&66, 25&50 etc)";
extern double Enter_Number_1                  = 38.2;
extern double Enter_Number_2                  = 61.8;
extern bool   Display_volume_percentages_text = true;
extern color  BelowAverageTextColor_0         = Red;
extern color  AboveAverageTextColor_1         = Gold;
extern color  AboveAverageTextColor_2         = LimeGreen;
extern color  AboveAverageTextColor_3         = CornflowerBlue;
extern color  AboveAverageTextColor_4         = MediumOrchid;
extern int    Text_in_window_0123             = 1;
extern int    Text_in_corner_0123             = 3;
extern int    __Text_Horizontal_Indent        = 5;
extern int    __Text_Vertical_Indent          = 4; 

//---- Buffers & Other Inputs
double VolBufferH1[];
double VolBufferH2[];
double VolBufferH3[];
double VolBufferH4[];
double VolBufferH5[];
double VolBufferH6[];
double VolBufferH7[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
   {
   IndicatorBuffers(7);
   
   SetIndexBuffer(0, VolBufferH1);   //This draws the zero line
   SetIndexStyle(1, DRAW_HISTOGRAM);
    
   SetIndexBuffer(1, VolBufferH2);   //This draws phantom normal bars forcing normal window height
   SetIndexStyle(2, DRAW_HISTOGRAM);
         
   SetIndexBuffer(2, VolBufferH3);   //This draws all of the normal volume bars
   SetIndexStyle(3, DRAW_HISTOGRAM);
  
   SetIndexBuffer(3, VolBufferH4);   //This draws the bars between 0% of avg. and the first graduation
   SetIndexStyle(4, DRAW_HISTOGRAM);

   SetIndexBuffer(4, VolBufferH5);   //This draws the bars betwee the first and second graduation
   SetIndexStyle(5, DRAW_HISTOGRAM);

   SetIndexBuffer(5, VolBufferH6);   //This draws the bars between the second graduation and 100% of avg.
   SetIndexStyle(6, DRAW_HISTOGRAM);

   SetIndexBuffer(6, VolBufferH7);   //This draws the bars over 100% of avg.
   SetIndexStyle(7, DRAW_HISTOGRAM);
     
   //---- Indicator Window name and data labels  
   IndicatorShortName("Volumes Emphasized  (" + VolumePeriod + ")  ");  
   SetIndexLabel(0, "Volume Baseline");
   SetIndexLabel(1,  NULL); 
   SetIndexLabel(2,  NULL);
   SetIndexLabel(3,  NULL);
   SetIndexLabel(4,  NULL);
   SetIndexLabel(5,  NULL);
   SetIndexLabel(6,  NULL);
   SetIndexLabel(7,  NULL);
    
   return(0);
   }
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
   {
   int obj_total= ObjectsTotal();  
   for (int i= obj_total; i>=0; i--) 
      {
      string name= ObjectName(i);    
          if (StringSubstr(name,0,6)=="voltxt")
          {
          ObjectDelete(name);
          }
      }   
      
      Comment("");
   return(0);
   }

//+-------------------------------------------------------------------+
//| Calculations body of program                                      |                                                        
//+-------------------------------------------------------------------+
int start()
   {
   int counted_bars = IndicatorCounted(); 
   double nvo; string Nvo = "";
   
   for(int i = Bars-1-IndicatorCounted(); i >= 0; i--)
      {
      VolBufferH1[i] = 0;                             //axis line always "0"
      VolBufferH2[i] = Volume[i]*Enter_ShrinkNumber;  //force window height larger based on shrink factor        
      VolBufferH3[i] = Volume[i];                     //normal volume bars can be displayed as backdrop
      VolBufferH4[i] = 0;                             //one of the four graduation level overlay buffers for each [i]
      VolBufferH5[i] = 0;                             //one of the four graduation level overlay buffers for each [i]  
      VolBufferH6[i] = 0;                             //one of the four graduation level overlay buffers for each [i]
      VolBufferH7[i] = 0;                             //one of the four graduation level overlay buffers for each [i]
      
      nvo = NormalizedVolume(i)*100 - 100; //jump to "NormalizedVolume" subroutine for that value
      
      if (nvo<0) {Nvo = DoubleToStr((nvo-(nvo*2)), Digits-2);}  //negative % changed to positive for the label 
         else {Nvo = DoubleToStr(nvo, Digits-2);}               //the % already in positive form
             
      if (nvo<0){        
         if(Display_volume_percentages_text) Label("Vol:  "+Nvo+"% below avg.",BelowAverageTextColor_0);}
         
      else{if (nvo<Enter_Number_1){ 
         if(Display_volume_percentages_text){Label("Vol:  "+Nvo+"% above avg.",AboveAverageTextColor_1);}
         VolBufferH4[i]=Volume[i];}
          
      else{if (nvo<Enter_Number_2){ 
         if(Display_volume_percentages_text){Label("Vol:  "+Nvo+"% above avg.",AboveAverageTextColor_2);}
         VolBufferH5[i]=Volume[i]; }
         
      else{if (nvo<100){
         if(Display_volume_percentages_text){Label("Vol:  "+Nvo+"% above avg.",AboveAverageTextColor_3);}
         VolBufferH6[i]=Volume[i]; }
         
      else if (nvo>=100){
         if(Display_volume_percentages_text){Label("Vol:  "+Nvo+"% above avg!",AboveAverageTextColor_4);}
         VolBufferH7[i]=Volume[i];}
         
               }
            }
         }
      }
          
   return(0);
   }

//+-------------------------------------------------------------------+
//| Subroutine to compute normalized volume                           |                                                        
//+-------------------------------------------------------------------+
double NormalizedVolume(int i)
   {
   double nv = 0;
   for (int j = i; j < (i+VolumePeriod); j++) nv = nv + Volume[j];
   nv = nv / VolumePeriod;
   return (Volume[i] / nv);
   }

//+-------------------------------------------------------------------+
//| Subroutine to draw volume bar annotation label                    |                                                        
//+-------------------------------------------------------------------+
void Label(string text, color Color)  
   {
      {
      ObjectDelete("voltxt");
      ObjectCreate("voltxt", OBJ_LABEL, Text_in_window_0123, 0, 0);
      ObjectSet("voltxt", OBJPROP_CORNER, Text_in_corner_0123);
      ObjectSet("voltxt", OBJPROP_XDISTANCE, __Text_Horizontal_Indent);
      ObjectSet("voltxt", OBJPROP_YDISTANCE, __Text_Vertical_Indent);
      ObjectSet("voltxt", OBJPROP_COLOR, Color);
      ObjectSetText("voltxt", text, 10, "Arial");    
      }
   }

//+-------------------------------------------------------------------+
//| End of program                                                    |                                                        
//+-------------------------------------------------------------------+

