How to compare current and previous candle in Amibroker AFL?

In my logic I need to find out the candle which makes new high as compared to latest 5 candles. And the candle must be a red candle which means its
As well as it should look more like an inverted hammer for which I have written something like this-
Now the biggest problem is, the next and the latest candle which comes after this candle that we've identified how we refer to this candle. I need to check whether this new candle breaks the low of the candle we identified its high does not break the high of the candle we identified earlier. if all this condition is satisfied then it gives a SELL signal. The reverse gives a BUY signal.

My code is below-

_SECTION_BEGIN("Ni3 Analysis System");

Title = "{{NAME}} {{DATE}} {{INTERVAL}} " + _DEFAULT_NAME();
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 


	Lasth = HHV(C,5);
	Lastl = LLV(C,5);
//for(i=1; i<BarCount; i++){

//	Sell= H[i-1]>Lasth AND O[i-1]<C[i-1] AND O[i-1]<=((H[i-1]-L[i-1])/2) AND H[i]<H[i-1];
//	Buy= L[i-1]<Lastl AND C[i-1]>O[i-1] AND O[i-1]>=((H[i-1]-L[i-1])/2) AND L[i]>L[i-1];
	H1 = Ref(HHV(High,5),1);
	L1 = Ref(LLV(Low,5),1);
	Sell= H>=H1 AND C<O AND O<=(H-L)/2;
	Buy= L<=L1 AND C>O AND O>=(H-L)/2;


	PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-25);
	PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-35);                      
	PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-30); 
	PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=25);
	PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=35);                      
	PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-30);

dist = 1.5*ATR(10);
	PlotText("Buy "+ C[ i ], i, L[ i ]-dist[i], colorGreen );
	PlotText("Sell " + C[ i ], i, H[ i ]+dist[i], colorRed, colorYellow );


I've tried a lot implementing the logic with no success. Many of the lines in this code is commented as I was trying different ways of implementing my logic. Any help is extremely appreciated.
Thank You.

Zerodha – Open Paperless Account

Open online account with Zerodha. Free delivery trading and Max Rs 20 for Intraday, F&O, Currency and Commodity Trading. Intraday High leverage with MIS, CO and BO.

Are you a day trader?