//+-------------------------------------------------------------------------------------------+
//|                                                                                           |
//|                                      SonicR VA.mq4                                        |
//|                                                                                           |
//+-------------------------------------------------------------------------------------------+
#property copyright "Copyright © 2011 traderathome"
#property link      "email:   traderathome@msn.com"

/*---------------------------------------------------------------------------------------------
Overview:

This indicator produces a multi-colored volume histogram.  The colors represent catagories of
volume described as follows: 
 
Red/Volume Climax Bars -  
Bar volume is of the highest activity over "Analysis_GoBack" period.  
Activity is defined as bar volume*candle spread.
While red is used for both bull and bear candles, you can use different colors.

Blue/Volume Rising Bars -
Bar volume is >= the period average volume by the input volume factor.  For example, if the
period is 10 and the factor is 1.38, then any bar with volume that is >= 1.38 times the average
volume of the last 10 bars will be blue, if not already qualified as a volume climax bar. 
While blue is used for both bull and bear candles, you can use different colors.

Gray/Volume Normal Bars -
All other volume histogram bars are displayed.

This indicator includes a voice alert "Volume!" that will trigger one time per TF (TFs > M1) 
at the first qualification of the bar as green or red.  On M1 TFs multiple alerts can happen 
during the minute. 
                                        
The indicator can be turned on/off without having to remove it from the chart, preserving your
chart settings.  You can specify narrow or wide bars be displayed, and change colors.  To make
such changes: modify the under the Inputs tab, close the External Inputs window and change the
chart TF back and forth once.  Your changes are then made and appear under the Colors tab also. 
They will be permanent for the chart until you make other changes.

The indicator ShortName in the study sub-window shows the numbers input for the "GoBack" time 
for candle analysis, the averaging period, and the volume factor.  The ShortName can be turned 
off to allow an unobstructed study when multiple small charts are simultaneously displayed in 
the MT4 main window. 

                                                                    - Traderathome, 06-01-2011 
-----------------------------------------------------------------------------------------------
Acknowledgements:
BetterVolume.mq4 - for some core coding (BetterVolume_v1.4).
     
-----------------------------------------------------------------------------------------------
Suggested Settings:         White Chart        Black Chart         Function

#property indicator_color1  C'0,0,196'         C'62,158,255'       Rising Bull
#property indicator_color2  C'0,0,196'         C'62,158,255'       Rising Bear
#property indicator_color3  C'31,190,79'       Lime                Climax Bull
#property indicator_color4  C'230,21,63'       Red                 Climax Bear
#property indicator_color5  Gainsboro          DimGray             Volume Bars
#property indicator_width1  2                  2
#property indicator_width2  2                  2
#property indicator_width3  2                  2
#property indicator_width4  2                  2           
#property indicator_width5  2                  2  
           
---------------------------------------------------------------------------------------------*/

#property indicator_separate_window
#property indicator_buffers 5

#property indicator_color1  C'0,0,196'        
#property indicator_color2  C'0,0,196'  
#property indicator_color3  C'31,190,79'      
#property indicator_color4  C'230,21,63'  
#property indicator_color5  Gainsboro                      
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2
#property indicator_width4  2
#property indicator_width5  2

//Global External Inputs 
extern bool   Indicator_On                    = true;
extern int    Volume_Averaging_Period         = 10;
extern double Volume_Over_Average_Factor      = 1.38;
extern bool   Show_Wide_Bars                  = false;
extern bool   Voice_Alert_On                  = false;
extern bool   Text_Alert_On                   = false;
extern int    AlertOnBar                      = 1;
extern bool   Show_Indicator_ShortName        = true;
extern int    Volume_Analysis_GoBack          = 20;
extern color  Volume_Rising_Bull              = C'0,0,196';
extern color  Volume_Rising_Bear              = C'0,0,196';
extern color  Volume_Climax_Bull              = C'31,190,79';
extern color  Volume_Climax_Bear              = C'230,21,63';
extern color  Volume_Normal_Bars              = Gainsboro;

//Global Buffers & Other Inputs
bool          FLAG_deinit;
int           i,j,n;
int           Volume_Rising_Width,Volume_Climax_Width,Volume_Normal_Width; 
double        Rising1[],Rising2[];
double        Climax1[],Climax2[]; 
double        Normal[];
double        avg,Range,Value2,HiValue2,tempv2;
datetime      dt1, dt2;

//+-------------------------------------------------------------------------------------------+
//| Custom indicator initialization function                                                  |
//+-------------------------------------------------------------------------------------------+
int init()
  {
  FLAG_deinit  = false;
  dt1 = iTime(NULL,0,1); dt2 = dt1;
  
   if (!Show_Wide_Bars)
      {
      Volume_Rising_Width= 2;
      Volume_Climax_Width= 2;
      Volume_Normal_Width= 2;
      }
   else 
      {
      Volume_Rising_Width= 3;
      Volume_Climax_Width= 3;
      Volume_Normal_Width= 3;
      } 
       
  //Indicators       
  SetIndexBuffer(0, Rising1);   
  SetIndexStyle(0, DRAW_HISTOGRAM, 0, Volume_Rising_Width, Volume_Rising_Bull);
  SetIndexBuffer(1, Rising2);   
  SetIndexStyle(1, DRAW_HISTOGRAM, 0, Volume_Rising_Width, Volume_Rising_Bear);  
  SetIndexBuffer(2, Climax1);   
  SetIndexStyle(2, DRAW_HISTOGRAM, 0, Volume_Climax_Width, Volume_Climax_Bull);
  SetIndexBuffer(3, Climax2);   
  SetIndexStyle(3, DRAW_HISTOGRAM, 0, Volume_Climax_Width, Volume_Climax_Bear);      
  SetIndexBuffer(4, Normal);  
  SetIndexStyle(4, DRAW_HISTOGRAM, 0, Volume_Normal_Width, Volume_Normal_Bars);      
  
 
  //Indicator subwindow data labels     
  SetIndexLabel(0,  NULL);
  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()
  {
  //Comment("");
  return(0);
  }
  
//+-------------------------------------------------------------------------------------------+
//| Custom indicator iteration function                                                       |
//+-------------------------------------------------------------------------------------------+
int start()
  {
  //If Indicator is "Off" deinitialize only once, not every tick------------------------------  
  if (!Indicator_On)
    {
    if (Show_Indicator_ShortName) 
      {
      IndicatorShortName("SonicR VA  ("+Volume_Analysis_GoBack+", "+Volume_Averaging_Period+", "+
      DoubleToStr(Volume_Over_Average_Factor,2)+")  -off.   ");
      }  
    else {IndicatorShortName("");}
    if (!FLAG_deinit) {deinit(); FLAG_deinit = true;}
    return(0);
    }  
             
  for(i = Bars-1-IndicatorCounted(); i >= 0; i--)       
    {            
    //Clear buffers                         
    Rising1[i]  = 0;
    Rising2[i]  = 0; 
    Climax1[i]  = 0;   
    Climax2[i]  = 0;         
    Normal[i] = Volume[i];
    Value2        = 0;
    HiValue2      = 0;
    tempv2        = 0;
            
    //Compute Average Volume
    avg = 0;
    for (j = i; j < (i+Volume_Averaging_Period); j++) {avg = avg + Volume[j];}   
    avg = avg / Volume_Averaging_Period;     
               
    //Input average and current volume into ShortName
    if (Show_Indicator_ShortName)
      {      
      if((!Voice_Alert_On) && (!Text_Alert_On))
        {
        IndicatorShortName("SonicR VA  ("+Volume_Analysis_GoBack+", "+Volume_Averaging_Period+", "+
        DoubleToStr(Volume_Over_Average_Factor,2)+")   Voice & Text Alerts off.   "); 
        }
      else {if((Voice_Alert_On) && (!Text_Alert_On))
        {
        IndicatorShortName("SonicR VA  ("+Volume_Analysis_GoBack+", "+Volume_Averaging_Period+", "+
        DoubleToStr(Volume_Over_Average_Factor,2)+")   Voice Alert On, Text Alert off.   "); 
        }
      else {if((!Voice_Alert_On) && (Text_Alert_On))
        {
        IndicatorShortName("SonicR VA  ("+Volume_Analysis_GoBack+", "+Volume_Averaging_Period+", "+
        DoubleToStr(Volume_Over_Average_Factor,2)+")   Voice Alert Off, Text Alert on.   ");
        }
      else {if((Voice_Alert_On) && (Text_Alert_On))
        {
        IndicatorShortName("SonicR VA  ("+Volume_Analysis_GoBack+", "+Volume_Averaging_Period+", "+
        DoubleToStr(Volume_Over_Average_Factor,2)+")   Voice & Text Alerts on.   ");               
        } }}}
      }                                                  
    else                                 
      {
      IndicatorShortName("");
      }  
        
    //Calculations necessary for candles                 
    Range = (High[i]-Low[i]);
    Value2 = Volume[i]*Range;                 
    for (n=i;n<i+Volume_Analysis_GoBack;n++)
      {
      tempv2 = Volume[n]*((High[n]-Low[n])); 
      if (tempv2 >= HiValue2) {HiValue2 = tempv2;}    
      }
        
      if(Value2 == HiValue2)
        { 
        //Bull Candle                                  
        if (Close[i] > Open[i]) 
          {
          Climax1[i] = NormalizeDouble(Volume[i],0);
          }
        //Bear Candle  
        else if (Close[i] <= Open[i]) 
          {
          Climax2[i] = NormalizeDouble(Volume[i],0);
          }
        Normal[i] = 0;
        //Voice & Text Alert
        if((Voice_Alert_On) || (Text_Alert_On))
          {  
          if((dt2 != iTime(NULL,0,0)) && (i == AlertOnBar))         
            {        
            dt2 = iTime(NULL,0,0);
            if(Voice_Alert_On) {PlaySound("vol_alert.wav");}
            if(Text_Alert_On) {Alert (Symbol()+", TF "+Period()+", volume alert!");}           
            }
          }                  
        } 
                                 
      //Volume high over average                       
      else if (Volume[i] >= avg * Volume_Over_Average_Factor)
        {
        //Bull Candle                                  
        if (Close[i] > Open[i]) 
          {
          Rising1[i] = NormalizeDouble(Volume[i],0);
          }
        // Bear Candle  
        else if (Close[i] <= Open[i]) 
          {
          Rising2[i] = NormalizeDouble(Volume[i],0);
          }                        
        Normal[i] = 0;          
        }              
                  
    }//End of "for" loop    
    
  return(0);
  }

//+-------------------------------------------------------------------------------------------+
//|Custom indicator end                                                                       |
//+-------------------------------------------------------------------------------------------+    
         