Rank: Newbie
Groups: Registered, Registered Users Joined: 1/29/2006(UTC) Posts: 2
|
Is there a way for an indicator to reference previous values other than "yesterday's" ?
Suppose it wants to reference its value 7 periods ago.
Or suppose it want's to know its "value when" (some condition was true).
Is any of this possible?
Thanks
|
|
|
|
Rank: Advanced Member
Groups: Registered, Registered Users, Subscribers Joined: 7/25/2005(UTC) Posts: 1,042
Was thanked: 57 time(s) in 54 post(s)
|
Lee
Here are a few legitimate ways you can use PREV. Obviously the supporting code must be constructed properly, but the essential point is that "PREV" can be used to point to other bars besides the immediate past bar.
ValueWhen(1,1,PREV) - this is essentially the same as PREV.
ValueWhen(7,1,PREV) - this points to 7 bars previously.
ValueWhen(1,PREV=0,C) - this points to the CLOSE of the bar following that on which the current variable last equal to zero.
ValueWhen(1,Ref(PREV,-1)=0,C) - this points to the CLOSE of the bar when the current variable was last equal to zero ( the CLOSE prior to "ValueWhen(1,PREV=0,C)" ).
There are more possibilities but hopefully you get the idea.
Never confuse PREV with Ref() - they are entirely different animals. Ref() allows you to access the past, present or future value of a variable that has already been defined (in the flow sequence of the current formula). PREV, on the other hand, allows you to access the previous value of the variable currently in the process of being defined. PREV does not need defining as the variable it is used in provides that definition.
When using Ref(PREV,-1) all you are doing is using PREV to access the value of the current variable two bars prior rather than one bar prior.
Roy
MetaStock Tips & Tools
|
|
|
|
Rank: Newbie
Groups: Registered, Registered Users Joined: 1/29/2006(UTC) Posts: 2
|
Roy,
Wow! That was eye opening. I guess I just never thought of PREV as a "data array" before.
BUT (and don't interpret my total confusion here as lack of appreciation) consider the following statement:
valuewhen(1,high>prev,low)
I mean, consider it as an entire indicator. To me it says, "go back and find the last (most recent) day when the high (of that day) was greater than the value of the indicator the day before (greater than the previous value of the indicator). Then, let today's value of the indicator be equal to the low of that day".
OK, so there would be some bootstrapping going on (each day's value would be dependent on some previous value, but this is also true in the calculation of an exponential moving average), and maybe it's dangerous to assume that the indicator has an initial value of zero (ie: that it's defined, so that metastock can actually find an initial day when high > prev).
In any event, when I drop this indicator on a price plot, it skips the first 4 or 5 months before plotting the first value, and then it plots this: if(high>prev,low,0). By this I mean, if I change the "valuewhen()" statement to the given "if()" statement, I don't change the plot, other than filliing it in for the 4 or 5 months that had not initially been plotted.
What am I missing?
Thanks in advance.
Lee
PS I know my indicator is meaningless. I just stripped away everything that wasn't part of the problem for clarity.
|
|
|
|
Rank: Advanced Member
Groups: Registered, Registered Users, Subscribers Joined: 7/25/2005(UTC) Posts: 1,042
Was thanked: 57 time(s) in 54 post(s)
|
Hi Lee
I'm not going to explain exactly what's going on with this, but I can point out the attributes of PREV and ValueWhen() that I think contribute to the result you see.
The first bar of a PREV variable is N/A. This is because it needs at least the current bar and one previous bar to plot a valid value.
It is not unusual for a PREV variable to start from zero by default when there is no other seeding value available. It's also possible to construct a (flawed) PREV expression so that it cannot seed itself and therefore cannot plot anything.
ValueWhen() plots an N/A until it receives a valid "trigger" (the "expression" parameter), assuming the data-array parameter is valid at that time.
ValueWhen() cannot plot when either the expression or data-array parameters are invalid (N/A).
ValueWhen() will eventually plot a zero if it is triggered with a valid expression parameter while the data-array parameter is still invalid (assuming no subsequent triggers have been received). Best practice is to ensure that the data-array is valid before the expression parameter goes active (triggers a new ValueWhen() plot). Otherwise a chart-distorting zero may plot when the expression becomes valid.
In the following two EMA examples (constructed using PREV), notice how the unseeded (first) formula is actually seeded with zero. This is easily proved even though the first value plotted is somewhat higher than zero.
{EMA example 1}
A:=10;
R:=2/(A+1);
PREV*(1-R)+C*R; {unseeded}
{EMA example 2}
A:=10;
R:=2/(A+1);
If(Cum(1)=1,C,PREV*(1-R)+C*R); {seeded with C}
It does not surprise me that mixing these factors together produce the result you see. The expression you've got is obviously not doing the job so it's time to rethink what you're trying to do and modify your approach accordingly.
Roy
MetaStock Tips & Tools
|
|
|
|
Users browsing this topic |
Guest (Hidden)
|
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.