Scale-out quantity management Problem in my code

I am making an indicator based on wave trend and I am facing issues in quantity management in my code. My indicator does scale out at the right time but I am unable to adjust the quantity. When my indicator enters a long trade It buys 25 shares but scales out all 25 shares at the same time (Which is the same as exiting). As a solution I wrote a code that buys 25 shares and scales out 5 shares.To do that I wrote this:

SetPositionSize(IIf(Buy == buyconditions,25,IIf(Buy == sigScaleOut,5,0)),spsShares);

But when I did that. Scale out stopped happening. This is where I am stuck.

NOTE: In my code I have marked all the important parts of the code relevant to my issues in the format given below.

///////////////////////////<Important Part>////////////////////////////////////

important code here

///////////////////////////<Important Part>////////////////////////////////////

full code:

dSystemName = "www";
SysVer ="v1.30";

#include <myfunctions.afl> //Code for this is given down below. Copy is and save it in include folder

SetTradeDelays(1,1,1,1);

#include <siff.afl>

paraCounter = 1;
_N(paraParam = "" + paraCounter + "." +  NumToStr(GetChartID(),1,false) + ".");

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat( "    |     {{DATE}} | Open: %.2f | Close: %.2f (%.2f%%) | Volume: %.0f |{{VALUES}}", O, C, SelectedValue( ROC( C, 1 ) ), V ));
///Plot( C, "Close", colorDefault, styleNoTitle | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("adx");
ADX_length = Param("ADX length",14,0,1000,1);//Param("VFI length",...
_SECTION_END();


//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH[ TIMEFRAME 4]HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

TimeFrameSet(inHourly*4);

	n1 = param("n1",14);
	n2 = param("n2",14);
	reaction_wt  = param("recation",1);
	nsc = param("nsc",50);
	nsv = Param("nsv",-50,-100,0);
	upper = Param("upper",1,0,1,1);
	lower = Param("lower",1,0,1,1);
	red=colorRed;
	green=colorBlue;
	ap = (H+L+c)/3; 
	esa = ema(ap, n1);
	d = ema(abs(ap - esa), n1);
	ci = (ap - esa) / (0.015 * d);
	tci = ema(ci, n2);
	wt1 = tci;
	wt2 = ma(wt1,4);
	direction11 = 0;
	direction11 = IIf(rising(wt1,reaction_wt), 1, IIf(falling(wt1,reaction_wt),-1, 0));
	direction2 = IIf(direction11 == 0,Ref(direction11,-1),direction11);
	direction = IIf(direction2 == 0,Ref(direction11,-1),direction2);
	cambio_direccion= change(direction,1);
	pcol = IIf(direction>0 , green ,IIf( direction<0,red,0));
	midpoint = (nsc + nsv) / 2;
	ploff = (nsc - midpoint) / 8;
	down = crossunder(wt1, wt2) and wt1 >= nsc and upper == True;
	up = crossover(wt1, wt2) and wt1 <= nsv and lower == True;
	higherTf = inHourly*4;
	data =wt1;
	data1=wt2;
	chartOnLowerTf = 1;
	tfIsintraday = 1;
	//////////hhhhhhhhhhhhhhhHHHHHHHHHHHHHHHHHHHHHHHHH
	
	sec1ft = Ref(wt1,-1); 

	sec1ft1 = Ref(wt2,-1); 
	//
	down1 = crossunder(sec1ft, sec1ft1) and sec1ft >= nsc and upper == True;

	up1 = crossover(sec1ft, sec1ft1) and sec1ft <= nsv and lower == True;
	//
	direction1 = 0;
	direction1 = IIf(rising(sec1ft,reaction_wt) , 1 ,IIf( falling(sec1ft,reaction_wt) , -1 , nz(Ref(direction1,-1))));
	cambio_direccion1= change(direction1,1);
	pcol1 = IIf(direction1>0 , green , IIf(direction1<0 , red , 0));
	
	isdirect = direction1 == 1;
	isdirects = direction1 == -1;
	valueoncrossoverb = ValueWhen(isdirect,sec1ft);
	valueoncrossovers = ValueWhen(isdirects,sec1ft);
	valueoncrossover = IIf(isdirect,valueoncrossovers ,IIf(isdirects,valueoncrossoverb,0));
	
	barsincecrossover = BarsSince(direction1 == 1 AND Ref(direction1,-1) == -1);
	
	
	is_p_or_n = IIf(isdirect,1,0);

	//

TimeFrameRestore();

is_p_or_n_timeframe_4 = TimeFrameExpand(is_p_or_n,inHourly*4);

_adx = adx(adx_length);


wtmain = ex(sec1ft);


bluedot = Ref(ex(up1),-1) == 0 AND ex(up1);
reddot = Ref(ex(down1),-1) == 0 AND ex(down1);

prev_blue = BarsSince(reddot) > BarsSince(bluedot) AND BarsSince(bluedot) < BarsSince(Cross(nsv,wtmain) OR Cross(wtmain,nsv));
prev_red = BarsSince(bluedot) > BarsSince(reddot) AND BarsSince(reddot) < BarsSince(Cross(nsc,wtmain) OR Cross(wtmain,nsc));

n1 = param("n1",14);
n2 = param("n2",14);
reaction_wt  = param("recation",1);

nsc = param("nsc",50);
nsv = Param("nsv",-50,-100,0);
upper = Param("upper",1,0,1,1);
lower = Param("lower",1,0,1,1);
red1=colorRed;
green1=colorBlue;
ap1 = (H+L+c)/3; 
esa1 = ema(ap1, n1);
d1 = ema(abs(ap1 - esa1), n1);
ci1 = (ap1 - esa1) / (0.015 * d1);
tci1 = ema(ci1, n2);
wt11 = tci1;
wt21 = ma(wt11,4);
direction111 = 0;
direction111 = IIf(rising(wt11,reaction_wt), 1, IIf(falling(wt11,reaction_wt),-1, 0));
direction21 = IIf(direction111 == 0,Ref(direction111,-1),direction111);
direction1 = IIf(direction21 == 0,Ref(direction111,-1),direction21);
cambio_direccion1= change(direction1,1);
pcol111 = IIf(direction1>0 , green1 ,IIf( direction1<0,red1,0));
pcol111cus = IIf(direction1>0 , colorWhite ,IIf( direction1<0,colorYellow,0));
pcol1111 = IIf(direction1>0 , colorWhite ,IIf( direction1<0,colorRed,0));
Plot(wt11,"wt11",pcol111);
Plot(wt21,"wt11",pcol1111);
Plot(nsv,"nsv",colorred);
Plot(nsc,"nsc",colorBlue);
sellmini = Cross(wtmain,wt11);
buymini = Cross(wt11,wtmain);
//buy
blueline1 = TimeFrameExpand(isdirects,inHourly*4) AND (ValueWhen(TimeFrameExpand(isdirects,inHourly*4),wtmain) > nsv AND ValueWhen(TimeFrameExpand(isdirects,inHourly*4),wtmain) < nsc)   AND (Ref(TimeFrameExpand(isdirect,inHourly*4),-1)) == 0;
blueline = Ref(blueline1,-1) == 0 AND (blueline1 and (Ref(TimeFrameExpand(isdirect,inHourly*4),-2)) == 1);
//sell
redline1 = TimeFrameExpand(isdirect,inHourly*4) AND (ValueWhen(TimeFrameExpand(isdirect,inHourly*4),wtmain) > nsv AND ValueWhen(TimeFrameExpand(isdirect,inHourly*4),wtmain) < nsc)   AND (Ref(TimeFrameExpand(isdirects,inHourly*4),-1)) == 0;
redline = Ref(redline1,-1) == 0 AND (redline1 and (Ref(TimeFrameExpand(isdirects,inHourly*4),-2)) == 1);
/////////////////////////

Plot(nsv,"nsv",colorred);
Plot(nsc,"nsc",colorBlue);
//////////
offset = Param("ofset",5,0,100,1,0);
//Buy mini
chwtaO = 0 > wtmain; // T = Dynamic signal genration method F = normal signal method (Buymini/Sellmini)
CrWta0 = Cross(0,wt11);
CrOawt = Cross(wt11,0);
Buy1 = 0;
Sell1 = 0;
Short1 = 0;
Cover1 = 0;
Buy = 0;
Sell = 0;
Short = 0;
Cover = 0;
Sellcond = sellmini;// AND vfi_avrage_rule AND Vfi_p_signal_of_rule AND OBV_avrage_rule AND OBV_p_signal_of_rule;
Buycond = buymini ;//AND blue_dot_r AND blue_line_r;
Shortcond = sellmini ;
Covercond = buymini;
dynamic_sell = 0;
dynamic_Buy = 0;
cdsab = BarsSince(dynamic_sell) < BarsSince(buy1);
//simulation
for (i = 1; i <BarCount-1; i++) 
{
	if(buycond[i])
	{
		Buy1[i] = 1;
	
	}
	
	if(cdsab[i] AND chwtaO[i] AND CrOawt[i])
	{
		Buy1[i] = 1;
		dynamic_Buy[i] = 1;
	}
	
	if(sellcond[i])
	{
		sell1[i] = 1;
		dynamic_sell[i] = 0;
	}
	
	if(chwtaO[i])
	{
		if(CrWta0[i])
		{
			Sell1[i] = 1;
			dynamic_sell[i] = 1;
		}
	}
	
	if(shortcond[i])
	{
		short1[i] = 1;
	}
	
	if(covercond[i])
	{
		cover1[i] = 1;
	}
	
}



cdsab = BarsSince(dynamic_sell) < BarsSince(buy1);

///////////////////////////<Important Part>////////////////////////////////////

scale_out = Cross(wt21,wt11)  AND wt11>nsc;
scale_out1 = Ref(Cross(wt21,wt11),-1) AND wt11>nsc;

///////////////////////////<Important Part>////////////////////////////////////
//re take
buy11 = 0;
sell11 = 0;
short11 = 0;
cover11 = 0;

for (i = 1; i <BarCount-1; i++) 
{
	if(buycond[i])
	{
		
		Buy11[i] = 1;
		
	}

	if(cdsab[i] AND chwtaO[i] AND CrOawt[i])
	{
		Buy11[i] = 1;
			
	}
		
	if(sellcond[i])
	{
		sell11[i] = 1;
		
	}
	
	if(chwtaO[i])
	{
		if(CrWta0[i])
		{
			Sell11[i] = 1;
			
		}
	}
	
	if(shortcond[i])
	{
		short11[i] = 1;
	}
	
	if(covercond[i])
	{
		cover11[i] = 1;
	}
	
	///////////////////////////<Important Part>////////////////////////////////////
	
	if(scale_out[i])
	{
		buy11[i] = sigScaleOut;
	}
	
	///////////////////////////<Important Part>////////////////////////////////////
}

///////////////////////////<Important Part>////////////////////////////////////

Buy = buy11;
Sell = sell11;
Short = short11;
Cover = cover11;

SetPositionSize(IIf(Buy == 1,25,IIf(Buy == sigScaleOut,5,0)),spsShares);

///////////////////////////<Important Part>////////////////////////////////////

PlotShapes(buymini*shapeHollowStar,colorAqua, 0, wt21, Offset=-20);
PlotShapes(sellmini*shapeHollowStar,colorRed, 0, wt21, Offset=20);
PlotShapes(bluedot*shapeCircle,colorYellow,0,wtmain);
PlotShapes(blueline*shapeCircle,colorWhite,0,wtmain);
PlotShapes(reddot*shapeCircle,colorBrightGreen,0,wtmain);
PlotShapes(redline*shapeCircle,colorOrange,0,wtmain);
PlotShapes(Cover*shapeSquare,colorWhite, 0, wt21, Offset=-25);
PlotShapes(Short*shapeDownTriangle,colorWhite, 0, wt21, Offset=-5);
PlotShapes(buy*shapeUpTriangle,colorBlue, 0, wt21, Offset=-5);
PlotShapes(sell*shapeSquare,colorBlue, 0, wt21, Offset=25);
PlotShapes(scale_out*shapeHollowSquare,colorYellow, 0, wt21, Offset=25);

Filter = 1;
AddColumn(Buy,"Buy",1,colorDefault,IIf(Buy,colorGreen,colorDefault));
AddColumn(sell,"sell",1,IIf(sell,colorBlack,colorDefault),IIf(sell,colorYellow,colorDefault));
AddColumn(short,"short",1);
AddColumn(cover,"cover",1);
AddColumn(Scale_out,"scale out",1,IIf(Scale_out,colorBlack,colorDefault),IIf(Scale_out,colorBlue,colorDefault));
//AddColumn(Buy_scale,"Buy",1,colorDefault,IIf(Buy OR Buy_scale,colorGreen,colorDefault));
Plot(0,"Base Line",colorWhite);

myfunctions.afl

function rising(array, length) 
{
	a = 0;
	b=0;
	prevc = Ref(array,-1);

		for (i = 1; i <BarCount; i++)
		{	
			if(array[i] > prevc[i])
			{
				a += 1;
			}
			else a = 0;
			
			b[i] = a;
		}
		
	answer = b >= length;
	return answer;

}

function falling(array, length) 
{
	a = 0;
	b=0;
	prevc = Ref(array,-1);

		for (i = 1; i <BarCount; i++)
		{	
			if(array[i] < prevc[i])
			{
				a += 1;
			}
			else a = 0;
			
			b[i] = a;
		}
		
	answer = b >= length;
	return answer;

}

function change(array, length) 
{
	
	refarray = Ref(array,length/-1);
	answer = array - refarray;
	
	return answer;

}

function crossunder(array, array1) 
{
	answer = cross(array1,array);
	return answer;

}

function crossover(array, array1) 
{
	answer = cross(array,array1);
	return answer;

}

function maxthree(array, array1, array2) 
{
	answer = IIf(array > array1,array,IIf(array1 > array2,array1,array2));
	return answer;

}

function minthree(array, array1, array2) 
{
	answer = IIf(array < array1,array,IIf(array1 < array2,array1,array2));
	return answer;

}

function rchain(array, nth) 
{
	
	answer = Ref(array,nth) == 0 AND array = 1;
	return answer;
}

function ex(array) 
{
	answer = TimeFrameExpand(array,inHourly*4);
	return answer;
}

Any amount of help will be appreciate.

Scale in/out has been covered many times already. You just need to search forum.

When using non-zero Trade Delays you need to use following structure.

SetPositionSize( 100, spsPercentOfEquity );
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // Scale out 50% of position
PositionSize = Ref( PositionSize, -1 ); // If using non-zero Trade Delays with scaling in/out

Or consider using Ref(-1) method instead for your signals with Trade Delays set to zero.

3 Likes

"Scale in/out has been covered many times already. You just need to search forum." - My bad!

And thanks a lot for an immediate reply. You helped me a ton.

1 Like

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