Breakout Entry - Enter Price X% Above Bar Close Price

Hello

I have a strategy that will trigger on a certain bar, and right now I am just entering on the next day open price. However, it's a breakout strategy, so I want to only enter when prices exceeded X% above the trigger/signal bar. I suppose like setting a limit order X % above bars closing price.

I have fund how to similar with ApplyStop, but not for an entry?

Thanks Marc

Assuming Price = Close, then something like Buy = Cross( Price, X% above the trigger/signal bar )!

1 Like

@topmarx I think it will help you if you become familiar with the Knowledge Base article on Limit Orders in AmiBroker.

http://www.amibroker.com/kb/2014/11/26/handling-limit-orders-in-the-backtester/

2 Likes

You would have to iterate to get it to work with initial impulse and state signals (if using Cross(C,ValueWhen(initial_sig,C)) -> then level will change since there are multiple consecutive true sigs at latter case).
Also the breakout above level occurs at unknown bar. You do not know the bar where it will trigger actual Buy entry. The upper mentioned KB article uses fix (known) number of bars after initial signal.

function fxBuyAtBreakoutLevel(InitialSig, SellSig, percent) {
	/// Buy at breakout level after initial signal
	/// by fxshrat@gmail.com
	/// @Link https://www.amibroker.com/guide/h_backtest.html
	/// @Link https://forum.amibroker.com/t/breakout-entry-enter-price-x-above-bar-close-price/16485/4
	/// (commercial use prohibited!)
	global Buy, Sell, BuyPrice, SellPrice;
	local BreakoutLevel, breakoutprice, breakoutbar, BuySignal,priceatbuy, pcnt_level;
	//
	Buy = Sell = 0;
	breakoutprice = breakoutbar = priceatbuy = 0;
	BreakoutLevel = Null;
	pcnt_level = C * (1 + percent/100);// breakout level
	for ( i = 1; i < BarCount; i++ ) {
		// store price and bar at initial signal
		if (breakoutprice == 0 AND priceatbuy == 0 AND InitialSig[ i ]) {			
			breakoutprice = pcnt_level[ i ];
			breakoutbar = i;
		} 
		// check for breakout after signal bar
		BuySignal = H[ i ] > breakoutprice AND i > breakoutbar;
		if ( BuySignal AND breakoutprice > 0 ) {
			Buy[ i ] = 1;	
			BuyPrice[ i ] = breakoutprice;		
			priceatbuy = BuyPrice[ i ];
			breakoutprice = breakoutbar = 0;
		} else Buy[ i ] = 0;	
		// Buy exit
		if( SellSig[ i ] AND priceatbuy > 0 ) {
			Sell[ i ] = 1; 
			SellPrice[ i ] = C[ i ];			
			priceatbuy = 0;
		} else Sell[ i ] = 0;		
		// store breakout level as array
		if ( breakoutprice > 0 )
			BreakoutLevel[ i ] = breakoutprice;
	}
	return BreakoutLevel;	
}

InitialSig = MACD() > 0;
SellSig = MACD() < 0;

// exposes Buy,Sell vars
BreakoutLevel = fxBuyAtBreakoutLevel(InitialSig, SellSig, percent = 1.0);

Plot( C, "", colorDefault, styleBar );
sigShape = ExRem(InitialSig, Sell);
Plot( BreakoutLevel, "BreakoutLevel", colorPaleGreen );
PlotShapes( sigShape * shapeSmallCircle, colorPaleGreen, layer = 0, y = L );
PlotShapes( Buy * shapeSmallUpTriangle, colorBrightGreen, layer = 0, y = L, -20 );
PlotShapes( Buy * shapeSmallCircle, colorBrightGreen, layer = 0, BuyPrice, 0 );
PlotShapes( Sell * shapeDownArrow, colorRed, layer = 0, y = H, -20 );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%), Vol %g {{VALUES}}",
                           O, H, L, C, SelectedValue( ROC( C, 1 ) ), V ) );
//                           
bi = Barindex();
fvb = FirstVisiblevalue( bi );
lvb = LastVisiblevalue( bi );
PlotTextSetFont( "", fnt = "Arial", fntsize = 8, lvb, 0, -1 );
for ( i = fvb; i <= lvb; i++ ) {
	if (sigShape[ i ])
		PlotText( "InitialSig", i, L[ i ], colorPaleGreen, colorDefault, -30 );
	if (Buy[ i ])
		PlotText( "Buy", i, L[ i ], colorBrightGreen, colorDefault, -35 );
	if (Sell[ i ])
		PlotText( "Sell", i, H[ i ], colorRed, colorDefault, 35 );   
}

9

12 Likes