Ok, I have a work around...(Courtesy of the code-boffins 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,Tp-Tn,Tn-Tp).
This equates to, give me the value of Tn-Tp when Tp-Tn was last true (or Tp-Tn =1).
The only way Tp-Tn 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 Tp-Tn = 1, then Tn-Tp 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(Tp-Tn),Tn-Tp);
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 ...
Edited by user Tuesday, July 26, 2022 12:44:49 PM(UTC)
| Reason: Not specified