Question about how best to approach scale-in scale-out with multiple stop/loss positions

I am going to start with (I'm new here go easy on me)...

My intention here is to ask the forum community to sanity check that what I am trying to achieve is in fact possible in AmiBroker.

Long only Position strategy that I am trying to create.

Maximum Positions Open: 10
Maximum Positions in 1 single Market: 4

An initial Trend following signal is triggered:

Long_period = 55 ; 
Long_HH = Ref( HHV( High, Long_period ), -1 ) ;
ChannelBreakUp = Close > Ref( HHV( High, Long_period ), -1 );

This will then trigger an initial Buy based on Core Equity 1% risk amount based on an ATR Stop-Loss.

TrailStopAmount = 2 * ATR( 20 );
Capital = 100000; /* IMPORTANT: Set it also in the Settings: Initial Equity */
Risk = 0.01*Capital;
PositionSize = (Risk/TrailStopAmount)*BuyPrice;

Intention to Open the position the following day after the Buy Signal is triggered:

SetTradeDelays( 1, 1, 0, 0 );

On opening the initial position I want to set 3 BuySignals for 3 Pyramid position entries:

//ATR
ATR_period = 15 ;
ATR_multiplier = 2 ;
ATR_val = ATR(ATR_period) ;
ATR_SL = (ATR(ATR_period)*ATR_multiplier) ;
ATR_stop = Long_HH-ATR_SL ;

// Pyramid
Pyramid_pos1 = Long_HH+ATR_val ;
Pyramid_pos2 = Long_HH+(2*ATR_val) ;
Pyramid_pos3 = Long_HH+(3*ATR_val) ;

I also want to set a Position Count variable to 1 when the first position is entered:

Position_Count = 1 ;
BarCount = 0

If the position count is 1 the I want to check for the Pyramid position BuySignal.

C > Pyramid_pos1

Each cycle of the IF statement I will increment the BarCount

for ( i = 0; i <  BarCount ; i++ ) 
{ 
if ( Position_Count == 1  AND   Pyramid_pos1 [ i ] ) 
{ 
 
}

above from: https://www.amibroker.com/guide/h_pyramid.html

If the conditions are met then I wish to Buy the 1st Pyramid Position and Set another ATR based Stop loss.

Question that I for the forum community, is it possible to set and alter the stop-loss points for each position. I have been reading around the scale-in and scale-out options though this does not look possible to support the custom position Stop Loss values and custom Buy conditions (checking BarCount and PositionCount)

Any suggestions or feedback will be appreciated.

Thank you

Hi,

your general approach is ok. For that type of trade with multiple entry and exit levels I think it is better to use loops to keep track of all variables (all variables that can be defined outside loop should be created as array variable like you did)
One remark is that you should not change Barcount. Barcount is a reserved constant which holds the number of bars in array.

From your description I'm not sure exactly how/when you define entry/exit levels (for example do you define all entry/exit price levels at the date of the 1st entry or at each scale-in). Do you want to exit 100% at once or use scale out?

Below is just a simplified example of how you can approach several entries and exits within the loop.
(in the example all the entry levels are defined at the time of 1st buy signal)


// initialize variables
position_count=0;
stop_level=0;
scalein1_level= scalein2_level=0;
for ( i = 0; i < BarCount ; i++ ) 
{ 
    
         if( Position_Count == 0  AND   ChannelBreakUp[i] )  // 1st entry
            { 
             Buy[i]=1; // 1st entry
             stop_level=C[i]-TrailStopAmount[i];
             Position_Count = 1;
             entry_price=C[i]; 
             scalein1_level=C[i]+ATR[i];
             scalein2_level=C[i]+ATR[i]*2;   
      }    

      if (Position_Count >0)
          { 
                      if(  Position_Count==1 AND H[i]> scalein1_level ) // 1st scale-in
                         { 
                           Buy[i]=sigScaleIn;
                           Position_Count = 2;
                           stop_level=C[i]-TrailStopAmount[i]; // raise stop
                         }
                                    
                         if(  Position_Count==2 AND H[i]> scalein2_level ) // 2nd scale-in
                         { 
                           Buy[i]=sigScaleIn;
                           Position_Count = 3;
                           stop_level=C[i]-TrailStopAmount[i];  // raise stop
                         }

                       if(  L[i]<stop_level ) // exit 100% 
                          {
                           Sell[i]=1;
                           Position_Count=0;  // reset all trade variables to zero
                           stop_level=0;  // reset all trade variables to zero
                            scalein1_level=  scalein2_level=0;  // reset all trade variables to zero
                            
                          }
           } 
                   
                 
   }
5 Likes

Hi pmxgs,

Thank you for taking a look and providing feedback, I will update my code and change the Barcount variable from the existing constant.

My intended approach is to set all the position entry values from the initial entry BuyPrice. Exit levels will be adjusted through the course of the trades being adjusted at position entries.

Appreciate your help again, I'll update the post when I progress a bit further :slight_smile: