Sell at the next red candle after a buy

What is the AFL equivalent of "sell at the next red candle after a buy"? Or "exit position on a red candle"?

Obviously, the below code doesn't quite work as "Sell at the next red candle" Or "Exit position at the next candle."

Buy = Gtrig0 AND Gtrig1 AND Gtrig2 AND Gtrig4; // The conditions for a buy 

Sell = IIf(HaClose < HaOpen, True, False); // SELL AT RED CANDLE

Short = Rtrig0 AND Rtrig1 AND Rtrig2 AND Rtrig4; // Conditions for a short

Cover = IIf(HaOpen > HaClose, True, False); // COVER AT GREEN CANDLE

What can be done?

Use Close instead of HaClose and Open instead of HaOpen.

1 Like

It's my bad. I should've put up the whole code. You might be right. But it doesn't feel right as it's Heikin Ashi Candle. Anyways, here's the whole code:

_SECTION_BEGIN("Price Heikin Ashi");
SetChartOptions(0,chartShowArrows | chartShowDates);

tmfrm = inHourly;
mode = expandFirst;

TimeFrameSet(tmfrm); 
	HaClose = (O + H + L + C)/4; 
	HaOpen = AMA( Ref( HaClose, -1 ), 0.5 ); 
	HaHigh = Max( H, Max( HaClose, HaOpen ) ); 
	HaLow = Min( L, Min( HaClose, HaOpen ) ); 	

	// 3 GREEN CANDLES AFTER 1 RED
	Gtrig0 = IIf(Ref(HaClose,-2)<=Ref(HaOpen,-2),True,False); // RED CANDLE BEFORE GREEN
	Gtrig1 = IIf(HaClose>=HaOpen,True,False); // 2ND GREEN CANDLE
	Gtrig2 = IIf(Ref(HaClose,-1)>=Ref(HaOpen,-1),True,False); // 1ST GREEN CANDLE
	//Gtrig3 = IIf(Ref(HaClose,-3)>=Ref(HaOpen,-3),True,False);
	Gtrig4 = IIf(HaClose>Ref(HaClose,-1) AND Ref(HaClose,-1)>Ref(HaClose,-2), True, False);

	// 3 RED CANDLES AFTER 1 GREEN
	Rtrig0 = IIf(Ref(HaClose,-2)>=Ref(HaOpen,-2),True,False); //GREEN CANDLE BEFORE RED
	Rtrig1 = IIf(HaOpen>=HaClose,True,False); // 2ND RED CANDLE
	Rtrig2 = IIf(Ref(HaOpen,-1)>=Ref(HaClose,-1),True,False); // 1ST RED CANDLE
	//Rtrig3 = IIf(Ref(HaOpen,-3)<=Ref(HaClose,-3),True,False);
	Rtrig4 = IIf(HaClose<Ref(HaClose,-1) AND Ref(HaClose,-1)<Ref(HaClose,-2), True, False);
TimeFrameRestore();

HaClose = TimeFrameExpand( HAClose, tmfrm, mode ); 
HaOpen = TimeFrameExpand( HaOpen, tmfrm, mode );
HaHigh = TimeFrameExpand( HaHigh, tmfrm, mode ); 
HaLow = TimeFrameExpand( HaLow, tmfrm, mode );

barcolor = IIf(HaClose >= HaOpen,colorGreen,colorRed);
xDiff = (HaHigh - Halow) * 10000;
PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "", barcolor, styleCandle );

for( i = 0; i < 5; i++ ) {
	VarSet( "Gtrig" + i, TimeFrameExpand( VarGet( "Gtrig" + i), tmfrm, mode ) ); 
	VarSet( "Rtrig" + i, TimeFrameExpand( VarGet( "Rtrig" + i), tmfrm, mode ) ); 
}

bir = Status("BarinRange");

Buy = Gtrig0 AND Gtrig1 AND Gtrig2 AND Gtrig4;
PlotShapes(IIf(Buy AND bir, shapeCircle, shapeNone), colorYellow, 0);

Sell = HaClose < HaOpen; // SELL AT RED CANDLE

Short = Rtrig0 AND Rtrig1 AND Rtrig2 AND Rtrig4;
PlotShapes(IIf(Sell AND bir, shapeCircle, shapeNone), colorWhite, 0);

Cover = HaOpen > HaClose; // COVER AT GREEN CANDLE

Filter = (Buy AND bir) OR (Short AND bir);

AlertIf(Buy OR Short, "SOUND C:\\Users\\pinba\\Downloads\\BUY.wav", "Bank Nifty Heikin Ashi Signal "+FullName(), 0, 15, 2);

_SECTION_END();

And thank you for responding!

You seemingly look for not having excessive sell signals in plot. Yes/No?

In that case (if yes) use ExRem() function.

BTW, you better should formulate your original thread question in more detail in the first place next time. "Doesn't work" and posting some unknown variables (no reproducible code) is not helpful to provide response. Your original code does work too but just has multiple raw signals. So it is not an error.

_SECTION_BEGIN("Price Heikin Ashi");
SetChartOptions(0,chartShowArrows | chartShowDates);

tmfrm = inHourly;
mode = expandFirst;

TimeFrameSet(tmfrm); 
	HaClose = (O + H + L + C)/4; 
	HaOpen = AMA( Ref( HaClose, -1 ), 0.5 ); 
	HaHigh = Max( H, Max( HaClose, HaOpen ) ); 
	HaLow = Min( L, Min( HaClose, HaOpen ) ); 	

	// 3 GREEN CANDLES AFTER 1 RED
	Gtrig0 = IIf(Ref(HaClose,-2)<=Ref(HaOpen,-2),True,False); // RED CANDLE BEFORE GREEN
	Gtrig1 = IIf(HaClose>=HaOpen,True,False); // 2ND GREEN CANDLE
	Gtrig2 = IIf(Ref(HaClose,-1)>=Ref(HaOpen,-1),True,False); // 1ST GREEN CANDLE
	//Gtrig3 = IIf(Ref(HaClose,-3)>=Ref(HaOpen,-3),True,False);
	Gtrig4 = IIf(HaClose>Ref(HaClose,-1) AND Ref(HaClose,-1)>Ref(HaClose,-2), True, False);

	// 3 RED CANDLES AFTER 1 GREEN
	Rtrig0 = IIf(Ref(HaClose,-2)>=Ref(HaOpen,-2),True,False); //GREEN CANDLE BEFORE RED
	Rtrig1 = IIf(HaOpen>=HaClose,True,False); // 2ND RED CANDLE
	Rtrig2 = IIf(Ref(HaOpen,-1)>=Ref(HaClose,-1),True,False); // 1ST RED CANDLE
	//Rtrig3 = IIf(Ref(HaOpen,-3)<=Ref(HaClose,-3),True,False);
	Rtrig4 = IIf(HaClose<Ref(HaClose,-1) AND Ref(HaClose,-1)<Ref(HaClose,-2), True, False);
TimeFrameRestore();

HaClose = TimeFrameExpand( HAClose, tmfrm, mode ); 
HaOpen = TimeFrameExpand( HaOpen, tmfrm, mode );
HaHigh = TimeFrameExpand( HaHigh, tmfrm, mode ); 
HaLow = TimeFrameExpand( HaLow, tmfrm, mode );

barcolor = IIf(HaClose >= HaOpen,colorGreen,colorRed);
xDiff = (HaHigh - Halow) * 10000;
PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "", barcolor, styleCandle );

for( i = 0; i < 5; i++ ) {
	VarSet( "Gtrig" + i, TimeFrameExpand( VarGet( "Gtrig" + i), tmfrm, mode ) ); 
	VarSet( "Rtrig" + i, TimeFrameExpand( VarGet( "Rtrig" + i), tmfrm, mode ) ); 
}

bir = Status("BarinRange");

Buy = Gtrig0 AND Gtrig1 AND Gtrig2 AND Gtrig4;
Sell = HaClose < HaOpen; // SELL AT RED CANDLE

Short = Rtrig0 AND Rtrig1 AND Rtrig2 AND Rtrig4;
Cover = HaOpen > HaClose; // COVER AT GREEN CANDLE

Filter = (Buy AND bir) OR (Short AND bir);

AlertIf(Buy OR Short, "SOUND C:\\Users\\pinba\\Downloads\\BUY.wav", "Bank Nifty Heikin Ashi Signal "+FullName(), 0, 15, 2);


Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

PlotShapes(IIf(Buy AND bir, shapeCircle, shapeNone), colorYellow, 0);
PlotShapes(IIf(Sell AND bir, shapeCircle, shapeNone), colorWhite, 0);

_SECTION_END();
4 Likes

I made a mistake. I didn't add the original source of the code (the solution to another problem). This is the solution: Why does the scan show only the morning signals?