Welcome Guest, please sign in to participate in a discussion. | Search | Active Topics | |
Registered User Joined: 10/7/2004 Posts: 36
|
Under a posting by the same subject in StockFinder 4.0 on September 28, 2009 Bruce developed a Real Code solution for a Cumulative Indicator request as follows:
'# Cumulative
Static XavgC(3) As Single
Static sumWeight(3) As Single
Static termRatio(3) As Single
Static Sum As Single
If isFirstBar Then
For i As Integer = 0 To 3
sumWeight(i) = 1
Next
termRatio(0) = 1 / 3
termRatio(1) = 2 / 4
termRatio(2) = 4 / 6
termRatio(3) = 6 / 8
Sum = 0
End If
For i As Integer = 0 To 3
Dim Weight As Single = 1 / sumWeight(i)
XAVGC(i) = XAVGC(i) * (1 - Weight) + Weight * Price.Last
sumWeight(i) = sumWeight(i) * termRatio(i) + 1
Next
If XavgC(1) > XavgC(3) Then Sum += 1
If XavgC(0) < XavgC(2) Then Sum -= 1
Plot = Sum
-------------------------------------------------------------------------------------
This works great for me in V4 & V5. However, I would like to change the above Real Code such that I can easily enter variable EMAs but my programming skills are not yet up to the task. Would appreciate if you could assist.
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
You don't need to manually expand the Exponential Moving Average in StockFinder 5.
'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 4.9 www.worden.com
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:My Indicator
'|******************************************************************
'# Period1 = UserInput.Integer = 2
'# Period2 = UserInput.Integer = 3
'# Period3 = UserInput.Integer = 5
'# Period4 = UserInput.Integer = 7
'# Cumulative
Static Sum As Single
Static Start As Integer
If isFirstBar Then
Sum = 0
Start = Math.Max(Period1, Period2)
Start = Math.Max(Start, Period3)
Start = Math.Max(Start, Period4)
Start = Start
End If
If CurrentIndex >= Start Then
If Price.XavgC(Period2) > Price.XavgC(Period4) Then Sum += 1
If Price.XavgC(Period1) < Price.XavgC(Period3) Then Sum -= 1
Plot = Sum
Else
Plot = Single.NaN
End If
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 10/7/2004 Posts: 36
|
Bruce, just excellent!
I appreciate your speedy assistance.
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
You're welcome.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 12/12/2009 Posts: 34
|
When I try to create indicator using Bruce's above code, got following errors on several lines used....
Name Math is not declared
XavgC is not a member of WBI.CommonBlocks.PriceScripting
Any help is highly appreciated.
Thx
Devtrader
|
|
Administration
Joined: 9/18/2004 Posts: 3,522
|
devtrader, are you running version 5?
Ken Gilb (Kuf) Chief Software Engineer - Worden Brothers Inc. Try/Catch - My RealCode Blog
|
|
Registered User Joined: 12/12/2009 Posts: 34
|
again that answers my question. I am running version 4
Thnaks
|
|
Registered User Joined: 12/12/2009 Posts: 34
|
# Cumulative
Static XavgC(3) As Single
Static sumWeight(3) As Single
Static termRatio(3) As Single
Static Sum As Single
If isFirstBar Then
For i As Integer = 0 To 3
sumWeight(i) = 1
Next
termRatio(0) = 1 / 3
termRatio(1) = 2 / 4
termRatio(2) = 4 / 6
termRatio(3) = 6 / 8
Sum = 0
End If
For i As Integer = 0 To 3
Dim Weight As Single = 1 / sumWeight(i)
XAVGC(i) = XAVGC(i) * (1 - Weight) + Weight * Price.Last
sumWeight(i) = sumWeight(i) * termRatio(i) + 1
Next
If XavgC(1) > XavgC(3) Then Sum += 1
If XavgC(0) < XavgC(2) Then Sum -= 1
Plot = Sum
---------------------------------------------------------------------------------
Bruce, I have been using this code for SF5 and 4.0. there are few things I can't understand :
(a) XavgC(3) sumWeight(3) termRatio(3) , What are these variables can you explain ?
(b) XAVGC Exponential Moving Average of Close/Last Price?
(c) so what is the difference between Xavgc and XAVGC in this program ?
(d) if isFirstBar means isFirstBar (Returns true if calculating for the first bar of the calculation) confusing to me. what is it ?
Is it possible for you to explain in order for me to understand how this works.
Thankx for what you do.
devtrader
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
QUOTE (devtrader) XavgC(3) sumWeight(3) termRatio(3) , What are these variables can you explain ?
This RealCode was written before the Price.XAVGC syntax was introduced (and may be faster than the new syntax in this case). All of these variables are involved in the calculation of the EMAs. The variable names are also quite descriptive of how they are used.
XavgC(3) is a variable that stores the values of the 4 Exponential Moving Averages as they are calculated from bar to bar.
sumWeight(3) is the sum of the Weights given to all of the terms (in this case Prices) used in the calculation of the EMA so far.
termRatio(3) stores the ratio of the decreasing weights of older terms (in this case Prices) as they are added to these Exponential Moving Averages. 1/3 is a 2-Period EMA, 2/4 is a 3-Period EMA, 4/6 is a 5-Period EMA and 6/8 is a 7-Period EMA.
QUOTE (devtrader) XAVGC Exponential Moving Average of Close/Last Price?
That is what it is calculating, yes.
QUOTE (devtrader) so what is the difference between Xavgc and XAVGC in this program ?
Nothing. The capitalization doesn't matter, both versions represent the same variable.
QUOTE (devtrader) if isFirstBar means isFirstBar (Returns true if calculating for the first bar of the calculation) confusing to me. what is it ?
Is it possible for you to explain in order for me to understand how this works.
I am not sure what is confusing you about this as you have identified exactly when it returns True.
In this particular RealCode, the variables used have their values reset at this point so the initial values are correct when switching between symbols.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 12/12/2009 Posts: 34
|
Bruce,
thankx and appreciate for explaining and being patience.I am just trying to understand the logic of the code
QUOTE(Bruce)
termRatio(3) stores the ratio of the decreasing weights of older terms (in this case Prices) as they are added to these Exponential Moving Averages. 1/3 is a 2-Period EMA, 2/4 is a 3-Period EMA, 4/6 is a 5-Period EMA and 6/8 is a 7-Period EMA.
How do you determine/calculate termratio 1/3= 2 period 2/4 = 3 periodand so on ?
I am using in #cumulative modified indicator
termRatio(0) = 1 / 8
termRatio(1) = 1 / 6
termRatio(2) = 1 / 4
termRatio(3) = 1 / 3
What will be the period for these termratios ?
so weights are basically prices here for the current bar and stored in the termratio based on the period EMAs(2 or 3 or 4 or so..on).
thanks
devtrader
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
The termRatio for an EMA using this algorithm (and "standard" EMAs in general) is (Period - 1) / (Period + 1). So only one of your example termRatios ends up resulting an Integer Period (the termRatio is valid when greater than zero and less than one):
termRatio = 1 / 8 would be an EMA Period of 9 / 7.
termRatio = 1 / 6 would be an EMA Period of 1.4.
termRatio = 1 / 4 would be an EMA Period of 5 / 3
termRatio = 1 / 3 would be an EMA Period of 2.
The termRatio is not adjusted over the course of the algorithm. What gets adjusted is the sumWeight and weight (and of course the value of the EMA itself).
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 12/31/2005 Posts: 2,499
|
if isFirstBar means isFirstBar (Returns true if calculating for the first bar of the calculation) confusing to me. what is it ?
Is it possible for you to explain in order for me to understand how this works.
The plot or condition is called by StockFinder over and over, once for each bar of the symbol. So isFirstBar and isLastBar is a way of detecting the first and last call of the routine for a particular symbol.
isFirstbar is typically when you want to reset and static variables in order to start with a clean slate for the symbol.
isLastBar is that last bar on the chart and from a trader's perspective the lastest information and is typically used to present a label.
When trying to improve performance one can also use isFirstBar and isLastBar to measure the performance, ie. the time taken to process all bars.
1. Store the time at isFirstbar and compute the time difference at isLastBar and us loginfo to display the elapsed time.
2. Make some code adjustments and time again.
|
|
Guest-1 |