# 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.

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

And here's the result that I got

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////////////////

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

// disable UI trade delays

SetTradeDelays( 0, 0, 0, 0 );

ShortPrice = pricearray;

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

//////

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

inShort = 0;

targetprofit =0;
stoptarget = 0;

priceatshort = 0;
stopprice = 0;

covertarget =0;
selltarget = 0;

inShortArray[0] = inShort[0];
targetprofitarray[0] = targetprofit[0];
stoptargetarray[0] = stoptarget[0];
priceatshortarray[0] = priceatshort[0];
stoppricearray[0] = stopprice[0];
covertargetarray[0] = covertarget[0];
selltargetarray[0] = selltarget[0];

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

if(inRange[i])

{

inShort = inShortArray[i-1];

priceatshort = priceatshortarray[i-1];
targetprofit = targetprofitarray[i-1];
stoptarget = stoptargetarray[i-1];
stopprice = stoppricearray[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;

}

{

targetprofit = FirstProfitTarget[i];
stoptarget = FirstStopTarget[i];

}

if (inBuy ==1 AND priceatbuy > 0)

selltarget= priceatbuy + targetprofit;

{

if (inBuy ==1 AND priceatbuy > 0 AND Low[i] <= stoppricebuy)

{

Sell[i] = 1;
targetprofit=0;
stoptarget=0;
selltarget =0;

}

if( inBuy ==1 AND priceatbuy > 0 AND High[i] >= selltarget)

{

SellPrice[i] = selltarget;
targetprofit = 0;
stoptarget = 0;
selltarget = 0;

}

if (inBuy ==1 AND priceatbuy > 0 AND Sell[i])

{

targetprofit = 0;
stoptarget = 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;
targetprofitarray[i] = targetprofit;

stoptargetarray[i] = stoptarget;
stoppricearray[i] = stopprice;

covertargetarray[i] = covertarget;
selltargetarray[i] = selltarget;

}
}

``````

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

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

``````

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.