Indicator not plotting!

 

Hey guys, I wrote a simple indicator that plots "Sections" on the chart. i checked that the buffer arrays are filled correctly, but it is still not plotting the lines.

I tried everything, and now simplified the code as simple as possible, still not plotting:

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   5

//--- plot TF1
#property indicator_label1  "1"
#property indicator_type1   DRAW_SECTION
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- plot TF2
#property indicator_label2  "2"
#property indicator_type2   DRAW_SECTION
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2
//--- plot TF3
#property indicator_label3  "3"
#property indicator_type3   DRAW_SECTION
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2
//--- plot TF4
#property indicator_label4  "4"
#property indicator_type4   DRAW_SECTION
#property indicator_color4  clrRed
#property indicator_style4  STYLE_SOLID
#property indicator_width4  2
//--- plot TF5
#property indicator_label5  "5"
#property indicator_type5   DRAW_SECTION
#property indicator_color5  clrRed
#property indicator_style5  STYLE_SOLID
#property indicator_width5  2

//--- input parameters
enum ENUM_TF {DISABLED, M1, M2, M3, M4, M5, M6, M10, M12, M15, M20, M30, H1, H2, H3, H4, H6, H8, H12, D1, W1, MN}; // Enumeration for Timeframes
input group "|||| TIMEFRAMES || - Current Timeframe excl.";
input ENUM_TF  TF1=M5; // Timeframe 1
input color    TF1color=clrAqua; // Timeframe 1 line color
input ENUM_TF  TF2=M15; // Timeframe 2
input color    TF2color=clrGreenYellow; // Timeframe 2 line color
input ENUM_TF  TF3=M30; // Timeframe 3
input color    TF3color=clrYellow; // Timeframe 3 line color
input ENUM_TF  TF4=H1; // Timeframe 4
input color    TF4color=clrDeepSkyBlue; // Timeframe 4 line color
input ENUM_TF  TF5=H4; // Timeframe 5
input color    TF5color=clrGoldenrod; // Timeframe 5 line color
input group "|||| GENERAL SETTINGS ||";
input int      Lookback=1000; // No. of Bars to illustrate

//--- indicator buffers
double         TF1Buffer[];
double         TF2Buffer[];
double         TF3Buffer[];
double         TF4Buffer[];
double         TF5Buffer[];


// HERE I AM CALCULATING THE VALUES AND WRITING THEM TO THE BUFFERS

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
   SetIndexBuffer(0,TF1Buffer,INDICATOR_DATA);
   SetIndexBuffer(1,TF2Buffer,INDICATOR_DATA);
   SetIndexBuffer(2,TF3Buffer,INDICATOR_DATA);
   SetIndexBuffer(3,TF4Buffer,INDICATOR_DATA);
   SetIndexBuffer(4,TF5Buffer,INDICATOR_DATA);

//--- Initialize the buffers
   ArrayInitialize(TF1Buffer, EMPTY_VALUE);
   ArrayInitialize(TF2Buffer, EMPTY_VALUE);
   ArrayInitialize(TF3Buffer, EMPTY_VALUE);
   ArrayInitialize(TF4Buffer, EMPTY_VALUE);
   ArrayInitialize(TF5Buffer, EMPTY_VALUE);

//---
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
{
   Print("Calculating...");

   ProcessTimeframe(TF1, TF1Buffer, rates_total);
   ProcessTimeframe(TF2, TF2Buffer, rates_total);
   ProcessTimeframe(TF3, TF3Buffer, rates_total);
   ProcessTimeframe(TF4, TF4Buffer, rates_total);
   ProcessTimeframe(TF5, TF5Buffer, rates_total);

   return(rates_total);
}
//+------------------------------------------------------------------+

My BufferArrays are filled as follows:

Array Print

Any advise why it is still not plotting?

 
The properties and OnInit is fine, so it must be a problem with your function "ProcessTimeFrame" which is not causing it to plot. Are you using CopyBuffer?
 

How comes the problem is from the function if the function is filling the array correctly as you can see from the screenshot? this print is after executing the function.

And I am not using CopyBuffer. Should I?

I am filling the buffer array like this:

//+------------------------------------------------------------------+
//| FUNCTION: Process Timeframes                                     |
//+------------------------------------------------------------------+
void ProcessTimeframe(ENUM_TF tf_enum, double &buffer[], int rates_total)
{
   if(tf_enum == DISABLED) return; // Do not process if it's DISABLED

   Print("Identifying similar closes between current Timeframe and ", EnumToString(tf_enum));

   ENUM_TIMEFRAMES tf = ENUMToTimeframe(tf_enum);
   int tfLookback = (Lookback * PeriodSeconds(PERIOD_CURRENT) / PeriodSeconds(tf)) + 1;

   datetime TimeArray[];
   ArrayResize(TimeArray, tfLookback);
   ArraySetAsSeries(TimeArray, true);

   double CloseArray[];
   ArrayResize(CloseArray, tfLookback);
   ArraySetAsSeries(CloseArray, true);

   CopyTime(_Symbol, tf, 0, tfLookback, TimeArray);
   CopyClose(_Symbol, tf, 0, tfLookback, CloseArray);

   Print("Current TimeArray Values:");
   ArrayPrint(TimeArray);
   Print("Current CloseArray Values:");
   ArrayPrint(CloseArray);

   for(int i=0; i<rates_total && i<Lookback; i++)
   {
      datetime currentBarTime = iTime(_Symbol, 0, i);
      bool found = false; // added to track if we found a valid value

      for(int j=0; j<tfLookback; j++)
      {
         if(TimeArray[j] == currentBarTime)
         {
            buffer[i] = CloseArray[j];
            found = true;
            break;
         }
      }

      if(!found) // if we didn't find a valid value, set buffer to EMPTY_VALUE
      {
         buffer[i] = EMPTY_VALUE;
      }
   }
   Print("Current ", EnumToString(tf_enum), " Buffer values:");
   ArrayPrint(buffer);
}


What I noticed too, when printing the array, all values of the array after my "lookback" period are filled with 0.00000 which seems to be the initialized value when I defined the array. the arrayprint function shows about 100.000 positions in the array, I don't know if this is normal?

 

No copybuffer isn't needed now that I see what you have in the function...these numbers like "2E+308" are too large beyond the visible range of the window. I don't really understand what the indicator is supposed to do so I can't provide much help 


Edit: For starters, try changing the for loop in your function to this:


   for(int i=0; i<rates_total && i<Lookback; i++)
   {

      bool found = false; // added to track if we found a valid value

         if(TimeArray[i] == time[i])
         {
            buffer[i] = close[i];
            found = true;
            break;
         }
      

      if(!found) // if we didn't find a valid value, set buffer to EMPTY_VALUE
      
         buffer[i] = EMPTY_VALUE;
      }
  }


the timeframe checking code has to different/may not be necessary

the PERIOD_CURRENT constant isn't actually a time based period like PERIOD_M15, it is only used in certain instances, you may have to try something else

   ENUM_TIMEFRAMES tf = ENUMToTimeframe(tf_enum);
   int tfLookback = (Lookback * PeriodSeconds(ChartPeriod(0)/ PeriodSeconds(tf)) + 1;
 
thank you very much for your suggestions. the problem was simply that it was assigning the values beginning from position 0 of the buffer, whereas the buffer is not really a timeseries as described in the documentation, it begins from the oldest bar to the newest one!
 
phade #:
   int tfLookback = (Lookback * PeriodSeconds(ChartPeriod(0)/ PeriodSeconds(tf)) + 1;

This assumes every bar every exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
          "Free-of-Holes" Charts - MQL4 Articles (2006)
          No candle if open = close ? - MQL4 programming forum (2010)

Use iBarShift.

 
Anas Morad #:
thank you very much for your suggestions. the problem was simply that it was assigning the values beginning from position 0 of the buffer, whereas the buffer is not really a timeseries as described in the documentation, it begins from the oldest bar to the newest one!

That's true, you could start your loop from "prev_calculated" instead of starting the loop from zero

 
William Roeder #:

This assumes every bar every exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
          "Free-of-Holes" Charts - MQL4 Articles (2006)
          No candle if open = close ? - MQL4 programming forum (2010)

Use iBarShift.

oh :-) that's very interesting. thank you william!

Reason: