TL;DR: please update your code with the new version above. Below is the story of why.
I believe I introduced a mathematical error into this via copy paste and not thinking.
When I ran it as originally posted, and at the default length, it looked passably correct. When I adjusted the length to 30 I noticed that the indicator line maxed out at 70. This was strange. Turns out it was maxed at 88 with the default length of 12.
It's a simple order of operations error, a lack of parentheses, and the difference between 0 and 1 indexed lists.
Code:
def w = (100/length – 1) * Rank;
should have been
Code:
def w = (100/(length – 1)) * Rank;
That gets us the canonical formula for the Worden Stochastic.
HOWEVER I used a 1 indexed list of positions not a zero indexed. If you think abuout that for a minute, you'll see this with a length of 26 and the highest value candle (number 26):
Code:
w = ( 100 / ( length - 1 ) ) * rank
w = ( 100 / ( 26 - 1 ) ) * 26
w = ( 100 / ( 25 ) ) * 26
w = 4 * 26
w = 104
Not Good.
So we actually, for this implementation, need to replace the original code with:
Code:
def w = ( 100 / ( length - 1 ) ) * ( Rank - 1 );
And then our maths should all work out for an indicator that will go from 0 to 100. If we had just used a zero indexed list it might have been simpler, but that's life. But since code isn't life, we'll change to a zero-indexed ranking list and save ourselves a little bit of processor cycling for the extra subtraction:
Code:
def rank = fold i=1 to length with rankcounter=0 do if close>GetValue(close,i) then rankcounter+1 else rankcounter;
If your strategy for this used crossovers, nothing really changed. but if you relied on OS and OB levels, it's a different story.
For all of you who made it this far through my pre-coffee rambling, thank you. This one actually kept me awake for a while puzzling it out.
Happy Trading,
mashume