Need help to verify a strategy!

#1
Hi
I would like to verify a strategy.
I found a AFL formula that I have tried to modify to sell 50% of my position when I reach my 1st profit target and than 50% when I get my 2nd profit target. After that it is supposed to be sell by my trailing stop! When I make a "scan & a Individual backtest" I do not get what I want.
Could you help me to verify what is wrong!

Buy = Cross( MACD( 12, 26 ) , Signal( 12, 26, 9 ); // I know this is just to have a buy signal!
Sell = 0;

priceatbuy=0;
highsincebuy = 0;
stoplineA = (((ATR(4) / Ref( Close , -1 ) ) * 1.0 ) + 1); // It gives me my 1st value that I will multiply by the BuyPrice!
stoplineC = (((ATR(4) / Ref( Close , -1 ) ) * 1.25 ) + 1); // It gives me my 2nd value that I will multiply by the BuyPrice!
stoplineB = HHV( Close - 1.5 * ATR(4) , 5 ); //This is my trailing stop!

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 ); // I do not understand this one!!!

if( exit == 0 AND High[ i ] >= stoplineA[ i ] * priceatbuy ) // My 1st target profit "stoplineA" is supposed to be hit! - scale-out
{ exit = 1; Buy[ i ] = sigScaleOut; }

if( exit == 1 AND
High[ i ] >= stoplineC[ i ] * priceatbuy ) // My 2nd target profit "stoplineC" is supposed to be hit! - scale-out
{ exit = 2; SellPrice[ i ] = Max( Open[ i ], stoplineC[ i ] ); }

if( Low[ i ] <= stoplineB[ i ] ) // trailing stop hit - exit
{ exit = 3; SellPrice[ i ] = Min( Open[ i ], stoplineB[ i ] ); }

if( exit >= 2 )
{ Buy[ i ] = 0;
Sell[ i ] = exit + 1; // mark appropriate exit code
exit = 0;
priceatbuy = 0; // reset price
highsincebuy = 0; } } }

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