//+------------------------------------------------------------------+
//|                                                                  |
//|                            _TRO_HA_History                       |
//|                                                                  |
//|   Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|                                                                  |
//|   therumpledone@gmail.com                                        |  
//+------------------------------------------------------------------+ 

// this updn was posted here: http://www.forex-tsd.com/general-discussion/7123-daily-average-indicator-2.html#post212976

#property  copyright "Copyright © 2008, Avery T. Horton, Jr. aka TRO" 
#property  link      "http://www.therumpledone.com/" 

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 Lime    
#property indicator_color2 DarkGreen         
#property indicator_color3 Orange      
#property indicator_color4 Maroon    
#property indicator_color5 Red        
   

//---- parameters
 
extern string myPair    = ""; 
 
extern int MaMetod  = 2;
extern int MaPeriod = 6;
extern int MaMetod2  = 0;
extern int MaPeriod2 = 3;
 
//---- buffers

   double     ExtBuffer0[];   
   double     ExtBuffer1[];
   double     ExtBuffer2[];
   double     ExtBuffer3[]; 
   double     ExtBuffer4[];   

 
//+------------------------------------------------------------------+

int myPeriod = 0 ;
 
int ExtCountedBars=0;

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];
double ExtMapBuffer8[];
//+------------------------------------------------------------------+

string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      case PERIOD_MN1: tfs="MN"; break;
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
   }
   return(tfs);
} 

//+------------------------------------------------------------------+

int init()
{
   SetIndexBuffer(0,ExtBuffer0);
   SetIndexBuffer(1,ExtBuffer1);
   SetIndexBuffer(2,ExtBuffer2);
   SetIndexBuffer(3,ExtBuffer3);
   SetIndexBuffer(4,ExtBuffer4);
 
   
   SetIndexStyle(0,DRAW_HISTOGRAM, EMPTY, 4, indicator_color1);
   SetIndexStyle(1,DRAW_HISTOGRAM, EMPTY, 4, indicator_color2);
   SetIndexStyle(2,DRAW_HISTOGRAM, EMPTY, 4, indicator_color3);
   SetIndexStyle(3,DRAW_HISTOGRAM, EMPTY, 4, indicator_color4);
   SetIndexStyle(4,DRAW_HISTOGRAM, EMPTY, 4, indicator_color5);
 
   
   

if (myPair == "") myPair = Symbol();
   
if( myPeriod == 0) myPeriod = Period() ;    


string tPeriod = TimeFrameToString(myPeriod) ;

IndicatorShortName("TRO HA HIST "+myPair+"("+tPeriod+")" );
 

    
return(0);
}

//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
  
//+------------------------------------------------------------------+
int start()
  {

   double maOpen, maClose, maLow, maHigh;
   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;
   int pos2=pos;
   

   ArrayResize(ExtMapBuffer1,Bars);
   ArrayResize(ExtMapBuffer2,Bars);   
   ArrayResize(ExtMapBuffer3,Bars);
   ArrayResize(ExtMapBuffer4,Bars); 
   ArrayResize(ExtMapBuffer5,Bars);       
   ArrayResize(ExtMapBuffer6,Bars);  
   ArrayResize(ExtMapBuffer7,Bars);       
   ArrayResize(ExtMapBuffer8,Bars);
   
      
   while(pos>=0)
     { 

      maOpen  = iOpen(NULL,0,pos);
      maClose = iClose(NULL,0,pos);
      maLow   = iLow(NULL,0,pos);
      maHigh  = iHigh(NULL,0,pos);

      haOpen=(ExtMapBuffer5[pos+1]+ExtMapBuffer6[pos+1])/2;

      haClose=(maOpen+maHigh+maLow+maClose)/4;
     
      
      haHigh=MathMax(maHigh, MathMax(haOpen, haClose));
      haLow=MathMin(maLow, MathMin(haOpen, haClose));

      if (haOpen<haClose) 
        {
         ExtMapBuffer7[pos]=haLow;
         ExtMapBuffer8[pos]=haHigh;
        } 
      else
        {
         ExtMapBuffer7[pos]=haHigh;
         ExtMapBuffer8[pos]=haLow;
        } 
      ExtMapBuffer5[pos]=haOpen;
      ExtMapBuffer6[pos]=haClose;
      

while(true)
{
  int i = pos ;   
  if (haOpen < haClose ) { PlotHist(4,0,0,0,0,i) ; break; }
  if (haOpen > haClose ) { PlotHist(0,0,0,0,4,i) ; break; }
                           PlotHist(0,0,4,0,0,i) ; break; 

} // while 

 	   pos--;
     }   

/*
Comment("i=", i, "\n",        
        "ExtMapBuffer7[i]=", DoubleToStr(ExtMapBuffer7[i],4), "\n",
        "ExtMapBuffer8[i]=", DoubleToStr(ExtMapBuffer8[i],4) );
*/

return(0);

}

//+------------------------------------------------------------------+
      
void PlotHist(int a,int b,int c,int d,int e, int bars)
{

ExtBuffer0[bars]=a;    
ExtBuffer1[bars]=b;
ExtBuffer2[bars]=c;
ExtBuffer3[bars]=d;
ExtBuffer4[bars]=e;
 
}

//+------------------------------------------------------------------+