Algorithm to detect support and resistance

 

Hello all guys.

I was wondering if there is any change to develop an algorithm to detect support and resistance. I wanted to try to make one to use for a future development of an expert advisor.

May I ask you to contribute to this post and add your code or articles or concepts as basic line ?

The only resource I found is an article from MQL4.com about drawing support and resistance using a certain criteria

The indicator and the article are here at this link https://www.mql5.com/en/articles/1440

Thanks in advance, Francesco.

 
What is your definition of Support and Resistance ?
 

Hello Raptor, thank you for replying.

I am trying to identify price ranges that usually get broken and retested during an uptrend or downtrend.

I think it's difficult for me to start to write a program since I cannot easily break the process of identification that I normally use when I open a chart.

The article on MQL4 goes clear to the point even if the code is quite complex and still trying to decrypt few things .

Have you ever developed anything similar to this indicator Raptor ?

 
zen4x:

Have you ever developed anything similar to this indicator Raptor ?

Probably . . . but unless you can define S R you have no chance of coding it or even getting someone else to code it . . .
 

Hello Raptor, thank you for the reply. I have been looking around for different indicators and to try to grasp how to determine the right level of support and resistance.

So far I have noticed that the only indicator that works the way I want is the one that has been developed from mql4.com but is a bit complicated for me.

I need to go though any iteration of price level to understand what it does. I was wondering if it does not require you too much effort, to explain how simplify the process with basic code.

It's a bit strange, but I am spending a lot of time researching similar scripts of algorithms but it does not seem a very popular topic.

I hope I have not been to vague with the explanation.

Thank you, Francesco.

 
zen4x:

Hello Raptor, thank you for the reply. I have been looking around for different indicators and to try to grasp how to determine the right level of support and resistance.

So far I have noticed that the only indicator that works the way I want is the one that has been developed from mql4.com but is a bit complicated for me.

I need to go though any iteration of price level to understand what it does. I was wondering if it does not require you too much effort, to explain how simplify the process with basic code.

It's a bit strange, but I am spending a lot of time researching similar scripts of algorithms but it does not seem a very popular topic.

I hope I have not been to vague with the explanation.

Thank you, Francesco.

And how far back are you willing to search for R/S?
 

The indicator is using a parameter for the loopback period.

 

Hi all!

You can use for example Donchian Channel (ihighest/ilowest) for detecting s/r levels, but the best indicator i ever used for doing it is this:

//+------------------------------------------------------------------+
//|                                modified version of ZZ_Orlova.mq4 |
//|                       Denis Orlov, http://denis-or-love.narod.ru |
//|                                    http://denis-or-love.narod.ru |
//|                  original version: https://www.mql5.com/en/code/10076 |

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Green
#property indicator_width1 2

extern int Points = 10;
extern bool ByClose = False;

double ZZ_Orlova[];
double LastUp;
double LastDn;
bool LastIsUp;
datetime LastPicTime=0;
int perB; 
int Zbar[3];
double Zval[3];
int ZObar[6];
double ZOval[6];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexStyle(0,DRAW_SECTION);
   SetIndexBuffer(0,ZZ_Orlova);
   SetIndexEmptyValue(0,0.0);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   if(perB== Time[0]) return(0);
   perB = Time[0];
   double upPrice, dnPrice;   
   int limit;
   int counted_bars=IndicatorCounted();
   limit=Bars-counted_bars-1;
   int fstBar=1;
   for(int i =limit; i>=fstBar; i--)
    { 
    if (LastPicTime==0)
     {
     FindZigZag(i, 3);
     if(Zval[0]==Low[Zbar[0]])
      {
      LastIsUp=False;
      LastUp=Zval[1]; LastDn=Zval[0];
      }
      else
      {
      LastIsUp=True;
      LastUp=Zval[0]; LastDn=Zval[1];
      } 
     ZZ_Orlova[Zbar[1]]=Zval[1]; 
     ZZ_Orlova[Zbar[0]]=Zval[0]; 
     LastPicTime=Time[Zbar[0]];
     }
     int LastBar=iBarShift(NULL,0,LastPicTime);
     if(ByClose)
      {
      upPrice=Close[i]; dnPrice=Close[i];  
      }
      else
      {
      upPrice=High[i]; dnPrice=Low[i];
      }
      if(LastIsUp)
       {
       if(upPrice>=LastUp)
        {
        LastUp=upPrice;
        ZZ_Orlova[LastBar]=0.0;
        ZZ_Orlova[i]=upPrice;
        LastPicTime=Time[i];
        } else if((LastUp-dnPrice)/Point>=Points)
           {
           LastDn=dnPrice;
           LastIsUp=False;
           ZZ_Orlova[i]=dnPrice;
           LastPicTime=Time[i];
           }
         }
         else if(!LastIsUp)
         {
         if(dnPrice<=LastDn)
          {
           LastDn=dnPrice;
           ZZ_Orlova[LastBar]=0.0; 
           ZZ_Orlova[i]=dnPrice;
           LastPicTime=Time[i];
           } else
           if((upPrice-LastDn)/Point>=Points)
              {
              LastUp=upPrice;
              LastIsUp=True;
              ZZ_Orlova[i]=upPrice;
              LastPicTime=Time[i];
             }
          }
       }
    return(0);
  }
//+------------------------------------------------------------------+
datetime FindZigZag(int bar, int nP )
   {
   int ExtDepth=3;
   int ExtDeviation=3;
   int ExtBackstep=3;
   int n; datetime res;
   for(int i=bar;i<Bars;i++)
   {
   double zz=iCustom(NULL,0,"ZigZag",3,3,3,0,i);
   if(zz!=0 && zz!=EMPTY_VALUE)
    {
    Zbar[n]=i;
    Zval[n]=zz;
    if(n==1) res=Time[Zbar[n]];
    n++;
    if(n>=nP) break;
    }
  }
 return(res);
}
//+------------------------------------------------------------------+

The original zigzag repaints and if you use it in visual mode for a while, and then you attach to that chart another one with the same settings, you will see differences between them. With this one both problems are solved. Many thanks to Denis for sharing it.

 

Hello LordOftheMoney. How do you use it for support and resistance ? Do you extend the swing points of the zig zag to the future ?

Have had a look to the script I was mentioning above ? I was looking to plot horizontal line, even if I would like to store this lines as variables and pass it to an EA

I attached the script.

Files:
 

Hello zen,

i use the last 4-6 turning points of 2-3 ones of this zigzag with different settings, but for determining chart patterns and elliot waves, not only one support or resistance level. For some more informations look at these pages:

https://forum.mql4.com/3930#10790

https://forum.mql4.com/36489#388421

Best Regards

 

Hello all.

I was trying to write a procedure do achieve this task.

I made the following considerations

1 - S/R is a price level that struggled to break a pre-existing range

2 - Has been retouched a number of times (to set as paramenter) but at least 2 or more times

3 - Scan the chart for X bars and to find min and max value within X bars

4 - Loop from the minimum price to the maximum price using step increase parameter

5 - I used the high of the candles to check if they were within a range +/- range (another parameter to set)

   int commonPoint;
   int minLevel, maxLevel;
   double highest, lowest, stepIncrement, tolerance, high;
   double storeLevel[];
   ///ArrayResize(storeLevel, loopback);
   
   minLevel = iLowest(Symbol(), Period(), MODE_LOW, loopback, 0);
   maxLevel = iHighest(Symbol(), Period(), MODE_HIGH, loopback, 0);
   highest = iHigh(Symbol(), Period(), maxLevel);
   lowest = iLow(Symbol(), Period(), minLevel);
  
   //Print("max: " + highest + " min: " + lowest);
   stepIncrement = 0.0005;
   tolerance = 0.0002;
   static double tmp;
   tmp = 0.0;
   
   
   for (double actPrice = lowest; actPrice <= highest; actPrice += stepIncrement) {
   
      
      for (int i = 1; i <= loopback; i++) {
         //do some stuff here...
         high = iHigh(Symbol(), Period(), i);
         double topRange, bottomRange;
         /**  if is the first value tmp stores the first high encountered until that moment **/
         if (tmp == 0) {
            tmp = high;
         } else {
            //define a buffer adding a subtracting from tmp to check if the new high is within that value
            topRange = tmp + tolerance;
            bottomRange = tmp - tolerance;
            
            if (high <= topRange && high >= bottomRange) {
               commonPoint++;
            }
         }
         //if has been touched at least three times reset common point
         //tmp goes to the new high value to keep looping
         if (commonPoint == 3) {
            commonPoint = 0;
            tmp = high;
            ///Print("valore tmp: " + tmp);
            storeLevel[i] = tmp;
         }
      }
   }