How to compare current and previous candle in Amibroker AFL?

#1
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
CLOSE<OPEN.
As well as it should look more like an inverted hammer for which I have written something like this-
O<(H-L)/2
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-

Code:
_SECTION_BEGIN("Ni3 Analysis System");

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

_SECTION_END();


_SECTION_BEGIN("Logic");
	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;

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

	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);
/*
if(Buy[i]){
	PlotText("Buy "+ C[ i ], i, L[ i ]-dist[i], colorGreen );
	PlotText("Sell " + C[ i ], i, H[ i ]+dist[i], colorRed, colorYellow );
}*/

//}

_SECTION_END();
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.