Need help with sigScaleOut, was: not working

Hi

Can someone help me please with sigscaleout? I tried to partially exit with 2 different exits but the trailing stop seems not working for me.

Thanks in advance.

Buy = Cross( MA( C, 10 ), MA( C, 50 ) );
Sell = 0; 
FirstProfitTarget = 5; // profit
SecondProfitTarget = 10; // in percent
TrailingStop = 3; // also in percent
TrailingStop2 = 5; // also in percent

priceatbuy=0;
highsincebuy = 0;

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

      if( exit == 0 AND
          High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy )
       {
         // first profit target hit - scale-out
         exit = 1;
         Buy[ i ] = sigScaleOut;
       }

      if( exit == 1 AND
          High[ i ] >= ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy )
       {
         // second profit target hit - exit
         exit = 2;
         SellPrice[ i ] = Max( Open[ i ], ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy );
       }
       
       if( Low[ i ] <= ( 1 - TrailingStop * 0.01 ) * highsincebuy )
       {
         // trailing stop hit - exit
         exit = 3;   
         SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 0.01 ) * highsincebuy );
       } 

      if( exit == 0 AND Low[ i ] <= ( 1 - TrailingStop * 0.01 ) * highsincebuy )
       {
         // trailing stop hit - sigscaleout
         exit = 4;   
         Buy[ i ] = sigScaleOut;
         SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 0.01 ) * highsincebuy );
       }
       
        if(exit == 4 AND Low[ i ] <= ( 1 - TrailingStop2 * 0.01 ) * highsincebuy )
       {
         // trailing stop 2 hit - exit
         exit = 5;   
         SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop2 * 0.01 ) * highsincebuy );
       }

      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

First try the code from manual: http://www.amibroker.com/guide/h_pyramid.html
Second, make sure you have set zero delays or adjust SetPositionSize to reflect delays that you are using. Third follow this advice: How do I debug my formula?
Fourth: everything in AmiBroker is working, so choose your subject line not to offend hard working people.

4 Likes