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

Notification

Icon
Error

Options
Go to last post Go to first unread
rcbenjamin  
#1 Posted : Monday, January 4, 2010 4:12:21 AM(UTC)
rcbenjamin

Rank: Newbie

Groups: Registered, Registered Users
Joined: 4/16/2006(UTC)
Posts: 4

How do I code the following:

(1) Buy Long at Cross(C,Mov(C,10,S)). {Close moves above the 10 day moving average.}

(2) Exit at Cross(Mov(C,100,S),Mov(C,25,S)) OR When C<Entry Price - 1.5*ATR(14) OR C>Entry Price + 1.5*ATR(14) {The 100 Day Moving Average moves above the 25 day moving average OR the Close moves above the Entry Price + 1.5 ATR (14) or moves below the Entry Price - 1.5 ATR. I am having problems with the Entry Price wanting to reset itself while I am in Long Trade because the buy condition reoccurs before the sell condition has been executed.
johnl  
#2 Posted : Monday, January 4, 2010 6:39:33 PM(UTC)
johnl

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 11/7/2005(UTC)
Posts: 602

Conditioning the buy signal with the "Latch" function (do search on this site) to
make sure you buy only after a stop and not after another buy signal might work.



mstt  
#3 Posted : Tuesday, January 5, 2010 1:50:57 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 John

A simple latch (MFL code or Forum DLL) won't do the job - what's required is a PREV-based latch that inhibits secondary signals and also gives access to the entry price. The EST Simulation functions should provide the same capability, but PREV code can be used in other tools besides the EST, whereas Simulation functions can't. They also even slower executing than PREVs.

Roy

rcbenjamin  
#4 Posted : Tuesday, January 5, 2010 10:24:32 AM(UTC)
rcbenjamin

Rank: Newbie

Groups: Registered, Registered Users
Joined: 4/16/2006(UTC)
Posts: 4

Thanks for your help. Would you please provide the actual detail code so I can try it and understand how to do it.
johnl  
#5 Posted : Tuesday, January 5, 2010 7:15:07 PM(UTC)
johnl

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 11/7/2005(UTC)
Posts: 602

rcbenjamin:

If I understand you correctly, try something like this:

a1:=Cross(C,Mov(C,10,S));
{--------------------------------}
b0:=ValueWhen(1,a1=1,C);
b1:=Cross(Mov(C,100,S),Mov(C,25,S));
b2:=C<b0 - 1.5*ATR(14);
b3:=C>b0 + 1.5*ATR(14);
b4:=b1 OR b2 OR b3;
{--------------------------------}
c1:=BarsSince(b4)<BarsSince(a1);
c1:=Alert(c1,2) AND a1;
c1;
{--------------------------------}
c2:=BarsSince(c1)<BarsSince(b4);
c2:=Alert(c2,2) AND b4;
-c2
mstt  
#6 Posted : Tuesday, January 5, 2010 8:49:22 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 RC
OK, there are a number of things with these formulas that you might want to do differently but consider them as templates. I've provided both indicators and EST code. You can do a number of things with the indicator sell code and I'll get to that. First off here's the EST code windows - Buy Order and Sell Order.
{Buy Order Window}
{RCB Buy}
X:=OPT1;
BuyLong:=Cross(C,Mov(C,10,S));
Ref(BuyLong,-1);
{Sell Order Window}
{RCB Sell}
BuyLong:=Cross(C,Mov(C,10,S));
BuyLong:=Ref(BuyLong,-1);
SellLong:=Cross(Mov(C,100,S),Mov(C,25,S));
ATRange:=Ref(ATR(14),-0) * 1.5;
Trade:=If(PREV<=0,If(BuyLong,OPEN,0), {only buy if not in a trade}
If(SellLong OR C>PREV+ATRange OR C<PREV-ATRange, {3 sell conditions}
-PREV,PREV)); {negative spike for exit, else hold prev value}
Ref(Trade<0,-1); {plot a TRUE when negative spike is present}
Now for the indicators.
{RCB Buy}
{BuyLong:=Cross(C,Mov(C,10,S));}
{Ref(BuyLong,-1);}

{RCB Sell}
BuyLong:=Cross(C,Mov(C,10,S));
BuyLong:=Ref(BuyLong,-1);
SellLong:=Cross(Mov(C,100,S),Mov(C,25,S));
ATRange:=Ref(ATR(14),-0) * 1.5;
Trade:=If(PREV<=0,If(BuyLong,OPEN,0),
If(SellLong OR C>PREV+ATRange OR C<PREV-ATRange,
-PREV,PREV));
Ref(Trade<0,-1);
Notice that there's not much difference (really only the comments). However, the RCB Sell indicator can tell you a lot about what's happening on a chart.
A final line of "Trade" will show you when a trade is entered AND when it is terminated. You'll have to figure out what delays to keep and what to dispense with but I've attempted to make all the code so that things are seen to happen on the bar following when they actually happened (how most of us trade). By including the delays in the buy and sell code you don't need to add delays to the EST. Using delays with the EST is one of the main reasons why the EST rejects trades without apparent reason. There is a reason though, usually that 100% of equity is applied to each trade and when the price is higher on the Execution bar the EST decides that it cannot afford to buy the number of shares it calculated on the previous bar, so it aborts the trade.
The PREV-based Trade latch is really two quite simple If() functions used in conjunction with PREV. The first If() checks if the latch was reset on the previous bar (<0) or reset some bars early (=0) or never set yet (also =0). If this test is passed then the first If() function allows the latch to accept a buy signal whereby the latch sets to the value of the OPEN 1 bar after the actual signal was detected on the CLOSE of the previous bar. If the Trade latch was already set then the first If() inhibits any new buy signals and passes control to the second If() function. This function checks for any of the three sell conditions being TRUE, and if they are then the latch outputs a negative spike indicating that the trade is coming to an end. If no sell condition is TRUE but the latch is still active then the PREVious (positive) value of the latch continues to be output. Got it?
There are all sorts of changes that you can make to the code but if you change the general structure of the PREV latch you'll probably end up with something that doesn't work well.
Here are some alternative outputs (last lines) that you should try with the Sell indicator.
Trade<0;
Trade;
ABS(Trade);
and so on.
If you use ABS(Trade) you may see some situations where the entry price seems to change mid-way through a trade. This in fact is not what is happening but that a new trade is initiated on the bar immediately following the negative spike/termination of the previous trade. Trying to enter a new trade on the same bar as the previous one terminates is problematic with this code and is beyond the scope of what I've tried to show you here.
Oh, I forgot to mention why I put OPT1 into the EST code. All values of this option should be set to 1, and that might make you think that it does nothing at all. What it does do is it forces the EST to present you with a single bar profit (or loss) summary before you move to the histogram showing all profits and losses. It can be quite difficult to assess whether a system is a winner or loser without that single bar graph to start with.
Roy
rcbenjamin  
#7 Posted : Wednesday, January 6, 2010 6:28:29 PM(UTC)
rcbenjamin

Rank: Newbie

Groups: Registered, Registered Users
Joined: 4/16/2006(UTC)
Posts: 4

Thank you for so much detail. I really appreciate it. I have just a few more questions and we can wrap this up. Using the symbol "A", I am seeing for 12/2, 12/11 and 12/22 three buy arrows with no sell arrows for that period in the Expert Advisor. This is what I used:

{Enter Long:}
BuyLong:=Cross(C,Mov(C,10,S));
Ref(BuyLong,-1);


{Exit Long:}
BuyLong:=Cross(C,Mov(C,10,S));
BuyLong:=Ref(BuyLong,-1);
SellLong:=Cross(Mov(C,100,S),Mov(C,25,S));
ATRange:=Ref(ATR(14),-0) * 1.5;
Trade:=If(PREV<=0,If(BuyLong,OPEN,0),
If(SellLong OR C>PREV+ATRange OR C<PREV-ATRange,
-PREV,PREV));
Ref(Trade<0,-1);


Please advise.
mstt  
#8 Posted : Wednesday, January 6, 2010 9:20:30 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 RC

Unfortunately the unused entry signals are are not filtered out unless passed through the PREV latch so you could see a string of entry signals before an exit (as you have already pointed out). I don't usually recommend using PREV code in an expert as it makes charts open more slowly if the expert is attached to the default template. This is particularly so when large amounts of data are loaded. It's not so much of a problem if only 1 to 2 years of data are opened with each chart. For this system you don't have a lot of choice about using the PREV code because you'll find it's impossible to synchronise Buy and Sell signals otherwise. The simple latch that John suggests is fine for tracking a trades progress when the entry and exit are indepedent of each other. However, when the exit is related to some aspect of the entry the use of PREV is essential. Without it the decision-making process quickly gets out of whack.

Of course there are also situations where the use of PREV is quite unnecessary - the Darvas Box code in MS11 is a case in point.

Assuming you want your expert to display only the valid entries and exits I would organise the existing code a little differently. The PREV latch needs to be in the Buy window, and what you then do is monitor for the leading edge of Trade going true (a delay is added by the second BuyLong definition). I'd also suggest that the PREV latch NOT be included in the Sell window. The reason for this is that you'd double the number of PREVs in the expert and have more speed issues. What you can do is write the Trade latch variable to a global variable (using the GV DLL), and then call the Trade from the DLL rather than replicating all that ugly PREV code.

{Enter Long}
BuyLong:=Cross(C,Mov(C,10,S));
BuyLong:=Ref(BuyLong,-1);
SellLong:=Cross(Mov(C,100,S),Mov(C,25,S));
ATRange:=Ref(ATR(14),-0) * 1.5;
Trade:=If(PREV<=0,If(BuyLong,OPEN,0),
If(SellLong OR C>PREV+ATRange OR C<PREV-ATRange,
-PREV,PREV));
Trade:=ExtFml("GV.SetVar","LongTrade",Trade);
Trade>0 AND Alert(Trade<=0,2);

{Exit Long}
Trade:=ExtFml("GV.GetVar","LongTrade");
Ref(Trade<0,-1);

Use the "Trade" global variable to derive any other alerts (or whatever) you reqire for the expert so that the PREV count is kept to five. You might wonder why the latch uses -PREV to signal an exit rather than, say, -1. Well, my tests show that -1 actually slows execution speed rather than speeding it up. Go figure. The GV DLL is available from the bottom of the page at http://www.metastocktips.co.nz/te_formulas.html.

Roy

rcbenjamin  
#9 Posted : Thursday, January 7, 2010 5:46:38 PM(UTC)
rcbenjamin

Rank: Newbie

Groups: Registered, Registered Users
Joined: 4/16/2006(UTC)
Posts: 4

Using the new formulas you give me does remove the extra buy signals but now the exit signals does not appear to be responding to the correct values. For the symbol, "A", I got a long on 12/2 with a Fixed Stop Profit around 30.48. On 12/28, I get an exit long but the Close was only 30.40 and the High was 30.44. The other moving average for exit long did not fire so the only thing that could have caused it to exit prematurely was the Stop Profit which was about 30.48.
mstt  
#10 Posted : Thursday, January 7, 2010 9:18:30 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 RC

Interesting that you mention a "fixed stop profit" 30.48. If you look at the code I provided you can see that the ATR value is not fixed at the entry point but floats during the trade and at the time the exit is signalled PREV+ATRange actually drops below C.

The code needs to be changed to correct that problem, but understand that it's your problem, not mine. Figuring out why things don't work the way they should is an excellent way to learn more about MetaStock so I'd encourage you to attempt your own troubleshooting.

Here's one way the previous entry code can be change to "lock in" the ATR value at the time of the entry signal. Just using ValueWhen() to remember the ATR value prior to the latch would allow the value to be changed should a secondary entry signal be received. To get around that the initial ATR value has to be available within the PREV latch for the duration of the trade. We can do that by using ValueWhen(1,PREV<=O,OPEN+ATRange) .

{Enter Long}
BuyLong:=Cross(C,Mov(C,10,S));
BuyLong:=Ref(BuyLong,-1);
SellLong:=Cross(Mov(C,100,S),Mov(C,25,S));
ATRange:=Ref(ATR(14),-0)*1.5;
Trade:=If(PREV<=0,If(BuyLong,OPEN,0),
If(SellLong OR
C>ValueWhen(1,PREV<=0,OPEN+ATRange) OR
C<ValueWhen(1,PREV<=0,OPEN-ATRange),
-PREV,PREV));
Trade:=ExtFml("GV.SetVar","LongTrade",Trade);
Trade>0 AND Alert(Trade<=0,2);

Unfortunately this structure is a little more demanding of resources than the previous version. I no longer use PREV with ValueWhen() if i can help it, but sometimes there is no better way. Somewhere in the distant past I figured out a way to merge the two PREV exit lines down to just one PREV expression but I haven't been able to get it quite right today. No matter, this "fixes" the problem you brought to my attention. From now on it's over to you to figure out what the wrinkles are and how to remove or get around them. Good luck.

Roy

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.