Rank: Advanced Member
Groups: Registered, Registered Users, Subscribers Joined: 7/25/2005(UTC) Posts: 1,042
Was thanked: 57 time(s) in 54 post(s)
|
lcl
It might help if you take a little time to understand the concept of a non-PREV latch before you move to using the latch in Patrick's Forum DLL. Here are some examples of a simple latch (as opposed to the PREV latch that you asked the question about). This latch uses BarsSince() to test whether the most recent event was a "buy" or a "sell". If the most recent even was a buy then the latch is "on" or TRUE, and if the most recent event was a sell then the latch is "off" or FALSE. See example 1.
{example 1}
N:=H>Ref(HHV(H,20),-1);
X:=L<Ref(LLV(L,10),-1);
TurtleBuy:=If(BarsSince(N)<BarsSince(X),1,0);
TurtleBuy;
If you test example 1 against your PREV latch you'll see that it can miss the first buy signal, and it will certainly plot N/A for more bars than the PREV version. This can be remedied by adding an "initialisation" variable that simply generates a fake buy and sell signal on the first bar that both the buy and sell signals are valid. These fake signals allow each BarsSince() function to begin a valid plot (instead of N/A), and therefore the resulting plot from TurtleBuy is also valid as soon as both the buy and sell signals are valid. See example 2.
{example 2}
N:=H>Ref(HHV(H,20),-1);
X:=L<Ref(LLV(L,10),-1);
I:=Cum(IsDefined(N AND X))=1;
TurtleBuy:=If(BarsSince(N OR I)<BarsSince(X OR I),1,0);
TurtleBuy;
The IsDefined() function offers nothing of particular value and the initialation variable can easily be constructed with just a Cum() function - see example 3.
{example 3}
N:=H>Ref(HHV(H,20),-1);
X:=L<Ref(LLV(L,10),-1);
I:=Cum(N+X>-1)=1;
TurtleBuy:=If(BarsSince(N OR I)<BarsSince(X OR I),1,0);
TurtleBuy;
You don't need to use the If() function to generate TRUE and FALSE states - a simple comparison does the trick as the If() function is redundant. If() is useful if you want to assign constant values other than ONE and ZERO to the latch result. Also notice how the "+" operator can be used instead of the logical "OR" operator. This is not mandatory but it can be a useful space-saving technique. See example 4.
{example 4}
N:=H>Ref(HHV(H,20),-1);
X:=L<Ref(LLV(L,10),-1);
I:=Cum(N+X>-1)=1;
TurtleBuy:=BarsSince(N+I)<BarsSince(X+I);
TurtleBuy;
You do not need to use PREV to construct a latch where the buy and sell signals (set and reset if you like) are independant of each other. Patrick's DLL-based latch function is an excellent substitute for the simple-latch exercises shown above. However, when a sell is based of some aspect of the buy (timing, price, indicator value, etc.) you do generally need to use a PREV-based latch to get the desired result. That's another story.
Roy
MetaStock Tips & Tools
|