This is my fault-tolerant ATR indicator for you

Posted By: Smon

This is my fault-tolerant ATR indicator for you - 05/16/19 12:20

Why you need it: Price Data can sometimes be crappy, like price not being updated for days (like the Dukascopy Forex Dataset). This will cause the normal ATR indicator to trend towards Zero or even reach Zero - really bad if you divide by ATR!

So my fault-tolerant ATR works like this: If the newest price bar is completely flat (High = Open = Close = Low), it's probably fake/missing data and will be ignored. This is the case when TrueRange is Zero. The ATR value will only update again if new price data arrives.


Code:
function ftATR(ATRPeriod)
{
 vars tr = series(0, -ATRPeriod); // create a static series
 if(TrueRange() != 0)
 {
 shift(tr, TrueRange(), ATRPeriod);
 }
 if(tr[ATRPeriod-1] == 0)
 { 
  ATRPeriod = 1;
  while(tr[ATRPeriod] != 0) ATRPeriod++;
 }
	
 var myATR = Sum(tr, ATRPeriod) / ATRPeriod;

 return(myATR);
}



usage:

(put the function code before the run function)

Code:
ftATR(int TimePeriod): var



example:

Code:
var my_atr = ftATR(30);

Posted By: OptimusPrime

Re: This is my fault-tolerant ATR indicator for you - 05/18/19 14:35

Thank you!
Posted By: Smon

Re: This is my fault-tolerant ATR indicator for you - 05/26/19 12:54

I just found an error that wasn't detected by the compiler. I mixed some R syntax in. It's

Code:
var ftATR(int ATRPeriod)



not

Code:
function ftATR(ATRPeriod)

Posted By: Smon

Re: This is my fault-tolerant ATR indicator for you - 12/06/20 19:50

And now without internal series (internal series just suck):

Code
var ftATR2(int ATRPeriod)
{
	var* tr = zalloc(ATRPeriod*sizeof(var));
	int i = 0;
	int ElementIDX = 0;
	var MyTrueRange;
	
	while(ElementIDX < ATRPeriod)
	{
		MyTrueRange = max(priceHigh(0+i),priceClose(1+i))-min(priceLow(0+i),priceClose(1+i));
		if (MyTrueRange != 0)
		{
			tr[ElementIDX] = MyTrueRange;
			ElementIDX++;
		}
		i++;
		if(i > 10*ATRPeriod) break;
	}
	
	var myATR = Sum(tr, ElementIDX) / ElementIDX;
	if(!Init && !is(LOOKBACK) && myATR == 0) printf("\nftATR is zero! This shouldn't happen!");
	return(myATR);
}
© 2024 lite-C Forums