Help - indicator buffers are empty

 

Dear all,

I am trying to write an MQL5 indicator, but the buffers are empty. Additionally, the arrows are logically not shown.

Can someone please help me? Here's the code:

#property description "Fractals"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots   4

#property indicator_label1  "Fractal Up"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Green
#property indicator_style1  STYLE_DOT

#property indicator_label2  "Fractal Down"
#property indicator_type2   DRAW_LINE
#property indicator_color2  Red
#property indicator_style2  STYLE_DOT

#property indicator_label3  "Up"
#property indicator_type3   DRAW_ARROW
#property indicator_color3  clrLime

#property indicator_label4  "Down"
#property indicator_type4   DRAW_ARROW
#property indicator_color4  clrRed


//--- input parameters
input int      period  =  20;         // Period

//--- indicator buffers
double         Up[];
double         Down[];
double         Long[];
double         Short[];

//--- global variables
bool upflagDownFrontier = true;
bool upflagUpFrontier0 = true;
bool upflagUpFrontier1 = true;
bool upflagUpFrontier2 = true;
bool upflagUpFrontier3 = true;
bool upflagUpFrontier4 = true;

bool downflagDownFrontier = true;
bool downflagUpFrontier0 = true;
bool downflagUpFrontier1 = true;
bool downflagUpFrontier2 = true;
bool downflagUpFrontier3 = true;
bool downflagUpFrontier4 = true;

bool flagUpFrontier = true;
bool flagDownFrontier = true;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   //--- indicator buffers mapping
      SetIndexBuffer(0,Up,INDICATOR_DATA);
      SetIndexBuffer(1,Down,INDICATOR_DATA);
      SetIndexBuffer(2,Long,INDICATOR_DATA);    PlotIndexSetInteger(2,PLOT_ARROW,233);
      SetIndexBuffer(3,Short,INDICATOR_DATA);   PlotIndexSetInteger(3,PLOT_ARROW,234);

      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[])
  {
      ArraySetAsSeries(open,true);
      ArraySetAsSeries(high,true);
      ArraySetAsSeries(low,true);
      
      int to_copy;
      if(prev_calculated>rates_total || prev_calculated<0)     to_copy=rates_total;
      else
         {
            to_copy=rates_total-prev_calculated;
            if(prev_calculated>0)      to_copy++;
         }
      
      int n = period;
      for(int i=1; i<n && !IsStopped(); i++)
         {
            ArrayInitialize(Up,EMPTY_VALUE);
            ArrayInitialize(Down,EMPTY_VALUE);
            ArrayInitialize(Long,EMPTY_VALUE);
            ArrayInitialize(Short,EMPTY_VALUE);
            
         //--- Up     
            upflagDownFrontier = (high[n-i] < high[n]) ? true : false;
            upflagUpFrontier0 = (high[n-i] < high[n]) ? true : false;
            upflagUpFrontier1 = (high[n+1] <= high[n] && high[n+i + 1] < high[n]) ? true : false;
            upflagUpFrontier2 = (high[n+1] <= high[n] && high[n+2] <= high[n] && high[n+i + 2] < high[n]) ? true : false;
            upflagUpFrontier3 = (high[n+1] <= high[n] && high[n+2] <= high[n] && high[n+3] <= high[n] && high[n+i + 3] < high[n]) ? true : false;
            upflagUpFrontier4 = (high[n+1] <= high[n] && high[n+2] <= high[n] && high[n+3] <= high[n] && high[n+4] <= high[n] && high[n+i + 4] < high[n]) ? true : false;
            flagUpFrontier = (upflagUpFrontier0 || upflagUpFrontier1 || upflagUpFrontier2 || upflagUpFrontier3 || upflagUpFrontier4) ? true : false;
             
            Up[i] = (upflagDownFrontier && flagUpFrontier) ? 1 : 0;
            if(Up[i] == 1)    Long[i] = open[i];
            
         //--- Down
            downflagDownFrontier = (low[n-i] > low[n]) ? true : false;
            downflagUpFrontier0 = (low[n+i] > low[n]) ? true : false;
            downflagUpFrontier1 = (low[n+1] >= low[n] && low[n+i + 1] > low[n]) ? true : false;
            downflagUpFrontier2 = (low[n+1] >= low[n] && low[n+2] >= low[n] && low[n+i + 2] > low[n]) ? true : false;
            downflagUpFrontier3 = (low[n+1] >= low[n] && low[n+2] >= low[n] && low[n+3] >= low[n] && low[n+i + 3] > low[n]) ? true : false;
            downflagUpFrontier4 = (low[n+1] >= low[n] && low[n+2] >= low[n] && low[n+3] >= low[n] && low[n+4] >= low[n] && low[n+i + 4] > low[n]) ? true : false;
            flagDownFrontier = (downflagUpFrontier0 || downflagUpFrontier1 || downflagUpFrontier2 || downflagUpFrontier3 || downflagUpFrontier4) ? true : false;
             
            Down[i] = (downflagDownFrontier && flagDownFrontier) ? 1 : 0;
            if(Down[i] == 1)  Short[i] = open[i];
         }      

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

Thanks!

 
EA_Trader1: the buffers are empty.

Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
          Code debugging - Developing programs - MetaEditor Help
          Error Handling and Logging in MQL5 - MQL5 Articles (2015)
          Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
          Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 
William Roeder #:

Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
          Code debugging - Developing programs - MetaEditor Help
          Error Handling and Logging in MQL5 - MQL5 Articles (2015)
          Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
          Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

Of course I did several checks on why the buffers are still empty.
However, I do not get any error at all. I also get e.g. the value 1 or 0 from the variable Up[i], but as stated, all those values are not available/stored in the buffer.

Maybe someone can see the reason for that. I would highly appreciate that, thanks

 

When you iterate the calculation loop that way, all buffers have be set as series not just open/high/low 

Don't use ArrayInitialize in the loop like that. Only initialize the buffers when prev_calculated = 0, and outside of the loop