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

Notification

Icon
Error

Options
Go to last post Go to first unread
wabbit  
#1 Posted : Monday, July 25, 2005 3:16:07 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)
Just to summarise what already has been posted by Patrick, and perhaps extend it slightly..... Downloading the file To download the file: [list=1:3d1d1c095b] [*:3d1d1c095b]You have to be a registered member of the Equis community [*:3d1d1c095b]Download the file, "ForumDll.[censored]" (current version available in this forum) to the MetaStock ExternalFunctionDll folder, most commonly found on your computer at C:\\Program Files\\Equis\\ExternalFunctionDll [*:3d1d1c095b]Rename the downloaded file (ForumDll.[censored]) ForumDll.dll [*:3d1d1c095b]If MetaStock is running, close it and re-open it [/list:o:3d1d1c095b] The file should now be able to be called from within MetaStock. Using the file Patrick has given us examples of how to use the latch for different purposes: ENTER LONG : LE:={your Condition here}; LX:={your Condition here}; SE:={your Condition here}; SX:={your Condition here}; B:= ExtFml("ForumDll.Latch",LE,LX,SE,SX); B = 1 AND Ref(B,-1) <> 1 ENTER SHORT : LE:={your Condition here}; LX:={your Condition here}; SE:={your Condition here}; SX:={your Condition here}; B:= ExtFml("ForumDll.Latch",LE,LX,SE,SX); B = -1 AND Ref(B,-1) <> -1 EXIT LONG : LE:={your Condition here}; LX:={your Condition here}; SE:={your Condition here}; SX:={your Condition here}; B:= ExtFml("ForumDll.Latch",LE,LX,SE,SX); B = 0 AND Ref(B,-1) = 1 EXIT SHORT : LE:={your Condition here}; LX:={your Condition here}; SE:={your Condition here}; SX:={your Condition here}; B:= ExtFml("ForumDll.Latch",LE,LX,SE,SX); B = 0 AND Ref(B,-1) = -1 Long Trades ONLY If you are interested in long trades only and do not short stocks, then you do not need to include the SE and SX variable declarations, but must replace the SE and SX passing variables with zero. ENTER LONG : LE:={your Condition here}; LX:={your Condition here}; B:= ExtFml("ForumDll.Latch",LE,LX,0,0); B = 1 AND Ref(B,-1) <> 1 EXIT LONG : LE:={your Condition here}; LX:={your Condition here}; B:= ExtFml("ForumDll.Latch",LE,LX,0,0); B = 0 AND Ref(B,-1) = 1 Short Trades ONLY If you are only interested in short trades, then you do not need to include the LE and LX variable declarations, but must replace the LE and LX passing variables with zero. ENTER SHORT : SE:={your Condition here}; SX:={your Condition here}; B:= ExtFml("ForumDll.Latch",0,0,SE,SX); B = -1 AND Ref(B,-1) <> -1 EXIT SHORT : SE:={your Condition here}; SX:={your Condition here}; B:= ExtFml("ForumDll.Latch",0,0,SE,SX); B = 0 AND Ref(B,-1) = -1 So how do I know when I am in a trade? or Putting this latch-thingy to work! If we only look at long trades for the moment... Lets say you have developed your system, with defined entry and exit criteria, like a moving average crossover system. Your entries are when the fast MA crosses above the slow MA, and exits when the reverse happens, coded as: fastMA:=Mov(C,7,S); slowMA:=Mov(C,21,S); LE:=Cross(fastMA,slowMA); LX:=Cross(slowMA,fastMA); I have changed the 'B' variable (below) to 'Z' to avoid getting confused with buy and sell signals.... Now the criteria has been defined, we call the external formula to only trigger a BUY signal if (and only if) a buy signal has not been previously set without a sell signal, or the last signal was a sell signal. Read both cases as, we arent already in a long trade. The sell signal will only set if we are in a long trade. Z:= ExtFml("ForumDll.Latch",LE,LX,0,0); EnterLong:=Z = 1 AND Ref(Z,-1) <> 1; ExitLong:=Z = 0 AND Ref(Z,-1) = 1; We now want to plot these on a chart. One of the easiest ways is to just plot: EnterLong; ExitLong; If we plot this on a chart, the indicator value be zero, except when a buy or sell signal is generated and the value will be one. (Its handy to plot this as a histogram) But how do we tell the two signals a part? We could just keep track of which signal occurred when, but it might get a little confusing, so try plotting this instead: EnterLong-ExitLong; This indicator line will be +1 when a buy signal is generated and -1 when a sell signal is generated. You can now tell at a glance which signal is which. Long Trade Code Summary: fastMA:=Mov(C,7,S); slowMA:=Mov(C,21,S); LE:=Cross(fastMA,slowMA); LX:=Cross(slowMA,fastMA); Z:= ExtFml("ForumDll.Latch",LE,LX,0,0); EnterLong:=Z = 1 AND Ref(Z,-1) <> 1; ExitLong:=Z = 0 AND Ref(Z,-1) = 1; EnterLong-ExitLong; You can follow exactly the same process for short trading. I hope this helps people use this new functionality. Remember if you like the latch system, post your thanks to Patrick for his excellent efforts here. He did this off his own back and in his own time, so we offer him our gratitude. Hope this helps. wabbit :D P.S. More applications of the latch at a slightly higher level will follow...
wabbit  
#2 Posted : Monday, July 25, 2005 3:17:00 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)
[size=18:1744a5785b]More Advanced Problems[/size:1744a5785b] This latch and the BarsSince problem The problem still exists if you want to show an indicator with a value whilst in a trade and zero when not in a trade. It isnt a problem in the latch external dll itself, its a problem with MS and the BarsSince() function. This issue has been dealt with before on the forum, and Patrick explains the problem and how to overcome it in the fourth training video. Based on the 'discovery' by Roy Larsen, we have to initialise the system to trigger on the first trades. I used the code below: fastMA:=Mov(C,7,S); slowMA:=Mov(C,21,S); LE:=Cross(fastMA,slowMA); LX:=Cross(slowMA,fastMA); Z:= ExtFml("ForumDll.Latch",LE,LX,0,0); NL:=Z=1 AND Ref(Z,-1)<>1; XL:=Z=0 AND Ref(Z,-1)=1; LongTrade:=BarsSince(NL OR (Cum(NL>-1)=1))<BarsSince(XL OR Cum(XL>-1)=1); LongTrade; This code will return a value of one when in a long trade and zero when out. Of course, the whole purpose of this latch is not to have to deal these issues any more. The work that Patrick has done alleviates the need for all this unnecessary code and when the same reult is achieved with the new, extremely simple way to do the same thing: fastMA:=Mov(C,7,S); slowMA:=Mov(C,21,S); LE:=Cross(fastMA,slowMA); LX:=Cross(slowMA,fastMA); ExtFml("ForumDll.Latch",LE,LX,0,0); We can see now the power in Patrick's solution... and again thank him for his efforts. wabbit :D
Patrick  
#3 Posted : Monday, July 25, 2005 3:23:38 AM(UTC)
Patrick

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 9/8/2004(UTC)
Posts: 2,266

Was thanked: 1 time(s) in 1 post(s)
=D> =D> =D> Great Job I was too lazy to do it today, but now that I see how good of an explanation you gave I'm quite happy I did not do it :D Seriously Thank You. People if you have a question after reading this ... Well don't ... Read the thread again 8:-) Patrick :mrgreen:
wabbit  
#4 Posted : Monday, July 25, 2005 3:49:31 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)
Going Long and Short This latch system, as I have previously raved about is exceptionally powerful, but it does have some catches that we wll have to learn about as we get used to the new system.... more later For now, lets have a look at the situation when once the trading system starts you will always be in a trade (some people call this a stop and reverse system?) I will continue to use the MA crossover system as described above, just for continuity, despite the fact this is not at all profitable! In this system you will either be long OR short, never out and never both. This means the long exit criteria is the same as the short entry criteria, and the long entry is the signal to exit shorts. fastMA:=Mov(C,7,S); slowMA:=Mov(C,21,S); LE:=Cross(fastMA,slowMA); LX:=Cross(slowMA,fastMA); SE:=LX; SX:=LE; ExtFml("ForumDll.Latch",LE,LX,SE,SX); This indicator will read zero until the first entry criteria is triggered (long or short). From there the indicator will return +1 for long trades and -1 for short trades. Remember the struggles we used to have trying to latch all these together? Isn't it so simple now?! Problems Encountered What if we have different criteria for LE and SX, LX and SE? It is now possible to have long and short positions open at the same time, or no positions open. How does the latch external dll handle this situation? Curious things (to me anyway) happen... [list:545a3b67ef][*:545a3b67ef]If you are long when a short entry is triggered, the trade will go short. [*:545a3b67ef]If you are short when a long entry is triggered, the trade will go long. [*:545a3b67ef]If you are long when a short exit is triggered, the trade will exit. [*:545a3b67ef]If you are short when a long exit is triggered, the trade will exit.[/list:u:545a3b67ef] I think that all of the combinations that might cause some confusion? To see all this in action: MA1:=Mov(C,7,S); MA2:=Mov(C,21,S); MA3:=Mov(C,13,S); MA4:=Mov(C,15,S); LE:=Cross(MA1,MA2); LX:=Cross(MA2,MA1); SE:=Cross(MA3,MA4); SX:=Cross(MA4,MA3); Long:=ExtFml("ForumDll.Latch",LE,LX,0,0); Short:=ExtFml("ForumDll.Latch",0,0,SE,SX); Combo:=ExtFml("ForumDll.Latch",LE,LX,SE,SX); Long; Short; Combo; I guess if you were to be develping such a system, that allowd long and/or short and/or no open trades, then it would be prudent to code the Long and Short sections separately, combining them might lead to unexpected (and therefore negative) results! Hope this helps. wabbit :D
wabbit  
#5 Posted : Monday, July 25, 2005 4:01:49 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)
Same day signals What happens when the long and short entry signals happen on the same day? Who gets priority? MA1:=Mov(C,7,S); MA2:=Mov(C,21,S); LE:=Cross(MA1,MA2); LX:=Cross(MA2,MA1); SE:=LE; SX:=LX; Long:=ExtFml("ForumDll.Latch",LE,LX,0,0); Short:=ExtFml("ForumDll.Latch",0,0,SE,SX); Combo:=ExtFml("ForumDll.Latch",LE,LX,SE,SX); Long; Short; Combo; shows us that long trades are 'preferred' over short trades when both entry signals are triggered on the same day. Entry and Exit signals on the same day I found this one hard to test because none of my normal codes have this ability (dunno why, they just don't??) so I have kept the same code theme running from the previous posts... MA1:=Mov(C,7,S); MA2:=Mov(C,21,S); LE:=Cross(MA1,MA2); LX:=Cross(MA1,MA2); SE:=LE; SX:=LX; Long:=ExtFml("ForumDll.Latch",LE,LX,0,0); Short:=ExtFml("ForumDll.Latch",0,0,SE,SX); Combo:=ExtFml("ForumDll.Latch",LE,LX,SE,SX); Long; Short; Combo; shows that when the entry signal and exit signal both occur on the same the day, the trade is entered but not exited. This might hold some cause for concern... so just be careful. Anyway, I think thats about it for my testing here... I hope you find some great uses for Patrick's work, I know I like it already! wabbit :D P.S. All my testing was completed on daily charts - timeframes (periodicity) shouldn't make any difference? P.P.S. If you find any problems, or other ways to exploit this new functionality, please post it so we can all benefit.
Jose  
#6 Posted : Tuesday, July 26, 2005 7:42:27 AM(UTC)
Jose

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 1/19/2005(UTC)
Posts: 1,065
Location: Koh Pha-Ngan, Earth

Was thanked: 2 time(s) in 2 post(s)
wabbit wrote:
...the BarsSince problem. ...Same day signals.
The MS indicator code below takes care of both issues raised above: MetaStock -> Tools -> Indicator Builder -> New -> -> Copy and paste complete formulae between "---8<---" lines. [code:1:f8b7f66719] ==================== System trade signals ==================== ---8<-------------------------- { System trade (latch) signals v2.5 } { Copyright (c)2005 Jose Silva } { For personal use only } { http://www.metastocktools.com } { Note on simultaneous entry/exit on same bar: #1 Ignore: entry/exit signals cancel each other; #2 Preference given to: new entry if currently Short, new exit if currently Long.} { Signals reference example } entry:=C>Mov(C,21,E); exit:=C<Mov(C,10,E); { User inputs } plot:=Input("Signals: [1]Clean, [2]All, [3]Trade binary",1,3,1); choose:=Input("Simultaneous entry/exit: [1]Ignore, [2]Preference",1,2,2); delay:=Input("Entry and Exit delay",-1,5,0); { Initialize } Init:=Cum(IsDefined(entry+exit))=1; { #1 - Ignore entry/exit on same bar } bin1:=ValueWhen(1,entry-exit<>0 OR Init, entry-exit); long:=bin1=1 AND (Alert(bin1<>1,2) OR Init); short:=bin1=-1 AND (Alert(bin1<>-1,2) OR Init); signals1:=long-short; { #2 - Preference to first entry/exit } long:=entry AND (Alert(entry=0,2) OR Init); short:=exit AND (Alert(exit=0,2) OR Init); bin2:=ValueWhen(1,long-short<>0 OR Init, long-short); long:=bin2=1 AND (Alert(bin2<1,2) OR Init); short:=bin2=-1 AND (Alert(bin2>-1,2) OR Init); signals2:=long-short; { Select #1 or #2 } binary:=If(choose=1,bin1,bin2); signals:=If(choose=1,signals1,signals2); { Plot in own window } Ref(If(plot=2,entry,0),-delay); Ref(If(plot=1,signals, If(plot=2,-exit,binary)),-delay) ---8<-------------------------- =============================== System trade signals - original =============================== ---8<-------------------------- { Original trade signals code } { BarsSince() function prevents first exit plot} { With thanks to Roy Larsen at http://www.metastocktips.co.nz } { Signals reference example } entry:=C>Mov(C,21,E); exit:=C<Mov(C,10,E); { User inputs } plot:=Input("Signals: [1]Clean, [2]All, [3]Trade binary",1,3,1); delay:=Input("Entry and Exit delay",-1,5,0); { Clean signals } Init:=Cum(entry+exit>-1)=1; entryInit:=Cum(entry)=1; flag:=BarsSince(Init OR entry) <BarsSince(Init OR exit)+entryInit; signals:=(entryInit AND Alert(entryInit=0,2) OR flag AND Alert(flag=0,2)) -(flag=0 AND Alert(flag,2)); binary:=ValueWhen(1,signals<>0,signals); { Plot in own window } Ref(If(plot=1,signals, If(plot=2,entry-exit,binary)),-delay) ---8<-------------------------- [/code:1:f8b7f66719] jose '-) http://www.metastocktools.com
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.