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

Notification

Icon
Error

Options
Go to last post Go to first unread
Dan A  
#1 Posted : Sunday, September 25, 2011 7:20:57 AM(UTC)
Dan A

Rank: Member

Groups: Registered, Registered Users
Joined: 4/13/2011(UTC)
Posts: 15
Location: Manchester, UK

Hi all, I am trying to code a system that will allow me to generate multiple entry points. I can program the entry condition for the 1st entry easy enough, but beyond that I am struggling. I would like the system to enter 2nd and subsequent positions based on the conditions of the previous entry, eg: Open a 2nd position when Close > Previous Entry Point + 2.5 * ATR(21), The 3rd entry point would be 2.5 * ATR(21) above the 2nd entry point, and so on. All bets would close under one single criteria, eg if the close dropped below the 20 day moving average, then all positions would be closed. I've searched the forums, and been looking at the "latch" formula, thinking that this may well be the right direction to be going, but admit to being a bit stumped. Am I headed in the right direction? Any pointers to get me moving would be greatly appreciated! Thanks.
mstt  
#2 Posted : Sunday, September 25, 2011 4:25:36 PM(UTC)
mstt

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)
Hi Dan

Coding to create multiple entry points is possible but not the easiest goal to achieve. You'll probably need a series of PREV-based latches to manage each trade, one latch for the primary trade and one latch for each secondary trade. The latches for monitoring secondary trades would be inhibited from setting until the previous stage became active - a previous stage being the primary trade or earlier secondary trade.

Depending on your system rules it may be possible to create one PREV-based latch to manage each new signal,. For this to work you'd need to use the latch as a counter such that it increments (probably by one) with each new signal generated and resets to zero when a common exit signal is triggered.

A "simple" latch (one without PREV) is not appropriate for what you're trying to achieve because it cannot manage exits that are related to the entry. Only by using PREV will you be able to tie all entry and exit signals together. Without system rules I'm not able to be more specific on how to proceed with the code, and you need to be aware that, for whatever reason, it may not be possible to build your system exactly as planned.

Roy

Dan A  
#3 Posted : Monday, September 26, 2011 1:48:20 PM(UTC)
Dan A

Rank: Member

Groups: Registered, Registered Users
Joined: 4/13/2011(UTC)
Posts: 15
Location: Manchester, UK

Hi Roy, Thanks for responding. It may be that one latch might do; what I would like to do is set a trigger for the next entry point when I hit the first one. The test system is based on a straightforward "highest high" trigger. So if set the H at 20 days, my entry trigger is: C > Ref(HHV(C,20),-1); When this is triggered, I'd like to set the next entry point at 2.5 * ATR(21) above this entry point, and so on. My exit is straightforward again, being based on the "lowest low" being breached. Say a 10 day LLV. This exit would trigger an exit of all positions. Does this sound like it could be programmed, and can you give me any more tips on the latch programming required? Thanks again, Dan
mstt  
#4 Posted : Monday, September 26, 2011 2:36:21 PM(UTC)
mstt

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)
Hi Dan

I'll look at this over the next couple of days and get back to you when I have a working example. With the rules you've provided it should be possible but we shall see.

Roy
mstt  
#5 Posted : Tuesday, September 27, 2011 6:36:45 PM(UTC)
mstt

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)
Normal 0 false false false MicrosoftInternetExplorer4 classid="clsid:38481807-CA0E-42D2-BF39-B33AF135CC4D" id=ieooui> <STyle> st1\:*{behavior:url(#ieooui) } <STyle> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;}

Hi Dan

Here’s a basic example of how a PREV-based latch (the "R" variable) might operate. The latch is formed in three stages that are defined by cascading If() functions.

The first If() function checks that the latch was previously reset (=0) or reset on the previous bar (<0). If those conditions are TRUE then if N is TRUE the latch sets to a value of C+2.5*ATR(21).

In the event that the latch was already set then the second If() function takes control and monitors for a pyramid entry signal, looking for a target of 2.5*ATR(21) above the first entry’s CLOSE. That value is fixed by the latch at the time of entry. The new target set is the higher of the existing and new targets.

If a pyramid entry signal is not present then control is passed to the third If() function. It monitors for an exit signal and produces a negative spike as the latch resets when one is seen. If there is not an exit signal then the latch retains its PREV value.

{Dan's Trade Latch}

A:=2.5*ATR(21);

N:=C>Ref(HHV(C,20),-1); {primary entry signal}

N:=N*Alert(N=0,2); {leading edge of N signal}

X:=C<Ref(LLV(C,10),-1); {exit signal}

X:=X*Alert(X=0,2); {leading edge of X signal}

R:=If(0>=PREV, N*(C+A), {first entry}

If(N*PREV>0,Max(PREV,N*(C+A)), {pyramid entry}

If(X,-PREV,PREV))); {exit - negative spike}

Abs(R); {entry price target}

There are many ways that this type of latch can be tweaked to do various things. Entries are kept in sync with exits by making all decisions within the one variable. Once you try to make decisions across 2 or more variables you lose an element of control and run the risk of your code contradicting your intentions.

The down side of using a PREV-based latch is that each PREV adds significant processing overheads. Adding one extra condition into the decision making process can add two or three PREVs and eventually you’d end up with code that is so slow to run that for all practical purposes it is quite useless.

This piece of code is just an example that uses my interpretation of the information that you gave and not necessarily the end product that I believe you should use. You should study the "R" and how it works until you are reasonably comfortable with it and can answer all your own questions. Then you may want to experiment and ask "what happens if I do this or that". A useful technique that might help is to create this as an indicator and see what various alternative outputs look like - "R", "ABS(R)", "R>0", "(R>0)*Alert(R=0,2)" and so on.

Hope this helps.

Roy

Dan A  
#6 Posted : Wednesday, September 28, 2011 12:43:59 AM(UTC)
Dan A

Rank: Member

Groups: Registered, Registered Users
Joined: 4/13/2011(UTC)
Posts: 15
Location: Manchester, UK

Hi Roy, Thanks so much for that. I am really very grateful! I think this seems to be exactly what I am looking to do. As you point out, introducing more conditions would only make the code not only more processor-hungry, but would also become quite complex to understand, and I am aiming to try and keep things simple. As such, this code looks perfect. I will do as you suggest and create an indicator, from which I can then interpret that it's working how I wish, after which I will then put it into test. Again, I am very grateful for your assistance, and I will post back to let you know how it all comes out. Thanks, Dan
Dan A  
#7 Posted : Wednesday, October 5, 2011 1:46:39 PM(UTC)
Dan A

Rank: Member

Groups: Registered, Registered Users
Joined: 4/13/2011(UTC)
Posts: 15
Location: Manchester, UK

Hi Roy, I've been testing out the code above in an indicator, as you suggested, to try and understand how it is working. I've also loaded it into system tester and tried "points only" testing. I am not quite sure what the 2 lines with the "Alert" function are doing. I have got as far as working out they are looking back 2 periods. Is the result of this alert function either a 1 (for true) or 0 (for false)? It has me stumped I'm afraid. Could you explain what the alert function is doing, as I think this would get me closer! Thanks as always, Dan
mstt  
#8 Posted : Wednesday, October 5, 2011 2:50:24 PM(UTC)
mstt

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)
Hi Dan

The N and X definitions with Alert() are there to filter the previous definitions of N and X so that only the leading exde of the buy or sell signal is acted upon. In the case of N a buy signal would be generated for every bar that a new CLOSE was higher than the previous bar's 20 period HHV of CLOSE. So if the price rose for 10 consecutive bars you'd get 10 contiguous buy signals. While this might be the way you'd trade, I'd only take the first signal (leading edge), and then wait for a new leading edge of C> previous HHV before executing another buy. Only you can decide what is appropriate for your system and my assumption was just that. Eliminate those lines if you don't want them, but don't overlook the possibility of complications with the latch if you do so.

So what Alert() does in this situation is convert the raw signal from "N=TRUE" to "N=TRUE AND Ref(N,-1)=FALSE" (N was false on the previous bar). The Alert() function extends "N=0" (it can also be written as N=FALSE) by one bar so that it remains TRUE for the first bar that N is TRUE. The Alert() function then "times out" and inhibits any further "N=TRUE" results from being passed to subsequent code.

The reason I use Alert() in preference to Ref() for generating a delayed signal from N=0 is that Alert(), unlike Ref(), does not imbed an N/A result into its result. N/A results inserted into code by any function are cumulative and can cause numerous problems for the unwary. For the same reason I will often use ValueWhen(2,1,X) rather than Ref(X,-1) when I want to dealy a particular expression by one or mare bars.

The expression "N*Alert(N=0,2)" can be written numerous ways. The asterisk in this context is a short method of ANDing the two elemants of the expression - any expression or value multiplied by 1 returns the same value or expression, and any value or expression multiplied by 0 returns 0. . Obviously there may be situations when this shortcut is inappropriate but for the most part it's an excellent space-saver. Here are some other ways that the expresion can also be written.

N*Alert(N=0,2)
N AND Alert(N=0,2)
N AND Ref(N=0,-1)
N=TRUE AND Ref(N=0,-1)
N=TRUE AND Ref(N=FALSE,-1)
N*ValueWhen(2,1,N=0)
N AND ValueWhen(2,1,N=0)
N=1 AND ValueWhen(2,1,N=0)
N=TRUE AND ValueWhen(2,1,N=0)
N=TRUE AND ValueWhen(2,1,N=FALSE)

And so the list goes on.

Notice that when you write "N" in a logical context, Metastock assumes that you mean "N=TRUE" and there is no need to add the qualifier of "=1" or "=TRUE" to N. However, when you want to stipulate that an expression, variable or whatever is false then you must add "=0" or "=FALSE" as a qualifier, hence the "N=0" amd "X=0" in the Alert() functions of my code. For the most part 1 and 0 are interchangable with TRUE and FALSE when expressing logical values. However, any numerical value other than 0 (zero) will be regarded as a TRUE when used in a logical context. Actually that's not quite true - the EST regards zero and negative numbers as FALSE and only positive numbers as TRUE. But that's another story.

Roy


Dan A  
#9 Posted : Saturday, October 8, 2011 4:19:45 AM(UTC)
Dan A

Rank: Member

Groups: Registered, Registered Users
Joined: 4/13/2011(UTC)
Posts: 15
Location: Manchester, UK

Hi Roy, Many thanks for your assistance, and also for explaining the code. I understand now about the leading edge, and you are right in that generating 10 contiguous buy signals for 10 consecutive bars isn't how I am looking to test (I suspect ruin lies in that direction!). After the 1st entry point of N (at C > Ref(HHV(C,20),-1), the 2nd entry would only be triggered at N + 2.5*ATR(21) I suspect this means changing the 2nd IF statement, but haven't been able to work it out. Can you offer any assistance on that one? As always, your assistance is useful, educational and greatly appreciated! Dan
mstt  
#10 Posted : Monday, October 10, 2011 1:59:37 AM(UTC)
mstt

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)
Hi Dan

The second If() statement controls the secondary entries and it is activated when PREV is already greater than zero and a new entry is signalled by N going TRUE again. The new target price is set as the greater of PREV (the target for the first trade) and C plus 2.5*ATR(21) ( or N*(C+A) ). The reason for including PREV in the new target is so that the secondary target is not less than the primary target, as might otherwise happen. While it might seem logical that a new target would automatically be higher than the first target there's nothing I can see in the rules you provided that would guarantee that. By using the Max() function to set the new target there's something in place that a series of lower targets aren't set.

If(N*PREV>0,Max(PREV,N*(C+A)), {pyramid entry}

I think each new target should still meet the "C plus 2.5*ATR(21)" target but I haven't attempted to resolve any potential entry timing issues that might be inherrent in the code that I offered. I don't see the problem that you see so can't offer any further help on that.

Roy
Dan A  
#11 Posted : Monday, October 10, 2011 1:44:17 PM(UTC)
Dan A

Rank: Member

Groups: Registered, Registered Users
Joined: 4/13/2011(UTC)
Posts: 15
Location: Manchester, UK

Hi Roy, The system seems to be generating buy orders every day once the 1st entry signal - N - is hit. I don't want it to add any more buys until the price gets to N + 2.5*ATR(21). When that price is reached, I want the system to make 1 more buy, and again wait until the price rises by a further 2.5*ATR(21) to make a 3rd buy. And so on. So for example, if the system triggered a first buy of, say, gold at $1600, and the ATR was 40, the 2nd buy would only occur at $1700 (when C > $1700). There would be no buys in between. If the ATR when the price reached $1700 was now 50 say, the next entry would be when close reached $1825. I only intend to generate another buy signal when the price has moved by a multiple of ATR. I hope that this clarifies my thoughts more, and apologise for not being clearer earlier. Your help has been most valuable in furthering my understanding of MS, and I am sure the answer to my query lies largely in the code already.
mstt  
#12 Posted : Monday, October 10, 2011 2:57:24 PM(UTC)
mstt

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)
Hi Dan

You should not be using the final line of the latch "Abs(R);" as a buy signal. "N" is the buy signal and "Abs(R)" is the output you would use on a chart to see where a trade is entered and what the current target price is. Use "N" as the entry signal (probably with a 1-bar delay and entry on the next OPEN) and "X" as the exit signal (with or without a delay as you see fit).

The latch formula I created is primarily an indicator and it's up to you to use the appropriate signals from that indicator to drive the EST. The EST is going to use every positive value presented by the last line of code as an entry signal so it's up to you add "N" as an output after (or instead of) "Abs(R);" if using the indicator in the Buy Order window.

Roy


Dan A  
#13 Posted : Thursday, October 13, 2011 1:11:55 PM(UTC)
Dan A

Rank: Member

Groups: Registered, Registered Users
Joined: 4/13/2011(UTC)
Posts: 15
Location: Manchester, UK

Hi Roy, This seems to be working great now. Thank you so much for your help. It is greatly appreciated. Dan
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.