logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Japple55  
#1 Posted : Thursday, July 17, 2008 1:57:05 PM(UTC)
Japple55

Rank: Newbie

Groups: Registered, Registered Users, Subscribers
Joined: 5/27/2007(UTC)
Posts: 6

I'm testing the Zweig 4% with Patrick's Latch on weekly data (HLC only) in version 7.2 show below. Although I have the tester set to trade on the CLOSE, it seems to be trading on the LOW price. Any thoughts on how to force this to trade on the CLOSE? Thanks in advance.

ENTER LONG :
a:=If(PREV=1,If(CLOSE<HighestSince(1,PREV<>1,CLOSE)*.96,-1,PREV),If(PREV=-1,If(CLOSE>LowestSince(1,PREV<>-1,CLOSE)*1.04,+1,PREV),If(PREV=0,If(CLOSE>Lowest(CLOSE)*1.04,+1,If(CLOSE<Highest(CLOSE)*.96,-1,PREV)),PREV)));
D:=If(A=1,1,0);
LE:=D=1;
SE:=D=0;
LX:=0;
SX:=0;
B:= ExtFml("ForumDll.Latch",LE,LX,SE,SX);
B = 1 AND Ref(B,-1) <> 1

EXIT LONG :
a:=If(PREV=1,If(CLOSE<HighestSince(1,PREV<>1,CLOSE)*.96,-1,PREV),If(PREV=-1,If(CLOSE>LowestSince(1,PREV<>-1,CLOSE)*1.04,+1,PREV),If(PREV=0,If(CLOSE>Lowest(CLOSE)*1.04,+1,If(CLOSE<Highest(CLOSE)*.96,-1,PREV)),PREV)));
D:=If(A=1,1,0);
LE:=D=1;
SE:=D=0;
LX:=0;
SX:=0;
B:= ExtFml("ForumDll.Latch",LE,LX,SE,SX);
B = 0 AND Ref(B,-1) = 1

wabbit  
#2 Posted : Friday, July 18, 2008 6:59:52 AM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Hi Japple,

Without getting into the EST just yet, let's have a look at what you are trying to achieve with your codes.

The first thing to notice is the abundance of PREVs in the code. PREV is a recursive function and is extremely processor intensive and can slow your entire computer to a crawl, so its a really good idea to limit these functions to the absolute minimum required.

In your function you do something like:

Code:

If(PREV=1,
{then do something here to see if we need to change the value of the function},
PREV)

e.g.
If(PREV=1, x, PREV);


As PREV is already 1 (we know this because that's how we ended up in the If() in the first place) then you can save a PREV by replacing the second instance with the first value:

Code:


If(PREV=1,

{then do something here to see if we need to change the value of the function},

{we don't change the value, which is} 1)

e.g.
If(PREV=1, x, 1);


Another point I would suggest is to maximise the use of variables as these will make code debugging and maintenance easier.

My last suggestion is to start logic problems, logically! Your trade latch system starts of by looking to see if we are already in a long trade, but it makes more sense (to me) to start the process with no trades open and go from there. Each has their own personal preferences, but try these ideas out, you might like them?!

As for the latching...

Your "a" variable is already the latch and does not require the use of the external function. As you have discovered for yourself, the generic forum.latch does not do entry price based exits so you are stuck with using PREV based latches or having custom codes written for you.

Correct me if I am wrong, but after rejigging the entire code section, I end up with something more "simple":

Code:

d:= 4 / 100;

LE:=C > Lowest(C)*(1+d);
SE:=C < Highest(C)*(1-d);

tr:=
{1:}If(PREV=0, LE-SE,
{2:}If(PREV=1, If(C<HighestSince(1,PREV<>1,C)*(1-d),-1,1),
{3:}If(C>LowestSince(1,PREV<>-1,C)*(1+d),1,-1)));

{plot in new inner window}
tr;


The trade latch reads:
{1:} If no trades are open PREV=0, then look for a trade entry. If LE=1 and SE=0, then we go long (LE-SE=1), otherwise, if LE=0 and SE=1 then we go short (LE-SE=-1) otherwise if LE and SE are both zero or both one, then we do nothing and sit aside.
{2:} PREV=1 so we are already in a long trade, so check for the exit condition that swing to the short trade. If this condition is triggered, go short, otherwise stay in the long trade.
{3:} As we have arrived here, PREV is not zero and it is not one, so in our latch which can only have three values, PREV=-1 so we are in a short trade. Check the exit condition that will swing the trade to the long side. If this event is triggered, go long, otherwise stay in the short trade.

To employ this in the EST, just set the entry and exit triggers using codes like:

Code:


{for buy}
tr=1

{for sell}
tr<>1

{short}
tr=-1

{buy to cover}
tr<>-1;


If this is all correct, then we can turn our attention to the EST and its pricing of trades?


Hope this helps

wabbit [:D]


[edit]
P.S. Roy Larsen wrote a very good tutorial available from http://forum.equis.com/files/3015/metastock_files/entry19675.aspx
Japple55  
#3 Posted : Thursday, July 24, 2008 7:52:13 AM(UTC)
Japple55

Rank: Newbie

Groups: Registered, Registered Users, Subscribers
Joined: 5/27/2007(UTC)
Posts: 6

Wow, thanks Wabbit. Admittedly, I'm a novice with MetaStock code and just pulled this off the forum. Your explanation is very clear and I will study it. I believe I read the tutorial from Roy Larsen, but I will review it again. Thanks again for your help!!!

Jim

Japple55  
#4 Posted : Sunday, August 3, 2008 12:55:38 PM(UTC)
Japple55

Rank: Newbie

Groups: Registered, Registered Users, Subscribers
Joined: 5/27/2007(UTC)
Posts: 6

Hi Wabbit,

I've been testing your code and trying to reference an external security on which to base the signals using the code below. I keep getting the following error yet I've checked and double-checked and the data is all there. I tried testing both daily and weekly periodicity and get the same result. I then tested a simple mov avg crossover system using the same data settings and all went fine, so I'm guessing the Security() function doesn't like something in your code but I can't figure it out. I couldn't find any reference to this error anywhere in the forum. Any ideas?

Thanks,

Jim

No data exists for requested date range in Security() function. Symbol: "VLE-I" Range: 9/2/1988 to 9/2/1988

{Buy Signal}

d:= opt1 / 100;

CF:= Security("C:\Data\MSDATA\FT\INDEXFAM\VLE-I",Close);

LE:=CF > Lowest(CF)*(1+d);
SE:=CF < Highest(CF)*(1-d);

tr:=
{1:}If(PREV=0, LE-SE,
{2:}If(PREV=1, If(CF<HighestSince(1,PREV<>1,CF)*(1-d),-1,1),
{3:}If(CF>LowestSince(1,PREV<>-1,CF)*(1+d),1,-1)));

{plot in new inner window}
tr<>1;

Users browsing this topic
Guest (Hidden)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.