S/R in MQL recognition

 

Hello guys,

I have a question about recognizing supports and resistance levels in MQL? My idea is this:

-input int checkbars = X; (in order to make it easy, lets imagine this variable is 21

-if 21st bars´ highs on each side (to the left and to the right on the graph) are above from the certain bar's high, it would draw a horizontal line on that price level which is highest

-Thus, the indicator would be checking from the bar 21 (considering 0 = current unclosed bar)

-by the variable checkbars we could change the sensitivity. Higher value – lower sensitivity and vice versa

-on the picture are the S/R levels. To explain further: the higher line would be where it is, if the “checkbars” would be 21 or less. If it was 22, then the indicator or function would also count in the next large bar penetrating this level. Thus, the line would not be created where it is… Also if we set the checkbars to 5, then the line would be created on the highest high on the visible graph in this picture

s/r levels

I believe it has some sense but I have to admit, that my programming skills finish right here and I have to ask for help - how would you make such an indicator? because of optimalization it is necessary to have the possibility to change the variable "checkbars", if it wasnt, I would just put this into endless conditions if(...) and would not bother you...

Any help much appreciated! 

 
You'd better search here or you google - many solution exists!
 
Yes, I do know that. I have found many solutions for displaying some kinds of S/R levels but I couldnt have found this one Im interested in. I believe I would be able to put this into the code myself but I just need some idea to push me - not sure in this moment how to put this into the code this...
 

I did the hard way... Below is the code which SHOULD BE capable of doing what I described above...

However, it shows me error during backtest "Array out of Range." Unfortunately, I don't understand the explanation very well... I thought this line would solve it but it did not. (Bars > Number_of_candles)

Any help? (I read what the error means but I dont get it) 

//+------------------------------------------------------------------+
//|                                           support_resistance.mq4 |
//|                                                               LJ |
//+------------------------------------------------------------------+
#property copyright "LJ"
#property link      "lukas.janca@icloud.com"
#property version   "1.00"
#property strict

input int Number_of_candles = 15; //Number of candles which are necessary to create S/R levels

int OnInit()
{
Alert("EA is ON");
return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
  Alert("EA is OFF");
}

void OnTick()
{
int static NewBar = Bars;

if(NewBar != Bars && Bars > Number_of_candles)
{
NewBar = Bars;

double static level_high;
int PS = Number_of_candles+1;

Print("Level High = ", level_high);

if(High[PS] >= High[PS-1] && High[PS] >= High[PS+1])
   {
   if(PS-1 == 1)
      {
      level_high = High[PS];
      }

   if(High[PS] >= High[PS-2] && High[PS] >= High[PS+2])
      {
      if(PS-1 == 2)
         {
         level_high = High[PS];
         }

      if(High[PS] >= High[PS-3] && High[PS] >= High[PS+3])
         {
         if(PS-1 == 3)
            {
            level_high = High[PS];
            }

         if(High[PS] >= High[PS-4] && High[PS] >= High[PS+4])
            {
            if(PS-1 == 4)
               {
               level_high = High[PS];
               }

            if(High[PS] >= High[PS-5] && High[PS] >= High[PS+5])
               {
               if(PS-1 == 5)
                  {
                  level_high = High[PS];
                  }

               if(High[PS] >= High[PS-6] && High[PS] >= High[PS+6])
                  {
                  if(PS-1 == 6)
                     {
                     level_high = High[PS];
                     }

                  if(High[PS] >= High[PS-7] && High[PS] >= High[PS+7])
                     {
                     if(PS-1 == 7)
                        {
                        level_high = High[PS];
                        }

                     if(High[PS] >= High[PS-8] && High[PS] >= High[PS+8])
                        {
                        if(PS-1 == 8)
                           {
                           level_high = High[PS];
                           }

                        if(High[PS] >= High[PS-9] && High[PS] >= High[PS+9])
                           {
                           if(PS-1 == 9)
                              {
                              level_high = High[PS];
                              }

                           if(High[PS] >= High[PS-10] && High[PS] >= High[PS+10])
                              {
                              if(PS-1 == 10)
                                 {
                                 level_high = High[PS];
                                 }

                              if(High[PS] >= High[PS-11] && High[PS] >= High[PS+11])
                                 {
                                 if(PS-1 == 11)
                                    {
                                    level_high = High[PS];
                                    }

                                 if(High[PS] >= High[PS-12] && High[PS] >= High[PS+12])
                                    {
                                    if(PS-1 == 12)
                                       {
                                       level_high = High[PS];
                                       }

                                    if(High[PS] >= High[PS-13] && High[PS] >= High[PS+13])
                                       {
                                       if(PS-1 == 13)
                                          {
                                          level_high = High[PS];
                                          }

                                       if(High[PS] >= High[PS-14] && High[PS] >= High[PS+14])
                                          {
                                          if(PS-1 == 14)
                                             {
                                             level_high = High[PS];
                                             }

                                          if(High[PS] >= High[PS-15] && High[PS] >= High[PS+15])
                                             {
                                             if(PS-1 == 15)
                                                {
                                                level_high = High[PS];
                                                }

                                             if(High[PS] >= High[PS-16] && High[PS] >= High[PS+16])
                                                {
                                                if(PS-1 == 16)
                                                   {
                                                   level_high = High[PS];
                                                   }

                                                if(High[PS] >= High[PS-17] && High[PS] >= High[PS+17])
                                                   {
                                                   if(PS-1 == 17)
                                                      {
                                                      level_high = High[PS];
                                                      }

                                                   if(High[PS] >= High[PS-18] && High[PS] >= High[PS+18])
                                                      {
                                                      if(PS-1 == 18)
                                                         {
                                                         level_high = High[PS];
                                                         }

                                                      if(High[PS] >= High[PS-19] && High[PS] >= High[PS+19])
                                                         {
                                                         if(PS-1 == 19)
                                                            {
                                                            level_high = High[PS];
                                                            }

                                                         if(High[PS] >= High[PS-20] && High[PS] >= High[PS+20])
                                                            {
                                                            if(PS-1 == 20)
                                                               {
                                                               level_high = High[PS];
                                                               }
                                                            }
                                                         }
                                                      }
                                                   }
                                                }
                                             }
                                          }
                                       }
                                    }
                                 }
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
}
}
 
Number_of_candles = 15
int PS = Number_of_candles+1;    //PS=16

when you reach

 if(High[PS] >= High[PS-17] && High[PS] >= High[PS+17])

 PS-17 is [-1]

There is no such bar, so you get "array out of range

 
Oh! How easy... Thank you very much! when I finish this one, I will upload it here for benefits of others
 
Tried to put it into the indicator to draw a line on the graph... I cannot figure it out how... again. Anybody? you might see the mistake immediately...
//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                                                               LJ |
//+------------------------------------------------------------------+
#property copyright "LJ"
#property link      "lukas.janca@icloud.com"
#property version   "1.00"
#property strict

#property indicator_chart_window // v/mimo okno
#property indicator_buffers 1 //množství buferů
#property indicator_plots   1 //počet linií?

//--- plot barsToProcess
#property indicator_label1  "barsToProcess = 100"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Green
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

//--- indicator buffers
double Buffer1[];

input int Number_of_candles = 15;

int OnInit()
   {
   IndicatorShortName("S/R levels");
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,Buffer1);
    
   return(0);
   return(INIT_SUCCEEDED);
   }

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();

int static NewBar = Bars;

for(int i = 0; i < Bars; i++)
   {
   
   if(NewBar != Bars && Bars > Number_of_candles)
      {
      NewBar = Bars;
      
      double static level_high;
      
      int PS = Number_of_candles+1;
      
      if(High[PS] >= High[PS-1] && High[PS] >= High[PS+1] && PS >= 2)
         {
         if(PS-1 == 1)
            {
            level_high = High[PS];
            }
      
         if(High[PS] >= High[PS-2] && High[PS] >= High[PS+2] && PS >= 3)
            {
            if(PS-1 == 2)
               {
               level_high = High[PS];
               }
      
            if(High[PS] >= High[PS-3] && High[PS] >= High[PS+3] && PS >= 4)
               {
               if(PS-1 == 3)
                  {
                  level_high = High[PS];
                  }
      
               if(High[PS] >= High[PS-4] && High[PS] >= High[PS+4] && PS >= 5)
                  {
                  if(PS-1 == 4)
                     {
                     level_high = High[PS];
                     }
      
                  if(High[PS] >= High[PS-5] && High[PS] >= High[PS+5] && PS >= 6)
                     {
                     if(PS-1 == 5)
                        {
                        level_high = High[PS];
                        }
      
                     if(High[PS] >= High[PS-6] && High[PS] >= High[PS+6] && PS >= 7)
                        {
                        if(PS-1 == 6)
                           {
                           level_high = High[PS];
                           }
      
                        if(High[PS] >= High[PS-7] && High[PS] >= High[PS+7] && PS >= 8)
                           {
                           if(PS-1 == 7)
                              {
                              level_high = High[PS];
                              }
      
                           if(High[PS] >= High[PS-8] && High[PS] >= High[PS+8] && PS >= 9)
                              {
                              if(PS-1 == 8)
                                 {
                                 level_high = High[PS];
                                 }
      
                              if(High[PS] >= High[PS-9] && High[PS] >= High[PS+9] && PS >= 10)
                                 {
                                 if(PS-1 == 9)
                                    {
                                    level_high = High[PS];
                                    }
      
                                 if(High[PS] >= High[PS-10] && High[PS] >= High[PS+10] && PS >= 11)
                                    {
                                    if(PS-1 == 10)
                                       {
                                       level_high = High[PS];
                                       }
      
                                    if(High[PS] >= High[PS-11] && High[PS] >= High[PS+11] && PS >= 12)
                                       {
                                       if(PS-1 == 11)
                                          {
                                          level_high = High[PS];
                                          }
      
                                       if(High[PS] >= High[PS-12] && High[PS] >= High[PS+12] && PS >= 13)
                                          {
                                          if(PS-1 == 12)
                                             {
                                             level_high = High[PS];
                                             }
      
                                          if(High[PS] >= High[PS-13] && High[PS] >= High[PS+13] && PS >= 14)
                                             {
                                             if(PS-1 == 13)
                                                {
                                                level_high = High[PS];
                                                }
      
                                             if(High[PS] >= High[PS-14] && High[PS] >= High[PS+14] && PS >= 15)
                                                {
                                                if(PS-1 == 14)
                                                   {
                                                   level_high = High[PS];
                                                   }
      
                                                if(High[PS] >= High[PS-15] && High[PS] >= High[PS+15] && PS >= 16)
                                                   {
                                                   if(PS-1 == 15)
                                                      {
                                                      level_high = High[PS];
                                                      }
      
                                                   if(High[PS] >= High[PS-16] && High[PS] >= High[PS+16] && PS >= 17)
                                                      {
                                                      if(PS-1 == 16)
                                                         {
                                                         level_high = High[PS];
                                                         }
      
                                                      if(High[PS] >= High[PS-17] && High[PS] >= High[PS+17] && PS >= 18)
                                                         {
                                                         if(PS-1 == 17)
                                                            {
                                                            level_high = High[PS];
                                                            }
      
                                                         if(High[PS] >= High[PS-18] && High[PS] >= High[PS+18] && PS >= 19)
                                                            {
                                                            if(PS-1 == 18)
                                                               {
                                                               level_high = High[PS];
                                                               }
      
                                                            if(High[PS] >= High[PS-19] && High[PS] >= High[PS+19] && PS >= 20)
                                                               {
                                                               if(PS-1 == 19)
                                                                  {
                                                                  level_high = High[PS];
                                                                  }
      
                                                               if(High[PS] >= High[PS-20] && High[PS] >= High[PS+20] && PS >= 21)
                                                                  {
                                                                  if(PS-1 == 20)
                                                                     {
                                                                     level_high = High[PS];
                                                                     }
                                                                  }
                                                               }
                                                            }
                                                         }
                                                      }
                                                   }
                                                }
                                             }
                                          }
                                       }
                                    }
                                 }
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
         Print("Level High = ", level_high);
      
         Buffer1[i]=level_high;
      }
   }
return(rates_total);
}
Reason: