Hi HG
PREV refers to the value of the variable currently being defined as on the previous bar. It makes no difference to the calculation whether the variable is named or unnamed.
I like to think of MetaStock as purely sequencial language - everything happens in a very orderly fashion. An indicator executes each variable in sequence on bar one, and then repeats the process on bar two and so on. If the same code was being used in all columns of an exploration processing would start in columb B after completing bar one of column A, and so on. When that security was scanned completely the whole process would begin again with the next security, line 1, bar 1 column A. Maybe it doesn't work this way but believing that it does has helped me solve a number of thorny problems over the year, like why some practices are acceptable and others are not.
The one one situation where MetaStock deviates slightly from the normal sequence is when PREV is used. It doesn't permit a jump to a variable out of sequence but it does allow a look back to the previous bars value or result that the current variable generated and use that information to determine a result for the bar (and variable) currently being processed.
You were spot on when you wrote the original line of code using Ref(Trigger,1), except that Metastock generally does not allow you to use any variable that has not already been defined for the bar currently being processed. PREV does and is exactly what you assumed was true for Ref(Trigger,1), but it's legal and MetaStock fakes the first value (usually with zero as far as I can work out) and then goes on from there. PREV is not a difficult concept so don't get hung up on it - it just returns the previous bars value of the variable currently being defined.
The code of mine that you're referring to is a slightly mangled trailing stop. X defines how far the trail should hang below the current HIGH. I'm changing it around a little so that hopehully it doesn't get mangled. Yep. that looks OK in Preview mode.
X:=H-3*ATR(10);
If(L>PREV,Max(X,PREV),X);
What the second line says is: If L is greater than the previous value of the stop set a new stop level for the higher of X or PREVious value (move the stop up or keep it the same), otherwise {else} reposition the stop downwards to start a new ascent because of L having just breached the stop.
Plot this code as a ew indicator on a chart that's trenind upwards quite smoothly and you'll quickly see how it works. Plot it with the result of the second line delayed by one bar and you'll see very clearly where the stop is breached and it's forced to reposition at a lower price.
X:=H-3*ATR(10);
Y:=If(L>PREV,Max(X,PREV),X);
Ref(Y,-1);
So in this simple trailing stop PREV is first being used to decide if the stop has been breached or not, then if the stop has not been breached PREV is used to maintain at least the exiting price level. With nested If() functions we could make a number of other deciions if we wanted to.
One problem that shows up with this type of stop construction, where the X variable refers to the current bar rather than the previous bar, (it isn't delayed) is that a large sudden rise will trigger the stop. Delaying X prevents this rather unusual but quite possible problem.
Hope this helps.
Roy