MQL4 - automated forex trading   /  

Code Base

Code Base  Indicators  Dynamic Support Resistance To post a new code, please log in or register

This library is for
MetaTrader 4
Download MT 4 - 5.5 Mb

Mobile trading!
Buy a license and be mobile in your trading!

Name:
Dynamic Support Resistance
Author: doshur (2010.01.13 16:49)
Downloaded: 4652
Download:
 DSR.mq4 (4.0 Kb) View

Description:

This indicator will draw support and resistance lines automatically based on the closing price. Refer to picture.

Image:


AUDUSD, H1


15 comments: 1 2   To post a new comment, please log in or register

http://www.doshur.com/forex/dsr.asp



updated with more accurate calculation


lots of bugs removed


13.03.2010 08:31 doshur
moni wrote:

The indicator is not working. Nothing is showing up on the chart. I am using 4 decimal broker. Moni

//+------------------------------------------------------------------+
//|                                                          DSR.mq4 |
//|                                                           doshur |
//|                 Donate via PayPal if you find this script useful |
//|                                                  dyrws@yahoo.com |
//|                                             www.doshur.com/forex |
//+------------------------------------------------------------------+
#property copyright "doshur"
#property link      "www.doshur.com"

extern int TF = 0;
extern color Clr = MediumOrchid;
extern int Style = 4;
extern int Lines = 5;
extern int Count = 100;
extern int Sensitivity = 15;

double PTs[1, 2];

#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//----

   ArrayResize(PTs, Count);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----

   string Name;

   for(int i = 1; i <= Lines; i++)
   {
      Name = "Line_" + DoubleToStr(i, 0);

      if(ObjectFind(Name) == 0)
      {
         ObjectDelete(Name);
      }
   }

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//---- VARIABLES

   int i, x, y, Pos, Cnt;
   double Curr_C, Prev_C, Past_C, tmpPrice;

//---- RESET

   ArrayInitialize(PTs, 0);

//---- SCAN CLOSE FRACTALS

   i = 1;
   x = 0;

   while(x < Count)
   {
      Cnt = 0;

      Curr_C = iClose(NULL, TF, i);
      Prev_C = iClose(NULL, TF, i + 1);
      Past_C = iClose(NULL, TF, i + 2);

      if((Curr_C < Prev_C && Prev_C > Past_C) || (Curr_C > Prev_C && Prev_C < Past_C))
      {
         PTs[x, 1] = Prev_C;
         x++;
      }

      i++;
   }

//---- ADD SENSITIVITY COUNTER

   for(i = 0; i < Count; i++)
   {
      Cnt = 0;

      for(x = 0; x < Count; x++)
      {
         if(MathAbs(PTs[i, 1] - PTs[x, 1]) <= Sensitivity * Point)
         {
            Cnt++;
         }
      }

      PTs[i, 0] = Cnt;
   }

//---- SORT ARRAY

   ArraySort(PTs, WHOLE_ARRAY, 0, MODE_DESCEND);

//---- MERGE NEIGHBOUR

   for(i = 0; i < Count; i++)
   {
      y = 0;
      Cnt = 0;
      tmpPrice = 0;

      for(x = 0; x < Count; x++)
      {
         if(i != x)
         {
            if(MathAbs(PTs[i, 1] - PTs[x, 1]) <= Sensitivity * Point)
            {
               y++;
               Cnt += PTs[x, 0];
               tmpPrice += PTs[x, 1];

               PTs[x, 0] = 0;
               PTs[x, 1] = 0;
            }
         }
      }

      if(y > 0)
      {
         y++;
         Cnt += PTs[i, 0];
         tmpPrice += PTs[i, 1];

         PTs[i, 0] = Cnt;
         PTs[i, 1] = tmpPrice / y;
      }
   }

//---- SORT ARRAY

   ArraySort(PTs, WHOLE_ARRAY, 0, MODE_DESCEND);

//---- DRAW LINES

   string Name;

   for(i = 1; i <= Lines; i++)
   {
      Name = "Line_" + DoubleToStr(i, 0);

      if(ObjectFind(Name) < 0)
      {
         if(PTs[i, 1] != 0)
         {
            ObjectCreate(Name, OBJ_HLINE, 0, 0, PTs[i, 1]);
            ObjectSet(Name, OBJPROP_COLOR, Clr);
            ObjectSet(Name, OBJPROP_STYLE, Style);
            ObjectSet(Name, OBJPROP_BACK, true);
         }
      }
      else
      {
         ObjectMove(Name, 0, Time[0], PTs[i, 1]);
      }
   }

//---- DEBUG

   //for(i = 0; i < Count; i++)
   //{
   //   Print(i, " - ", PTs[i, 0], " : ", PTs[i, 1]);
   //}

//----
   return(0);
  }

26.02.2010 22:28 qwerewq
moni wrote:

The indicator is not working. Nothing is showing up on the chart. I am using 4 decimal broker. Moni

this is for 5 decimal broker.


09.02.2010 15:42 doshur

The indicator is not working. Nothing is showing up on the chart. I am using 4 decimal broker. Moni

09.02.2010 08:49 moni

regsklongshort identify another bug in the code. sad to see no rating.

02.02.2010 20:06 doshur

regsklongshort wrote:

doshur wrote:

i have made some improvements to this indicator. any comments to improve it further before i release the next version?



Great indicator, I tried to code something like this, but yours is better.

If i understand your code correctly you are calculating from bar 1, so why not add something like this just after the start. Then you are using less mt4/computer resources

datetime dtBar; (declared before the init section)

if (Time[0]==dtBar) return(0);
dtBar=Time[0];


I don't understand what you mean. Care to elaborate more?

I have come up with version 2 of this indicator which will calculate more accurately.

29.01.2010 14:04 doshur
doshur wrote:

i have made some improvements to this indicator. any comments to improve it further before i release the next version?



Great indicator, I tried to code something like this, but yours is better.

If i understand your code correctly you are calculating from bar 1, so why not add something like this just after the start. Then you are using less mt4/computer resources

datetime dtBar; (declared before the init section)

if (Time[0]==dtBar) return(0);
dtBar=Time[0];

29.01.2010 13:36 regsklongshort

i have made some improvements to this indicator. any comments to improve it further before i release the next version?


25.01.2010 17:45 doshur
aquaibm wrote:

Doesn't work, nothing show up.I've tried to change the color, what is the problem?



can check your object list, is there any horizontal lines objects?

u using 4 or 5 decimal broker?

20.01.2010 10:02 doshur

Doesn't work, nothing show up.I've tried to change the color, what is the problem?


20.01.2010 03:52 aquaibm