in Search

Getting and Using ForumDll.xxx Latch Function

Last post 07-26-2005, 7:42 by Jose. 5 replies.
Sort Posts: Previous Next
  •  07-25-2005, 3:16 5127

    Getting and Using ForumDll.xxx Latch Function

    Just to summarise what already has been posted by Patrick, and perhaps extend it slightly.....

    Downloading the file
    To download the file:

    1. You have to be a registered member of the Equis community
    2. Download the file, "ForumDll.xxx" (current version available in this forum) to the MetaStock ExternalFunctionDll folder, most commonly found on your computer at C:\\Program Files\\Equis\\ExternalFunctionDll
    3. Rename the downloaded file (ForumDll.xxx) ForumDll.dll
    4. If MetaStock is running, close it and re-open it

      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...

      "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
      Edsgar W. Dijkstra

       

      MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC C, 11 PRO w/QC & MDK
      For custom MetaStock programming : http://www.wabbit.com.au
      My SkyPE status :
      My SkyPE account : wabbit.com.au

  •  07-25-2005, 3:17 5128 in reply to 5127

    Re: Getting and Using ForumDll.xxx Latch Function

    More Advanced Problems
    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

    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC C, 11 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

  •  07-25-2005, 3:23 5129 in reply to 5127

    Wow - Thanks Wabbit

    =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:
    My Site
  •  07-25-2005, 3:49 5131 in reply to 5127

    Re: Wow - Thanks Wabbit

    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]
  • If you are long when a short entry is triggered, the trade will go short.
  • If you are short when a long entry is triggered, the trade will go long.
  • If you are long when a short exit is triggered, the trade will exit.
  • 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

    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC C, 11 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

  •  07-25-2005, 4:01 5136 in reply to 5127

    Re: Wow - Thanks Wabbit

    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.

    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC C, 11 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

  •  07-26-2005, 7:42 5194 in reply to 5127

    Re:

    wabbit:
    ...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:

    ====================
    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<--------------------------



    jose '-)
    http://www.metastocktools.com
    MetaStockTools.com
View as RSS news feed in XML