Normalized ATR

 

Hi,

I need to use normalized ATR (Average True Range) indicator that oscialtes between 0 and 1 in my EA. I searched but could not find such a function written in MQL language. The logic must be something like below as I found from google:

(ATR of last bar – min ATR in last n-bars) / (max ATR in last n-bars – min ATR in last n-bars)

Does anybody have an idea how to write a function like iATR? I don't want an indicator just an function for EA.

Any help or guidance is appreciated

Thanks

 
aed71: Does anybody have an idea how to write a function like iATR?
Code exactly what you wrote. Get the last n ATRs. Find the min and max compute your value. learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.
 

I understand what you are pointing, you are right. However my problem is not on the code and delivering is FoC. My problem is if the logic/formula is correct or not. Thus I just wanted to know if there is an off-the-shelf function that was created and tested before.

My code is below, it can be poorly coded, sorry for that. The problem is the nATR should be between 0 and 1 and when I trace, it is going down 0.45 minimum even the ATR is about 0.0005 (very close to zero). I saw maximum 1 when ATR is highest (0.0015) which seems ok.

I guess nATR should approach to zero when I saw ATR is around 0.0005. I'm using period 14 both in the code and as indicator on screen and I put Alert message printing the nATR(14) for every tick of 15min. I'm trying to compare the both values at the same ticks.

What could be the problem? Any comment about the formula or my code?

Thanks

double nATR(int period)
{
   //(ATR of last bar – min ATR in last n-bars) / (max ATR in last n-bars – min ATR in last n-bars)
   
   int z,c_max,c_min;
   double arr_min[100],arr_max[100];
   
   // Find minimum ATR 
   for(z=0;z<15;z++) arr_min[z]=99999;
   for(z=0;z<15;z++) arr_min[z]=iATR(Symbol(),0,period,z);
   c_min=ArrayMinimum(arr_min,0,0);
   
   // Find maximum ATR
   for(z=0;z<15;z++) arr_max[z]=-99999;
   for(z=0;z<15;z++) arr_max[z]=iATR(Symbol(),0,period,z);
   c_max=ArrayMaximum(arr_max,0,0);
  
   double nATR=( ( iATR(Symbol(),0,period,1) -  arr_min[c_min] ) / ( arr_max[c_max] - arr_min[c_min] ) ); 
   
   return(nATR);

}
 

Why do you have 2 arrays, arr_min and arr_max when you assign exactly the same values to each? and why is the array declared with 100 elements, when only 15 are used?

 
GumRai:

Why do you have 2 arrays, arr_min and arr_max when you assign exactly the same values to each? and why is the array declared with 100 elements, when only 15 are used?


Hi, Thank you for your comments. You are right 100 is too large but in case i call the function with a higher period I want it to work without problems. For arrays, I just want to clean it -9999 for max and +9999 for min before I use it. It can be reduced to one array as you pointed.

However changing those does not currently help my issue, I don't think that the result would be different :-(

I guess it would be better if I change the code as below as you suggested but the main logic (formula did not change)...

double nATR(int period)
{
   //(ATR of last bar – min ATR in last n-bars) / (max ATR in last n-bars – min ATR in last n-bars)
   
   int z,c_max,c_min;
   double arr[100];
   if (period<1) period=3;     // Min period is 3
   if (period>100) period=100; // Max period is 100

   // Find min & max ATR
   for(z=0;z<period;z++) arr[z]=iATR(Symbol(),0,period,z+1);
   c_min=ArrayMinimum(arr,0,0);
   c_max=ArrayMaximum(arr,0,0);
  
   double nATR=( ( iATR(Symbol(),0,period,1) -  arr[c_min] ) / ( arr[c_max] - arr[c_min] ) ); 
   
   return(nATR);

}
 
aed71:


Hi, Thank you for your comments. You are right 100 is too large but in case i call the function with a higher period I want it to work without problems. For arrays, I just want to clean it -9999 for max and +9999 for min before I use it. It can be reduced to one array as you pointed.

However changing those does not currently help my issue, I don't think that the result would be different :-(

I guess it would be better if I change the code as below as you suggested but the main logic (formula did not change)...


I don't know enough about working with arrays in this way to offer any suggestions, but I do have a question to ask, if you don't mind

c_min=ArrayMinimum(arr,0,0);
//I understand that the last 0 means to scan from the very first element in the array, but what does the 0 preceding it mean?
//Does it mean that the EA scans 0 elements or does it mean scan the whole array?
//If it means scan the whole array, then is this line of code exactly the same as
c_min=ArrayMinimum(arr);
//The description in the help file is not very clear.

I would also like to know if there are any problems with using nATR as the function name as well as the variable double nATR that the function returns?

 
 double arr_min[100],arr_max[100];
   for(z=0;z<15;z++) arr_min[z]=iATR(Symbol(),0,period,z);
   c_min=ArrayMinimum(arr_min,0,0);
That index do you think ArrayMinimum is returning when you only initialize 16 values but tell it to scan all 100?
 

Thanks for your valuable comments.

I've updated the code as below, now ArrayMinimum and ArrayMaximum will look only periods of data in the array and start from the first element in the array which is zero.

Now the result swings between 0 and 1 but not in a correct form, such that I saw zero value although the ATR is not close to zero and see 1 although it's not even close to high values...

double nATR(int period)
{
   //(ATR of last bar – min ATR in last n-bars) / (max ATR in last n-bars – min ATR in last n-bars)
   
   int z,c_max,c_min;
   double arr[100];
   if (period<1) period=3;     // Min period is 3
   if (period>100) period=100; // Max period is 100

   // Find min & max ATR
   for(z=0;z<period;z++) arr[z]=iATR(Symbol(),0,period,z+1);
   c_min=ArrayMinimum(arr,period,0);
   c_max=ArrayMaximum(arr,period,0);
  
   double nATR=( ( iATR(Symbol(),0,period,1) -  arr[c_min] ) / ( arr[c_max] - arr[c_min] ) ); 
   
   return(nATR);

Here is the screenshot what I got.

Any other idea?

Many thanks.

ATR Screenshot

 

Do you also think that the formula is correct and

1-) is "the last bar of ATR" statement correct in my code?

2-) it says min ATR in the last n bars, do you think that I'm delivering this by calculating minimum and maximum array values of 14 period ATR???

I'm not really sure about the below formula and/or my implementation of the formula...

(ATR of last bar – min ATR in last n-bars) / (max ATR in last n-bars – min ATR in last n-bars)