Entry & Exit in same Candle

I am trying price action based strategy, where I want to exit in the same candle if my exit price matches. But none of my trades are exiting in the same candle even though price hit the target in the same candle, its exiting in the next candle when the price hit the target or exiting at open, if open is above target.
Not sure if I am missing any function.

Attaching a simple sample code as reference only.

Buy = H>Prev_Day_High;
BuyPrice = Prev_Day_High;
Sell = H>(BuyPrice + Prev_Day_High*0.001);

Can anyone help here, or share any similar reading material.

1 Like

Please refer to Allow same bar exit section of System test settings window guide. Search the forum too (this is discussed several times).

Also refrain from using ExRem() function while same bar exit is "on".

Limitation of Exrem or Flip functions: It has nothing to do with the platform (in this case AB) but more to do with the logic of your strategy. As per definition, Exrem( ARRAY1, ARRAY2 ) removes excessive signals; returns 1 on the first occurrence of "true" signal in Array1 then returns 0 until Array2 is true even if there are "true" signals in Array1. Now, what if by virtue of the strategy used, both Array1 and Array2 becomes true at the same time on a single bar? The below code can be used to control such specific situation otherwise Exrem function is well-off - depending on the strategy.

//Using For-Loop to generate Buy/Sell Signal instead of using ExRem
_SECTION_BEGIN( "For-Loop Buy/Sell" );
	 //A Simple Demo strategy
	 Per = Param( "Periods", 15, 3, 144, 1 );	 
	 HigherHighs = HHV( H, Per );
	 LowerLows = LLV( L, Per );	 
	 _Buy = Cross( H, Ref( HigherHighs, -1 ) );
	 _Short = Cross( Ref( LowerLows, -1 ), L );	 
	 
	 //Array Initialization
	 Buy = Sell = Short = Cover = Null;
	 LongFlag = ShortFlag = 0; //Simple Flag arrays to identify whether in a Long or Short position
	 
	 //Using Loop to generate signals
	 for( i = 0; i < BarCount; i++ )
	 {
		 //Long Positions
		 if( _Buy[ i ] AND LongFlag == 0 )
		 {
			 Buy[ i ] = 1;
			 LongFlag = 1;  //To record that we are in Long position
		 }		 
		 if( _Short[ i ] AND LongFlag == 1 )
		 {
			 Sell[ i ] = 1;  //Selling-off the Long position
			 LongFlag = 0;   //Reseting LongFlag back to False, to denote that we are no longer in "Long" position
		 }
		 
		 //Short Positions
		 if( _Short[ i ] AND ShortFlag == 0 )
		 {
			 Short[ i ] = 1;
			 ShortFlag = 1;	  //To record that we are in Short position
		 } 
		 if( _Buy[ i ] AND ShortFlag == 1 )
		 {
			 Cover[ i ] = 1;  //Covering the Short position
			 ShortFlag = 0;   //Reseting ShortFlag back to False, to denote that we are no longer in "Short" position
		 }
	 }
	 
	 //Plotting
	 Plot( C, "", colorDefault, styleBar | styleThick );
	 Plot( HigherHighs, "Highs", colorDarkGreen );
	 Plot( LowerLows, "Lows", colorBrown );
	 
	 PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L, -30 ); //Long Entry
	 PlotShapes( IIf( Sell, shapeSmallDownTriangle, shapeNone ), colorBrown, 0, H, -15 ); //Long Exit
	 
	 PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorRed, 0, H, -30 ); //Short Entry
	 PlotShapes( IIf( Cover, shapeSmallUpTriangle, shapeNone ), colorDarkGreen, 0, L, -15 ); //Short Exit
_SECTION_END();

10 Likes

Thanks mate, your help worked.:+1:

1 Like

If you want to delve deeper, you might consider applying a:

Controlled Trailing Stop-Loss (TSL): It is necessary to ensure that TSL always goes up for Long positions and goes down for Short positions or remains constant to the previous value. "Percentage or Points" based TSLs does not account market Volatility, so it is not dynamic. "Standard Deviation" based TSL (for e.g. Kase DevStop ) or "ATR" based TSLs are ideal. For demo using ATR based TSL here:

_SECTION_BEGIN( "Controlled TSL" );
	 MultATR = Param( "ATR Multiplier", 2, 1, 4, 0.5 );
	 PerATR = Param( "ATR Period", 14, 3, 89, 1 );
	 _ATR = ATR( PerATR );
	 
	 //A Simple Demo strategy
	 Per = Param( "Swing Periods", 15, 3, 144, 1 );
	 HigherHighs = HHV( H, Per );
	 LowerLows = LLV( L, Per );
	 _Buy = Cross( H, Ref( HigherHighs, -1 ) );
	 _Short = Cross( Ref( LowerLows, -1 ), L );
	 
	 //Array Initialization
	 Buy = Sell = Short = Cover = TSL = Null;
	 LongFlag = ShortFlag = 0; //Simple flags
	 
	 //Using Loop to generate signals
	 for( i = 0; i < BarCount; i++ )
	 {
		 //Long Positions
		 if( _Buy[ i ] AND LongFlag == 0 )
		 {
			 Buy[ i ] = 1;
			 LongFlag = 1; //To record that we are in Long position
		 }		 
		 if( LongFlag )
		 {
			 if( TSL[ i - 1 ] > H[ i ] - _ATR[ i ] * MultATR )
				 TSL[ i ] = TSL[ i - 1 ];
			 else
				 TSL[ i ] = H[ i ] - _ATR[ i ] * MultATR;
		 }
		 
		 if( ( _Short[ i ] OR ( L[ i ] < TSL[ i - 1 ] ) ) AND LongFlag == 1 )
		 {
			 Sell[ i ] = 1; //Selling-off the Long position
			 LongFlag = 0;  //Reseting LongFlag back to False, to denote that we are no longer in "Long" position
		 }
		 
		 //Short Positions
		 if( _Short[ i ] AND ShortFlag == 0 )
		 {
			 Short[ i ] = 1;
			 ShortFlag = 1;	 //To record that we are in Short position
		 }
		 if( ShortFlag )
		 {
			 if( TSL[ i - 1 ] < L[ i ] + _ATR[ i ] * MultATR )
				 TSL[ i ] = TSL[ i - 1 ];
			 else
				 TSL[ i ] = L[ i ] + _ATR[ i ] * MultATR;
		 }
		 
		 if( ( _Buy[ i ] OR H[ i ] > TSL[ i - 1 ] ) AND ShortFlag == 1 )
		 {
			 Cover[ i ] = 1; //Covering the Short position
			 ShortFlag = 0;  //Reseting ShortFlag back to False, to denote that we are no longer in "Short" position
		 }
	 }
	 
	 //Plotting
	 Plot( C, "", colorDefault, styleBar | styleThick );
	 Plot( HigherHighs, "Highs", colorDarkGreen, styleDashed | styleNoRescale );
	 Plot( LowerLows, "Lows", colorBrown, styleDashed | styleNoRescale );
	 
	 PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L, -30 ); //Long Entry
	 PlotShapes( IIf( Sell, shapeSmallDownTriangle, shapeNone ), colorBrown, 0, H, -15 ); //Long Exit
	 
	 PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorRed, 0, H, -30 ); //Short Entry
	 PlotShapes( IIf( Cover, shapeSmallUpTriangle, shapeNone ), colorDarkGreen, 0, L, -15 ); //Short Exit
	 
	 Plot( TSL, "TSL", colorPink, styleThick | styleNoRescale ); //Trailing Stop-Loss
_SECTION_END();
5 Likes

Can same be achieved without using loop?
Using array or staticvar.

1 Like

Purely circumstantial - depends on the strategy! If the system demands explicit segregation, then TINA than looping.

Few things previously mentioned by Tomasz:

Trying to get the same formula (supertrend) in array:

function SuperTrend(lenATR, width)
{
	nATR = ATR(lenATR);
	pAvg = (H+L) / 2;
	
	upperBand = pAvg + width * nATR;
	lowerBand = pAvg - width * nATR;
	isUpTrend = True;
	dn = DateNum();
	
	for (i=lenATR; i<BarCount; ++i)
	{
		if (C[i] > upperBand[i-1])
			isUpTrend[i] = True;
		else if (C[i] < lowerBand[i-1])
			isUpTrend[i] = False;
		else
			isUpTrend[i] = isUpTrend[i-1];
			
		if (isUpTrend[i])
			lowerBand[i] = Max(lowerBand[i], lowerBand[i-1]);
		else
			upperBand[i] = Min(upperBand[i], upperBand[i-1]);
	}
	
	super = IIf(isUpTrend, lowerBand, upperBand); 	
	return super;
}

st = SuperTrend(10,3);


///////////////////////////////////////////////////////////////////////////////
BASIC UPPERBAND  =  (HIGH + LOW) / 2 + Multiplier * ATR
BASIC LOWERBAND =  (HIGH + LOW) / 2 - Multiplier * ATR
FINAL UPPERBAND = IF( (Current BASICUPPERBAND  < Previous FINAL UPPERBAND) and (Previous Close > Previous FINAL UPPERBAND)) THEN (Current BASIC UPPERBAND) ELSE Previous FINALUPPERBAND)
FINAL LOWERBAND = IF( (Current BASIC LOWERBAND  > Previous FINAL LOWERBAND) and (Previous Close < Previous FINAL LOWERBAND)) THEN (Current BASIC LOWERBAND) ELSE Previous FINAL LOWERBAND)
SUPERTREND = IF(Current Close <= Current FINAL UPPERBAND ) THEN Current FINAL UPPERBAND ELSE Current  FINAL LOWERBAND
/////////////////////////////////////////////////////////////////////////////////
_AT  = ATR(10); 
_Mul  = 3;
BUB = ((High + Low)/2) + (_Mul * _AT);
BLB = ((High + Low)/2) - (_Mul * _AT);
FUB = IIf(BUB <= P_FUB OR P_C >= P_FUB, BUB, P_FUB);

I am stuck in getting the first value for "Previous Final upperband".

Please guide.

@yogi,

Do you have an AmiBroker License?

You cannot post in this section of the forum if you don't.

See license-verified-badge.

1 Like

Also, the SuperTrend function that you have posted was written by me. So first, you should either ask permission or at least give credit of some sort when you're referencing someone else's work. Second, I resorted to a loop because you can't write the SuperTrend logic using only array functions. The reason is that the upper and lower band values get modified as you iterate through the array, and they feed back into the isUpTrend array.

9 Likes