ATR indicator for a specific time range

 

Hi,

I am a beginner in programming (know some phython basics and can write basic functions in mql4) and I would like to program an intraday ATR indicator for a specific time range. For example I want to have the ATR(5) of the NY Trading session from 1430pm to 2100pm.

I think I would use a for-loop with each interation counts the past days range for the specific time and calculate the average then. But I am stuck here a bit in the planning and any guidance how and which functions to use to calculate the time and range is highly appreciated.

 

When you start writing the codes ... let us know. Provide samples of the relevant code you're having problems with. What you have done to debug it. What you're trying to achieve.

We cannot tell you what to code. We can however help with coding specifics. And we're certainly not going to code it for you ... well 99% of the time anyways :)

 

I use similar calculations, so I can share an example here.

(The indicator is designed for a simple iCustom call, not for display.)

Files:
 

Thank you Ovo. I haven't learnt buffers yet, so I cannot comprehend your code completely.

 

@ubzen

Well, I didn't mean that anyone should code for me. I just don't know how to start.

I've tried for hours and the outcome isn't working at all. :)

Not sure if I am on the right track. What doesn't work in this early stage:

1) The calculation is dependent on the timeframe chosen in the chart. Why is that even though I set the timeframe parameter to PERIOD_M1 in each function?

2) While the ATRhigh calculation seems to be correct the ATRlow isn't. I suppose it is going back further than it should.



#property indicator_chart_window
extern int ADR_count = 1;                                           //Number of days to go back to calculate the average
extern int starthour = 1230;                                        //Starting time of the range
extern int endhour = 2000;                                          //Ending time of the range

int start()
  {
   datetime midnight=iTime(Symbol(),PERIOD_D1, 0);                  //Calculates midnight as reference point
   int start = midnight + (starthour/100)*60*60+(starthour%100)*60; //Calculates the starting time in seconds after midnight
   int end = midnight + (endhour/100)*60*60+(endhour%100*60)-1;     //Calculates the ending time in seconds after midnight
   int range = end - start;                                         //Calculates the range in seconds
   int shift_D1 = PERIOD_D1 * 60;                                   //Set the shift by one day which is needed in the loop
   int TimeStart = end - shift_D1;                                  //The calculation should start with yesterday
   double avgATR, ATRsum, ATRhigh, ATRlow;

   for(int i=0;i<ADR_count;i++)                                     //Iterate once for each day
      {
         if(TimeDayOfWeek(TimeStart)==0)        TimeStart = TimeStart - 2 * shift_D1;                                 //Because of SundayData jump to Friday if its Sunday
         ATRhigh = High[iHighest(Symbol(), PERIOD_M1, MODE_HIGH, range, iBarShift(Symbol(),PERIOD_M1, TimeStart))];   //Calculate the range high
         ATRlow  = Low[iLowest(Symbol(), PERIOD_M1, MODE_LOW, range, iBarShift(Symbol(),PERIOD_M1, TimeStart))];      //Calculate the range low
         TimeStart -= shift_D1;                                                                                       //Go to the next day in the next loops iteration
         ATRsum = ATRsum + (ATRhigh - ATRlow);                                                                        //Average calculation part I
      }
   avgATR = (ATRsum / ADR_count) * 1000;                                                                              //Average calculation part II
   Print("ATRhigh: " + ATRhigh +"\nATRlow: " + ATRlow + "\nATRsum: " + ATRsum);                                       //just for checking
  

   return(0);
  }