Profit-Taking by Moving sell points (stops?) to subsequent price targets

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:

Capture

Ok this is what I have so far, can some of you that are far more experienced than I am check my logic please? It seems to work so far. Thank you!
Tony

ApplyStop(stopTypeLoss,stopModePercent,6);
Buy = Cross(c, MA( C, 200 ) );
Sell = 0;

FirstProfitTarget = 7.5; // profit
SecondProfitTarget = 15; // in percent
ThirdProfitTarget = 25;

priceatbuy=0;
highsincebuy = 0;

exit = 0;

for( i = 0; i < BarCount; i++ )
{
   if( priceatbuy == 0 AND Buy[ i ] )
    {
       priceatbuy = BuyPrice[ i ];
    }

   if( priceatbuy > 0 )
    {
       highsincebuy = Max( High[ i ], highsincebuy );

      if( exit == 0 AND
			High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy)
       {
         // first profit target hit - scale-out
         exit = 1;
         Buy[ i ] = sigScaleOut;
       }
       if( exit == 1 AND
          High[ i ] >= ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy )
       {
         // second profit target hit - scale out
         exit = 2;
         Buy[i] = sigScaleOut;
       }
       if( exit == 2 AND
			High[ i ] >= (1 + thirdprofittarget * 0.01 ) * priceatbuy )
		{
			// third profit target hit - scale out
			exit = 3;
			Buy[i] = sigScaleOut;
		}
		//1st stop is the 6% max loss from buy, that's taken care of by applystop
		
       if( exit==1 AND Low[i] < priceatbuy)
       {
       //first target hit, stop at the buyprice
			SellPrice[i]=Max(priceatbuy,High[i]);
			exit = 4;
		}
       		
       if( exit==2 AND Low[i] <= (1+firstprofittarget*0.01) * priceatbuy)
		{
			//stop moves up to the first target - exit
			SellPrice[i] = Min(High[i],(1+firstprofittarget*0.01)*priceatbuy);
			exit = 4;
		}
		if( exit==3 AND Low[i] <= (1+secondprofittarget*0.01)*priceatbuy)
		{
			//stop moves up to the second target - exit
			SellPrice[i]=Min(High[i],(1+secondprofittarget*0.01)*priceatbuy);
			exit=4;
		}
      if( exit == 4 )
       {
         Buy[ i ] = 0;
         Sell[ i ] = 1; // mark appropriate exit code
         exit = 0;
         priceatbuy = 0; // reset price
         highsincebuy = 0;
       }
    }
}

SetPositionSize( 95, spsPercentOfEquity );
SetPositionSize( 20, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.