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:

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.