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 Guys
The problem with "L < ValueWhen(1, entry, c) * (1/1.10) " as a stop is that successive "entry" signals set a new stop price that's unrelated to the actual entry price. In some situations the above code might be sufficient, but if multiple entry signals are received while the trade remains active then it's going to fail.
The original question was for a formula so I'm assuming you want to code the exit rather than use the "max loss" System Tester stop. To code an accurate stop you need to use the PREV function and base a latch around it such that the stop always references tha actual entry price, not the price of any subsequent entry signal. Even then there can be minor timing issues in certain situations.
The underlying latch can act to store the actual entry price until reset to zero, and in that case the entry price is referenced within the latch reset code as PREV. Alternately, the latch might operate in pseudo or pure binary (by pseudo binary I mean it has states of 1, 0, or -1), and then the entry price is referenced within the latch with, for example, by ValueWhen(1,PREV=0,CLOSE).
Instead of a PREV-based formula you may be able to use something like ValueWhen(1,Simulation.CurrentPositionAge=1,C) in the System Tester (or is it "=0"), but there appears to little advantage in using Simulation as it slows things down as much as PREV.
A minimum of 2 PREVs is necessary (assuming you don't have access to a dll to perfom some of the tasks) in a latch formula to manage a stop based on the first entry signal. The first PREV ensures that the latch cannot accept a second entry (and price) until after it has reset (stop or normal exit); the second PREV monitors the latch for the stop condition, and resets it when the stop is activated.
The PREV-based "R" variable in the following formula is a latch that monitors for both stop-loss and profit targets without adding a 3rd PREV. This is only possible when both exits are calculated on the CLOSE (don't ask why as it's difficult to explain). Setting the profit target to 999% would effectively disable the profit target and leave only the stop-loss operational. Obviously you'd have to remove the Input() functions befor this code could be used directly in the System Tester but it might be something you'd like to play with.
The formula can have a number of outputs, and what output you use depends on what you want the code to do.
{Trade Stop Lite}
{2004 Roy Larsen, www.metastocktips.co.nz}
No:=Input("Entry Price 1=O 2=C 3=H 4=L",1,4,1);
Nd:=Input("Entry Delay",0,3,1);
Xd:=Input(" Exit Delay",0,3,1);
Pf:=1+Input("Profit Target %",1,999,40)/100;
Lf:=1-Input("Stoploss %",1,99,4)/100;
N:=Cross(Mov(WC(),10,E),Mov(WC(),30,E)); {Buy}
X:=Cross(Mov(WC(),30,E),Mov(WC(),10,E)); {Sell}
{* end of user area *}
N:=N AND Alert(N=0,2);
X:=X AND Alert(X=0,2);
N:=ValueWhen(1+Nd,1,N);
N:=If(N>0,If(No=1,O,If(No=3,H,If(No=4,L,C))),0);
X:=ValueWhen(1+Xd,1,X);
I:=Cum(N+X>-1)=1;
M:=C<ValueWhen(2,1,C);
R:=If(PREV<=0,N>0,If(X OR
If(M,-1,1)*ValueWhen(1,PREV=0,N)<=
If(M,-C/Lf,C/Pf),-1,1));
X:=R<0;
R:=Abs(R)*ValueWhen(1,I+Alert(R=0,2)*R,N);
{ R; R*Pf; R*Lf; X*C; } X;
Roy
|