/***********************************************************************
 *
 *               MTF Pin bar indicator
 *               ---------------------
 *
 *              (C) 2009 Mikko Mattila
 *            http://www.currencylabs.com
 *
 ***********************************************************************/
 
#property copyright "mikkom"
#property link      "http://www.currencylabs.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Pink
#property indicator_color2 Lime

double redBody[];
double greenBody[];

extern int tf = 1440;

int init() {

   IndicatorBuffers(2);
   
   SetIndexBuffer(0, redBody);     
   SetIndexStyle(0, DRAW_HISTOGRAM, EMPTY, 3);
   
   SetIndexBuffer(1, greenBody);     
   SetIndexStyle(1, DRAW_HISTOGRAM, EMPTY, 3);
   
   IndicatorShortName("Mikko pin bar");
   
   if(tf == 0)
      tf = Period();
      
   return(0);
}
  
int deinit() {
   return(0);
}
     
int start() {

   SetIndexBuffer(0, redBody);     
   SetIndexStyle(0, DRAW_HISTOGRAM, EMPTY, 10);
   
   SetIndexBuffer(1, greenBody);     
   SetIndexStyle(1, DRAW_HISTOGRAM, EMPTY, 10);
       
   int counted_bars=IndicatorCounted();
  
   if(counted_bars < 0) 
       return(-1);
   if(counted_bars > 0) 
       counted_bars--;
       
   int limit = Bars - counted_bars;     
      
   for(int i = limit; i >= 1; i--) {

      int s = iBarShift(Symbol(), tf, Time[i], true);     
      
      if(s == -1) {
         redBody[i] = redBody[i+1];
         greenBody[i] = redBody[i+1];
      } else {
      
      redBody[i] = 0;
      greenBody[i] = 0;      
      
      double hi = iHigh(Symbol(),tf,s);
      double lo = iLow(Symbol(),tf,s);
      double cl = iClose(Symbol(),tf,s);
      double op = iOpen(Symbol(),tf,s);

      double hi1 = iHigh(Symbol(),tf,s+1);
      double lo1 = iLow(Symbol(),tf,s+1);
      
      if(hi > hi1 &&
         op < hi1 && op > lo1 &&
         cl < hi1 && cl > lo1 &&
         //lo1 < lo &&                  
         hi-MathMax(op,cl) > (MathMax(op,cl)-lo)*1 &&
         hi-hi1 > (hi1-lo)*0.5) {
         
         redBody[i] = hi;      
         greenBody[i] = lo;  

      }
      if(lo < lo1 &&
         op < hi1 && op > lo1 &&
         cl < hi1 && cl > lo1 &&
         //hi1 > hi &&
         MathMin(op,cl)-lo > (hi-MathMin(op,cl))*1 &&
         lo1-lo > (hi-lo1)*0.5) {
         
         greenBody[i] = hi;      
         redBody[i] = lo;  

      }
      }      
   } 
   return(0);
}

