The MetaStock reference function is one of the most commonly used functions, as it allows us to refer to the value of any expression, or the selected ‘data array’, any number of periods back or subsequent periods into the future.

It’s important to note that if you are using the reference function to refer to a period into the future it is obviously dynamic, meaning that it can change. Therefore be careful when using it within system tests, since it can create deceptively good results.

MetaStock Syntax: Ref(Data Array, Periods)

Data Array = The value of the data array that will be returned from ‘x’ number of periods ago.

Periods = This dictates how many periods forward or back to reference the value of the data array from. A negative number will refer to a value in the past, whereas a positive number will refer to a value in the future.

Here’s how it would look in an example. The following formula refers to the closing price yesterday:

Ref(C,-1)

In the MetaStock formula above:

Data Array = C

Periods = -1

Here’s how you would use the formula in a more useful application of this example:

C>Ref(C,-1) AND Ref(C,-1)> Ref(C,-2)

This MetaStock formula specifies that the closing price from the current period must be above the closing price from the previous period; and the closing price from the previous period must be greater than the closing price from the period before that. In this example, we have used the closing price as the data array, however you can use almost any function or indicator.

Another common application of the reference function is to accurately describe peaks and troughs, without using the peak and trough functions within MetaStock. A simple definition of a peak is a bar that has had 2 previous periods with lower highs and 2 subsequent periods with lower highs. Using the reference function, we can accurately code this as follows:

H>Ref(H,-1) AND Ref(H,-1)>Ref(H,-2) AND H>Ref(H,1) AND Ref(H,1)>Ref(H,2)

Note that since we are using a positive application of the Reference function (denoted by ‘H>REF(H,1)’), a signal will only appear after the event has passed. In other words, you only know a peak has occurred after it has happened.

If you are new to MetaStock formula then this might seem complex or confusing but once you ‘get it’ you’ll understand why it’s used by so many professional traders.