Dynamic scale outs based on condition in trade/differing scale out %'s

Hey guys, hope you are all doing well.

First big achievement for me being able to code in scale outs using loops similar to the pyramiding out example in the AFL guide.

I have two things that I would like to do further and not sure how to go about it. The first is that based on my code for buying 52week highs and selling below ma's. I have scaleout set at 50% of the positionsize as it closes below a certain moving average.

I have noticed it does that for each close below. as in the 2nd close below the next ma or second scale it is the 50% of the already 50% taken off. Is their a way than I can assign different scale %'s to the different ma's as in maybe it starts at like 30%, then 40% then 50% etc? Hope that makes sense. The example of the code I am using is below in example 1.

The second thing I would like to do is to have the scale outs change based on a condition in the trade. For example, if I'm in the trade, its a trending stock and it goes parabolic, scaling changes to a high % on a shorter period ma. I tried to do this using a conditional statement based on rates of change on the sigscaleout value (example 2) but that doesn't seem to work. My guess was i need to have this code inside the loop somewhere but I'm not sure where and what to say.

I hope that make sense. Any thoughts would be a huge help and highly appreciated!

Example1 below

MA20 = EMA(C,21);
MA50 = MA(C,50);
MA100=MA(C,100);
MA200 = MA(C,200);



Buy = Close >= HHV(C,250) AND Close > Max(MA20 , Max(MA50 ,Max(MA100, MA200)));
Sell = 0

priceatbuy = 0; 

exit = 0;

for( i = 0; i < BarCount; i++ )
{
   
    if( priceatbuy == 0 AND Buy[ i ] )
    {
        BuyPrice[i] = Close[i];
        priceatbuy = BuyPrice[ i ];
    }

    
    
  
    if( priceatbuy > 0 )
    {
    
        
       
        if( exit == 0 AND
            (Close [i] < ma20 [i])) { 
                
                    
            exit = 1; 
            
           
            Buy[ i ] = sigScaleOut;            
        }
        
       
        if( exit == 1 AND
                (Close [i] < ma50 [i])) 
        {
                      
            exit = 2; 
            
           
            Buy[ i ] = sigScaleOut; 
        }

		if( exit == 2 AND
                (Close [i] < ma100 [i])) 
        {
                      
            exit = 3; 
            
           
            Buy[ i ] = sigScaleOut; 
        }

		
        if( exit==3 AND (Close [i] < ma200 [i])) 
        {
                exit = 4; 
                
            SellPrice[ i ] = Close[i]; 
        }

	
        if( exit >= 4 ) 
        {
            Buy[ i ] = 0;
            Sell[ i ] = exit + 1;   // not sure exactly what this does but seems to not impact much         
      
           
            exit = 0; 
         
            priceatbuy = 0; 
          
             
        }
    }
}

SetOption("MaxOpenPositions",100);
SetPositionSize( 5, spsPercentOfEquity );
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position

Example 2 below with the conditional scaling change on the end line I thought may do what I wanted it to do. seems to not work. essentially if short term rate of change in trade is above certain value. change the scale outs to a higher %

IIF(ROC(C,10,false)>=20, SetPositionSize( 80, spsPercentOfPosition * ( Buy == sigScaleOut ) ),SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) );

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.