Hello friends, I'm working on code that uses simple buy/sell entry rules that involves scaling out as 3 profit targets are hit, which I have working due to the very intelligent generous members of this forum. However, I'm having difficulty with the best way to go about raising stops/sell points that will sell out the position completely after each profit target is achieved.
The gist is once the trade is put on the first stop is just a 6% loss, then after the first profit target is hit the stop moves up to the buy price. When the second target is hit, the stop moves to the first target. And when the third target is hit, the stop moves to the second target.
To get the scale-out to work correctly I'm using ExRem to remove the excess signals once the targets are achieved. But now I'm thinking I need to make another array that keeps it as a state signal, but not sure what the second array should be for the Flip function.
This is what I have so far. Thank you for any help!
ApplyStop(stopTypeLoss,stopModePercent,6);
entrysig=Cross(C,MA(C,100));
sellsig=Cross(MA(C,100),C);
intrade=Flip(entrysig,sellsig);
valueatbuy=ValueWhen(entrysig,C);
profittarget1=valueatbuy*1.05;
profittarget2=valueatbuy*1.1;
profittarget3=valueatbuy*1.15;
rising=intrade AND C<profittarget1;
achievedtarget1=ExRem(intrade AND C>profittarget1,sellsig);
achievedtarget2=ExRem(intrade AND C>profittarget2,sellsig);
achievedtarget3=ExRem(intrade AND C>profittarget3,sellsig);
scaleout=achievedtarget1 OR achievedtarget2 OR achievedtarget3;
Buy=IIf(entrysig,1,IIf(scaleout,sigScaleOut,0));
entrypossize1=100;
scaleoutsize=25;
possize=(entrysig*entrypossize1) + (scaleout*scaleoutsize);
SetPositionSize(possize,spsPercentOfEquity);
Filter=1;
AddColumn(entrysig,"entrysig");
AddColumn(valueatbuy,"valueatbuy");
AddColumn(Close,"Close");
AddColumn(profittarget1,"pt1");
AddColumn(profittarget2,"pt2");
AddColumn(profittarget3,"pt3");
AddColumn(achievedtarget1,"at1");
AddColumn(achievedtarget2,"at2");
AddColumn(achievedtarget3,"at3");
AddColumn(scaleout,"so",1.0,colorDefault,IIf(scaleout==1,colorGreen,colorDefault));
//profit targets all work--now let's add in stops
//must use state signals to trigger when profit targets hit
Here's the exploration: