Multi-buy/sell sequence

Hi all, I'm wanting to implement the following sequence of entries/exits. Buy 1 contract at the buy signal. Sell that contract at the red line if it's met without first touching the black line. If it touches the black line first, buy a 2nd contract and sell both at the green line. Perhaps someone has used such an approach and can lend a hand. Thanks for the response.

x

Are you serious to Average down?
What if the Green line never came, still continue to buy lower???

Average down, yes. Then sell 2 contracts if price crosses under pink line (added below).

x

https://www.amibroker.com/guide/h_pyramid.html

Just google Amibroker scale in/scale out. Theirs lots of examples. I’ve never dabbled with it myself.

I’d really encourage trying to figure it out yourself. Sometimes you’ll get stuck and posting where your stuck here you’ll generally get some help.

If someone just throws some AFL here at you here, you’ll find yourself coming back next week with more rules and conditions. Just have to dig in there with baby steps if it’s too complicated.

1 Like
function AverageDownOneTime(target_points1, scale_points, target_points2, loss_points) {
	/// Average down one time
	/// If scale in target is not hit then profit target 1 is active
	/// if scale in target is hit then profit target2 and loss target are active
	/// Copyright AmiBroker.com and fxshrat@gmail.com
	/// Commercial use prohibited!
	/// @link https://www.amibroker.com/guide/h_pyramid.html
	/// @link https://forum.amibroker.com/t/multi-buy-sell-sequence/15700/5
	global Buy, Sell, Short, Cover;
	global BuyPrice, SellPrice, ShortPrice, CoverPrice;
	local i, priceatbuy1, priceatbuy2, exit, pab1, pab2;
	local target1, target2, scalein_price, loss2, datamat;

	priceatbuy1 = priceatbuy2 = exit = 0;
	pab1 = pab2 = Null;
	for ( i = 0; i < BarCount; i++ ) {
		if ( priceatbuy1 == 0 AND Buy[ i ] == 1 ) {
			priceatbuy1 = BuyPrice[ i ];
			target1 = priceatbuy1 + target_points1;
			scalein_price = priceatbuy1 - scale_points;
		} else Buy[ i ] = 0;
		
		// activate scale in
		if ( priceatbuy2 == 0 AND priceatbuy1 > 0 AND L[ i ] < scalein_price )  {
			Buy[i] = sigScaleIn;
			BuyPrice[ i ] = Min(O[ i ], scalein_price);
			priceatbuy2 = BuyPrice[ i ];
			target2 = priceatbuy2 + target_points2;
			loss2 = priceatbuy2 - loss_points;
			exit = 1;
		}

		// exit in profit 
		if ( priceatbuy1 > 0 )  {
			if ( exit == 0 AND H[ i ] > target1 ) {   
				Sell[ i ] = 1; 
				SellPrice[ i ] = Max(O[ i ], target1);
				priceatbuy1 = priceatbuy2 = 0;
			}
		}
		
		if ( priceatbuy2 > 0 ) {
			if ( exit == 1 ) {                 
				// exit above scale in
				if ( H[ i ] > target2 ) {
					Buy[ i ] = 0;
					Sell[ i ] = 1; 
					SellPrice[ i ] = Max(O[ i ], target2);
					priceatbuy2 = priceatbuy1 = exit = 0;
				}
				// exit below scale in
				if ( L[ i ] < loss2 ) {
					Buy[ i ] = 0;
					Sell[ i ] = 1; 
					SellPrice[ i ] = Min(O[ i ], loss2);
					priceatbuy2 = priceatbuy1 = exit = 0;
				}
			}
		} 
		
		if ( priceatbuy1 > 0 )
			pab1[ i ] = priceatbuy1;			
		if ( priceatbuy2 > 0 )
			pab2[ i ] = priceatbuy2;
	}
	
	datamat = Matrix(BarCount, 2 );
	datamat = MxSetBlock(datamat, 0, MxGetSize(datamat,0)-1, 0, 0, pab1 );
	datamat = MxSetBlock(datamat, 0, MxGetSize(datamat,0)-1, 1, 1, pab2 );
	return datamat;
}

SetTradeDelays( 0, 0, 0, 0);
Buy = Sell = Short = Cover = 0;

time = 093000;
tn = TimeNum();

Buy = Cross(tn, time);//Cross(C, MA(C, 20));

TickSize = 0.01;

target_points1 = 10*TickSize;
scale_points = 10*TickSize;
target_points2 = 10*TickSize;
loss_points = 10*TickSize;

datamat = AverageDownOneTime(target_points1, scale_points, target_points2, loss_points);
pab1 = MxGetBlock(datamat, 0, BarCount-1, 0, 0, True);
pab2 = MxGetBlock(datamat, 0, BarCount-1, 1, 1, True);

SetPositionSize( 1, spsShares ); 
SetPositionSize( 1, spsShares * ( Buy == sigScaleIn ) ); // scale in 1 share/contract

// ############################## Plot ##############################################
BuyShape = Buy > 0;
Plot( C, "Price", colorDefault, styleBar );
PlotShapes( BuyShape * shapeUpArrow + Sell * shapeDownArrow,
            IIf( BuyShape, colorGreen, colorRed ), layer = 0,
            y = IIf( BuyShape, L, H ), dist = -12 );
PlotShapes( BuyShape * shapeHollowUpArrow + Sell * shapeHollowDownArrow,
            colorLightGrey, layer, y, dist );
PlotShapes( BuyShape * shapeSmallCircle + Sell * shapeSmallCircle,
            IIf( BuyShape, colorGreen, colorRed ), layer,
            y = IIf( BuyShape, BuyPrice, SellPrice ), 0 );
Plot( pab1, "Entry1", colorGreen );
Plot( pab2, "Entry2 (Scalein)", colorLime );

SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );

9

5 Likes

Here is an update because previous quick one had some incorrect exits (see picture below).
Don't forget to set TickSize in line 91 (or remove all TickSize variables completely).

Update:

function AverageDownOneTime(target_points1, scale_points, target_points2, loss_points) {
	/// Average down one time
	/// If scale in target is not hit then profit target 1 is active
	/// if scale in target is hit then profit target2 and loss target are active
	/// Copyright AmiBroker.com and fxshrat@gmail.com
	/// Commercial use prohibited!
	/// @link https://www.amibroker.com/guide/h_pyramid.html
	/// @link https://forum.amibroker.com/t/multi-buy-sell-sequence/15700/6
	/// version 1.1
	global Buy, Sell, Short, Cover;
	global BuyPrice, SellPrice, ShortPrice, CoverPrice;
	local i, priceatbuy1, priceatbuy2, pab1, pab2;
	local target1, target2, scalein_price, loss2, datamat;

	pab1 = pab2 = Null;
	priceatbuy1 = priceatbuy2 = 0;
	scalein_price = target1 = target2 = loss2 = 0;		
	for ( i = 0; i < BarCount; i++ ) {
		if ( priceatbuy1 == 0 AND priceatbuy2 == 0 AND Buy[ i ] == 1 ) {			
			priceatbuy1 = BuyPrice[ i ];
			target1 = priceatbuy1 + target_points1;
			scalein_price = priceatbuy1 - scale_points;
		} else Buy[ i ] = 0;			
		
		if ( priceatbuy1 > 0 AND Buy[i] != 1 )  {
			if ( priceatbuy2 == 0 ) { // if scale-in not hit	
				// exit at main profit 
				if ( target1 > 0 AND H[ i ] >= target1 ) {   
					//Buy[ i ] = 0;
					Sell[ i ] = 1; 
					SellPrice[ i ] = Max(O[ i ], target1);
					priceatbuy1 = priceatbuy2 = 0;
					scalein_price = target1 = target2 = loss2 = 0;	
				} else Sell[ i ] = 0;
				
				// if scale-in target is hit
				if ( L[ i ] <= scalein_price ) {
					Sell[ i ] = 0;
					Buy[i] = sigScaleIn;
					BuyPrice[ i ] = Min(O[ i ], scalein_price);
					priceatbuy2 = BuyPrice[ i ];
					target2 = priceatbuy2 + target_points2;
					loss2 = priceatbuy2 - loss_points;
					target1 = scalein_price = 0;
				} else Buy[i] = 0;
			} 		
			//		
			if ( priceatbuy2 > 0 ) {           
				// exit above scale-in level
				if ( target2 > 0 AND H[ i ] >= target2 ) {
					Buy[ i ] = 0;
					Sell[ i ] = 1; 
					SellPrice[ i ] = Max(O[ i ], target2);
					priceatbuy2 = priceatbuy1 = 0;
					scalein_price = target1 = target2 = loss2 = 0;	
				} 
				// exit below scale-in level
				if ( loss2 > 0 AND L[ i ] <= loss2 ) {
					Buy[ i ] = 0;
					Sell[ i ] = 1; 
					SellPrice[ i ] = Min(O[ i ], loss2);
					priceatbuy2 = priceatbuy1 = 0;
					scalein_price = target1 = target2 = loss2 = 0;	
				} //
			} 			
		} 
		
		if ( priceatbuy1 > 0 )
			pab1[ i ] = priceatbuy1;
		if ( priceatbuy2 > 0 )
			pab2[ i ] = priceatbuy2;
	}
	
	datamat = Matrix(BarCount, 2);
	datamat = MxSetBlock(datamat, 0, MxGetSize(datamat,0)-1, 0, 0, pab1);
	datamat = MxSetBlock(datamat, 0, MxGetSize(datamat,0)-1, 1, 1, pab2);
	return datamat;
}

SetBarsRequired(1000);

SetTradeDelays( 0, 0, 0, 0);
Buy = Sell = Short = Cover = 0;

//time = 093000;
//tn = TimeNum();

Buy = Cross(C, MA(C, 50));//Cross(tn, time);//
BuyPrice = C;

TickSize = 1;

target_points1 = 10*TickSize;// initial target points
scale_points = 10*TickSize;// scale-in points
target_points2 = 10*TickSize;// secondary  target points above scale-in
loss_points = 10*TickSize;// loss points below scale-in

datamat = AverageDownOneTime(target_points1, scale_points, target_points2, loss_points);
pab1 = MxGetBlock(datamat, 0, BarCount-1, 0, 0, True);
pab2 = MxGetBlock(datamat, 0, BarCount-1, 1, 1, True);

SetPositionSize( 1, spsShares ); 
SetPositionSize( 1, spsShares * ( Buy == sigScaleIn ) ); // scale in 1 share/contract

// ############################## Plot ##############################################
BuyShape = Buy > 0;
Plot( C, "Price", colorDefault, styleBar );
Buy_col = IIf( Buy == 1, colordarkGreen, IIf( Buy == sigScaleIn, colorLime, colorRed ));
PlotShapes( BuyShape * shapeUpArrow + Sell * shapeDownArrow,
            Buy_col, layer = 0, y = IIf( BuyShape, L, H ), dist = -12 );
PlotShapes( BuyShape * shapeHollowUpArrow + Sell * shapeHollowDownArrow,
            colorLightGrey, layer, y, dist );
PlotShapes( BuyShape * shapeSmallCircle + Sell * shapeSmallCircle,
            Buy_col, layer, y = IIf( BuyShape, BuyPrice, SellPrice ), 0 );
Plot( pab1+target_points1, "TGT1", colorAqua );
Plot( pab2+target_points2, "TGT2", colorLime );
Plot( pab2-loss_points, "Loss Exit", colorRed );

SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );

11

9

10 Likes