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

Notification

Icon
Error

Options
Go to last post Go to first unread
itourei  
#1 Posted : Tuesday, July 28, 2009 1:26:19 PM(UTC)
itourei

Rank: Newbie

Groups: Registered, Registered Users, Subscribers
You have been a member since:: 7/5/2009(UTC)
Posts: 4

Hello everybody,

I am new to the forum. Greetings to all experts here! =D
I have been reading a number of posts about trailing stops and latching in this forum and learned a lot from them... However, I am building something a bit different and I am really have no idea what should I do.

I am building a trailing stop :
(1) once entered a long or short position, there will be an initial stoploss value, which is (for instance) 50 points away from the entry price;
(2) there will be an trailing stop price. The trailing stop price could only move in favour of the current position. The trailing stop price is the lower of the current and the previous bar (llv(L,2) in long position) or the higher of the current and the previous bar(hhv(H,2) in short position) IF the current bar's close breaks the highest value reached since long position entry (or the current close breaks below the lowest value reached since short position entry). If no breaking of highest/lowest value, the trailing stop will remain as the last value.
(3) the initial stoploss will continue to be effective until the current price has moved in favour of the position and reached a point that the trailing stop is higher than the entry price+handling charge (i.e. break even point). After breakeven point, trailing stop is used instead of initial stoploss.

So I programmed the formula but I am stucked :

{Ladder2: main}

SCL := 50;{Standard CutLoss 50 Points away from position}
hc := 3;{handling charge 3 points}

set:=fml("my entry signal"); {my entry signal, 1=enter long, -1=enter short, 0=usual state, no signal}


ladderv:=abs(ref(tr,-1));
laddersign:=if(ref(tr,-1)>0,1,if(ref(tr,-1)<0,-1,0));

changep:=if(laddersign=0,if(set=1,1,if(set=-1,1,0)),
if(laddersign=1,(L<=ladderv OR set=-1),
if(laddersign=-1,(H>=ladderv OR set=1),0)));
change:=ref(changep,-1);


lowof2 := if(barssince(change)=0,L,llv(L,2));
highof2 :=if(barssince(change)=0,H,hhv(H,2));

newladderbuyv := C-SCL;
newladdersellv := C+SCL;
tr:=if(laddersign=0,if(set=1,newladderbuyv,if(set=-1,newladdersellv,0)),
if(laddersign=1,(L>ladderv) * if(C>highestsince(1,change,H),
if(lowof2>valuewhen(1,changep,C)+hc,lowof2,ladderv),ladderv),
if(laddersign=-1,if(H<ladderv,-1,0) *
if(C<lowestsince(1,change,L),
if(highof2<valuewhen(1,changep,C)-hc,highof2,ladderv),ladderv)
,0)));
tr;



=====================
the problem are in the lines

ladderv:=abs(ref(tr,-1));
laddersign:=if(ref(tr,-1)>0,1,if(ref(tr,-1)<0,-1,0));

The tr value is in fact a variable that contains 2 meaning:
abs(previous tr) is the trailing stop value,
and the +ve, 0, -ve of tr is in fact a latch. If tr is +ve, now we are in long position. If -ve, short. If zero, we are not in a position.
E.g. a tr in last bar valued "+12300" means we are currently in long position and the trailing/stoploss is now 12300 points.
a tr in last bar valued "-12000" means we are currently in short position and the trailing / stoploss is now 12000 points.
a tr valued "0" means we are not in a position and so trailling / stoploss value is not essential in this bar.

however, the ref(tr,-1) I used was incorrect. The tr value is defined after this line and so metastock reported error.
I've tried to replace ref(tr,-1) to PREV. But it seems that PREV is referring to the previous value of the current statement, not the previous bar value of tr.
I've tried to replace all ladderv and laddersign as abs(PREV) and if(PREV>0,1,if(PREV<0,-1,0)) (which seemed rationalthat the tr:= statement use PREV to refer itself recrusively) but metastock is complaining a "insufficient memory" error.

How could i retrieve the last bar value of tr?

Helps are really appreciated!! Thanks a lot!
wabbit  
#2 Posted : Tuesday, July 28, 2009 6:04:10 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)
Hi itourei,

Welcome to the Forum and great first post -- well done. You have provided a full description of the intent, the code and a description of where things are going awry; thankyou.

I don't have time this morning to give you a complete answer, but recommend in the mean time you find Roy Larsen's latch tutorial in the Files section. In your case you'll need to use a PREV based latch to remember the entry price but the trick in differentiating between long and short trades is to invert the short trades (prices are < 0).

Basically, you'll need something which goes along the lines of:

Code:
tr:=
if(prev=0,
 {no trades open} yourTradingSignal*CLOSE, {LONG trades>0, SHORT Trades<0}
{trade open, check for exit conditions}if(prev>0,{long}{initial stop and trailing stop logic goes in here},
{short}{initial stop and trailing stop logic goes in here, remember prices here<0},PREV));


or something along those lines... I have shown a few PREV latches on the forum before, do a search and see what is there which might help you out.

Right now I have to go to work!


Hope this helps.

wabbit [:D]

itourei  
#3 Posted : Tuesday, July 28, 2009 10:19:44 PM(UTC)
itourei

Rank: Newbie

Groups: Registered, Registered Users, Subscribers
You have been a member since:: 7/5/2009(UTC)
Posts: 4

wabbit wrote:
Hi itourei,

Welcome to the Forum and great first post -- well done. You have provided a full description of the intent, the code and a description of where things are going awry; thankyou.

I don't have time this morning to give you a complete answer, but recommend in the mean time you find Roy Larsen's latch tutorial in the Files section. In your case you'll need to use a PREV based latch to remember the entry price but the trick in differentiating between long and short trades is to invert the short trades (prices are < 0).

Basically, you'll need something which goes along the lines of:

Code:
tr:=
if(prev=0,
 {no trades open} yourTradingSignal*CLOSE, {LONG trades>0, SHORT Trades<0}
{trade open, check for exit conditions}if(prev>0,{long}{initial stop and trailing stop logic goes in here},
{short}{initial stop and trailing stop logic goes in here, remember prices here<0},PREV));


or something along those lines... I have shown a few PREV latches on the forum before, do a search and see what is there which might help you out.

Right now I have to go to work!


Hope this helps.

wabbit [:D]



Hi wabbit,

Thanks a lot for your prompt reply =) You guys are really so kind to our beginners!

Yes I have read Roy's document. So I think my last line in the last post (I've tried to replace all ladderv and laddersign as abs(PREV) and if(PREV>0,1,if(PREV<0,-1,0)) (which seemed rationalthat the tr:= statement use PREV to refer itself recrusively) but metastock is complaining a "insufficient memory" error.) seems a rational way.

I've rewritten my code as follow:


{Ladder2: main}

SCL := 50;{Standard CutLoss 50 Points away from position}
hc := 3;{handling charge 3 points}

set:=fml("my entry signal"); {my entry signal, 1=enter long, -1=enter short, 0=usual state, no signal}
changep:=if(if(PREV>0,1,if(PREV<0,-1,0))=0,if(set=1,1,if(set=-1,1,0)),
if(if(PREV>0,1,if(PREV<0,-1,0))=1,(L<=abs(PREV) OR set=-1),
if(if(PREV>0,1,if(PREV<0,-1,0))=-1,(H>=abs(PREV) OR set=1),0)));
change:=ref(changep,-1);


lowof2 := if(barssince(change)=0,L,llv(L,2));
highof2 :=if(barssince(change)=0,H,hhv(H,2));


tr:=if(PREV=0,if(set=1,C-SCL,if(set=-1,C+SCL,0)), {no position}
if(PREV>0,(L>abs(PREV)) * if(C>highestsince(1,change,H), {long position}
if(lowof2>valuewhen(1,changep,C)+hc,lowof2,abs(PREV)),abs(PREV)),
if(PREV<0,if(H<abs(PREV),-1,0) * {short position}
if(C<lowestsince(1,change,L),
if(highof2<valuewhen(1,changep,C)-hc,highof2,abs(PREV)),abs(PREV))
,0)));
tr;

=================================
I think my logic seems correct. However, metastock is reporting "insufficient memory" error.
I know the tr:= line has too much PREV but I have no idea how to eliminate some of them.
And it seems not easy to split it to more than 1 indicator as the tr:= line has to be "as one line".

Any idea on the issue? Really need to seek for you experts for help.
Thanks a lot!


itourei
itourei  
#4 Posted : Friday, July 31, 2009 6:31:29 AM(UTC)
itourei

Rank: Newbie

Groups: Registered, Registered Users, Subscribers
You have been a member since:: 7/5/2009(UTC)
Posts: 4

Would somebody kindly help? I'm really stucked...

To summerize, the problem is, I got insufficient memory error with the following line:

tr:=if(PREV=0,if(set=1,C-SCL,if(set=-1,C+SCL,0)), {no position}
if(PREV>0,(L>abs(PREV)) * if(C>highestsince(1,change,H), {long position}
if(lowof2>valuewhen(1,changep,C)+hc,lowof2,abs(PREV)),abs(PREV)),
if(PREV<0,if(H<abs(PREV),-1,0) * {short position}
if(C<lowestsince(1,change,L),
if(highof2<valuewhen(1,changep,C)-hc,highof2,abs(PREV)),abs(PREV))
,0)));

how could I eliminiate the use of multiple PREV or split the logic to multiple indicators?
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.