How to set the buffer of my indicator to the correct candle index

 

Hi guys, 

I recently develop an indicator that detects support and resistance levels of the 100 previous peaks and troughs. I am trying to implement the data and calculations of this indicator to an EA I am trying to develop but I am having some problems. Since the indicator detects structure levels, it does not output data every candle (just when it identifies a new relevant structure levels).


So the problem I am having, as you might expect, is that when I try to set a buffer to retrieve the data from the indicator, using SetIndxBuffer(), I get my outputs (data/calculations) stored in the most recent candles of the market. By this I mean that the data points from my array SRLevels[], which stores the values where the structure levels were found, are stored within every new candle of the market instead of being stored in the candle of the chart where the new structure level was found (indexed in order). Below I leave attached a picture of my MT5 so that you can understand better (in green you can see the value that I am getting for the first index/candle of the chart). Since I am a newbie, I can understand all the code but I am not completely sure where to implement changes in it, so could you guys help me out?


#property indicator_buffers 1
#property indicator_applied_price PRICE_TYPICAL
#property indicator_plots 1



//ZigZag inputs
input int InpDepth = 12;
input int InpDeviation = 5;
input int InpBackstep = 3;
//Peak analysis inputs
input int InpGapPoints = 100; //gap between peak/trophs points 
input int InpSensitivity = 2; //how many times it touches the level
input int InpLookback = 50;  //how many peaks we look back
//Drawing Supports and Resistances
input string InpPrefix = "SRLevel_"; //name of the line
input color InpColour = clrYellow; 
input int InpLineWidth = 2;
//Store Levels(supports and resistances)
double SRLevels[];
//Store ZigZag data
double Buffer[];
int Handle;

int OnInit()
  {
   Handle = iCustom(Symbol(), Period(), "Market\\ArrowZigZag",InpDepth, InpDeviation, InpBackstep); //ZigZag indicator for support and resistance levels
   if(Handle == INVALID_HANDLE){
      Print ("Could not create a handle to zigzag indicator");
      return(INIT_FAILED);
   }
   ArraySetAsSeries (Buffer, true);
   ArraySetAsSeries(SRLevels, true);
   ObjectsDeleteAll(0,InpPrefix, 0, OBJ_HLINE); 
   ArrayResize(SRLevels, InpLookback);
   
   SetIndexBuffer(0, SRLevels, INDICATOR_DATA);
 
   string shortname;
   StringConcatenate(shortname, "Structure Level (",InpDepth, ",", InpDeviation, ",",InpBackstep, ")" ); //set a label do display in DataWindow
   PlotIndexSetString(0,PLOT_LABEL,shortname);   //set a name to show in a separate sub-window or a pop-up help
   IndicatorSetString(INDICATOR_SHORTNAME,shortname); //set accuracy of displaying the indicator values
   IndicatorSetInteger(INDICATOR_DIGITS,5);
   
   ChartRedraw(0);
   
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason){
   Print("EA STOPPED or DEINITIALIZED");

   IndicatorRelease(Handle); //release zigzag indicator
   ObjectsDeleteAll(0, InpPrefix, 0, OBJ_HLINE); //delete all objects in the chart
}

//STRUCTURE FINDER (support and resistance)
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 &spred[]){
              
  
  //Convert pips to price gap
  static double levelGap = InpGapPoints*SymbolInfoDouble(Symbol(), SYMBOL_POINT);

   //if there is an error with retrieving data
   if (rates_total==prev_calculated) return(rates_total);

   //Get data from peaks
   double zz = 0;
   double zzPeaks [];
   int zzCount = 0;
   ArrayResize(zzPeaks, InpLookback);
   ArrayInitialize(zzPeaks, 0.0);

   int count = CopyBuffer(Handle, 0, 0, rates_total, Buffer);
   if (count<0){
      int err=GetLastError();
      return (0);
      }
    

   for(int i=1; i<rates_total && zzCount<InpLookback; i++){
      
      zz = Buffer[i];
      if (zz!=0 && zz!=EMPTY_VALUE){
         zzPeaks[zzCount] = zz;
         zzCount++;
      }
   }
   ArraySort(zzPeaks);

   //Search for points where peaks touched as many times as previously inputed
   int srCounter = 0;
   double price;
   int priceCount = 0;
   ArrayInitialize (SRLevels, 0.0);
   for (int i=InpLookback-1; i>=0; i--){
      price += zzPeaks[i];
      priceCount++;
      if (i==0 || (zzPeaks[i]-zzPeaks[i-1])>levelGap){
         if (priceCount>=InpSensitivity){
            price = price/priceCount;
            SRLevels[srCounter] = price;
            srCounter++;
         }
         price = 0;
         priceCount = 0;
      }
   }
   DrawLevels();

   return (rates_total);
}
   
   //fucntion to draw the different support/resistances
   void DrawLevels(){
   
      for (int i=0; i<InpLookback; i++){
         string name = InpPrefix + IntegerToString(i);
         
         if (SRLevels[i]==0){
            ObjectDelete(0, name);
            continue;
         }
   
         if (ObjectFind(0, name)<0){
            ObjectCreate(0, name, OBJ_HLINE, 0, 0, SRLevels[i]);
            ObjectSetInteger(0, name, OBJPROP_COLOR, InpColour);
            ObjectSetInteger(0, name, OBJPROP_WIDTH, InpLineWidth);
            ObjectSetInteger(0, name, OBJPROP_SELECTABLE, true);
         }  else{
            ObjectSetDouble(0, name, OBJPROP_PRICE, SRLevels[i]);
         }
      }
      ChartRedraw(0);
   }


I have been looking around for other post or articles but I haven't been able to find an answer so if anyone knows how to solve this issue, please do not hesitate to give your thoughts.


Thank you

Files:
ChartMQL5.jpg  1032 kb