# 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);

scaleout=achievedtarget1 OR achievedtarget2 OR achievedtarget3;

entrypossize1=100;
scaleoutsize=25;
possize=(entrysig*entrypossize1) + (scaleout*scaleoutsize);
SetPositionSize(possize,spsPercentOfEquity);

Filter=1;

//profit targets all work--now let's add in stops
//must use state signals to trigger when profit targets hit

``````

Here's the exploration:

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;

exit = 0;

for( i = 0; i < BarCount; i++ )
{
{
}

{

if( exit == 0 AND
High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy)
{
// first profit target hit - scale-out
exit = 1;
}
if( exit == 1 AND
High[ i ] >= ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy )
{
// second profit target hit - scale out
exit = 2;
}
if( exit == 2 AND
High[ i ] >= (1 + thirdprofittarget * 0.01 ) * priceatbuy )
{
// third profit target hit - scale out
exit = 3;
}
//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
exit = 4;
}

if( exit==2 AND Low[i] <= (1+firstprofittarget*0.01) * priceatbuy)
{
//stop moves up to the first target - exit
exit = 4;
}
if( exit==3 AND Low[i] <= (1+secondprofittarget*0.01)*priceatbuy)
{
//stop moves up to the second target - exit
exit=4;
}
if( exit == 4 )
{
Sell[ i ] = 1; // mark appropriate exit code
exit = 0;
priceatbuy = 0; // reset price