//+------------------------------------------------------------------+
//| HP_COC (Compute On Close)                                        |
//| Repainting SUCKS                                                 |
//| This will plot the HP value of the just completed bar            |
//| 02/13/09                                                         |
//+------------------------------------------------------------------+
#property copyright "z"
#property link      "z"

#property  indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 White

double dHPVal=0;
double HPBuf[];
bool bFirst=True;
int prevPeriod=0;
int initBars=0;
int prevBars=0;
int iRtn=0;

string sCustomNameToCall = "HP";

//parms for HP
extern int nobs   = 1000; //Number of bars to smooth
extern int lambda = 1600; //Higher lambda leads to the smoother data

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   SetIndexBuffer(0,HPBuf); 
   SetIndexLabel(0,"HPVal");
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   //Initialize
   //save time of right-most bar
   if(Period() != prevPeriod) {
      prevPeriod = Period();
       bFirst = True;
   }
   if(IndicatorCounted() == 0) bFirst = True;
   if(bFirst == True) {
      initBars = Bars;
      prevBars = Bars;
      //get trend on loaded bars
      int kklmt = Bars-20;   //allow for lookback
      for(int kk=kklmt; kk>=1; kk--) {
         iRtn = Get_Trend(kk);
      }
      bFirst = False;
   }
   //Initialize End
   //
   //All bars (full and tick)
   if(Bars <= initBars) return;        //are we past initially loaded bars   
   if(Bars == prevBars) {              //still filling out current bar
      //Print("continuation bar.", TimeToStr(CurTime()),"  bars: ",Bars,TimeToStr(Time[0]));
   }
   else {                              //first tick of new bar, process prior bar
      //Print("new bar.", TimeToStr(CurTime()),"  bars: ",Bars,TimeToStr(Time[0]));
      iRtn = Get_Trend(1);
   }
   prevBars = Bars;
   return(0);
}   

int Get_Trend(int i) {
   dHPVal = iCustom(NULL,0,sCustomNameToCall,nobs,lambda,0,i);
   ////largest number on no find 2147483647.0
   if( (dHPVal > 0) && (dHPVal < 2147483000.0) )
      { HPBuf[i] = dHPVal;}
   else
      { HPBuf[i] = HPBuf[i+1]; }
   return(0);
}
//+------------------------------------------------------------------+



