Custom indicator not drawing arrows.

 

I am new to MQL4 and this is my first try of coding an indicator. I have just tuned some but not built one.

I have coded a custom indicator based on Heiken Ashi candles and two Envelopes applied to this candles. It should draw an arrow (dot) when buying or selling conditions are trigered. 

I think the problem is in the iCustom functions or the iEnvelopesOnArray ones. Could someone help me understand what is going wrong?

Thanks you

This is the Heiken Ashi code:

//+------------------------------------------------------------------+
//|                                                  Heiken_Ashi.mq4 |
//|                   Copyright 2006-2014, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright   "2006-2014, MetaQuotes Software Corp."
#property link        "https://www.mql5.com"
#property description "We recommend the following chart settings (press F8 or select menu 'Charts'->'Properties...'):"
#property description " - on 'Color' Tab select 'Black' for 'Line Graph'"
#property description " - on 'Common' Tab disable 'Chart on Foreground' checkbox and select 'Line Chart' radiobutton"
#property strict

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 Lime
#property indicator_color3 Red
#property indicator_color4 Lime
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 2
#property indicator_width4 2

//---
input color ExtColor1 = Red;    // Shadow of bear candlestick
input color ExtColor2 = Lime;  // Shadow of bull candlestick
input color ExtColor3 = Red;    // Bear candlestick body
input color ExtColor4 = Lime;  // Bull candlestick body
//--- buffers
double ExtLowHighBuffer[];
double ExtHighLowBuffer[];
double ExtOpenBuffer[];
double ExtCloseBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
void OnInit(void)
  {
   IndicatorShortName("Heiken Ashi");
   IndicatorDigits(Digits);
//--- indicator lines
   SetIndexStyle(0,DRAW_HISTOGRAM,0,1,ExtColor1);
   SetIndexBuffer(0,ExtLowHighBuffer);
   SetIndexStyle(1,DRAW_HISTOGRAM,0,1,ExtColor2);
   SetIndexBuffer(1,ExtHighLowBuffer);
   SetIndexStyle(2,DRAW_HISTOGRAM,0,2,ExtColor3);
   SetIndexBuffer(2,ExtOpenBuffer);
   SetIndexStyle(3,DRAW_HISTOGRAM,0,2,ExtColor4);
   SetIndexBuffer(3,ExtCloseBuffer);
//---
   SetIndexLabel(0,"Low/High");
   SetIndexLabel(1,"High/Low");
   SetIndexLabel(2,"Open");
   SetIndexLabel(3,"Close");
   SetIndexDrawBegin(0,10);
   SetIndexDrawBegin(1,10);
   SetIndexDrawBegin(2,10);
   SetIndexDrawBegin(3,10);
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtLowHighBuffer);
   SetIndexBuffer(1,ExtHighLowBuffer);
   SetIndexBuffer(2,ExtOpenBuffer);
   SetIndexBuffer(3,ExtCloseBuffer);
//--- 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;
   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);
//--- 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;
     }
//--- done
   return(rates_total);
  }
//+------------------------------------------------------------------+

And here is my custom indicator code:

#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2

//--- plot Buy
#property indicator_label1  "Buy"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Sell
#property indicator_label2  "Sell"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrCrimson
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1


//--- input parameters
input ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT;
input int      FastEnvPeriod = 10;
input double   FastEnvDesv = 10.0;
input int      FastMaMethod = 3;
input ENUM_APPLIED_PRICE FastPrice = PRICE_CLOSE;
input int      SlowEnvPeriod = 50;
input double   SlowEnvDesv = 10.0;
input int      SlowMaMethod = 3;
input ENUM_APPLIED_PRICE SlowPrice = PRICE_CLOSE;
input int      margin = 100;
input int      barsToProcess = 500;


//--- indicator buffers
double         temp[];
double         heikenashiEnv[];
double         SellBuffer[];
double         BuyBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,SellBuffer);
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,159);
   //SetIndexEmptyValue(0,0.0);
   
   SetIndexBuffer(1,BuyBuffer);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,159);
   //SetIndexEmptyValue(1,0.0);
   
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   //PlotIndexSetInteger(0,PLOT_ARROW,159);
   //PlotIndexSetInteger(1,PLOT_ARROW,159);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 counted_bars=IndicatorCounted(),
       limit,
       start = 1;
 
   if(counted_bars>0)
      counted_bars--;
   
   limit=Bars-counted_bars;
  
   if(counted_bars<Bars-1){
      ArraySetAsSeries(heikenashiEnv,false);
      ArrayResize(heikenashiEnv,Bars);
      ArraySetAsSeries(heikenashiEnv,true);
      }
      
   for(int i = 0; i < limit; i++){
      temp[i] = iCustom(NULL,TimeFrame,"Heikin_Ashi",3,i); 
      heikenashiEnv[i] = temp[i];
   }
   
   for(int i=0;i<limit;i++){
      //heikenashiEnv[i] = iCustom(NULL,TimeFrame,"Heikin_Ashi",3,i); 
      double heikenashiOpen = iCustom(NULL,TimeFrame,"Heikin_Ashi",2,i+1);
      double heikenashiClose = iCustom(NULL,TimeFrame,"Heikin_Ashi",3,i+1);   
      double heikenashiOpenPrev = iCustom(NULL,TimeFrame,"Heikin_Ashi",2,i+2);
      double heikenashiClosePrev = iCustom(NULL,TimeFrame,"Heikin_Ashi",3,i+2);
      double upFastValue = iEnvelopesOnArray(heikenashiEnv,FastEnvPeriod,FastMaMethod,0,FastPrice,FastEnvDesv,MODE_UPPER,i+2);
      double downFastValue = iEnvelopesOnArray(heikenashiEnv,FastEnvPeriod,FastMaMethod,0,FastPrice,FastEnvDesv,MODE_LOWER,i+2);
      double upSlowValue = iEnvelopesOnArray(heikenashiEnv,SlowEnvPeriod,SlowMaMethod,0,SlowPrice,SlowEnvDesv,MODE_UPPER,i+2);
      double downSlowValue = iEnvelopesOnArray(heikenashiEnv,SlowEnvPeriod,SlowMaMethod,0,SlowPrice,SlowEnvDesv,MODE_LOWER,i+2);
      
      if( SELLING CONDITION using the above calculated values ){
            SellBuffer[i] = High[i]+margin*Point;
            }
         }

         
      if( BUYING CONDITION using the above calculated values ){
            BuyBuffer[i] = Low[i]-margin*Point;         
            }
         }

   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Thank you a lot. I don't know what is the return of iCustom and iEnvelopesOnArray and that maybe a big problem.

Step on New Rails: Custom Indicators in MQL5
Step on New Rails: Custom Indicators in MQL5
  • www.mql5.com
Finally we've got an opportunity to try the new trade terminal - MetaTrader 5 . No doubt, it is noteworthy and has many new features as compared to its predecessor. The important advantages of this platform among others are: Essentially modified language allowing now to use the object-oriented programming, still allowing to use the rich...
 
double heikenashiOpen = iCustom(NULL,TimeFrame,"Heikin_Ashi",2,i+1);

Is that the name of the indicator?

 
Keith Watford:

Is that the name of the indicator?

Yes, it is and it is inside the indicators folder in mql4 folder. I already managed to solve the opening error.

Thank you