Scaleout Level as Function of ATR

I've been trying to code a scaleout level based on a target price which is a function of the ATR at the time of entry. In testing it, I have been trying to replicate the take profit stop for the first scale out signal, but I can't seem to get the short price and the atr level stored properly.

Here's the code below.


Buy = Ref(BuyRule1,-1);
Short = Ref(ShortRule1,-1);
Cover = 0;
Sell =0;




FirstProfitTarget= Ref(ATR(14),-1);
priceatshort = 0;
lowsinceshort = 0;
exit = 0;
targetprice = 0;

for( i = 0; i < BarCount; i++ )
{
   
   //Short Code/////////////////////
    
   if( priceatshort == 0 AND Short[ i ] == 1 )
    {
       priceatshort = ShortPrice[ i ];
       targetprice = FirstProfitTarget[i];
    }
    
    

   if( priceatshort > 0 )
    {
       lowsinceshort = Min( Low[ i ], lowsinceshort);

      if( exit == 0 AND
          Low[ i ] <=  priceatshort - targetprice)
       {
         // first profit target hit - scale-out
         exit = 1;
         CoverPrice[i] = priceatshort - targetprice;
         Cover[i] = 1; 
         targetprice = 0;
       }
      

      if( exit >= 1 )
       {
        
         exit = 0;
         priceatshort = 0; // reset price
         lowsinceshort = 0;
         Cover =0;
         
       }
    } 
    
    
    
    
}


I tried exploration using the following code:

Filter = 1;
AddColumn(FirstProfitTarget,"FPT");
AddColumn(Targetprice,"TP");
AddColumn(Priceatshort,"PAS");
AddColumn(Short,"SS");

And here's the result that I got

image

I tried looking at other codes, but I can't still figure out why this is not working.

Any help would be appreciated.

Thanks.

Hi,

First you need to get your badge explained here

I really didn't get "what" is not right but if you can clear that out because priceatshort and targetprice are not arrays so you cant use them in Exploration the way you want.
Store those values with array operator.

ShortPrice[ i ] this variable is not defined in your code.

Ok so I have partially figured this one out, and reached the point where the code actually already exits at the target price. I used arrays as suggested. Now I'm having problems getting the scaleout signal right, especially getting the correct position size.

To provide more clarity, here is how the strategy was supposed to work --

a. If Buy signal in previous bar, enter position at Open, position sized as a percent of equity
b. If Low is less than or equal to the stop price (which is price at buy less 1.5x of ATR(14), sell the position.
c. If High is greater than or equal to target price( which is price at buy plus ATR(14), scaleout 50% of the position
d. Exit at sell signal

{same logic with shorts, but reverse}

Here's the code.

/////POSITION SIZING/////////
RoundLotSize = 1; 
PointValue = 1000; 
RiskPerContract = 1.5 * Ref(ATR(14),-1);
PositionRisk = 2;
PctSize = (PositionRisk*MarginDeposit)/(RiskPerContract*PointValue); 
maxpos = 15; // maximum number of open positions
SetOption( "MaxOpenPositions", maxpos );
SetPositionSize(pctsize,spsPercentOfEquity);

////###################################//////////////////
////////Scaleout Code////////////////

//Trade Delay Setup////


tradedelay = 1; // entry/exit bar delay
if ( tradedelay > 0 ) {
    pricearray = Open;
} else {
    pricearray = Close;
}

// disable UI trade delays


SetTradeDelays( 0, 0, 0, 0 );
 
BuyPrice = pricearray;
ShortPrice = pricearray;


//////
Buy = Ref(BuyRule1,-1);
Short = Ref(ShortRule1,-1);
Cover = 0;
Sell =0;


//////

FirstProfitTarget= Ref(ATR(14),-1);
FirstStopTarget = 1.5*(Ref(ATR(14),-1));
inRange = Status("BarInRange");






inBuy = 0;
inShort = 0;


targetprofit =0;
stoptarget = 0;


priceatshort = 0;
stopprice = 0;

priceatbuy = 0;
stoppricebuy =0;


covertarget =0;
selltarget = 0; 



inBuyArray[0] = inBuy[0];
inShortArray[0] = inShort[0];
targetprofitarray[0] = targetprofit[0];
stoptargetarray[0] = stoptarget[0];
priceatshortarray[0] = priceatshort[0];
stoppricearray[0] = stopprice[0];
priceatbuyarray[0] = priceatbuy[0]; 
stoppricebuyarray[0] = stoppricebuy[0];
covertargetarray[0] = covertarget[0]; 
selltargetarray[0] = selltarget[0];











for( i = 1; i < BarCount; i++ )
{
 	
 	
 	if(inRange[i])
 	
 	{			
	
	
	inShort = inShortArray[i-1];
	inBuy = inBuyArray[i-1];
	
	priceatshort = priceatshortarray[i-1];
	targetprofit = targetprofitarray[i-1];
	stoptarget = stoptargetarray[i-1];
	stopprice = stoppricearray[i-1];
	
			
	
	priceatbuy = priceatbuyarray[i-1];
	stoppricebuy = stoppricebuyarray[i-1];
	
	covertarget = covertargetarray[i-1];
	
	selltarget = selltargetarray[i-1];
	
	
	
	
	
	
	if (inShort == 0 AND Short[i])
	
		{
		
		priceatshort = ShortPrice[i];
		targetprofit = FirstProfitTarget[i];
		inShort =1;
		stoptarget = FirstStopTarget[i];
		stopprice = stoptarget + priceatshort; 
		covertarget = priceatshort - targetprofit;
		
		
		}
	
	
	
	if (inBuy == 0 AND Buy[i])
	
		
		{
		
		priceatbuy = BuyPrice[i];
		targetprofit = FirstProfitTarget[i];
		inBuy = 1;
		stoptarget = FirstStopTarget[i];
		
		}
		
		
		
		
	
	if (inBuy ==1 AND priceatbuy > 0)
	
		stoppricebuy = priceatbuy - stoptarget; 
		selltarget= priceatbuy + targetprofit;
		
		
		{
		
		if (inBuy ==1 AND priceatbuy > 0 AND Low[i] <= stoppricebuy)
		
			{
			
			SellPrice[i] = stoppricebuy;
			Sell[i] = 1;
			priceatbuy=0;
			targetprofit=0;
			stoptarget=0;
			inBuy=0;
			stoppricebuy =0;
			selltarget =0;
			
		
			
			}
			
		if( inBuy ==1 AND priceatbuy > 0 AND High[i] >= selltarget)
		
			{
			
			SellPrice[i] = selltarget;
			Buy[i] = sigScaleOut;
			priceatbuy = 0;
			targetprofit = 0;
			stoptarget = 0;
			inBuy = 0;
			stoppricebuy =0;
			selltarget = 0;
			
			}
		
		
		if (inBuy ==1 AND priceatbuy > 0 AND Sell[i])
		
			{
			
			
			priceatbuy = 0;
			targetprofit = 0;
			stoptarget = 0;
			inBuy = 0;
			stoppricebuy =0;
			selltarget=0;
			
			
			}
			
			
		}	
			
			

	

	
	
	
	
	
	
	
	if (inShort==1 AND priceatshort > 0 )
	
		
	{
	
		if (inShort==1 AND inBuy==0 AND priceatshort > 0 AND High[i] >= stopprice)
		
		{	
		
		CoverPrice[i] = stopprice;
		Cover[i] = 1;
		inShort = 0;
		priceatshort=0;
		targetprofit=0;
		stoptarget=0;
		stopprice=0;
		covertarget=0;
		
		}
		
	
	if  (inShort==1 AND priceatshort > 0 AND Low[i] <= covertarget)
		
		{
		
		CoverPrice[i] = covertarget;
		Short[i] = sigScaleOut;
		inShort = 0;
		priceatshort=0;
		targetprofit=0;
		stoptarget=0;
		stopprice=0;
		covertarget=0;
		
		
		}
	
	
	if (inShort ==1 AND priceatshort > 0 AND Cover[i])
	
		{
		
		inShort = 0;
		priceatshort=0;
		targetprofit=0;
		stoptarget=0;
		stopprice=0;
		covertarget=0;
		
		
		}
	
	
	
	
	}	
	
	inShortArray[i]  = inShort; // probably needs to be at the end of the loop code
	
	
	priceatshortarray[i] = priceatshort;
	priceatbuyarray[i] =  priceatbuy;
	targetprofitarray[i] = targetprofit;
	
	
	
	
	inBuyArray[i] = inBuy;
	stoptargetarray[i] = stoptarget;
	stoppricearray[i] = stopprice;
	stoppricebuyarray[i] = stoppricebuy;
	
	
	covertargetarray[i] = covertarget;
	selltargetarray[i] = selltarget;
	
		
		
	}
}	



So when I use this code, I get an exit like this when the scaleout signal is triggered:

image

But when I set the position size using this code, it doesn't even trigger the entry signal in the first place:


scaleoutpos = IIf(Short==sigScaleOut,spsPercentOfPosition,spsNoChange);
SetPositionSize(50,scaleoutpos);




image

Any help on figuring this out will be greatly appreciated.

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