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