Profile Picture

Using AverageTrueRange in an indicator

Posted By rterbush 7 Years Ago
Message
Posted Friday July 02 2010
I'm trying to use AverageTrueRange in an indicator. My indicator is currently erroring out when loading, complaining with:

"An exception of type System.ArgumentOutOfRangeException was thrown.
Value must be between 0 and -1, value was 0"

I am using inputs as defined:

[SeriesInputAttribute("Input High", 1, Value = BarElement.High)]

[SeriesInputAttribute("Input Low", 2, Value = BarElement.Low)]

I'm initializing the indicator with:

ATR = new AverageTrueRange(length);

And I am trying to use the current value of the ATR with:

ATR.Current which is where it is blowing up in the debugger.

Here is the relevant piece of code:

protected override double CalcNewValue(int index)

{

smaHigh.AddValue(inputs[0].LookBack(index));

smaLow.AddValue(inputs[1].LookBack(index));

if (!smaHigh.Full || !smaLow.Full)

return double.NaN;

double relLow = inputs[1].LookBack(index) - ((smaHigh.Average + smaLow.Average) / 2);

return relLow / (ATR.Current * 0.16);

}

Do I need to have more inputs available to ATR and do I need to be using ATR.AddValue to populate it?

Thanks for any help.

Posted Friday July 02 2010
One of your indicators (presumably ATR) doesn't have a value yet, or you're looking back further than there are values. It may require a certain number of bars to obtain that value, so you'll have to check to make sure there is a value before you reference.
Posted Friday July 02 2010
If that is true, and I don't deny that it probably is, I would think that the following bit of code before referencing ATR.Current would avoid the issue.

if (double.IsNaN(ATR.Current))
return double.NaN;

Which it does not. So I am thinking that I have not properly initialized the AverageTrueRange within the code. Is there more that I need to do, beyond what I have shown that I am doing, to use this indicator within the indicator I am writing?
Posted Friday July 02 2010
Is there a need to overload any of the AverageTrueRange methods when used in an indicator?

IF so, can someone tell me which ones?
Posted Friday July 02 2010
I believe I am getting closer to figuring this out but could use some help to get over the hump.

The indicator I am writing uses SeriesCalculatorBaseWithValues

AverageTrueRange appears to be an IIndicator.

From what I have gathered so far, it appears I need to overload SetBars and AppendBar. Is there an example of how I can do this within my SeriesCalculatorBaseWithValues indicator?

 

 

Posted Tuesday July 06 2010
Yes, open the Samples. From the Start menu in the RightEdge program group, there is a shortcut. Open that, click either the C# or VB.NET folder and open the SeriesCalculatorBaseWithValuesIndicator folder. This will give you an example.

rterbush (7/2/2010)
I believe I am getting closer to figuring this out but could use some help to get over the hump.

The indicator I am writing uses SeriesCalculatorBaseWithValues

AverageTrueRange appears to be an IIndicator.

From what I have gathered so far, it appears I need to overload SetBars and AppendBar. Is there an example of how I can do this within my SeriesCalculatorBaseWithValues indicator?



Posted Thursday December 15 2011
Dear all,

I got the same problem when using the Average True Range in my indicator class which implements SeriesCalculatorBaseWithValues!
I could not solve the problem yet.


Also the example you have mentioned does not help!

This is the point where I run into the problem.

Protected Overrides Function CalcNewValue(ByVal index As Integer) As Double
'
'   Note that you need to call Double.IsNaN() to check for NaN
'
If (Double.IsNaN(_waEMA.LookBack(index)) Or Double.IsNaN(_waMACD.LookBack(index)) Or Double.IsNaN(_waATR.Current)) Then
_switchMACDDist = 0
_switchMACDInteg = 0
Return Double.NaN
End If

Do you have a simple example to show me how to use and initialize the Average True Range within an indicator class which is based on the SeriesCalculatorBaseWithValues class?

Thanks,
willy

Edited: Thursday December 15 2011 by willy
Posted Saturday December 17 2011
Dear All,

may be I'm on the wrong way with my little project so I will give you some more information what I'm trying to realize.

At the end I want to develop a MinMax process to find relavant minima and maxima of the underlying price process.

Befor I can do that I need some SAR process also based on this underlying price process that indicates whether the price is currently move up or down.
So this SAR process is needed as input for my MinMax process and should return alternating +1 and -1.
+1 in case the price moves up, -1 in case the price move down

My first SAR process should be constructed based on a standard MACD(12,26) with a signal line constructed out of EMA(9). To avoid that the SAR process produces false signals I also like to use a multiple of the average true range ATR(n) as some kind of threshold, i.e. the process should change its direction only when the distance of the MACD and signal line is above some minimal threshold.

I started coding this SAR process as an indicator based on SeriesCalculatorBaseWithValues. In a first step I used a fixed value as threshold in my SAR indicator class and all is working fine. The indicator is well plotted in the chart.

In a next step I like to use a multiple of the average true range as threshold and now I run into some problems, I do not know how to do this within my SAR indicator.
In another post http://www.rightedgesystems.com/forums/Topic11260-10-1.aspx I read that because the RE.ATR implements IIndicator and my SAR indicator implements ISeriesCalculator it is not possible to use this ATR in my indicator class!

May be the SAR process I created should not be based on SeriesCalculatorBaseWithValues but I like to use all the advantages the indicator class give to me like displaying it in the indicator pane, changing attributes via the GUI and plotting it within the price pane or in a separate pane etc.

So is there any other solution to overcome my problem? Maybe create my SAR process on any other class which implements most of the advantages the indicator class does?

Thanks in advance for your help.
willy

Edited: Sunday December 18 2011 by willy
Posted Monday December 19 2011
SeriesCalculatorBaseWithValues and IndicatorBase are both first class citizens when it comes to being displayed in the UI of RightEdge for drag and drop. So you can use either one without fear of that. The ATR indicator is derived from IndicatorBase which does implement IIndicator. I don't understand why you cannot use ATR in your indicator. Why not simply create an instance of ATR within your indicator class? For example, the "Commodity Channel" indicator within RightEdge uses the "Average Price" indicator (which is also derived from IndicatorBase). I've attached it to serve as an example of this being used. Let me know if this is what you're trying to accomplish.

willy (12/17/2011)
Dear All,

may be I'm on the wrong way with my little project so I will give you some more information what I'm trying to realize.

At the end I want to develop a MinMax process to find relavant minima and maxima of the underlying price process.

Befor I can do that I need some SAR process also based on this underlying price process that indicates whether the price is currently move up or down.
So this SAR process is needed as input for my MinMax process and should return alternating +1 and -1.
+1 in case the price moves up, -1 in case the price move down

My first SAR process should be constructed based on a standard MACD(12,26) with a signal line constructed out of EMA(9). To avoid that the SAR process produces false signals I also like to use a multiple of the average true range ATR(n) as some kind of threshold, i.e. the process should change its direction only when the distance of the MACD and signal line is above some minimal threshold.

I started coding this SAR process as an indicator based on SeriesCalculatorBaseWithValues. In a first step I used a fixed value as threshold in my SAR indicator class and all is working fine. The indicator is well plotted in the chart.

In a next step I like to use a multiple of the average true range as threshold and now I run into some problems, I do not know how to do this within my SAR indicator.
In another post http://www.rightedgesystems.com/forums/Topic11260-10-1.aspx I read that because the RE.ATR implements IIndicator and my SAR indicator implements ISeriesCalculator it is not possible to use this ATR in my indicator class!

May be the SAR process I created should not be based on SeriesCalculatorBaseWithValues but I like to use all the advantages the indicator class give to me like displaying it in the indicator pane, changing attributes via the GUI and plotting it within the price pane or in a separate pane etc.

So is there any other solution to overcome my problem? Maybe create my SAR process on any other class which implements most of the advantages the indicator class does?

Thanks in advance for your help.
willy

Attachments
CommodityChannel.cs (789 views, 6.00 KB)
Posted Monday December 19 2011
Hi billb,

the example did not really help, I'm sorry.

Attached you will find my indicator class example, so you get a better understanding what I try to realize.

From what I have learned so far, it is not possible to include an incicator which was derived from IIndicator like the Average True Range in an indicator class which was derived from SeriesCalculatorBaseWithValues.

Is this correct?

I do not know how to initialize the ATR in my indicator class.

At the following point the code crashes!

If (Double.IsNaN(_waEMA.LookBack(index)) Or Double.IsNaN(_waMACD.LookBack(index)) Or Double.IsNaN(_waATR.LookBack(index))) Then
Return Double.NaN
End If

Thanks in advance,
willy

Attachments
ClassLibrary12.vb (290 views, 7.00 KB)

Edited: Monday December 19 2011 by willy


Similar Topics


Reading This Topic


2005-2017 © RightEdge Systems