Why does trailing stop not work with my code?

Hi guys,
I am trying to create a trailing buy. (i.e. buy if price goes up by a set amount) and combine that with a trailing stop.
I used a modified version of the trailing stop plot code (http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/) for the buy signal.

But the trail stop no longer works. I suspect my code clashes with the waiy the inbuilt trailstop is programmed.


SetOption( "InitialEquity", 100000 );
SetOption("CommissionMode",2);
SetOption("CommissionAmount", 7);
SetTradeDelays(1,1,0,0);
PositionSize = -5; //max position size 5%
SetOption( "MaxOpenPositions", 20 );
ApplyStop( stopTypeTrailing, stopModePercent, amount=20, exitatstop=2 ); 


StopLevel = 1.2;

Sellsignal = L<0.8*HHV(H,50);
Buysignal = 0;
trailbuy = 0;

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

   if( trailbuy == 0 AND Sellsignal[ i ] ) 
   { 
      trailbuy = Low[ i ] * stoplevel;
   }
   else Sellsignal[ i ] = 0; // remove excess sellsignal signals

   if( trailbuy > 0 AND High[ i ] > trailbuy )
   {
      Buysignal[ i ] = 1;
      trailbuy = 0;
   }

   if( trailbuy > 0 )
   {   
      trailbuy = Min( Low[ i ] * stoplevel, trailbuy );
   }

}

PVolFilter = MA(C*V,7)>500000 AND MA(V,7)>500000 ;


Buy=Buysignal AND PvolFilter;
Sell=L<0.5*HHV(H,50);

I assume ApplyStop must have something which breaks down when mixed with my code?