Using Heiken Ashin in an EA (mql4)

 

Guys,

I am trying to write a simple EA to create an alert based on Heiken Ashi candles and MAs.

If

  • Last 3 candles were red (Open > Close)
  • Current candle (candle [0] ) is green
  • Current candle has no lower wick (Low  [0] = Open [0])
  • Current candle has to be above  EMA50 and EMA200

I found a function on internet to call Heiken Ashi using iCustom:

const string nameOfHA = "Heiken Ashi";
double GetHA(int bar, int type=PRICE_CLOSE, int tf=0, string symb=NULL)
  {
   if(tf==0)
      tf = Period();
   else
     {
      int tfs[] = {1,5,15,30,60,240,1440,10080,43200};
      int tfIdx = ArrayBsearch(tfs,tf);
      tf = tfs[tfIdx];
     }

   if(symb==NULL)
      symb = Symbol();

   if(type==PRICE_CLOSE)
      return (iCustom(symb,tf,nameOfHA,3,bar));

   if(type==PRICE_OPEN)
      return (iCustom(symb,tf,nameOfHA,2,bar));

   if(type==PRICE_HIGH || type==PRICE_LOW)
     {
      double hl = iCustom(symb,tf,nameOfHA,0,bar);
      double lh = iCustom(symb,tf,nameOfHA,1,bar);

      if(type==PRICE_HIGH)
         return (MathMax(hl,lh));
      else
         return (MathMin(hl,lh));
     }

   return (0);
  }


And I call this function:

void OnTick()
  {
//---
   if(isNewBar())
     {

      if( GetHA(2,PRICE_OPEN) > GetHA(2,PRICE_CLOSE) && GetHA(3,PRICE_OPEN) > GetHA(3,PRICE_CLOSE) && GetHA(4,PRICE_OPEN) > GetHA(5,PRICE_CLOSE))// last 3 Heikan Ashi were red
        {
         if( (GetHA(1,PRICE_OPEN) < GetHA(1,PRICE_CLOSE)) && (GetHA(0,PRICE_LOW) == GetHA(0,PRICE_OPEN)))// Heikan Ashi is green and no wick at the bottom
           {
            if( (GetHA(1,PRICE_LOW) > iMA(NULL,0,50,0,MODE_EMA,PRICE_MEDIAN,0)) &&  (GetHA(0,PRICE_LOW) > iMA(NULL,0,200,0,MODE_EMA,PRICE_MEDIAN,0)))  //candle above 50 and 200 MAs
              {
               Print("Low 1= ", (GetHA(1,PRICE_LOW)), " ,Low 1= ", (GetHA(1,PRICE_LOW)), " ,Open 1= ", (GetHA(1,PRICE_OPEN)), " ,Close 1= ", (GetHA(1,PRICE_CLOSE)));
               SendNotification(Symbol() + " " + getTimeframeName(Period()) + " Buy Heikan Ashi");
               ObjectCreate("vline_" + IntegerToString(Bars),OBJ_VLINE,0, iTime(0,0,0),0);
               ObjectSet("vline_" + IntegerToString(Bars),6,clrBlue);
               ObjectSet("vline_" + IntegerToString(Bars),7,2);
              }
           }
        }
     }

it appears to be working but sometimes during backtesting red candles pass the above mentioned conditions (green and no wick) and they are marked wrongly. (I load the Heiken Ashi indicator on the chart while in strategy tester and that's how I compare the result with candles on chart)

In the attached charts you can see the first two marked candles are correct but in the second chart a red candles has been marked by EA which is wrong.

Any idea what could be the problem?

Thanks in advance


The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
Files:
correct.png  18 kb
wrong.PNG  13 kb
 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
Reason: