How to calculate the first heiken ashi bar mql4

Posted on 2019-06-05 19:48:21 by Jacob

I have seen that there where lots of questions about how to calculate a Heiken Ashi candle stick. Especially the first one as the open always referes to the previous open price. So here we go. A free example code that you can use in your script for an indicator or EA robot. That will calculate the Open, Close, High and low. 

double haClose(string sym, int Per, int Shift){
  return (iOpen(sym, Per, Shift) +iHigh(sym, Per, Shift)+ iLow(sym, Per, Shift) + iClose(sym, Per, Shift))/4;
}
double haOpen(string sym, int Per, int Shift){
  double haopen5 = (iOpen(sym, Per, Shift+5) + haClose(sym, Per, Shift+5) ) / 2;
  double haopen4 = (haopen5 + haClose(sym, Per, Shift+5) ) / 2;
  double haopen3 = (haopen4 + haClose(sym, Per, Shift+4) ) / 2;
  double haopen2 = (haopen3 + haClose(sym, Per, Shift+3) ) / 2;
  double haopen1 = (haopen2 + haClose(sym, Per, Shift+2) ) / 2;
  return (haopen1 + haClose(sym, Per, Shift+1)) / 2 ;
}

double haHigh(string sym, int Per, int Shift){
  return MathMax(iHigh(sym, Per, Shift),MathMax(haOpen(sym, Per, Shift),haClose( sym, Per, Shift)));
}

double haLow(string sym, int Per, int Shift){
  return MathMin(iLow(sym, Per, Shift),MathMin(haOpen(sym, Per, Shift),haClose( sym, Per, Shift)));
}

bool isGreenHeiki(string sym, int Per, int Shift) {
   return haOpen(sym, Per, Shift)<haClose(sym, Per, Shift);
}

bool isRedHeiki(string sym, int Per, int Shift){
   return !isGreenHeiki( sym, Per, Shift);
}

 


Back to listing