Need help with my S/R indicator

 

Hey Guys,

I wrote this Indicator showing Support and Resistance Lines in the 15min which works perfectly fine. I also want to see these lines in the 5min TF. But when I switch to the M5 TF other lines appear which are not supposed to be there.. Can someone help me out, please?


extern int periode_to_next_extremum = 20;
extern int bars_to_check = 40;
extern int history = 250;
double ResLevel[], SupLevel[];
datetime ResTime[],SupTime[];
int objectcounterRES = 1;
int objectcounterSUP = 1;
string NameRES = "lineRES";
string NameSUP = "lineSUP";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   ArrayResize(ResLevel,1);
   ArrayResize(ResTime,2);
   ArrayResize(SupLevel,1);
   ArrayResize(SupTime,2);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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 limit;
   int counted_bars = IndicatorCounted();
//---- check for possible errors 
   if(counted_bars<0) return(-1);
   limit=Bars - counted_bars;
// limit to n bars
   if(limit > history) limit = history;
   int timeframe_15 = 15;
//---- S/R_Calculations_15min----------------------
   for(int i = 1; i < limit; i++)
      {
      //--Resistance_15min----
      if(iHighest(NULL,timeframe_15,MODE_HIGH,bars_to_check,i) == i + periode_to_next_extremum)
         {
         ResLevel[0] = High[i + periode_to_next_extremum];
         ResTime[0] = Time[i + periode_to_next_extremum];
         ResTime[1] = Time[i + periode_to_next_extremum] + 9999999;
         objectcounterRES++;
         //------Colour-----
         if(!ObjectCreate(NameRES + (string)objectcounterRES,OBJ_TREND,0,ResTime[0],ResLevel[0],ResTime[1],ResLevel[0]))
            {
            Print("Failed to create Trend: ", GetLastError());      
            }
         //------Colour-----
         if(!ObjectSetInteger(0,NameRES + (string)objectcounterRES,OBJPROP_COLOR,clrRed))
            Print("Failed to set colour", GetLastError());   
         //------Show_in_5min_chart-----
         if(!ObjectSetInteger(ChartID(),NameRES + (string)objectcounterRES,OBJPROP_TIMEFRAMES,OBJ_PERIOD_M5|OBJ_PERIOD_M15))
            {
            Print("Failed to adjust timeframe: ", GetLastError());
            }
            
         }
      //--Support_15min---
      if(iLowest(NULL,timeframe_15,MODE_LOW,bars_to_check,i) == i + periode_to_next_extremum)
         {
         SupLevel[0] = Low[i + periode_to_next_extremum];
         SupTime[0] = Time[i + periode_to_next_extremum];
         SupTime[1] = Time[i + periode_to_next_extremum] + 9999999;
         objectcounterSUP++;
         //------Colour-----
         if(!ObjectCreate(NameSUP + (string)objectcounterSUP,OBJ_TREND,0,SupTime[0],SupLevel[0],SupTime[1],SupLevel[0]))
            {
            Print("Failed to create Trend: ", GetLastError());      
            }
         //------Colour-----
         if(!ObjectSetInteger(0,NameSUP + (string)objectcounterSUP,OBJPROP_COLOR,clrGreen))
            Print("Failed to set colour", GetLastError());
         //------Show_in_5min_chart-----
         if(!ObjectSetInteger(ChartID(),NameSUP + (string)objectcounterSUP,OBJPROP_TIMEFRAMES,OBJ_PERIOD_M5|OBJ_PERIOD_M15))
            {
            Print("Failed to adjust timeframe: ", GetLastError());
            }          
         }
      }
//+-------moving_Vline-----------------------------------------------------------+ 
  
   datetime max_time =  TimeCurrent() - history * 5 * 60;
   ObjectCreate(ChartID(),"moving_Vline",OBJ_VLINE,0,max_time,0);
   ObjectSetInteger(ChartID(),"moving_Vline",OBJPROP_COLOR,clrTurquoise);
   ObjectMove(ChartID(),"moving_Vline",OBJ_VLINE,max_time,0);
   
//+-------check_for_objectdelete-----------------------------------------------------------+ 

  check_for_objectdelete();      
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

void OnDeinit(const int reason)
   {
   int number_of_objects_deleted = ObjectsDeleteAll(0,"",0,-1);
   Print("number_of_objects-deleted = ", number_of_objects_deleted);
   }
//+-------check_for_objectdelete-----------------------------------------------------------+   
void check_for_objectdelete()
   {
   datetime max_time = TimeCurrent() - history * 5 * 60;
   for(int i = 0; i <= ObjectsTotal()-1; i++)
      {
      string object_name = ObjectName(i);
      datetime object_time = ObjectGet(object_name,OBJPROP_TIME1);
      if(object_time < max_time)
         {
         ObjectDelete(ChartID(),object_name);
         } 
      }
   }
 

If it is not running on m15 you have to be aware of the fact that iHighest(..,15,..) and Time[] (may be on m5?) requires different indexes for the bars with the same open time!

Look at your indexes with Comment() and what time open time they will show.

Instead of Time[], High[], and Low[] just use iTime(), iHigh(), and iLow().

 
Carl Schreiber:

If it is not running on m15 you have to be aware of the fact that iHighest(..,15,..) and Time[] (may be on m5?) requires different indexes for the bars with the same open time!

Look at your indexes with Comment() and what time open time they will show.

Instead of Time[], High[], and Low[] just use iTime(), iHigh(), and iLow().

Wow, that was a quick reply... thanks a lot! it works perfectly fine!
Reason: