Time Range Indicator

 

Hello,

I am working to make an indicator which draws two lines making a range from a 22:00 to 06:00 everyday & continue till value changes next day at 06:00. I have used the iHigh & iHighest functions but I still need to give the Shift Count of the bar with highest high & lowest low in between these two ranges. I tried to solve this problem with some approach which needs datetime variable. I dont understant how to create a dynamic datetime variable in the format " D'2013.8.29 06:00' ". Im attaching my code which is incomplete and using fixed datetime. kindly help me is there is any way to resolve it. If there is any other approach to create a Time based range then please share.

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Blue

double Ind_Array0[], Ind_Array1[];  //Indicator Arrays
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

   //Assigning Arrays to buffer
   
   SetIndexBuffer(0, Ind_Array0); //Indicator Array0 is assigned to Buffer Index0
   SetIndexStyle(0,DRAW_LINE, STYLE_SOLID,9);
   
   SetIndexBuffer(1, Ind_Array1); //Indicator Array1 is assigned to Buffer Index1
   SetIndexStyle(1,DRAW_LINE, STYLE_DASHDOT,2);
   
   return;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  
         
   int    Cur_Hour=Hour();             // Server time in hours
   double Cur_Min =Minute();           // Server time in minutes
   double Cur_time=Cur_Hour + (Cur_Min)/100; // Current time
  
   int    counted_bars=IndicatorCounted();   
   int    i=Bars-counted_bars-1;           // Index of the first uncounted
   
   
  // D'2003.12.31'
   
    int Yr = Year();
    int Mnt = Month();
    int Dy = Day();
    
    string TodayIs = StringConcatenate("D'",Yr,".",Mnt,".",Dy," 09:00'");
    datetime Today = StrToTime(TodayIs);  //HELP HERE
     
   //int iBarShift(     string symbol, int timeframe, datetime time, bool exact=false)
  int Candle2Shift = iBarShift(NULL,0, D'2013.8.29 6:00');  //HELP HERE TO CREATE DYNAMIC DATE
   int a = iHighest(NULL,0,MODE_HIGH,8,Candle2Shift);
   
   while(i>=0)
   {
      Ind_Array0[i] = iHigh(NULL,0,a);//High[i];
      //Ind_Array1[i] = Low[i];
     i--;
   }
//----
  // Alert(TimeCurrent());

//----
   return(0);
  }
//+------------------------------------------------------------------+
 
jp_forex:

Hello,

I am working to make an indicator which draws two lines making a range from a 22:00 to 06:00 everyday & continue till value changes next day at 06:00. I have used the iHigh & iHighest functions but I still need to give the Shift Count of the bar with highest high & lowest low in between these two ranges. I tried to solve this problem with some approach which needs datetime variable. I dont understant how to create a dynamic datetime variable in the format " D'2013.8.29 06:00' ". Im attaching my code which is incomplete and using fixed datetime. kindly help me is there is any way to resolve it. If there is any other approach to create a Time based range then please share.

Use a fixed reference to work from, for example Midnight
 

Hello,

Thank you Raptor. I tried your way but I was stuck in that too. I found one way to solve this. See the code below. it solves that "datetime" problem, but only problem that stays is that the range from 22:00 till 6:00 shows two lines for all bars instead for showing me the range for each day. It draws both range lines from start of chart to end by using Last Two values in Buffer Arrays. Try using the code on H1 chart.

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Blue

extern string Time6am = "06:00";
//extern datetime Time22pm = D'2013.8.28 22:00';


double Ind_Array0[], Ind_Array1[];  //Indicator Arrays
int init()
  {
//---- indicators

   //Assigning Arrays to buffer
   
   SetIndexBuffer(0, Ind_Array0); //Indicator Array0 is assigned to Buffer Index0
   SetIndexStyle(0,DRAW_LINE, STYLE_SOLID,3);
   
   SetIndexBuffer(1, Ind_Array1); //Indicator Array0 is assigned to Buffer Index0
   SetIndexStyle(1,DRAW_LINE, STYLE_DASHDOT,3);
   
   return;

  }
  
int start()
  {
  
         
   int    Cur_Hour=Hour();             // Server time in hours
   double Cur_Min =Minute();           // Server time in minutes
   double Cur_time=Cur_Hour + (Cur_Min)/100; // Current time
  
   int    counted_bars=IndicatorCounted();   
   int    i=Bars-counted_bars-1;           // Index of the first uncounted
   
   
    int Yr = Year();
    int Mnt = Month();
    int Dy = Day();

    string TodayIs = StringConcatenate(Yr,".",Mnt,".",Dy," ",Time6am);
    datetime Today = StrToTime(TodayIs);  

    int Candle2Shift = iBarShift(NULL,0, Today);
    int a = iHighest(NULL,0,MODE_HIGH,8,Candle2Shift);
   
    int Candle1Shift = iBarShift(NULL,0, Today);
    int b = iLowest(NULL,0,MODE_LOW,8,Candle1Shift);


for(int pos = i; pos >= 0; pos--)
   {  
      Ind_Array0[pos] = iHigh(NULL,0,a);//High[i];
      Ind_Array1[pos] = iLow(NULL,0,b);//High[i];
 
   }
  

   return(0);
  }
//+------------------------------------------------------------------+
 
jp_forex:

Hello,

Thank you Raptor. I tried your way but I was stuck in that too. I found one way to solve this. See the code below. it solves that "datetime" problem, but only problem that stays is that the range from 22:00 till 6:00 shows two lines for all bars instead for showing me the range for each day. It draws both range lines from start of chart to end by using Last Two values in Buffer Arrays. Try using the code on H1 chart.

You need to move most of your code inside the for loop, currently you are calculating the Highest and Lowest once for all the past bars on the chart. You are going to have some challenges to overcome, for example, the weekend, what will you do there if you have Sunday bars ?
 
RaptorUK:
You need to move most of your code inside the for loop, currently you are calculating the Highest and Lowest once for all the past bars on the chart. You are going to have some challenges to overcome, for example, the weekend, what will you do there if you have Sunday bars ?


Yes, you are right. Weekends will be a problem for me. And the code in for loop, that's the only solution to count values for each day. Thanks for the idea. I'll do that for loop thing first. That weekend problem, i'll try to find out some way & surely share here whatever comes to my mind. Thank You.
 

I've been working on a very similar indicator, but calculating the time periods differently.

I've adapted it to fit in with your code.

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Blue

double Ind_Array0[], Ind_Array1[];  //Indicator Arrays
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

   //Assigning Arrays to buffer
   
   SetIndexBuffer(0, Ind_Array0); //Indicator Array0 is assigned to Buffer Index0
   SetIndexStyle(0,DRAW_LINE, STYLE_SOLID,1);
   
   SetIndexBuffer(1, Ind_Array1); //Indicator Array1 is assigned to Buffer Index1
   SetIndexStyle(1,DRAW_LINE, STYLE_DASHDOT,2);
   
   return;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   if(Period()>PERIOD_H1)  //Indicator will not calculate in higher time-frames
      return(0);
   int    counted_bars=IndicatorCounted();   
   int    i=Bars-counted_bars-1;           // Index of the first uncounted
   
   int chartstart =Bars - (PERIOD_D1/Period() ); //do not include first 24 hours of the chart
   if(i>chartstart)
      i=chartstart;
     
   while(i>=0)
      {
      if(TimeHour(Time[i])>=6 && TimeHour(Time[i+1])<6)
         {
         for (int bshift=1;;bshift++)
            {
            if(TimeHour(Time[i+bshift])>=22 && TimeHour(Time[i+bshift+1])<22)
            break;
            }
        
         int a = iHighest(NULL,0,MODE_HIGH,bshift,i+1);
         Ind_Array0[i] = High[a];
         }
      else  
      Ind_Array0[i] = Ind_Array0[i+1];
      
      i--;
      }


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

I did have a problem with it crashing my platform though. I'm not sure if it was due to trying to get values from beyond the start of the chart

Adding this bit of code

int chartstart =Bars - (PERIOD_D1/Period() ); //do not include first 24 hours of the chart
   if(i>chartstart)
      i=chartstart;

seems to have cured that problem, but I have not had the opportunity to test it with live ticks coming in.

RaptorUK, I would be very grateful if you could cast your experienced eye over the code and see if there is something wrong with it.

JP Forex, I have posted this in your thread because the indicator is so similar to what you are coding and there may be something that you could use

EDIT:Added additional code to prevent it from running on chart time-frames higher than H1, because bar times may not exist in the higher tf

Reason: