Rank: Advanced Member
Groups: Moderators, Registered, Registered Users, Subscribers Joined: 10/8/2010(UTC) Posts: 1,960
Thanks: 92 times Was thanked: 155 time(s) in 150 post(s)
|
Originally Posted by: MrMcChart 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 ...
Hi again, "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)." I think this is probably where the confusion is. Tp-Tn 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. Tp-Tn=1 would be a conditional statement. Tp-Tn is simply returning the value of one variable minus the other.
|