[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How might this be done in TS?



PureBytes Links

Trading Reference Links

> Metastock has a function called "valuewhen" the syntax is:
> valuewhen(Nth, EXPRESSION, DATA ARRAY)
> Example: valuewhen(2,average(c,10) = C,rsi(c,20));
> would return the value of RSI on the 2nd most recent occurrence of 
> close equal to the 10 bar moving average. 

Bill Vedder pointed out the best solution:  using MRO.  Then you 
just have to extend MRO to the complete ValueWhen functionality, 
by using MRO's return value to look back in the variable history 
to when the event happened.

Try this.  I replaced your "average(c,10) = C" with a "crossed 
over" test, since the average will almost never exactly equal the 
close.  Plot 0 and C-average(C,10) in another indicator so you 
can see when the "C crosses over average(C,10)" condition is 
true, and you'll see that ValueWhen matches the RSI value as of 
Nth crosses ago.

The MRO only looks for the condition in the last Lookback bars, 
so if you expect your condition to happen very seldom, you might 
want to increase Lookback.

Gary


inputs: Nth(2), Lookback(50);
vars: BarsBack(0), ValueWhen(0);

BarsBack = MRO(C crosses over Average(C,10), Lookback, Nth);
if BarsBack >= 0 then ValueWhen = RSI(C,20)[BarsBack];

plot1(ValueWhen, "ValWhen");
plot2(RSI(C,20), "RSI");