Help with a support/resistance indicator

 

Hi I'm trying to create a custom indicator, and I'm having trouble with what I need to do next in my code.

There is obviously a lot which I need to add and perhaps some that I need to leave out. However, I'm finding it difficult what I need to do next, at the moment my indicator compiles with no errors, but nothing happens when attached to a chart.

So what I want my indicator to do is:

  1. check the last three candles o the chart so 0,1 and 2. 
  2. if the close of candle 1 is greater than the open of candle 1 AND the close of candle 2 is less than candle 2 open, THEN draw a horizontal line at the close of candle 2. I call this Support. Red Line
  3. if the close of candle 1 is less than the open of candle 1 AND the close of candle 2 is greater than candle 2 open, THEN draw a horizontal line at the close of candle 2. I call this Resistance. Blue Line
I understand an indicator can have 8 buffers in MT4, so I would like a max of 4 of each on a chart at any one time. So if there are already 4 support lines on the chart and the indicator detects another support line to be drawn it will delete the oldest line then create the new one. Please look at the picture to see what I want.

I have attached the what I have so far of the indicator (which I know is not much) to this post for anyone that would be so kind enough to help me by telling what I need to do in order to get it working as I want. 

I appreciate your time.

#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 clrRed     // Color of the 1st line
#property indicator_color2 clrBlue      // Color of the 2nd line

extern color   UpperClr=Blue;
extern color   LowerClr=Red;
extern int     LineStyle=STYLE_SOLID; 
extern int     LineWidth=1;
double         Support,Resistance;             
int            BarsOnChart;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
/*--- indicator buffers mapping
   SetIndexBuffer(0,Support);         
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,clrRed);// Line style
   SetIndexLabel(0,"Support");
   SetIndexBuffer(1,Resistance);         
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1,clrBlue);// Line style
   SetIndexLabel(1,"Resistance");
*/
   return(INIT_SUCCEEDED);
  }
  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//the indicator programme should only run when a new candle is formed.
bool IsNewCandle()
{
   if (Bars==BarsOnChart)
   return(false);
   BarsOnChart = Bars;
   return(true);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double draw_support()
{
   ObjectCreate(ChartID(),"LowerLine",OBJ_HLINE,0,0,iLowest(NULL,0,3,2,0));
   ObjectSet("LowerLine",OBJPROP_COLOR,LowerClr);
   ObjectSet("LowerLine",OBJPROP_WIDTH,LineWidth);
   ObjectSet("LowerLine",OBJPROP_STYLE,LineStyle);
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double draw_resistance()
{
   ObjectCreate(ChartID(),"UpperLine",OBJ_HLINE,0,0,iHighest(NULL,0,3,2,0));
   ObjectSet("UpperLine",OBJPROP_COLOR,LowerClr);
   ObjectSet("UpperLine",OBJPROP_WIDTH,LineWidth);
   ObjectSet("UpperLine",OBJPROP_STYLE,LineStyle);
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 Uncounted_bars,Counted_bars;// Bar index, Number of counted bars
   int i;
   
   Counted_bars=IndicatorCounted();
   if(Counted_bars<0)return(-1);
   if(Counted_bars>0) Counted_bars--;
   Uncounted_bars=Bars-Counted_bars-1;
   
 for(i=0; i<Uncounted_bars; i++)
  {
   Support=iLowest(NULL,0,3,2,0);
   Resistance=iHighest(NULL,0,3,2,0);
  }
  
  {
  if(IsNewCandle())
    if(Close[1]>=Open[1] && Close[2]<=Open[2])draw_support();
  }
  
  {
  if(IsNewCandle())
    if(Close[1]<=Open[1] && Close[2]>=Open[2])draw_resistance();
  }
  
   return(rates_total);
  }

 

Example 

 

Don't check for a new bar more than once. The 2nd check will return false

Why set up buffers if you don't use them?

Use different names for each line that you want to draw. Once a line has been drawn, another line cannot be drawn with the same name 

 
Thanks I will try that. The advice was very useful.
Reason: