//+-------------------------------------------------------------------------------------------+
//|                                                                                           |
//|                                   SonicR PVA Volumes.mq4                                  |
//|                                                                                           |
//+-------------------------------------------------------------------------------------------+
#property copyright "Copyright © 2013 traderathome"
#property link      "email:   traderathome@msn.com"

/*---------------------------------------------------------------------------------------------
Overview, _v1:
 
This indicator creates a PVA (Price Volume Analysis) color coded volume histogram.

1. PVA Volumes-
   The volume bars have special colors for special situations regarding individual candle
   range, individual candle volume and average volume, and are designed to be used in 
   conjuction with the co-released SonicR PVA Candles indicator.  Volume bars emphasized 
   with special colors for special situations help you quickly associate a volume bar to the 
   corresponding candle of the same colors.
   A. Volume Bars for Climax Candles
      Volume bars where volume is >= "Extreme_Factor" percent of the average volume of the 
      "Climax_Period" number of the most recent previous bars, and volume bars where the 
      product of candle spread x candle volume is highest for the "Climax_Period" number of 
      the most recent previous bars.  Volume bars for bull candles are green and for bear 
      cndles are red.  The volume bars qualifying as "Climax" bars can trigger an alert.   
   b. Volume Bars for Candles With Volume Rising Above Average 
      Volume bars where volume is >= "Rising_Factor" percent of the average volume of the 
      "Rising_Period" number of the most recent previous bars.  Volume bars for bull candles
      are blue and for bear candles are blue-violet.    
   C. Colors Display
      The coding of colors allows the user to alter any color.  But this must be done on 
      the "Inputs" tab, not the "Colors" tab. 

2. Selecting the Width of the Volume Bars-
   MT4 has six chart zoom settings referred to as number selections 1 thru 6 as you zoom a 
   chart in, going from thinnest to thickest bars.  Use numbers 1-6 in the external input
   "Chart_Zoom_0123456" to manually control bar widths. Use "1 or 2" for zoomed out 
   charts (thinnest bars).  Use "3" for the Mt4 default chart zoom.  Use "4,5, or 6" for
   zoomed in charts (wider bars).  Use "0" to get the bar width externally, meaning from
   another indicator (i.e., the Filled Dragon or PVA Candles indicator).  If the setting 
   for "Chart_Zoom_0123456" is not between 0-6, it defaults to "3". 
   
   The proper sequence to follow is....
   A. First, enter the "Chart_Zoom_0123456  " input you want.
   B. Second, use the MT4 icons "+/-" to zoom the chart in/out, per the input you made.
   C. Switch chart TF once to reset the chart to your "Chart_Zoom_0123456" input.
       
   Setting "Chart_Zoom_0123456" to "0" turns control of bar widths over to the
   SonicR Filled Dragon indicator, so when you want to zoom in/out on the chart you only 
   have to change your zoom setting in that indicator.  The SonicR Filled Dragon indicator 
   does not have to be turned on for the SonicR PVA Volumes indicator to be slaved to it's 
   zoom setting. however, if the SonicR Filled Dragon indicator is not in the list of chart 
   indicators, then the SonicR PVA Volumes indicator is automatically slaved to the 
   SonicR PVA Candles indicator instead, if it is on the chart list of indicators, whether
   it is turned on or off.  If neither the SonicR Filled Dragon nor the SonicR PVA Candles 
   indicators are included in the list of chart indicators then "Chart_Zoom_0123456" should 
   be set to the appropriate number (1-6) based to how you have zoomed in/out on the chart.
   
3. Setting the Volume Alert-
   This indicator includes the MT4 sound with text alert that will trigger once per TF at 
   the first qualification of the volume bar as a PVA Climax volume bar in the PVA mode, or
   as an Extreme volume bar in the SRV mode.  Set the input "Volume_Alert_On"  to "true" to 
   activate the alert.  Use "Broker_Name_In_Alert" to input your broker name so it will  
   appear in the on-screen alert.  This helps to avoid confusion if you are simultaneously 
   using multiple MT4 platforms from different brokers.

4. On/Off & Display Range-   
   The indicator can be turned on/off without having to remove it from the chart, thereby 
   preserving your chart settings. 

Changes from VSA Histogram indy released 05-01-2012 to release 05-25-2013 of this new indy:
01 - Removed the automatic zoom feature and revised the manual zoom feature.  Bar widths can 
     now be slaved to the SonicR Solid Dragon or SonicR PVA Candles indicators.
02 - PVA "Rising Volume" Bull/bear candles now have Bull/bear colors.
   - Classifies "Rising Volume" candles that are >= "Extreme_Factor" percent of the average 
     volume as "Climax".
   - Shifts the averaging period for "Rising Volume" by one TF.
03 - Provides easy access to colors used.    
05 - Revises Alert code so it now works on all TFs (including M1) and adds an External Input 
     so the broker name can appear in the text alert (helpful if running multiple MT4s), and
     eliminates the "sound only" alert.  
          
                                                                    - Traderathome, 05-25-2013
-----------------------------------------------------------------------------------------------
Acknowledgements:
BetterVolume.mq4 - for "climax" candle code definition (BetterVolume_v1.4). 

----------------------------------------------------------------------------------------------
Suggested Colors            White Chart        Black Chart        Remarks
                             
indicator_color1            C'113,131,149'     C'102,099,163'     Normal Volume
indicator_color2            C'045,081,206'     C'062,158,255'     Bull Rising
indicator_color3            C'154,038,232'     C'173,051,255'     Bear Rising
indicator_color4            C'000,151,090'     C'033,207,077'     Bull Climax 
indicator_color5            C'208,017,074'     C'231,001,006'     Bear Climax
 
Note: Suggested colors coincide with the colors of the SonicR Suite of Candles indicator.             
---------------------------------------------------------------------------------------------*/


//+-------------------------------------------------------------------------------------------+
//| Indicator Global Inputs                                                                   |                                                        
//+-------------------------------------------------------------------------------------------+ 
#property indicator_separate_window
#property indicator_buffers 5

#property indicator_color1  C'102,099,163'
#property indicator_color2  C'062,158,255'
#property indicator_color3  C'173,051,255' 
#property indicator_color4  C'031,192,071' 
#property indicator_color5  C'231,001,006'

//Global External Inputs 
extern bool   Indicator_On                    = true;
extern int    Chart_Zoom_0123456              = 0;
extern bool   Volume_Alert_On                 = true;
extern string Broker_Name_In_Alert            = "";
extern color  Std_Volume                      = C'102,099,163'; 
extern color  Bull_Rising                     = C'062,158,255';    
extern color  Bear_Rising                     = C'173,051,255'; 
extern color  Bull_Climax                     = C'031,192,071';  
extern color  Bear_Climax                     = C'231,001,006';   


//Global Buffers and Variables
bool   Deinitialized;       
int    i,j,n,Bar_Width,counted_bars,limit,lastzoom; 
double Normal[],RisingBull[],RisingBear[],ClimaxBull[],ClimaxBear[],
       av,rv,Range,Value2,HiValue2,tempv2;
string ShortName,rf,ef;
int    PVA_Climax_Period               = 10; 
int    PVA_Rising_Period               = 10;
double PVA_Rising_Factor               = 1.5;
double PVA_Extreme_Factor              = 2.0;

//Alert
bool   Alert_Allowed;
double prevBid;

//Global Variable Controlling Bar Widths
double B;
string Bar = "Bar_Setting";
  
//+-------------------------------------------------------------------------------------------+
//| Custom indicator initialization function                                                  |
//+-------------------------------------------------------------------------------------------+
int init()
  {
  Deinitialized = false;
    
  if(Volume_Alert_On == true) {Alert_Allowed = true;} 
  prevBid = 0; 

  //Use Global or Internal instructions for bar widths
  if(Chart_Zoom_0123456 == 0)
    {
    //Get GV Bar Widths for candle bars
    Bar_Width = GlobalVariableGet(Bar); 
    }
  else 
    {  
    //Manually Adjust Width                        
    if(Chart_Zoom_0123456 < 0 || Chart_Zoom_0123456 >6) {Chart_Zoom_0123456 = 3;}    
          if(Chart_Zoom_0123456 == 1) {Bar_Width = 1;}
    else {if(Chart_Zoom_0123456 == 2) {Bar_Width = 2;}      
    else {if(Chart_Zoom_0123456 == 3) {Bar_Width = 2;}
    else {if(Chart_Zoom_0123456 == 4) {Bar_Width = 3;}
    else {if(Chart_Zoom_0123456 == 5) {Bar_Width = 6;}
    else {Bar_Width = 13;} }}}}
    }

  //Indicators
  //Normal Bars
  SetIndexBuffer(0, Normal);  
  SetIndexStyle(0, DRAW_HISTOGRAM, 0, 1, Std_Volume);                        
  //PVA: Rising volume 
  SetIndexBuffer(1, RisingBull);   
  SetIndexStyle(1, DRAW_HISTOGRAM, 0, Bar_Width, Bull_Rising);
  SetIndexBuffer(2, RisingBear);                                                                
  SetIndexStyle(2, DRAW_HISTOGRAM, 0, Bar_Width, Bear_Rising);

  //PVA: Climax volume     
  SetIndexBuffer(3, ClimaxBull);
  SetIndexStyle(3, DRAW_HISTOGRAM, 0, Bar_Width, Bull_Climax); 
  SetIndexBuffer(4, ClimaxBear);        
  SetIndexStyle(4, DRAW_HISTOGRAM, 0, Bar_Width, Bear_Climax);
                           
  //Indicator ShortName 
  ShortName= "SonicR PVA";
  if(Volume_Alert_On) {ShortName= ShortName + "   Alert On";}
  IndicatorShortName (ShortName);           
                           
  //Indicator subwindow data                  
  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()
  {
  return(0);
  }
  
//+-------------------------------------------------------------------------------------------+
//| Custom indicator iteration function                                                       |
//+-------------------------------------------------------------------------------------------+
int start()
  {
  //If Indicator is "Off" deinitialize only once, not every tick 
  if (!Indicator_On)
    {
    if (!Deinitialized) {deinit(); Deinitialized = true;}
    return(0);
    }
    
  //Otherwise indicator is "On" & chart TF is in display range, so proceed.          
  Deinitialized = false;

  //Confirm range of chart bars for calculations   
  counted_bars = IndicatorCounted();
  if(counted_bars < 0) return(-1);
  if(counted_bars > 0) counted_bars--;
  limit = Bars-counted_bars;
  
  //Begin the loop of calculations for the range of chart bars.  With first tick all bars are 
  //calculated.  With each tick thereafter, no calculations occur for previous unchanged bars.
  for(i=0; i<limit; i++)      
    {       
    //Clear buffers
    Normal[i]     = 0;   
    RisingBull[i] = 0;
    RisingBear[i] = 0;   
    ClimaxBull[i] = 0;
    ClimaxBear[i] = 0;    
    Value2        = 0;
    HiValue2      = 0;
    tempv2        = 0; 
    av            = 0;      
            
    //Define Normal histogram bars      
    Normal[i] = Volume[i];   

    //Rising Volume           
    for (j = i+1; j < (i+PVA_Rising_Period); j++) {av = av + Volume[j];}   
    av = av / PVA_Rising_Period;                           
    if(Volume[i] >= av * PVA_Rising_Factor)
      {
      if(Close[i] > Open[i]) {RisingBull[i] = NormalizeDouble(Volume[i],0);}
      if(Close[i] <= Open[i]) {RisingBear[i] = NormalizeDouble(Volume[i],0);}
      }
                       
    //Climax Volume                  
    Range = (High[i]-Low[i]);
    Value2 = Volume[i]*Range;                 
    for(n=i+1;n<i+PVA_Climax_Period;n++)
      {
      tempv2 = Volume[n]*((High[n]-Low[n])); 
      if (tempv2 >= HiValue2) {HiValue2 = tempv2;}    
      }      
    //Define Current and Historic "Climax" bars                  
    if((Value2 >= HiValue2) || (Volume[i] >= av * PVA_Extreme_Factor))
      { 
      //Bull Candle                                  
      if(Close[i] > Open[i]) 
        {
        ClimaxBull[i] = NormalizeDouble(Volume[i],0);
        }
      //Bear Candle  
      else if (Close[i] <= Open[i]) 
        {
        ClimaxBear[i] = NormalizeDouble(Volume[i],0);
        }               
      //Sound & Text Alert
      if((Volume_Alert_On) && (Alert_Allowed == true) && (i == 0) && (prevBid >0)) 
        {     
        Alert(Broker_Name_In_Alert,":  ",Symbol(),"-",Period(),"   PVA alert!");
        Alert_Allowed = false;        
        }//End Alert                               
      }//End Climax Volume                                
    }//End PVA "for i" loop
    prevBid = Bid;      

  return(0);
  }

//+-------------------------------------------------------------------------------------------+
//|Custom indicator end                                                                       |
//+-------------------------------------------------------------------------------------------+    
         