/*------------------------------------------------------------------------------------
  Tape chart of the heiken ashi indicator
-------------------------------------------------------------------------------------*/
// Indicator properties
#property copyright ""
#property link      ""

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  White
#property indicator_color2  Red
#property indicator_width1  4
#property indicator_width2  4
#property indicator_maximum 1
#property indicator_minimum 0

// Indicator buffers
double ha_up_buf[];
double ha_dn_buf[];
double ha_open_buf[];
double ha_close_buf[];
double ha_high_buf[];
double ha_low_buf[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() {
  IndicatorBuffers(6);
  SetIndexStyle(0,DRAW_HISTOGRAM);
  SetIndexBuffer(0,ha_up_buf);
  SetIndexLabel(0,"HeikenAshi Up");
  SetIndexStyle(1,DRAW_HISTOGRAM);
  SetIndexBuffer(1,ha_dn_buf);
  SetIndexLabel(1,"HeikenAshi Down");
  SetIndexStyle(2,DRAW_NONE);
  SetIndexBuffer(2,ha_low_buf);
  SetIndexStyle(3,DRAW_NONE);
  SetIndexBuffer(3,ha_high_buf);
  SetIndexStyle(4,DRAW_NONE);
  SetIndexBuffer(4,ha_open_buf);  
  SetIndexStyle(5,DRAW_NONE);
  SetIndexBuffer(5,ha_close_buf);
  IndicatorDigits(0);     
  //IndicatorShortName(INDICATOR_NAME);
  return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                         |
//+------------------------------------------------------------------+
int deinit() {
   return (0);
}


//+------------------------------------------------------------------+
//| Heiken Ashi Tape                                                 |
//+------------------------------------------------------------------+
int start() {
  int i, new_bars, counted_bars; 
  double ha_open, ha_high, ha_low, ha_close;
  
  // Get unprocessed data
  counted_bars=IndicatorCounted();
  if(counted_bars < 0) return (-1); 
  if(counted_bars>0) counted_bars--;
  new_bars=Bars-counted_bars;
  
  for(i=new_bars; i>=0; i--) {
    if (ha_open_buf[i+1]==EMPTY_VALUE || ha_close_buf[i+1]==EMPTY_VALUE) {
      ha_close_buf[i+1]=Close[i+1];
      ha_open_buf[i+1]=Open[i+1];
    }
    
    // Calc heiken ashi
    ha_open=(ha_open_buf[i+1]+ha_close_buf[i+1])/2;
    ha_close=(Open[i]+High[i]+Low[i]+Close[i])/4;
    ha_high=MathMax(High[i], MathMax(ha_open, ha_close));
    ha_low=MathMin(Low[i], MathMin(ha_open, ha_close));
    
    ha_open_buf[i]=ha_open;
    ha_close_buf[i]=ha_close;
    
    if (ha_open<ha_close) {
      ha_low_buf[i]=ha_low;
      ha_high_buf[i]=ha_high;
    } 
    else {
      ha_low_buf[i]=ha_high;
      ha_high_buf[i]=ha_low;
    } 
    
    // Draw tape chart
    ha_up_buf[i] = EMPTY_VALUE;
    ha_dn_buf[i] = EMPTY_VALUE;
    if (ha_open<ha_close)
      ha_up_buf[i]=1;
    else if (ha_open>ha_close)
      ha_dn_buf[i]=1;
  }
  
  return(0);
}
//+------------------------------------------------------------------+