SR indicator need help!

 
//this code attempt to chart out the major SR levels. it's logic are as follow:
//1.  look back say (400 bars). look at all the turning points using fractuals. put into a temp array
//2.  sort the temp array from low to high. 
//3. compare each of the SR level, and see if they are close togehter, using average range of the past 400 bars, countup for each line, enough lines are cluster together, 
//  these level are deemed to be significant, and plot out in chart. 


#define BufferAmount 50
#define MaxColorNo 6

#property indicator_chart_window                                  //drawing indicator in same window
#property indicator_buffers BufferAmount * 2                      //number of indicator buffers 2
#property indicator_plots   BufferAmount                          //only one plot is used

input int LookUpBar = 400;                                        //how many historical bar to lookup for SR anslysis
input double AvgRangeFactor = 0.2;                                //average range Multipler to determine if SR lines are cluster togehter
input int ClusterThreshold = 5;                                   //how many number of lines need to cluster together before it is consider to be a significant level

struct Struct_SR
{
   double Buffer[];
   double Color[];
};

Struct_SR SR[BufferAmount];

void OnInit()
{
   for (int i = 0 ; i < BufferAmount ; i++)
   {
      SetIndexBuffer((i * 2), SR[i].Buffer, INDICATOR_DATA);
      SetIndexBuffer((i * 2) + 1, SR[i].Color, INDICATOR_COLOR_INDEX);
   }
   
   for (int i = 0 ; i < BufferAmount ; i++)
   {
      PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_COLOR_ARROW);
      PlotIndexSetInteger(i, PLOT_ARROW, 115);
      PlotIndexSetInteger(i, PLOT_LINE_WIDTH, 1);
      PlotIndexSetInteger(i, PLOT_LINE_STYLE, STYLE_SOLID);
      PlotIndexSetInteger(i, PLOT_COLOR_INDEXES, 6);
      PlotIndexSetInteger(i, PLOT_LINE_COLOR, 0, clrPaleTurquoise);     //use color to determine the level of SR line, the more darker the color, the more the liens are clustered together.
      PlotIndexSetInteger(i, PLOT_LINE_COLOR, 1, clrSkyBlue); 
      PlotIndexSetInteger(i, PLOT_LINE_COLOR, 2, clrDodgerBlue); 
      PlotIndexSetInteger(i, PLOT_LINE_COLOR, 3, clrRoyalBlue); 
      PlotIndexSetInteger(i, PLOT_LINE_COLOR, 4, clrBlue); 
      PlotIndexSetInteger(i, PLOT_LINE_COLOR, 5, clrDarkBlue);
      PlotIndexSetString(i, PLOT_LABEL, "SR" + (string)i );
      PlotIndexSetInteger(i, PLOT_DRAW_BEGIN, LookUpBar);              
      PlotIndexSetDouble(i, PLOT_EMPTY_VALUE, 0.0);             
   }
   
   IndicatorSetString(INDICATOR_SHORTNAME, "SR");              
}

int OnCalculate(const int rates_total,                            // amount of history in bars at the current tick
                const int prev_calculated,                        // amount of history in bars at the previous tick
                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 StartLoopBarNo;
   
   if (prev_calculated == 0)
      StartLoopBarNo = LookUpBar;
   else 
      StartLoopBarNo = prev_calculated - 1;
  
   for(int i = StartLoopBarNo ; i < rates_total - 1 ; i++)                //main loop for the indicator                            
   {
      int SRNo = 0;
      double Range = 0;
      double AvgRange = 0;
      
      double SRTemp[];
      ArrayResize(SRTemp, LookUpBar);
      
      for (int j = i - LookUpBar + 2 ; j < i ; j++)                   //look up the previous 400 bar to search for SR
      {
         if (High[j] > High[j+1] &&                                  //using fractual definition to determine turning point for SR 
            High[j] > High[j+2] &&
            High[j] > High[j-1] &&
            High[j] > High[j-2])
         {
            SRTemp[SRNo] = High[j];
            SRNo++;            
         }
         
         if (Low[j] < Low[j+1] &&
            Low[j] < Low[j-1] &&
            Low[j] < Low[j+2] &&
            Low[j] < Low[j-2])
         {
            SRTemp[SRNo] = Low[j];
            SRNo++;            
         }
         
         Range += High[j] - Low[j];
      }
      
      AvgRange = (Range / (LookUpBar - 2)) * AvgRangeFactor;           //average range to determine if the level are close together 

      ArrayResize(SRTemp, SRNo);  
      ArraySort(SRTemp);
   
      int n = 0;
      for (int j = 0 ; j < ArraySize(SRTemp) ; j++)                  //Compare each SR line against each other, to check if the 2 lines are within the average range distance.  
      {
         int ClusterNo = 0; 
         for (int k = 0 ; k < ArraySize(SRTemp) ; k++)             
         {
            if (MathAbs(SRTemp[j] - SRTemp[k]) < AvgRange)
               ClusterNo++;                                          //count up each time it is close together, if there are more than the set threshold amount of times(5), then chart it out. 
         }

         if (ClusterNo >= ClusterThreshold) 
         {
            SR[n].Buffer[i] = SRTemp[j]; 
            SR[n].Color[i] = ClusterNo - ClusterThreshold;
         
            if (SR[n].Color[i] > MaxColorNo - 1)
               SR[n].Color[i] = MaxColorNo - 1;
               
            n = n + 1;
         }
         
         if (n >= ArraySize(SR))                                     //saefty code, if too many SR line, then the rest is ignored. to prevent buffer overflow. 
            break;
      }
   }
   return(rates_total -1);
}



 

Hi All, 

First time post, i am trying to code a SR indicator, the main logic are as follow:


1.  look back say (400 bars). look at all the turning points using fractals. put into a temp array

2.  sort the temp array from low to high. 

3. compare each of the SR level, and see if they are close together, using average range of the past 400 bars, countup for each line, enough lines are cluster together, 

  these level are deemed to be significant, and plot out in chart. 


however i have encounter the following problem,

-if i change the input value halfway, it seems like the indicator would give different value than if i reload it. 

-the SR levels seems to be at a non-sense level. 

-some buffer output values are missing. 

any expert in there can give me some direction on how to fix the problem? 


thanks. 




Files:
Reason: