Different behavior of indicator and same indicator in EA

 

Good evening,

with Google and the documentation in this forum I solved a lot of my beginner problems, but now I'm stuck and I would really appreciate the help from the experts.

I like Heiken Ashi and as a programming exercise I added a Price Action Channel and a bar counter, which counts the number auf bullish and bearish candles in a row. Bullish candles are positive, bearish candles are negative. Once the direction of the candles changes, the counter starts either with 1 or with -1. The indicator itself works as intended, but as soon as I add it to an EA using iCustom, it changes the behaviour immediately. Please find below a screenshot as an example: The lower chart shows the indicator and the first red candle is counted with -1, as intended. The upper chart shows the EA and for some reason the counting doesn't work anymore. What really surprises me: Not only the Comment() from the EA shows the wrong value, even the data screen from the indicator itself shows the wrong value, so I assume this problem is related to the Indicator and not to the EA (EA = based on MT4 "Moving Average" example).

Screenshot and Indicator code are attached. Any help is really appreciated!

Thanks in advance!


//+------------------------------------------------------------------+
//|                                                  Heiken Ashi.mq4 |
//|                   Copyright 2006-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2006-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property description "HA Neuer Versuch"
#property strict

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Red
#property indicator_color2 White
#property indicator_color3 Red
#property indicator_color4 White
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 3
#property indicator_width4 3

//---
input color ExtColor1 = Red;    // Shadow of bear candlestick
input color ExtColor2 = White;  // Shadow of bull candlestick
input color ExtColor3 = Red;    // Bear candlestick body
input color ExtColor4 = White;  // Bull candlestick body

input int InpPeriods = 5;
input ENUM_MA_METHOD InpMethod = MODE_SMMA;

//--- buffers
double ExtLowHighBuffer[];
double ExtHighLowBuffer[];
double ExtOpenBuffer[];
double ExtCloseBuffer[];

double ExtMaHigh[], ExtMaLow[];
double ExtCounter[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
void OnInit(void)
  {
   IndicatorShortName("Heiken Ashi Neuer Versuch");
   IndicatorDigits(Digits);
//--- indicator lines
   SetIndexStyle(0,DRAW_HISTOGRAM,0,1,ExtColor1);
   SetIndexStyle(1,DRAW_HISTOGRAM,0,1,ExtColor2);
   SetIndexStyle(2,DRAW_HISTOGRAM,0,3,ExtColor3);
   SetIndexStyle(3,DRAW_HISTOGRAM,0,3,ExtColor4);
   
   SetIndexStyle(4,DRAW_LINE,0,1,clrWhite);
   SetIndexStyle(5,DRAW_LINE,0,1,clrRed);
   SetIndexStyle(6,DRAW_NONE);
   
//---
   SetIndexLabel(0,"Low/High");
   SetIndexLabel(1,"High/Low");
   SetIndexLabel(2,"Open");
   SetIndexLabel(3,"Close");
   SetIndexLabel(4,"Ma High");
   SetIndexLabel(5,"Ma Low");
   SetIndexLabel(6,"Bar Counter");
   
   SetIndexDrawBegin(0,10);
   SetIndexDrawBegin(1,10);
   SetIndexDrawBegin(2,10);
   SetIndexDrawBegin(3,10);
   SetIndexDrawBegin(4,10);
   SetIndexDrawBegin(5,10);
   SetIndexDrawBegin(5,10);
   
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtLowHighBuffer);
   SetIndexBuffer(1,ExtHighLowBuffer);
   SetIndexBuffer(2,ExtOpenBuffer);
   SetIndexBuffer(3,ExtCloseBuffer);
   SetIndexBuffer(4,ExtMaHigh);
   SetIndexBuffer(5,ExtMaLow);
   SetIndexBuffer(6,ExtCounter);
//--- initialization done
  }
//+------------------------------------------------------------------+
//| Heiken Ashi                                                      |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   int    i,pos, ipos;
   double haOpen,haHigh,haLow,haClose;
//---
   if(rates_total<=10)
      return(0);
//--- counting from 0 to rates_total
   ArraySetAsSeries(ExtLowHighBuffer,false);
   ArraySetAsSeries(ExtHighLowBuffer,false);
   ArraySetAsSeries(ExtOpenBuffer,false);
   ArraySetAsSeries(ExtCloseBuffer,false);
   ArraySetAsSeries(open,false);
   ArraySetAsSeries(high,false);
   ArraySetAsSeries(low,false);
   ArraySetAsSeries(close,false);
   ArraySetAsSeries(ExtCounter,false);
   
//--- preliminary calculation
   if(prev_calculated>1)
      pos=prev_calculated-1;
   else
     {
      //--- set first candle
      if(open[0]<close[0])
        {
         ExtLowHighBuffer[0]=low[0];
         ExtHighLowBuffer[0]=high[0];
        }
      else
        {
         ExtLowHighBuffer[0]=high[0];
         ExtHighLowBuffer[0]=low[0];
        }
      ExtOpenBuffer[0]=open[0];
      ExtCloseBuffer[0]=close[0];
      //---
      pos=1;
     }
//--- main loop of calculations
   for(i=pos; i<rates_total; i++)
     {
      haOpen=(ExtOpenBuffer[i-1]+ExtCloseBuffer[i-1])/2;
      haClose=(open[i]+high[i]+low[i]+close[i])/4;
      haHigh=MathMax(high[i],MathMax(haOpen,haClose));
      haLow=MathMin(low[i],MathMin(haOpen,haClose));
      if(haOpen<haClose)
        {
         ExtLowHighBuffer[i]=haLow;
         ExtHighLowBuffer[i]=haHigh;
        }
      else
        {
         ExtLowHighBuffer[i]=haHigh;
         ExtHighLowBuffer[i]=haLow;
        }
      ExtOpenBuffer[i]=haOpen;
      ExtCloseBuffer[i]=haClose;
     }
   
   int limit = rates_total-prev_calculated-1;
   
   if(prev_calculated>0) limit++;
   for(ipos=limit;ipos>=0;ipos--)
   {
      int range = ((rates_total-ipos)<InpPeriods) ? (rates_total-ipos) : InpPeriods;
      
      double maHighCur = iMAOnArray(ExtOpenBuffer,0,range,0,InpMethod,ipos);
      double maLowCur = iMAOnArray(ExtCloseBuffer,0,range,0,InpMethod,ipos); 
      
      if(maHighCur>=maLowCur)
      {
         ExtMaHigh[ipos] = maHighCur; 
         ExtMaLow[ipos] = maLowCur;
      }
      else
      {
         ExtMaHigh[ipos] = maLowCur;
         ExtMaLow[ipos] = maHighCur;
      }
   }
      
   if(prev_calculated == 0)   
   {
      if(ExtOpenBuffer[0]<=ExtCloseBuffer[0])
         ExtCounter[0] = 1;
      else
         ExtCounter[0] = -1;
   }
 
   for(i = MathMax(1,prev_calculated) ; i < Bars; i++) // Lookback = 1, da eine Kerze zurückgeschaut wird
   {
      if(ExtOpenBuffer[i]<=ExtCloseBuffer[i]) // aktuelle Kerze ist bullish
      {
         if(ExtOpenBuffer[i-1]<=ExtCloseBuffer[i-1]) // letzte Kerze auch bullish?
            ExtCounter[i]=ExtCounter[i-1]+1;
         else
            ExtCounter[i]=1;
      }
      else
      {
         if(ExtOpenBuffer[i-1]>ExtCloseBuffer[i-1]) // letzte Kerze auch bearish?
            ExtCounter[i]=ExtCounter[i-1]-1;
         else
            ExtCounter[i]=-1;
      }
   }
     
//--- done
   return(rates_total);
  }
//+------------------------------------------------------------------+
Files:
 

Good morning,

unfortunately I'm really stuck without your help. Does nobody have an idea which my indicator does not show the correct behavior once I use it in an EA?

Any feedback is highly appreciated!

Thanks!



 
SteLev #:

Good morning,

unfortunately I'm really stuck without your help. Does nobody have an idea which my indicator does not show the correct behavior once I use it in an EA?

Any feedback is highly appreciated!

Thanks!

Nobody is going to be able to answer you as you have not provided all the code, but even if you did it is a big ask to expect others to do your debugging. 

Clearly the answer is that either the indicator or the EA are not coded as intended, so you will need to review your code, use the debugger, compare values step by step to find the discrepencies.

 

Thanks for your reply. Neither am I expecting somebody to do my debugging, nor did I ask for it. But I was hoping for some ideas like "this could happen when...", which would make things a lot easier for me. I'm not a MQL4 expert, but if an Indicator is showing the intended behavior in a chart, but shows a different behavior once it's used in an EA, I would expect some bells are ringing inside the heads of the experts, but maybe I'm wrong.

I see your point in not providing the full code. I will further try to reduce my code to the bare minimum in order to locate the issue.

 
SteLev #:

Thanks for your reply. Neither am I expecting somebody to do my debugging, nor did I ask for it. But I was hoping for some ideas like "this could happen when...", which would make things a lot easier for me. I'm not a MQL4 expert, but if an Indicator is showing the intended behavior in a chart, but shows a different behavior once it's used in an EA, I would expect some bells are ringing inside the heads of the experts, but maybe I'm wrong.

I see your point in not providing the full code. I will further try to reduce my code to the bare minimum in order to locate the issue.

The most probable case, is that in your EA you are collecting the wrong information from your Indicator, or you you are using different parameters when calling the Indicator (or EA). In other words, the problem is most likely in your EA code (which you did not provide) and probably not in your Indicator code.

Also, since an EA does not worry about "colours", only the data, I would leave those inputs at the end of the input list, so that they can be skipped in the iCustom() call. However, it's best to leave the colour information out of the inputs and defined by the normal display setup that MetaTrader provides.

 
  1. The problem is your EA, which you don't show.

  2. You should encapsulate your iCustom calls to make your code self-documenting.
              take candle color hekin ashi - MQL4 and MetaTrader 4 #8-10 or #1 (2018)

 
Fernando Carreiro #:

The most probable case, is that in your EA you are collecting the wrong information from your Indicator, or you you are using different parameters when calling the EA. In other words, the problem is most likely in your EA code (which you did not provide) and probably not in your Indicator code.

Also, since an EA does not worry about "colours", only the data, I would leave those inputs at the end of the input list, so that they can be skipped in the iCustom() call.

Thank you, William and Fernando, this already helps me a lot. Really appreciate it. 👍