# Buy signals are repeated when using loop

Hi everyone,

I wrote a trading strategy with 2 exit conditions. Condition 1: exit when stoploss is reached. Condition 2: when profit exceeds 25%, start using trailing stop. The problem is that it repeats the buy signal and each time it uses the stop from the first condition.
I have read some other posts on the forum but have not been able to resolve this issue. Please help me fix it

``````Plot(C,"" , colorBlueGrey,styleCandle);
Ema25 = EMA(C,25);
Plot(Ema25,"EMA25",colorBlack);

HL1 = abs(Ref(H,-1) - Ref(L,-1))/2;
OC1 = abs(Ref(O,-1) - Ref(C,-1))/2;

fkupbar = L < Ref(L,-1) AND Cross(C , (Ref(C,-1)+OC1)) AND C > Ema25  AND V > Ref(V,-1);

Sell = 0; // replace with your sell rule

maxprofit =  (HHV(C,countT)-entry)/entry*100;

StopLevel1 = 1 - Param("stoploss %", 7, 0.0, 10, 0.1)/100;
StopLevel2 = 1 + Param("trailing stop %", 10, 0.0, 20, 0.1)/100;

trailARRAY = Null;
trailstop = 0;

for( i = 1; i < BarCount; i++ )
{

if( trailstop == 0 AND Buy[ i ] )
{
trailstop = entry[ i ] * stoplevel1;
}

if( trailstop > 0 AND Low[ i ] < trailstop  AND countT[i]>=2)
{
Sell[ i ] = 1;
SellPrice[ i ] = trailstop;
trailstop = 0;
}

if( trailstop > 0 AND maxprofit[i] < 25 )
{
trailstop = entry[i] * stoplevel1;
trailARRAY[ i ] = trailstop;

}
else if ( trailstop > 0 AND maxprofit[i] > 25 )
{

trailstop = entry[i] * stoplevel2 + add;
trailARRAY[ i ] = trailstop;
PlotText(""+WriteVal(maxprofit[i],5.1) + "%",i,H[i]*1.05, colorBrightGreen,colorDefault,30);
}

}