Multi-Timeframe Heiken Ashi MA

 

Ok - I have been attempting to do this for about 10 hours now and I am going nuts. There has to be something very simple that I'm missing.

I wanted to make an indicator that determines whether which direction the Heiken Ashi MA candles are facing, and then graphs JUST THE COLOR in a separate indicator window. This works just fine when I set the chart's timeframe as the Heiken Ashi timeframe. However when I attempt to determine the candle color on higher timeframes, it acts very very oddly! Basically, it works right when I attach it to the chart, but every subsequent bar of the higher timeframe will render as the same color!

The top indicator is running on M1 (the same as the chart). The bottom one is running on H1. Top one works, bottom doesn't.

The indicators are included in the zip file, but I'll post the code here too:

Here is the base indicator... this one seems to work fine and has no problems:

#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_width1 3
#property indicator_color2 White
#property indicator_width2 3
#property indicator_maximum 1.05
#property indicator_minimum -0.05
//#property indicator_color3 Red/
//#property indicator_color4 White
//---- buffers

extern int MaMetod  = 1;
extern int MaPeriod = 3;

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {
//---- indicators
   IndicatorBuffers(4);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1, ExtMapBuffer2);
  // SetIndexStyle(2,DRAW_HISTOGRAM, 0, 3, Red);
   SetIndexBuffer(2, ExtMapBuffer3);
  // SetIndexStyle(3,DRAW_HISTOGRAM, 0, 3, White);
   SetIndexBuffer(3, ExtMapBuffer4);
//----
   SetIndexDrawBegin(0,10);
   SetIndexDrawBegin(1,10);
   //SetIndexDrawBegin(2,10);
   //SetIndexDrawBegin(3,10);
//---- indicator buffers mapping
   //SetIndexBuffer(0,ExtMapBuffer1);
   //SetIndexBuffer(1,ExtMapBuffer2);
   //SetIndexBuffer(2,ExtMapBuffer3);
   //SetIndexBuffer(3,ExtMapBuffer4);
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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;
   while(pos>=0)
     { 
      maOpen=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_OPEN,pos);
      maClose=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_CLOSE,pos);
      maLow=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_LOW,pos);
      maHigh=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_HIGH,pos);

      haOpen=(ExtMapBuffer3[pos+1]+ExtMapBuffer4[pos+1])/2;
      haClose=(maOpen+maHigh+maLow+maClose)/4;
      haHigh=MathMax(maHigh, MathMax(haOpen, haClose));
      haLow=MathMin(maLow, MathMin(haOpen, haClose));
      if (haOpen<haClose) 
        {
         ExtMapBuffer1[pos]=0;
         ExtMapBuffer2[pos]=1;
        } 
      else
        {
         ExtMapBuffer1[pos]=1;
         ExtMapBuffer2[pos]=0;
        } 
      ExtMapBuffer3[pos]=haOpen;
      ExtMapBuffer4[pos]=haClose;
           pos--;
     
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

And here is the Multi Timeframe indicator that feeds off the first one. I have been trying to fix it for hours.... I must be some sort of retard :(

#property copyright "Copyright © 2006, Forex-TSD.com "
#property link      "https://www.forex-tsd.com/"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_width1 3
#property indicator_color2 White
#property indicator_width2 3
#property indicator_maximum 1.05
#property indicator_minimum -0.05

extern int MaMetod  = 1;
extern int MaPeriod = 3;
extern int TimeFrame=1440;

double ExtMapBuffer1[];
double ExtMapBuffer2[];

int init()
{
   string short_name;
   
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1,ExtMapBuffer2);
      
   
   short_name="HeikenAshiSW("+TimeFrame+")";
   IndicatorShortName(short_name);
   
   
   switch(TimeFrame) 
   {
   case 1    : TimeFrame=PERIOD_M1;  break; 
   case 5    : TimeFrame=PERIOD_M5;  break;
   case 15   : TimeFrame=PERIOD_M15; break;
   case 30   : TimeFrame=PERIOD_M30; break;
   case 60   : TimeFrame=PERIOD_H1;  break;
   case 240  : TimeFrame=PERIOD_H4;  break;
   case 1440 : TimeFrame=PERIOD_D1;  break;
   case 7200 : TimeFrame=PERIOD_W1;  break;
   case 28800: TimeFrame=PERIOD_MN1; break;
   default  : TimeFrame=Period();   break;
   }
return(0);
}

int start()
{
   
   datetime TimeArray[];
   int i=0,y=0, prevy=0;  
   int counted_bars=IndicatorCounted();
   
   
      if (TimeFrame<Period()) 
      {
      SetIndexDrawBegin(0,Bars);  
      SetIndexDrawBegin(1,Bars);
      Comment("Incorrect TimeFrame");
      return(0);
      }

   if ( counted_bars > 0 )  int limit=Bars-counted_bars;
   if ( counted_bars < 0 )  return(0);
   if ( counted_bars ==0 )  limit=Bars-1;  
     
   
   
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame);
         
   for(i=0,y=0;i<limit;i++)
   {
   if (Time[i]<TimeArray[y]) y++;
   double UpBuffer =iCustom(NULL,TimeFrame,"Heiken Ashi_SW",MaMetod,MaPeriod,0,y) ;
   double DnBuffer =iCustom(NULL,TimeFrame,"Heiken Ashi_SW",MaMetod,MaPeriod,1,y) ; 
   
   if(UpBuffer > DnBuffer && UpBuffer != EMPTY_VALUE && DnBuffer != EMPTY_VALUE) ExtMapBuffer1[i] = 1;
   if(UpBuffer < DnBuffer && UpBuffer != EMPTY_VALUE && DnBuffer != EMPTY_VALUE) ExtMapBuffer2[i] = 1;

   }
   
   
   
   
  
 
   return(0);
}
 

Is it really wrong?

The white field of the bottom indicator on your first pic above seem to start around 19:30 and end around 21:30.

Isn't it so that the 19, 20 & 21 candles on H1 are bullish and it is displaying as it should? For every candle there will be 60 white bars if displaying the indicator ON a m1 chart.

Just a thought...

/ McKeen

 

McKeen, unfortunately if I let it run the indicator will remain white the entire time... for over a year.

If I remove the indicator and re-attach it the problem is fixed until the next bar...at which point it shows up white again no matter what.

 
for(i=0,y=0;i<limit;i++)
   {
   if (Time[i]<TimeArray[y]) y++;
   double UpBuffer =iCustom(NULL,TimeFrame,"Heiken Ashi_SW",MaMetod,MaPeriod,0,y) ;
The bar you want in the iCustom is the H1 bar corresponding to your Time[i]. Get rid of TimeArray[] and use
y = iBarShift(NULL,TimeFrame, Time[i]);
Reason: