Trailing Stop Code

Folks,

I have copied this code from the source below;

Can some one tell me what changes need to be made so the stop is below the price action immediately after the buy ?

/// INCORRECTLY FORMATTED CODE FOLLOWS

//http://www.traderji.com/community/threads/trailing-stop-afl.101500///

StopATRFactor = Param("ATR multiple", 4.3, 0.5, 10, 0.1 );
StopATRPeriod = Param("ATR period", 14, 3, 50,1 );

sup = C - StopATRFactor * ATR( StopATRPeriod );
res = C + StopATRFactor * ATR( StopATRPeriod );

// calculate trailing stop line
trailARRAY = Null;
trailstop = 0;
for( i = 1; i < BarCount; i++ )
{
if( C[ i ] > trailstop AND C[ i - 1 ] > trailstop )
trailstop = max( trailstop, sup[ i ] );
else
if( C[ i ] < trailstop AND C[ i - 1 ] < trailstop )
trailstop = Min( trailstop, res[ i ] );
else
trailstop = IIf( C[ i ] > trailstop, sup[ i ], res[ i ] );
trailARRAY[ i ] = trailstop;
}

/// SNIP INCORRECTLY FORMATTED CODE

Capture

You might have copied it from traderji site but the code is just bad (incorrect) modification of original given in Knowledge Base:
http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/

ATR based stops do NOT require that kind of coding at all and are single liners using built-in ApplyStop and given in the manual
http://www.amibroker.com/guide/h_backtest.html
(scroll down to Dynamic stops / Chandelier stop)

BTW: Follow forum rules and use CODE TAGS !!!