Rank: Advanced Member
Groups: Moderators, Registered, Registered Users, Subscribers Joined: 10/8/2010(UTC) Posts: 1,842
Thanks: 79 times Was thanked: 148 time(s) in 144 post(s)

Originally Posted by: MrMcChart Ok, I have a work around...(Courtesy of the codeboffins at Share Wealth Systems)... for the original code that now produces an output as follows:
Hi J,
We're still not sure how MetaStock is computing this.
Let's evaluate each line of the code. Particularly Tn, Tp & Td.
Tn & Tp are both cross functions meaning they can only return a 1 (For True, when the cross happens) or a 0 (for false, no crossover).
Td uses ValueWhen(), which is structured as follows: ValueWhen(n, Expression, Data Array).
...
The ValueWhen function says give me the value of the Data Array on the date the nth most recent occurrence of the Expression was true.
In our example Td := ValueWhen(1,TpTn,TnTp).
This equates to, give me the value of TnTp when TpTn was last true (or TpTn =1).
The only way TpTn can = 1 is if Tp = 1 and Tn = 0. (Remember these are both cross functions and can only return a 1 or 0)
So if TpTn = 1, then TnTp must = 1.
The output will always be 1...
It appears what MetaStock is doing is using an Absolute Value for the expression in the ValueWhen() statement. I can't see why they are doing this. That is fundamentally flawed logic in my opinion. But nevertheless, here is a work around of the code so it produces the same output as MetaStock.
Pd:=Input("ATR Periods",1,100,10);
Factor:=Input("Factor",1,10,2);
Up:=MP()+(Factor*ATR(Pd));
Dn:=MP()(Factor*ATR(Pd));
Tn:= Cross(C,LLV(Up,13));
Tp:= Cross(HHV(Dn,13),C);
Td:=ValueWhen(1,ABS(TpTn),TnTp);
Dxx:=HighestSince(1,Cross(Td,0),Dn);
Dnx:=ValueWhen(1,ABS(Dn=Dxx),Dn);
Upp:=LowestSince(1,Cross(0,Td),Up);
Upx:=ValueWhen(1,ABS(Up=Upp),Up);
ST:=ValueWhen(1,ABS(Td),If(Td=1,Dnx,Upx));
ST;
Kind Regards,
Vincent ...
Hi again, "In our example Td := ValueWhen(1,TpTn,TnTp).
This equates to, give me the value of TnTp when TpTn was last true (or TpTn =1)." I think this is probably where the confusion is. TpTn is not a conditional statement. It's just a simple mathematical operation. Plugging it into ValueWhen({Nth},{Expression},{Data Array}) does not turn it into a conditional statement, so being "True" is not required for the expression. You can make a conditional statement out of this (I'm not sure that is recommended but I haven't really looked at the full logic of the formula) by adding a condition to the expression. TpTn=1 would be a conditional statement. TpTn is simply returning the value of one variable minus the other.
