Registered User Joined: 4/29/2006 Posts: 16

I'm trying to get the average absolute range of the open to the close (in percent) for a set of specific dates. Please help me in correcting this formula. Thanks.
Static Sum As Single
If isFirstBar Then
Sum = 0
End If
If Price.DateValue.Year = 2010 AndAlso _
((Price.DateValue.Month = 12 AndAlso _
Price.DateValue.Day = 2) OrElse _
(Price.DateValue.Month = 12 AndAlso _
Price.DateValue.Day = 7))Then
Sum += Math.Abs(Price.Last / Price.Open)
End If
If isLastBar Then
Plot = 100 * (Sum / 2  1)
Else
Plot = Single.NaN
End If

Worden Trainer
Joined: 10/7/2004 Posts: 65,138

Move the 1 to the Sum += line as Price.Last / Price.Open will always be positive anyway for actively trading stocks.
Sum += Math.Abs(Price.Last / Price.Open  1)
Then your Plot line would need to remove the 1:
Plot = 100 * Sum / 2
Bruce Personal Criteria Formulas TC2000 Support Articles

Registered User Joined: 12/31/2005 Posts: 2,499

So your code as writtem takes 12/2/2010 and 12/7/2010 values and sums them. Then on the last bar it plots 100 * (sum/2  1). So only 1 data point is plotted.
The value = abs(close / open). This looks strange as it will never be negative. Did you mean (Close  Open)?
ABS(Close/Open)
12/2/2010 Open = 5, close = 10 = 10/5 = 2
12/7/2010 Open = 10 close = 5 = 5/10 = .5
sum = 2.5
plot = 100 * (2.5/2  1) = 25
ABS(close  open) yields
5
5
sum = 10
plot = 100 * (10/2  1) = 400
What are you trying to plot in English?

Registered User Joined: 12/31/2005 Posts: 2,499

Looking closer
average absolute range of the open to the close (in percent)
in not ABS(Close / Open)
but
ABS(Close  Open)/Open
ABS(10  5)/5 = 1
ABS(510)/10 = .5
Sum = 1.5
plot = 100 *(1.5/2  1) = 25 ' wrong formula. Should be plot = 100 * (sum/2)
Plot = 100 * (1.5/2) = 75
Note the average is 75% but the total price change is zero. 5>10 10>5

Worden Trainer
Joined: 10/7/2004 Posts: 65,138

jas0501,
The original question is in plain English and has already been answered. The range from the Open to Close as defined in the question is the same as the absolute Percent Change from Open to Close.
The only problem with the original version was that the 1 was in the wrong line. And the only reason it was wrong was because the result needed to be the Average of the Absolute Percent Change instead of the Average of the Percent Change.
Bruce Personal Criteria Formulas TC2000 Support Articles

Registered User Joined: 12/31/2005 Posts: 2,499

Programming hint:
When developing code, or more specificly formulas using hypothetical values and doing the math beforehand often helps clarify the logic and reveals what will plot. It may not be what you want and often will reveal logic errors.
This is why is used the O=5 C=10, and O=10 C=5 data point as an illustration. It is much easier to "understand" the result and judge the correcness of the formulus.

Registered User Joined: 4/29/2006 Posts: 16

Works exactly as I wanted. Thank you Bruce.
Jas0501, Thanks for your suggestions.

Worden Trainer
Joined: 10/7/2004 Posts: 65,138

You're welcome. Our pleasure.
Bruce Personal Criteria Formulas TC2000 Support Articles
