# MetaStock Value When Function

Provided By www.meta-formula.com

The ValueWhen function returns the value of the selected `data array' when a defined `expression' is true on the `N'th most recent occurrence. That is to say, it allows you to specify which data array value to display (e.g. closing price, the RSI, another formula etc.) at the time a given expression occurred. Furthermore, if the expression has occurred more than once, you can also select which occurrence to use (e.g. the third most recent occurrence).

SYNTAX ValueWhen(Nth, Expression, Data Array)

Nth _ Is the number of occurrences, of the `expression', you wish to refer back when obtaining the value of the selected data array.

Expression _ The technical condition that you are going back through the data to search for and the `N' will dictate how many occurrences you count back.

Data Array _ On the period that `Expression' is found `N' times ago, it is the value of this data array that is returned.

EXAMPLE

The following formula identifies the last occasion that an 8 period simple moving average crossed with a 21 period simple moving average, and then took the closing value for that period:

ValueWhen(1,Cross(Mov(C,8,S),Mov(C,21,S)),C)

In the above example:

Nth = 1

Expression = Cross(Mov(C,8,S),Mov(C,21,S))

Data Array = C

APPLICATION

A more useful application of this example could be:

C>ValueWhen(1,Cross(Mov(C,8,S),Mov(C,21,S)),C)*1.03

This formula identifies the last occasion that an 8 period simple moving average crossed with a 21 period simple moving average and then took the closing value for that period. It added 3% to that value (denoted by `*1.03') and then searched for when the closing price closed above this new value (denoted by the `C>').

Looking at Figure 3.22, we can see this example applied to the chart below as an indicator.

EXERCISES

Construct formulas for the following:

1. Return the value of the low on the most recent occasion that the close was at least 2% lower than the open for the period:

__________________________________________________

2. Return the value of the close on the most recent period that the 10 period simple moving average of the closing price crossed above the 30 period simple moving average of the closing price:

__________________________________________________