Want to make an ADR without using Period_D1 - can you please help me fix my first attempt?

 

I am relativily new to programming and trying to learn by deciding what I want to do and then researching and investigating how to do that one thing.  I learn a lot and find it easier to learn with real world application.

I want the ADR(20) in the buffer for each candle, for a defined session.  I am thinking session and day as interchangeable right now, because my intention in the way I think I am writing this indicator is so I could easily modify it to be for market sessions, not only days in the future.  Also, I don't want to use Period_D1, because then it will not work in backtesting, which is the main reason I need it at this time.  But I can easily see the need (or nice to have) to see the average range for a particular session (e.g. first 2 hours of the London open) and compare that to other periods of time.

I have looked at many examples of ADR indicator code on internet, but every one of them except one uses Period_D1 (which by using that makes the job easy, but does not meet my requirements).  The other one used a complex array, which I did not fully understand and wanted to stick with what I have done so far.

 So I figured I would ask and hopefully someone could guide me.

Where I have gotten is I have calculated the previous day's range (a session length and the seseeion end hour are defined in the indicator).

 The difficulty I am having is getting the 20 session periods' ranges summed and then averaging them by 20.

 I have used a for loop, but it just does not compare to when I use Period_D1 as a sanity check.  So I figured there is a bug in my code.

So how can I fix what I have?  Any suggestions? 


 Here is a piece of my code:

 

   int mVcnt, ADR2, ADR1,ADR0, aVcnt, BarCnt,NumForSessionCloseBar, ShiftToDayClosebar, BarsForthePeriod,i;
   datetime aTime;
   
   int SesionLength = LengthofSessioninHours * 60;
   
if(Time[0] > aTime)  // update only once per bar
{     
 for(mVcnt=0; mVcnt<=NumBarsBack; mVcnt++)
 {
   if(TimeHour(Time[mVcnt]) > SessionHourClosed)
    {
     NumForSessionCloseBar           = (((TimeHour(Time[mVcnt])- SessionHourClosed)*60) + TimeMinute(Time[mVcnt]))/ ChartPeriod1; //calculate how many bars away is the close of the session.
     ShiftToDayClosebar          = (NumForSessionCloseBar + mVcnt); // adds the current candle's bar to the number of bars so can shift to the session close bar
     BarsForthePeriod           = (SesionLength / ChartPeriod1); // Number of bars from the beginning of the session to the end of the seesion if 24 hours
          //Using this below two lines as it shows the ADR for the previous session
          ADR0 =  ((iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,BarsForthePeriod,ShiftToDayClosebar))  - iLow(NULL,0,iLowest(NULL,0,MODE_LOW,BarsForthePeriod,ShiftToDayClosebar)))/point/10);
          PreviousSessionRange[mVcnt]   =  ADR0;
                
                for(i=1;i<=ADRAvg;i++,ShiftToDayClosebar += BarsForthePeriod )
                  ADR1   +=  ((iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,BarsForthePeriod,ShiftToDayClosebar))  - iLow(NULL,0,iLowest(NULL,0,MODE_LOW,BarsForthePeriod,ShiftToDayClosebar)))/point);
        
      ADR1 = ADR1/ADRAvg;
      AvgDayRange[mVcnt] = ADR1;

 This is used for when the candle is after the end of the last session hour.

At the first 'if' statement, i find out where the current candle is compared to the session close hour.  If the current candle time is after the session's close, then it goes here.

 First I calculate the number bars away the beginning of the session is from the current candle.  This then gives me the shift I need to get to the sessions' close.

Then I can calculate how many bars are in the session by dividing the number of hours of the session length by the chart period.

Then I use the shift and the session length for the ihighest and ilowest, within a loop.  The loop continues 20 times.  I then take that and sum at the end of the loop and divide by 20.  Then I place that in the buffer and it goes on to the next candle in the orignal loop.

So as I said, the ADR does not appear to be correct.  

I appreciate any guidance, explanations, example code, etc.  

 I am first to admit that my method of code might not be the best, cleanest, nor the quickest way.  But it is how my thought process was and the extent of my understanding so far to this point.  If I may be so bold to presume to ask, but could you focus your guidance and advice to help me fix my current way of doing it.  If it is completely wrong and there is no way for me to accomplish my goal, then sure, I am ready to hear that and learn the proper way.  But if I can do it with making some changes with what I have, I would like that more.

 I thank you in advance.

-Stearno 

 

I learned from another indicator I found and now have a much cleaner way of doing the sessions than I originally did.

But now my issue is that it does not average. It is only giving me the range of the previous period. I tried even putting a Comment up to show each array's value, but they are all the same value.

I appreciate advice on what my next step should be.  Thank you in advance for your help.

 

-Stearno 

 

Okay, new attempt.  I am using the arrrays to add the values for each session up.  But in this version, I put the buffers to show the array values.  The array values are the same.  

What is wrong with my "for" cycle or with my assigning the value to the array element?

 

Thanks for any help or advice you can give!

-Stearno

 

i guess you are making progress, but it will be much faster if you start using debugger.

To run your code with debugger, first you have to adapt to H1 which is the only TF on which debugger works.

 
graziani:

i guess you are making progress, but it will be much faster if you start using debugger.

To run your code with debugger, first you have to adapt to H1 which is the only TF on which debugger works.

Can you explain that ? I never get a problem using the debugger on any TF.
 

ah, you have to specify the symbol and TF in tools->options->debug, otherwise it runs on USDCHF(?) H1....

as i tried out this sample it failed with the USDCHF H1 so i concluded that stearno is not using a debugger....

sorry, my bad. 

 

 

the question now is ... how do i debug every day?????  :))))

well, 99% of my sources work on every tick and on every TF so this obviously didn't trigger any alert in my case....

 
graziani:
...

 

the question now is ... how do i debug every day?????  :))))

well, 99% of my sources work on every tick and on every TF so this obviously didn't trigger any alert in my case....

Sorry I don't get your point ?
Reason: