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

Notification

Icon
Error

Options
Go to last post Go to first unread
johnnic  
#1 Posted : Wednesday, November 29, 2006 3:11:19 PM(UTC)
johnnic

Rank: Member

Groups: Registered, Registered Users, Subscribers
Joined: 11/20/2006(UTC)
Posts: 21

I know this is a VERY basic question but I have lost my old Metastock 7 manuals and I'm in need of help.

How do you code a simple stop loss? For example, assuming my exit trigger is when the current price drops by more than, say, 10% of the entry price.

I assumed it would have to declare a variable, such as BuyPrice, and set that equal to the entry formula and then use an exit trigger something like BuyPrice - (EntryPrice*10/100). That doesn't seem to work very well though.

Please help.

John

wabbit  
#2 Posted : Wednesday, November 29, 2006 5:16:12 PM(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)
John,

You need to use a PREV based latch to store the ValueWhen() the trade is entered, this is the entry price. You can now compare the exit criteria to the entry price.

Do a search for "latch" and see what comes up. If you have any trouble, post your best attempt at your exit code and I am sure someone will come to your assistance.


wabbit [:D]


jssuccess  
#3 Posted : Wednesday, April 22, 2009 2:09:21 PM(UTC)
jssuccess

Rank: Member

Groups: Registered, Registered Users
Joined: 4/15/2009(UTC)
Posts: 14

Dear all,

I remain puzzled after looing at the valuewhen() for several days. Is it possible to set a stop loss at 100 points below buy price or 100 points above sell price (other than predefined stop loss rule) ?

tks in advance

wabbit  
#4 Posted : Wednesday, April 22, 2009 6:16:21 PM(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)
For indicators and experts: Although it is possible to capture the trade entry price using ValueWhen(), it runs into a few problems, so it's better to use a trade latch. Have a read of Roy Larsen's excellent guide to using latches in MS, available in the files section.

For the EST: You can use simulation.currentpositionage and the forum.dll


Hope this helps.

wabbit [:D]

jssuccess  
#5 Posted : Friday, April 24, 2009 10:46:31 PM(UTC)
jssuccess

Rank: Member

Groups: Registered, Registered Users
Joined: 4/15/2009(UTC)
Posts: 14

now I come up with something like below after several days of hardworking:

{Input}

A1:=18;

A2:=18;

{Section}

B1:=C>Mov(c,a1,e);

B2:=Min(Min(Mov(C,3,E),Mov(C,5,E)),Min(Min(Mov(C,8,E),Mov(C,10,E)),Min(Mov(C,12,E),Mov(C,15,E)))) > Mov(C,100,E);

C1:=C<Mov(c,a2,e);

C2:=Max(Max(Mov(C,3,E),Mov(C,5,E)),Max(Max(Mov(C,8,E),Mov(C,10,E)),Max(Mov(C,12,E),Mov(C,15,E)))) < Mov(C,100,E);

Var1:=If(Ref(ROC(DayOfWeek(),1,$)=0,1) AND

b1 AND b2,1,If(Ref(ROC(DayOfWeek(),1,$)=0,1) AND

c1 AND c2,-1,0) );

EL:=var1=1;

CL:=cross(mov(c,45,e),L);

ES:=var1=-1;

CS:= cross(H,mov(c,45,e));

State:=If(Cum(1)=1,0,If(EL,1,If(ES,-1,If((CL AND PREV=1) OR (CS AND PREV=-1),0,PREV))));

Buy:=State=1 AND Ref(State,-1)<1;

Buyprice:=valuewhen(1,buy=1,c);

now the problem is I can only capture the buy entry price but once I add it into my "CL:=" (as i also want to exit whenever price is below 100 points of the buy entry price),

it does not work what I expect.

Where is the error?

Can someone kindly give me a hand ?

wabbit  
#6 Posted : Saturday, April 25, 2009 12:14:12 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)
These sorts of trading "systems" are best dealt with in the EST which has slightly better capabilities than "standard" indicators, but even better is to use the MDK and code these up; it alleviates the need for PREVs and is essentially much more easy to program and maintain!

Try this out though:

Code:

{Input}
a1:=18;
a2:=18;

{MAs}
ma1:=mov(C,3,E);
ma2:=mov(C,5,E);
ma3:=mov(C,8,E);
ma4:=mov(C,10,E);
ma5:=mov(C,12,E);
ma6:=mov(C,15,E);
ma7:=mov(C,100,E);

{Section}
B1:=C>mov(C,a1,E);
B2:=min(min(min(min(min(ma1,ma2),ma3),ma4),ma5),ma6) > ma7;

C1:=C<mov(C,a2,E);
C2:=max(max(max(max(max(ma1,ma2),ma3),ma4),ma5),ma6) < ma7;

notLastBarOfDay:=ref(ROC(DayOfWeek(),1,$)=0,1);

EL:=notLastBarOfDay AND b1 AND b2;
ES:=notLastBarOfDay AND c1 AND c2;

CL:=Cross(mov(C,45,E),L);
CS:= Cross(H,mov(C,45,E));

stopPoints:=.0100;

tr:=If(prev=0, {no trades open}
ref(EL-ES,-1)*OPEN,{execute a trade at OPEN the next bar}
If((prev>0 AND (CL OR (LOW+stopPoints)<prev)) OR (prev<0 AND (CS OR (-HIGH+stopPoints)<prev)),0,{check for exit criteria}
prev));

tr;


NOTES:
* Untested, but it's going to be very slow and CPU intensive.
* I took the liberty to improve the readability.\



Hope this helps.

wabbit [:D]

jssuccess  
#7 Posted : Saturday, April 25, 2009 12:46:33 AM(UTC)
jssuccess

Rank: Member

Groups: Registered, Registered Users
Joined: 4/15/2009(UTC)
Posts: 14

Wabbit,

Yes you are right. It is more readable and I will try to figure out the rest of the program.

Tks again

jssuccess  
#8 Posted : Saturday, April 25, 2009 6:26:25 AM(UTC)
jssuccess

Rank: Member

Groups: Registered, Registered Users
Joined: 4/15/2009(UTC)
Posts: 14

Wabbit,

After working on your code for three hours, I got two problems that remain unsolved.

(1) I failed to plot "tr" after I replace your OPEN with CLOSE (because I want to capture the close of the current bar, instead of the open of the next bar);

(2) even I copy your code in indicator editor, "tr" line was not plotted.

does anything go wrong ?

Tks in advance.

wabbit  
#9 Posted : Saturday, April 25, 2009 7:28:35 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)
(1) Your decision making process is made on knowing the CLOSE, therefore you cannot trade at the close as that bar is already closed: see http://forum.equis.com/forums/thread/29444.aspxrs of tinkering that I cannot see from here. If you don't post your code exactly as you are using it then members here cannot diagnose the fault.


wabbit [:D]


jssuccess  
#10 Posted : Saturday, April 25, 2009 1:08:40 PM(UTC)
jssuccess

Rank: Member

Groups: Registered, Registered Users
Joined: 4/15/2009(UTC)
Posts: 14

Wabbit,

Now I have more concept on the need of executing trades on open of the next bar rather than on close of the current bar.

By the way, your formula is correct as i made a tiny mistake that led me failed to plot "tr".

Your advice and help is strongly appreciated.

Users browsing this topic
Guest (Hidden)
Similar Topics
Stop Loss in Exploreer (Formula Assistance)
by temperino 11/13/2024 8:33:46 AM(UTC)
Custom stop loss issue (Formula Assistance)
by slowthinker 10/15/2019 4:43:15 PM(UTC)
Trailing Stop loss system with optimisation poarametres (Formula Assistance)
by mt4eas 1/30/2015 3:45:10 AM(UTC)
GV.dll for Stop loss with Entry point (Formula Assistance)
by ltosamon 6/27/2014 1:50:06 AM(UTC)
Stop Loss Triggering on Wide Ranging Breakout Entry Day (Formula Assistance)
by davestan 4/29/2013 6:39:23 PM(UTC)
October 8 - Stop Losses (MetaStock Spotlight)
by Alex 10/8/2012 7:49:11 AM(UTC)
Formula to optimize days delay and stop losses (Formula Assistance)
by Warmuth327 3/19/2012 8:30:12 AM(UTC)
stop loss (Formula Assistance)
by m_armia 12/4/2011 3:21:44 AM(UTC)
Multiple Stop Loss (Formula Assistance)
by GameTime 10/17/2011 5:28:51 AM(UTC)
Exit Long / Stop Loss exists one bar too late (MetaStock)
by TrendyTrader 1/18/2011 8:04:32 AM(UTC)
System Tester Good-till-cancelled Stop Loss (Formula Assistance)
by NickRosie1 7/20/2010 2:16:47 AM(UTC)
Question about Take Profit and Stop Loss Point (Formula Assistance)
by Shalafi 4/12/2010 7:04:46 AM(UTC)
Trailing stop loss in system tester (Formula Assistance)
by dhirbisht 11/16/2009 11:33:32 PM(UTC)
Floating Stop Loss Line (FSL) (Basic Coding Techniques)
by Dendee 8/13/2009 10:38:57 PM(UTC)
Stop loss and trailing stop loss in Enhanced system tester ?? Help plz.. (Formula Assistance)
by schumii7 4/26/2009 9:28:29 AM(UTC)
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.