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

Re: PREV function



PureBytes Links

Trading Reference Links

Neo,

PREV in Metastock is a hugely resource-intensive way of setting and holding a variable in the
formula language.

Before I give an example and comparison with Ref(), I want to make sure that it's clear how
Metastock and most other programs will run code against any given chart.  When a chart is "loaded"
onto your screen, all of the OHLC values are held in an array as numbers.  Operations on that
array will progress from the lowest array position to the highest.

Visually, this means your indicator or system or any other object you've created in Formula
Language will be evaluated bar by bar, starting from the earliest loaded bar (Bar=1)all the way to
the latest, most recent bar (e.g. Bar=5000).  So it moves from left to right across your chart.

Let say you have an indicator that is usually zero.  When the conditions for this indicator all
line up, it becomes =1, then the next bar it's over and back to =0.  Let say that this happens on
Bar 5 of your chart.   Then your indicator is 0 on Bar 2, 0 on Bar 3, 0 on Bar 4, 1 on Bar 5, 0 on
Bar 6, ....

If you want to store this occurance of Indicator=1 in a variable, you have to use PREV.  Here's
what happens when you use Ref(Signal,-1):
Bar 2 shows Ref()=0, Bar 3 shows Ref()=0, Bar 4 shows Ref()=0, Bar 5 shows Ref()=0, Bar 6 shows
Ref()=1, Bar 7 shows Ref()=0, Bar 8 shows Ref()=0,.....

But you want Bar 7 and Bar 8 etc. to =1 also.

To do that, you'd have to define a new variable and say that if Signal=0, then the new variable is
equal to PREV, and if signal=1, then the new variable is equal to 1.  Run through this logic on
Bars 2 through 8 and you'll see that on Bar 5 the new variable becomes 1, and on Bar 6, since the
signal=0, the new variable is equal to it's previous value, which is 1, and on and on...

This new variable will stay =1 until you somehow "shut it off."

With regards to your point about finding a formula to determine if you are long or short, you can
build on this work above.  Suppose your Signal is your EnterLong conditions.  Your new variable
will stay=1 until you shut it off.  You could shut it off by creating Signal2 which is your
EnterShort signal.  

So now, you have a way to change the value of your new variable.  Let's call your new variable
your TradePosition.

TradePosition:=If(EnterLong=1,1,If(EnterShort=1,-1,PREV),PREV);

Now you can look at your TradePosition and if it =1 then you are long, and if it =-1 you are
short.

(I didn't check the syntax of the above Metastock expression, since I'm not near my program right
now.  I'm doing this all from memory--which ain't so great all the time....<grin>.)

-----WARNING!!!  The PREV function is a huge resource drain.  Try to avoid its use wherever
possible, especially in real time trading systems!!!!!-------------

One workaround that's been discussed on this list is the following:

TradePosition:=If(BarsSince(EnterLong)<=BarsSince(EnterShort),1,-1);

is very similar to the above statement, but misses the first Long or Short trade, whichever
happens first.

------------------------------

Neo, you recently asked about Tradestation and why it is considered the "standard."  In this
example, Tradestation has a built in function called MarketPosition which does everything I've
built up to above, so you can avoid all of the coding and logic, and just use

{EasyLanguage example:  if you are long, apply a Chandelier Exit for a stoploss}

If MarketPosition=1 then ExitLong MaxTradeHigh - (3 * AvgTrueRange(20)) Stop;

all in one statement which runs very quickly on a real time chart.  Compare this with some other
recent posts of Metastock Formula Language solutions to the Chandelier Exit....

Dave Nadeau
Fort Collins, CO

--- neo <neo1@xxxxxxxxx> wrote:
> I have been using MS Pro for over 1 1/2 years and I do not
> understand the PREV function. It is not well explained in either
> the manual or the help file. How does it differ from Ref(X,-1)?
> 
> I am trying to have a formula determine if I am long or short.
> 
> Any help?
> 
> neo
> 


__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/