Welcome Guest, please sign in to participate in a discussion. | Search | Active Topics | |
Platinum Customer
Joined: 2/14/2005 Posts: 169
|
HIgh,
I'm trying to write a Realcode indicator that will calculate
100*((price.high/price.close)-1) but only for up days and then get n bar simple average or standard deviation as a user choice of output options with n also a user choice.
I have not found a way to do this either with a plotted indicator or inside a Realcode indicator because of the requirement to use only up days.
Waddya tink? Is there a way to do this?
Steve
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
So do you want to use the most recent n up days over however many days it takes or just the up days over an n period span?
One is not any more difficult than the other, but the using the most recent n up days over however many days it takes does avoid the possibility of not having any up days at all during an n period span.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Platinum Customer
Joined: 2/14/2005 Posts: 169
|
I see what you mean. Let the period be a variable until sufficient bars (User specified) that are up days have been found to get the stix results.
Need to get experience trying this before I can provide a more educated choice.
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
Please try the following RealCode Indicator.
'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 5.0 www.worden.com
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:Up Bars SMA or SD
'|******************************************************************
'# Period = UserInput.Integer = 60
'# SMAorSD = UserInput.String = "SD"
Static UpDays As New List(Of Integer)
Static Sum As Single
Static SumSq As Single
Static Output As Single
If isFirstBar Then
UpDays.Clear
Sum = 0
SumSq = 0
Output = Single.NaN
Else If Price.Last > Price.Last(1) Then
UpDays.Add(CurrentIndex)
Dim Perc As Single = 100 * (Price.High / Price.Last - 1)
Sum += Perc
SumSq += Perc ^ 2
End If
If UpDays.Count >= Period Then
If SMAorSD = "SD" Then
Output = Math.Abs((SumSq - Sum ^ 2 / Period) / Period) ^ .5
Else
Output = Sum / Period
End If
Dim Offset As Integer = CurrentIndex - UpDays(0)
UpDays.RemoveAt(0)
Dim Perc As Single = 100 * (Price.High(Offset) / Price.Last(Offset) - 1)
Sum -= Perc
SumSq -= Perc ^ 2
End If
Plot = Output
If isLastBar Then
UpDays.Clear
Sum = 0
SumSq = 0
Output = Single.NaN
End If
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Platinum Customer
Joined: 2/14/2005 Posts: 169
|
Bruce,
OK That worked!
I can see that using the defined LIST named UpDays created an indexed array that you referenced to select the values to keep in the sum and sumSq varibles after the period number of up days had been reached, in essence removing the first and adding the last values to the running values of sum and sumSq.
I had seen the LIST method when checking on-line how to handle the array nature of this programming request but was unable to see exactly how to use it from examples I found. The use of commands such as Updays.clear, Updays.add ... were new to me.
Well, nicely done -- I not only got the indicator that I wanted to use but also learned some more about programmming this kind of problem.
Thx,
Steve
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
You're welcome.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Guest-1 |