Profile Picture

Bug with ATR?

Posted By brianhoang 2 Years Ago
Posted Tuesday April 18 2017
I sent Daniel an email a couple days ago, but haven't heard back so I thought I'd post here.  I was looking at the code of ATR included in the Samples directory (e.g. C:\Program Files (x86)\Yye Software\RightEdge 2010\Samples\Samples\RightEdgePlugins), and discovered that the TrueRange code (which ATR uses) does not use Absolute values in its calculation.  If this sample code is indeed the same as the actual code, this would be a very serious bug.  Below is the code in question in the TrueRange class.  The highMinusLow, highMinusPrevClose , prevCloseMinusLow should have been wrapped by Math.Abs

public override double CalcNextValue(BarData bar)
    int pBarIndex = pBars.TotalCount - 1;

    // This is the first bar, so do things a little different
    if (pBarIndex == 0)
return double.NaN;

    double highMinusLow = pBars.Current.High - pBars.Current.Low;
    double highMinusPrevClose = pBars.Current.High - pBars.LookBack(1).Close;
    double prevCloseMinusLow = pBars.LookBack(1).Close - pBars.Current.Low;
    double maxValue = double.MinValue;
    maxValue = Math.Max(maxValue, highMinusLow);
    maxValue = Math.Max(maxValue, highMinusPrevClose);
    maxValue = Math.Max(maxValue, prevCloseMinusLow);

    return maxValue;

Posted Wednesday April 19 2017
How would the numbers go negative?

Posted Monday April 24 2017
 double highMinusPrevClose = pBars.Current.High - pBars.LookBack(1).Close;
  double prevCloseMinusLow = pBars.LookBack(1).Close - pBars.Current.Low;

one of them could go negative: current's high could be lower than yesterday's close, or yesterday's close could be lower than today's low.  By not wrapping these around Math.Abs, the True Range calculation potentially discards one of the calculations, violating the original intent of True Range / ATR.
Posted Saturday April 29 2017
Yes. I was not looking at yesterdays values. You are correct.

Posted Monday May 01 2017
I believe the code is correct as written.  The way it’s written, if either calculation 2 or 3 produces a negative number, the other of 2 and 3 will produce a positive number, and that positive number, or calculation 1, which is always produces a positive number, will be the correct result.  For example, if the price gaps down, the second calculation (current high minus previous close) will produce a negative number but the third calculation (previous close minus current low) will produce a positive number, which will be the right result (unless calculation 1 is a larger number, in which case it would be the right result).  You get same results if you add Math.Abs to calculations 2 and 3.
Posted Sunday May 07 2017
You are correct.  I ran through difference scenarios, and the code works as TR is intended. Phew!

Similar Topics

Reading This Topic

2005-2019 © RightEdge Systems