//+-------------------------------------------------------------------------------------------+
//|                                                                                           |
//|                                   SonicR PVA Candles.mq4                                  |
//|                                                      				                         |
//+-------------------------------------------------------------------------------------------+
#property copyright "Copyright @ 2013 traderathome"
#property link      "email: traderathome@msn.com"
 
/*---------------------------------------------------------------------------------------------
Overview, _v1:

This indicator creates PVA (Price Volume Analysis) color coded standard candlesticks. 

1. PVA Candles-   
   The candle 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 Volumes indicator.  PVA Candle bars emphasized 
   with special colors for special situations help you quickly associate any candle to the 
   corresponding PVA volume bar of the same color.
   A. Candle Bars for Climax Candles
      Candle bars where volume is >= "Extreme_Factor" percent of the average volume of the 
      "Climax_Period" number of the most recent previous candles, and candle bars where the 
      product of candle spread x candle volume is highest for the "Climax_Period" number of  
      the most recent previous candles.  Bull candles are green and bear candles are red.    
   B. Candle Bars for Candles With Volume Rising Above Average 
      Candle bars where volume is >= "Rising_Factor" percent of the average volume of the 
      "Climax_Period" number of the most recent previous candles.  Bull candles are blue and 
      bear are blue-violet, but both can be displayed blue.         
   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 Candle 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 Dragon 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 need to
   change your zoom setting in that indicator.  The SonicR Filled Dragon indicator does not
   have to be turned on for the SonicR PVA Candles indicator to be slaved to it's zoom 
   setting.  It only has to be included in the chart list of indicators above this indicator.
   If the SonicR Filled Dragon indicator is not included in the list of chart indicators, 
   then "Chart_Zoom_0123456" should be set to the 1-6 number appropriate to how you have 
   zoomed in/out on the chart. 
      
   The SonicR Control Panel special Bid Line and Bid Dot use different hard code values
   that depend on the chart zoom setting, which this indicator provides if it is used to
   control the chart zoom setting instead of the SonicR Filled Dragon, i.e. if the 
   SonicR Filled Dragon is either not on the chart list of indicators or this PVA Candles
   indicator is not slaved to it.       

3. Chart Properties Settings-
   To see Candles displayed as intended, be sure in chart Properties on the Common tab you 
   have selected "Chart on foreground" and "Line chart", and on the Colors tab be sure the 
   color for "Line graph" is "None".
   
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 Candlesticks release 05-01-2012 to current release 05-25-2013 of this indy:
01 - Removed the automatic zoom feature. 
02 - Revised the manual zoom feature.  Bar widths can now be controlled by the SonicR
     Filled Dragon indicator if used.  Otherwise this indicator controls it's bar widths and 
     the bar widths of the SonicR PVA Volumes indicator, and also controls spacing of the
     special Bid Line and Bid Dot in the SonicR Control Panel.
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.  

                                                                    - 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'015,015,068'     Gray               Candle Wicks Up
indicator_color2            C'015,015,068'     Gray               CandleWicks Dn
indicator_color3            C'163,163,163'     C'163,163,163'     Bull STD Candle 
indicator_color4            C'100,100,100'     C'100,100,100'     Bear STD Candle
indicator_color5            C'045,081,206'     C'062,158,255'     Bull Rising
indicator_color6            C'154,038,232'     C'173,051,255'     Bear Rising                                                           
indicator_color7            C'000,151,090'     C'031,192,071'     Bull Climax                      
indicator_color8            C'222,018,080'     C'231,001,006'     Bear Climax                      
 
Note: Suggested colors coincide with the colors of the SonicR PVA Volumes indicator.                                                            
---------------------------------------------------------------------------------------------*/


//+-------------------------------------------------------------------------------------------+
//| Indicator Global Inputs                                                                   |                                                        
//+-------------------------------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 8
     
#property indicator_color1  Gray      
#property indicator_color2  Gray
#property indicator_color3  C'163,163,163'      
#property indicator_color4  C'100,100,100'
#property indicator_color5  C'062,158,255'
#property indicator_color6  C'173,051,255'
#property indicator_color7  C'031,192,071'  
#property indicator_color8  C'231,001,006'

//Global External Inputs 
extern bool   Indicator_On                    = true;
extern int    Chart_Zoom_0123456              = 0;
extern color  Bull_Wick                       = Gray;
extern color  Bear_Wick                       = Gray;   
extern color  Bull_Candle                     = C'163,163,163';
extern color  Bear_Candle                     = C'100,100,100';    
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;    
double Bar1[],Candle1[],Bar2[],Candle2[],bodyHigh,bodyLow,	   
       RisingBull[],RisingBear[],ClimaxBull[],ClimaxBear[],
       av,Range,Value2,HiValue2,tempv2,high,low,open,close;              
int    Bar_Width,va,i,j,n,shift1,shift2,time1,counted_bars,limit,lastzoom; 
int    PVA_Color_Set_123               = 1; 
int    PVA_Climax_Period               = 10; 
int    PVA_Rising_Period               = 10;
double PVA_Rising_Factor               = 1.5;
double PVA_Extreme_Factor              = 2.0;

//Global Variable Controlling Bar Widths
string Zoom = "Zoom_Setting";
string Bar = "Bar_Setting";
  
//+-------------------------------------------------------------------------------------------+
//| Custom indicator initialization function                                                  |
//+-------------------------------------------------------------------------------------------+
int init()
  {
  Deinitialized = false;

  //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;} }}}}
       
    //Set bar widths for use by PVA Volumes indicator 
    GlobalVariableSet(Bar, Bar_Width); 
    
    //Set Chart Zoom for use by Control Panel and Trade Levels indicators 
    GlobalVariableSet(Zoom, Chart_Zoom_0123456);             
    }
      
  //Indicators
  //PVA: Normal bodies and wicks         
  SetIndexBuffer(0,Bar1);
  SetIndexStyle(0,DRAW_HISTOGRAM, 0, 1, Bull_Wick);   
  SetIndexBuffer(1,Bar2);  
  SetIndexStyle(1,DRAW_HISTOGRAM, 0, 1, Bear_Wick);   			
  SetIndexBuffer(2,Candle1);
  SetIndexStyle(2,DRAW_HISTOGRAM, 0, Bar_Width, Bull_Candle);  
  SetIndexBuffer(3,Candle2);  
  SetIndexStyle(3,DRAW_HISTOGRAM, 0, Bar_Width, Bear_Candle);    
  //PVA: Rising volume bodies
  SetIndexBuffer(4, RisingBull);
  SetIndexStyle(4, DRAW_HISTOGRAM, 0, Bar_Width, Bull_Rising);
  SetIndexBuffer(5, RisingBear);                           
  SetIndexStyle(5, DRAW_HISTOGRAM, 0, Bar_Width, Bear_Rising);       
  //PVA: Climax volume bodies    
  SetIndexBuffer(6, ClimaxBull);
  SetIndexStyle(6, DRAW_HISTOGRAM, 0, Bar_Width, Bull_Climax); 
  SetIndexBuffer(7, ClimaxBear);        
  SetIndexStyle(7, DRAW_HISTOGRAM, 0, Bar_Width, Bear_Climax);                            
                
  //Indicator ShortName   
  IndicatorShortName("SonicR PVA Candles");        
 	       	 		
  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   
  //check for possible errors
  counted_bars = IndicatorCounted();
  if(counted_bars < 0)  return(-1);     
  //last counted bar will be recounted
  if(counted_bars > 0) counted_bars--;    
  limit = Bars - counted_bars;
  
  //Begin the loop of calculations for the range of chart bars. 
  for(i = limit - 1; i >= 0; i--)       
    {              
    //First, calculate OHLC etc., to construct standard candle     
	 shift1  = iBarShift(NULL,0,Time[i]);
	 time1   = iTime    (NULL,0,shift1);
	 shift2  = iBarShift(NULL,0,time1);
	 high    = iHigh(NULL,0,shift1);
    low     = iLow(NULL,0,shift1);
	 open    = iOpen(NULL,0,shift1);
	 close   = iClose(NULL,0,shift1);
	 bodyHigh= MathMax(open,close);
	 bodyLow = MathMin(open,close);
			 
	 if(close>open)
		{
	   Bar1[shift2] = high;		Candle1[shift2] = bodyHigh;
	   Bar2[shift2] = low;		Candle2[shift2] = bodyLow;
		}
	 else if(close<open)
		{
	 	Bar1[shift2] = low;		Candle1[shift2] = bodyLow;
		Bar2[shift2] = high;		Candle2[shift2] = bodyHigh;
		}
	 else //(close==open)
	   {	
		Bar1[shift2] = low;		Candle1[shift2] = close;
		Bar2[shift2] = high;		Candle2[shift2] = open-0.000001;
      }
           
    //Compute Average Volume and Volume Rising Above Average  
    //Clear buffers
    RisingBull[i] = 0;
    RisingBear[i] = 0;
    ClimaxBull[i] = 0;
    ClimaxBear[i] = 0;
    Value2        = 0;  
    HiValue2      = 0; 
    tempv2        = 0;
    av            = 0;
    va            = 0;      
                 
    //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) {va= 2;} 
                                   
    //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;}    
      } 
    if((Value2 >= HiValue2) || (Volume[i] >= av * PVA_Extreme_Factor)) {va= 1;}                    
             
    //Apply Correct Color to Candle
    if (va==1)
      {
      ClimaxBull[i]=iClose(0,0,i);
      ClimaxBear[i]=iOpen(0,0,i);
      if(iClose(0,0,i) == iOpen(0,0,i)) {ClimaxBear[i]=iOpen(0,0,i)+0.000001;}
      }
    else if (va==2)
      {
      RisingBull[i]=iClose(0,0,i);
      RisingBear[i]=iOpen(0,0,i);
      if(iClose(0,0,i) == iOpen(0,0,i)) {RisingBear[i]=iOpen(0,0,i)+0.000001;}  
      }
    }    
                         
  return(0);
  }

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