HA averaging

 

Is there a way to draw a linear weighted moving average of the highs and lows of a Heiken Ashi candle?

It often seems that the HA candles reach a higher/lower price than the actual candle - is this normal?


Also, is there a way to get the readings of the high and low of a Heiken candle? For example, a global function that I could call (MQL4).

 
hi, if the heiken candles a drawn to the chart then a buffer must be used. i assume you can get their values trought the standart iCustom() function... you only have to find the right buffer.
 
zzuegg:
hi, if the heiken candles a drawn to the chart then a buffer must be used. i assume you can get their values trought the standart iCustom() function... you only have to find the right buffer.

Is it buffer 1 and 2 from the HA indicator below?

So:

double HAvalHigh=iCustom(NULL, 0, "Heiken Ashi",?????);

double HAvalLow=iCustom(NULL, 0, "Heiken Ashi",?????);


//+------------------------------------------------------------------+
//|                                                  Heiken Ashi.mq4 |
//|                      Copyright c 2004, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
//| For Heiken Ashi we recommend next chart settings ( press F8 or   |
//| select on menu 'Charts'->'Properties...'):                       |
//|  - On 'Color' Tab select 'Black' for 'Line Graph'                |
//|  - On 'Common' Tab disable 'Chart on Foreground' checkbox and    |
//|    select 'Line Chart' radiobutton                               |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Red
#property indicator_color4 Blue
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 3
#property indicator_width4 3

//----
extern color color1 = Red;
extern color color2 = Blue;
extern color color3 = Red;
extern color color4 = Blue;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM, 0, 1, color1);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1,DRAW_HISTOGRAM, 0, 1, color2);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexStyle(2,DRAW_HISTOGRAM, 0, 3, color3);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexStyle(3,DRAW_HISTOGRAM, 0, 3, color4);
   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);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   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)
     {
      haOpen=(ExtMapBuffer3[pos+1]+ExtMapBuffer4[pos+1])/2;
      haClose=(Open[pos]+High[pos]+Low[pos]+Close[pos])/4;
      haHigh=MathMax(High[pos], MathMax(haOpen, haClose));
      haLow=MathMin(Low[pos], MathMin(haOpen, haClose));
      if (haOpen<haClose) 
        {
         ExtMapBuffer1[pos]=haLow;
         ExtMapBuffer2[pos]=haHigh;
        } 
      else
        {
         ExtMapBuffer1[pos]=haHigh;
         ExtMapBuffer2[pos]=haLow;
        } 
      ExtMapBuffer3[pos]=haOpen;
      ExtMapBuffer4[pos]=haClose;
           pos--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
SanMiguel:

Is it buffer 1 and 2 from the HA indicator below?

So:

double HAvalHigh=iCustom(NULL, 0, "Heiken Ashi",?????);

double HAvalLow=iCustom(NULL, 0, "Heiken Ashi",?????);


Externs in order seperated by comma,then buffer number 0-3 then shift

...Red,Blue,Red,Blue,3,0

hth

V

 

It seems to work but I have the values the opposite way round in some charts, ie some charts show the Low lower than the High, which is correct but others have the Low value higher than the High.

It must be this in the HA code:

if (haOpen<haClose)
{
ExtMapBuffer1[pos]=haLow;
ExtMapBuffer2[pos]=haHigh;
}
else
{
ExtMapBuffer1[pos]=haHigh;
ExtMapBuffer2[pos]=haLow;

}


Do I simply do a check on the values and tell it which buffer to show in the comments?

double HAvalHigh=iCustom(NULL, 0, "Heiken Ashi",Red,Blue,Red,Blue,0,0); //high ExtMapBuffer1

double HAvalLow=iCustom(NULL, 0, "Heiken Ashi",Red,Blue,Red,Blue,1,0); //low ExtMapBuffer2


...


Comment (
"HA Low: " + DoubleToStr(HAvalLow, 5) +
"\nHA High: " + DoubleToStr(HAvalHigh, 5)
);

 

I am trying to set a varibal ebasede on the colour of last week's candle.

It seems to be setting everything to -1 - any ideas on a bug in the code below?


//adjust biases according to last week's candle colour. Red=down, Blue = up
   double HAvalOpen=iCustom(NULL, 0, "Heiken Ashi",Red,Blue,Red,Blue,3,1); 
   double HAvalClose=iCustom(NULL, 0, "Heiken Ashi",Red,Blue,Red,Blue,4,1); 
   if (HAvalOpen<HAvalClose) GlobalVariableSet(Symbol()+"BiasDirection",1); //bullish
   if (HAvalOpen>HAvalClose) GlobalVariableSet(Symbol()+"BiasDirection",-1); //bearish candle
 

hi,

buffer index start with 0.

you try to get a value from buffer 4 which is inexistent.

try this:

//adjust biases according to last week's candle colour. Red=down, Blue = up
   double HAvalOpen=iCustom(NULL, 0, "Heiken Ashi",Red,Blue,Red,Blue,2,1); 
   double HAvalClose=iCustom(NULL, 0, "Heiken Ashi",Red,Blue,Red,Blue,3,1); 
   if (HAvalOpen<HAvalClose) GlobalVariableSet(Symbol()+"BiasDirection",1); //bullish
   if (HAvalOpen>HAvalClose) GlobalVariableSet(Symbol()+"BiasDirection",-1); //bearish candle

i suggest also to avoid global variables. but thats my opinion..